Hi all
building a very simple timer that only shows seconds 0-59 and restarts at 0. I choose to use Timer 1 overflow for more precision. Using delay() did not satisfy me.
The timing should startup immediately, but it doesn’t. At startup it displays 00, but it waits 3-4 seconds before proceeding.
Variables:
long preset = 49911; // ------------------------------65535 - (1 / (1024 / 16000000) -1)
Timer initialization:
void timer_init()
{
TCNT1 = preset; // for 1 sec at 16 MHz
TCCR1A = 0x00;
TCCR1B = (1 << CS10) | (1 << CS12); // Timer mode with 1024 prescaler
TIMSK1 = (1 << TOIE1) ; // Enable timer1 overflow interrupt(TOIE1)
sei(); // Enable global interrupts by setting global interrupt enable bit in SREG
}
The actual interrupt routine:
ISR (TIMER1_OVF_vect) // Timer1 ISR
{
TCNT1 = preset;
sec++;
if (sec > 59) {
sec = 0;
}
}
timer_init() is called from within setup, immediately followed by an instruction to display the timer.
void setup()
{
Serial.begin(57600);
Serial.println("timer gestart");
mydisplay.shutdown(0, false); // turns on display
mydisplay.setIntensity(0, brt);
timer_init();
displayTeller(sec); // routine to display the seconds, with Serial.print or with 2 digit-7-seg display
prevSec = sec;
}
I am using a ‘barebones’ Atmega328 at 16MHz.
Running the sketch from an Arduino Uno with Serial.printing or running it from the AtMega328 with 7 segments display does not matter, the problem remains the same: at startup the sketch immediately displays the initial value of 00 and then waits 3-4 seconds to start counting up. It looks like the timer overflow needs more than 1 sec for the first cycle…?
Any ideas what I could do?
Thanks!
Jan
(edit after first comment)
Please note that I do not see any reason why the printing should make it hang.
This is a short version with Serial.print only:
long preset = 49911; // ------------------------------65535 - (1 / (1024 / 16000000) -1)
unsigned long startTijd;
unsigned int brt = 15, // helderheid, max 15
sec = 0,
prevSec ;
ISR (TIMER1_OVF_vect) // Timer1 ISR
{
TCNT1 = preset;
sec++;
if (sec > 59) {
sec = 0;
}
}
void timer_init()
{
TCNT1 = preset; // for 1 sec at 16 MHz
TCCR1A = 0x00;
TCCR1B = (1 << CS10) | (1 << CS12); // Timer mode with 1024 prescaler
TIMSK1 = (1 << TOIE1) ; // Enable timer1 overflow interrupt(TOIE1)
sei(); // Enable global interrupts by setting global interrupt enable bit in SREG
}
void displayTeller(int v) {
Serial.println(sec);
}
void setup()
{
Serial.begin(57600);
Serial.println("timer gestart");
timer_init();
displayTeller(sec);
prevSec = sec;
}
void loop()
{
if (prevSec != sec) {
displayTeller(sec);
prevSec = sec;
}
}
…which still hangs for 3-4 seconds after printing the first 0.
I changed the variable type of preset to int, without any difference. BTW, I have used the same code in other sketches, with preset as a long, but those sketches only needed the ISR after a period of time. That is maybe why I never noticed the hanging.