本例中调节可变电阻,然后AD转换,从而控制PWM宽度,从而控制电机转速。
CCR1A = 0x83 低两位将TCCR1A寄存器波形发生器模式(Waveform Generation mode)选择位WGM1[1:0]设为11,和前两位。
为0011可知T/C1工作模式为10位PWM,相位可调。
TCCR1B = 0x02 低两位同TCCR1A高4位COM1A[1:0]设置T/C1的比较输出模式(Compare Output Modulation:COM).
加1计数与OCR1A比较匹配时将OC1A引脚清零,减1计数写OCR1A比较匹配时将OC1A引脚置1
Proteus运行截图:
Studio6.2运行截图:
程序清单:
- /*
- * GccApplication10.c
- *
- * Created: 2014-11-5 19:25:00
- * Author: Administrator
- */
- #define F_CPU 4000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdint.h>
- uint16_t ADC_Convert(uint8_t CH)
- {
- int Result;
- ADMUX = CH;
- Result = (uint16_t)(ADCL+(ADCH << 8));
- return Result;
- }
-
- int main(void)
- {
- uint16_t x= 0;
- uint16_t PRE_ADC_Result = 0;
- DDRA = 0x00;
- PORTA = 0xFF;
- DDRD = 0xFF;
- PORTD = 0xFF;
- DDRC = 0xFF;
- ADCSRA = 0xE6;
- _delay_ms(3000);
- TCCR1A = 0x83;
- TCCR1B = 0x02;
- while(1)
- {
- x = ADC_Convert(7);
- if(x != PRE_ADC_Result)
- {
- PRE_ADC_Result = x;
- if(x==1023) x=0;
- else if(x==0) x=1023;
- OCR1A = x;
- }
- }
-
- }
-
|