我使用 Atmega328p Timer0 及其两个 OCR:
void timer0_ini(void)
{
TCCR0A = 0;// set entire TCCR0A register to 0
TCCR0B = 0;// same for TCCR0B
TCNT0 = 0;//initialize counter value to 0
OCR0B = 125; // OCR0B is less than OCR0A, so it works
OCR0A = 250;
TCCR0A = (0<<COM0A1)|(0<<COM0A0)|(0<<COM0B1)|(1<<COM0B0)|(1<<WGM01)|(0<<WGM00);
TCCR0B |= (0<<FOC0A)|(0<<FOC0B)|(0<<WGM02)|(1<<CS02)|(1<<CS00);
TIMSK0 = (1 << OCIE0A)|(1 << OCIE0B);
}
ISR (TIMER0_COMPB_vect)
{
PORTB ^= (1 << PORTB2);
}
ISR (TIMER0_COMPA_vect)
{
PORTB ^= (1 << PORTB1);
}
它仅在 OCR0B < OCR0A 时起作用。我不明白为什么这有意义。