我有一个要通过模数转换器控制的伺服电机(tower pro微型服务器99)。当adc读数为0伏时,电动机应处于0度;否则,电动机应处于0度。当adc读数为5伏时,电动机应处于180度。为了实现这一点,我使用了arduino uno(atmega328p微控制器)。我将电压读数馈送到板上的ADC中,读取该值,然后基于该值生成一个周期,该周期具有适当的占空比以旋转电机。下面的代码使用C:
#include <stdlib.h>
#include "eel4746c.h"
void delay_8us()
{
tc0->TCCRA=0x00; //set mode (normal) and prescaler (64)
tc0->TCCRB=0x03;
tc0->TCNT=255; //set counter value to 255
*TIFR0=1; //clear TOVO flag in TIFR0 register
while((*TIFR0&0x01)==0); //count until overflow, results in 8us delay
}
void delay_in_8us(uint16_t x) //calls 8us delay a certain amount of times
{
uint16_t i;
for (i=0;i<x;i++) delay_8us();
}
int main()
{
uint16_t T=2500; //define max period value, 8us * 2500 is 20ms
uint16_t value=75; //values that defines duty cycle of period
portd->DDR=0x01; //sets pin 0 and port D as output
adc->ADCSRA=0x87; //enables ADC, sets scale to 128
adc->ADMUX=0x40; //sets AREF pin, sets ADC0 pin as input for ADC
uint16_t adcL=0; //variables to read in values
uint16_t adcH=0;
uint32_t adcR=0;
while(1){
adc->ADCSRA= adc->ADCSRA | 0x40; //starts ADC conversion
while ((adc->ADCSRA&0x10) == 0 ); //waits for ADC conversion to finish
adcL = adc->ADCL; //read in ADC results
adcH = adc->ADCH & 0x0003;
adcR = (256*adcH)+adcL; //convert ADC results to integer value
value = 75 + ((180)*(adcR) / 1023); //calculates value from ADC value to
//set period
portd->PORT=0x01; //sets port d, pin 0 to 1
delay_in_8us(value); //delays 8us * value
portd->PORT=0x00; //sets port d, pin 0 to 0
delay_in_8us(T - value); //delays 8us * (2500 - value) to get rest of
//period
}
}
头文件(仅具有端口定义和内存位置/结构):
#include <stdint.h>
#ifndef _EEL4746C_H_
#define _EEL4746C_H_
// Structure for GPIO ports
typedef struct gpio_port_struct {
uint8_t PIN; // Input Register
uint8_t DDR; // Data Directional Register
uint8_t PORT; // Output Register
} gpio_port_t;
// The three GPIO ports (B,C, and D)
volatile gpio_port_t *portb=(gpio_port_t *) 0x0023;
volatile gpio_port_t *portc=(gpio_port_t *) 0x0026;
volatile gpio_port_t *portd=(gpio_port_t *) 0x0029;
// 8-bit Time/Counter Structure
typedef struct timer_counter_8bit_struct {
uint8_t TCCRA; // Control Register A
uint8_t TCCRB; // Control Register B
uint8_t TCNT; // Counter (value)
uint8_t OCRA; // Output Compare Register A
uint8_t OCRB; // Output Compare Register B
} timer_counter_8bit_t;
// Timer/Counter 0
volatile timer_counter_8bit_t *tc0=(timer_counter_8bit_t *) 0x0044;
// Timer/Counter 0 Interrupt Flag Register
volatile uint8_t *TIFR0= (uint8_t *) 0x0035;
// Timer/Counter 0 Interrupt Mask Register
volatile uint8_t *TIMSK0=(uint8_t *) 0x006e;
// Analog to Digital Converter Structure
typedef struct adc_struct {
uint8_t ADCL; // ADC Data Register Low
uint8_t ADCH; // ADC Data Register High
uint8_t ADCSRA; // ADC Control and Status Register A
uint8_t ADCSRB; // ADC Control and Status Register B
uint8_t ADMUX; // ADC Multiplexer Selection Register
uint8_t not_used;
uint8_t DIDR0; // Digital Input Disable Register 0
uint8_t DIDR1; // Digital Input Disable Register 1
} adc_t;
// ADC
volatile adc_t *adc=(adc_t *) 0x0078;
#endif
当我将此代码编写到arduino并运行时,它通常运行良好。我只是使用电位计将电压读入ADC0引脚。我唯一的问题是,在低电压(接近0)下,电动机会持续旋转。这向我表明该时间段是错误的,但是,当查看我的代码时,我看不到任何方法来实际得到一个不正确的时间段。
到目前为止,我已经仔细检查了所有使用/期望正确值的内容;根据{{3}}和ATmega328p数据表进行了检查。这些值似乎都是正确的。关于句号为何错误的我唯一可能的线索与我等待转换的while循环有关:</ p>
while((adc-> ADCSRA&0x10)== 0);
有趣的是,当我将其注释掉时,代码实际上完全可以正常工作,但是我并不真正理解为什么这样做会导致-我能想到的最好的是,while循环在代码中生成了足够的额外循环稍微改变周期,足以使电压值0导致某些意外情况。但是,根据数据表,ADC转换不应花费很多周期(第一次转换大约需要15个周期,之后大约需要3个周期),但是,这不足以影响周期。
这个问题的长短是我相信我的ADC转换使我的代码给了我不正确的时间。为什么这种转换会给我这么一段时期?还是我看错了方向?