我试图数我的点击按钮(库恩和模拟它在4个leds,它必须数到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
}
}发布于 2020-03-24 18:29:16
最好避免“神奇数字”:
TCCR0 = 0x4E; //Counter mode(falling edge),CTC mode .要设置CTC位#6,WGM00应该是0,而位#3 WGMM01应该是1(参见数据表,表38,第80页)。
两个位都设置为1,因此计数器在FastPWM模式下工作。
使用具有位名的宏:
TCCR0 = (1 << WGM01) | (1 << CS02) | (1 << CS01); // = 0x0Ehttps://stackoverflow.com/questions/60836676
复制相似问题