Timer1 interrupt debuging
hi all.. have problem code here. try set interrupt trigger every 200us, when checking show 400us instead of 200us timer1 setting.. can me wrong setting.
here code:
here result serial monitor
here code:
code: [select]
volatile int val;
volatile int isnewval;
volatile unsigned long time;
unsigned long timer;
void setup() {
serial.begin(115200); //setup serial comm.
delay(500);
nointerrupts(); //disable interrupt first
tccr1a =0; // clear timer setting
tccr1b =0; // ~~
tcnt1=0; //~~
ocr1a= 400; // 16m/8/5000 = 400. target freq 5khz
tccr1b |= (1<<wgm12); //enable ctc mode
tccr1b |= (1<<cs11); //setup prescaler 8.
timsk1 |= (1<< ocie1a); //enable timer interrupts
interrupts(); //enable interrupt
}
isr(timer1_compa_vect){
// val = analogread(a0);
isnewval=1;
}
void loop() {
if (isnewval == 1){
timer = micros()-time;
time = micros();
serial.println(timer);
isnewval =0;
}
}
here result serial monitor
printing 4 characters (three digits plus newline) @ 115200 baud take 347µs.
the timer setup looks ok me, though. technically ocr1a should 399, 0-399 400 counts.
the timer setup looks ok me, though. technically ocr1a should 399, 0-399 400 counts.
Arduino Forum > Using Arduino > Programming Questions > Timer1 interrupt debuging
arduino
Comments
Post a Comment