[英]Output Compare match doesn't happen - AVR Atmega32
I'm trying to count my clicks on a push button (Coun and simulates it on 4 leds it must count till 9 , then TCNT0 equals OCR0 , so Interrupt is fired and TCNT0 becomes zero again and so on . but it continues after 9 till 255 . output compare match flag isn't set . (no compare match happens).我正在尝试计算我在按钮上的点击次数(在 4 个 LED 上模拟它必须计数到 9,然后 TCNT0 等于 OCR0,因此中断被触发并且 TCNT0 再次变为零等等。但它在 9 之后继续直到255 . 未设置输出比较匹配标志。(不发生比较匹配)。
ISR(TIMER0_COMP_vect){
}
int main(){
DDRC=0xff; //configure PORTC leds
CLEAR_BIT(DDRB,0); //configure T0 Pin as input
SET_BIT(PORTB,0); //enable internal PULL-UP resistance
TCCR0 = 0x4E; //Counter mode(falling edge),CTC mode .
TCNT0=0; //timer register initial value
OCR0=9; //set MAX value as 9
SET_BIT(TIMSK,OCIE0); //Enable On compare interrupt
SET_BIT(SREG,7); //Enable All-interrupts
while (1){
PORTC=TCNT0; //Let Leds simulates the value of TCNT0
}
}
Better avoid "magic numbers":最好避免“幻数”:
TCCR0 = 0x4E; //Counter mode(falling edge),CTC mode .
To set CTC bit #6 WGM00 should be 0, while bit #3 WGMM01 should be 1 (Refer to the datasheet , table 38 at page 80).设置 CTC 第 6 位 WGM00 应为 0,而第 3 位 WGMM01 应为 1(请参阅数据表,第 80 页的表 38)。
You have both bits set to 1, thus the counter is working in FastPWM mode.您将两个位都设置为 1,因此计数器在 FastPWM 模式下工作。
Use macros with bit names:使用带有位名称的宏:
TCCR0 = (1 << WGM01) | (1 << CS02) | (1 << CS01); // = 0x0E
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.