我正在对atmega328进行一些计时器实验 我正在尝试使用每个中断(OVF除外)。 试图使timer1行为像3个人定时器。 当计时器耗尽时,这可能会派上用场。
根据手册,有4个与timer1相关的中断向量。
我的想法是让TCNT1从0到65535继续计数
然后在OCR1A,OCR1B和ICR1寄存器中设置所需的比较值
当中断时,手动将值添加到寄存器,因此下一个中断将在相同的持续时间内触发。
我写了一些代码来验证,结果似乎很理想。
我在考虑这是使用计时器的正确方法吗?
或者这可能会导致一些错误,错误或效率问题?
谢谢!
int A_Ticks = 15624; //64us*(15624+1) = 1000ms
int B_Ticks = 23436; //1500ms
int ICR_Ticks = 31248; //2000ms
void setup()
{
Serial.begin(115200);
TCCR1A = 0x00;
TCCR1B = 0x00;
TCCR1B|= _BV(WGM13); //enable TIMER1_CAPT_vect
TCCR1B|= _BV(WGM12); //enable CTC
TCCR1B |= _BV(CS12); //prescaler = CPU clock/1024
TCCR1B &= ~_BV(CS11); //per tick = 64us
TCCR1B |= _BV(CS10);
OCR1A = A_Ticks;
OCR1B = B_Ticks;
ICR1 = ICR_Ticks;
char oldSREG = SREG;
noInterrupts();
TIMSK1 |= _BV(OCIE1A); // enable timer interrupt
TIMSK1 |= _BV(OCIE1B);
TIMSK1 |= _BV(ICIE1);
TIMSK1 |= _BV(TOIE1);
TCNT1=0;
SREG = oldSREG;
}
void loop()
{
delay(100);
}
ISR (TIMER1_COMPA_vect)
{
Serial.print(millis());
Serial.println(":COMPA");
OCR1A+=A_Ticks; //add value manually
}
ISR (TIMER1_COMPB_vect)
{
Serial.print(millis());
Serial.println(":COMPB");
OCR1B+=B_Ticks; //add value manually
}
ISR (TIMER1_OVF_vect)
{
Serial.print(millis());
Serial.println(":OVF_vect");
}
ISR (TIMER1_CAPT_vect)
{
//When interrupt into TIMER1_CAPT_vect
//TCNT will be clear to 0 due to CTC is on
//manually set it to value of ICR1 to make TCNT keep counting
TCNT1=ICR1;
ICR1+=ICR_Ticks; //add value manually
Serial.print(millis());
Serial.println(":CAPT_vect");
}
999:COMPA
1499:COMPB
1999:CAPT_vect
1999:COMPA
2999:COMPA
2999:COMPB
3999:CAPT_vect
3999:COMPA
4194:OVF_vect
4499:COMPB
4999:COMPA
5999:CAPT_vect
5999:COMPA
5999:COMPB
6999:COMPA
7498:COMPB
7999:CAPT_vect
7999:COMPA
8388:OVF_vect