This is my code which I have written as a library
#include "avr/interrupt.h"
#include "Arduino.h"
#include "AllTimer.h"
AllTimer::AllTimer()
{}
void AllTimer::dofun(void)
{
//TIFR1 |= _BV(OCF1A);
TCCR1B = 0X00;
//TIFR1 = 0X01;
digitalWrite(13,!digitalRead(13));
Serial.print("I printed");
TCNT1 = 0X0000;
TCCR1B |= (1<<CS12) && (1<<CS10);
//sei();
}
void AllTimer::setTimer(void)
{
TCNT1 = 0x0000;
TCCR1A = 0x00;
TCCR1B = 0x00;
TCCR1B |= (1<<CS12) && (1<<CS10);
TIMSK1 |= (1<<TOIE1);
sei();
}
ISR (TIMER1_OVF_vect)
{
AllTimer::dofun();
}
Code in Arduino IDE:
#include <AllTimer.h>
AllTimer mytimer;
void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop()
{
mytimer.setTimer();
//delay(200);
}
But I am not getting anything on the serial monitor. What is the error I have made?
explorer
asked Mar 16, 2015 at 10:12
-
2Using Serial.print inside an ISR is asking for problems. Does the led blink? Also, there is no need to write TCCR1B and TCNT1 inside doFun.Gerben– Gerben2015年03月16日 14:12:16 +00:00Commented Mar 16, 2015 at 14:12
1 Answer 1
The Arduino main()
calls loop()
continuously, in a tight loop. Thus
your AllTimer::setTimer()
is called continuously, and each time it
resets the counter (TCNT1 = 0x0000;
). Thus the counter can never
overflow.
answered Mar 16, 2015 at 10:49
-
2So put
mytimer.setTimer();
insidesetup
instead ofloop
.Gerben– Gerben2015年03月16日 14:12:54 +00:00Commented Mar 16, 2015 at 14:12 -
also, in
TCCR1B |= (1<<CS12) && (1<<CS10);
shouldn't it be|
instead of the logic&&
?Sim Son– Sim Son2019年04月06日 21:04:21 +00:00Commented Apr 6, 2019 at 21:04 -
@SimSon: Indeed! I hadn't spotted that. Good catch.Edgar Bonet– Edgar Bonet2019年04月07日 09:02:11 +00:00Commented Apr 7, 2019 at 9:02