Reputation: 17
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).
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
}
}
Upvotes: 0
Views: 292
Reputation: 4654
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).
You have both bits set to 1, thus the counter is working in FastPWM mode.
Use macros with bit names:
TCCR0 = (1 << WGM01) | (1 << CS02) | (1 << CS01); // = 0x0E
Upvotes: 1