Ahhh, nevermind.... I found another solution. I took a look at this page:
http://popdevelop.com/2010/04/mastering-timer-interrupts-on-the-arduino/
And create my own MyTimer library. ![]()
It works great, and has no overflow problem. I'm still checking how I can improve it, and even how to have a better resolution, but for now its pretty neat. ![]()
Here's the whole library and also an example below.
MyTimer.h
/*
Original code by Sebastian Wallin
http://popdevelop.com/2010/04/mastering-timer-interrupts-on-the-arduino/
Adapted and Optimized by WilliamK @ Wusik Dot Com (c) 2010
http://arduino.wusik.com
This timer will clock 125000 times in a second.
You must add to your Arduino code the following:
ISR(TIMER2_OVF_vect) { timer.tick(); } // "timer" as in your "MyTimer timer = MyTimer();" variable //
*/
#ifndef MYTIMER_h
#define MYTIMER_h
#include <inttypes.h>
#include "HardwareSerial.h"
// ------------------------------------------------------------------------------------------- //
class MyTimer
{
public:
MyTimer();
inline void tick(void)
{
TCNT2 = 255;
timerCounter++;
if (timerCounter == timerTimeValue) { eventCounter++; timerCounter = 0; }
}
void stop(void);
void start(void);
void setTime(unsigned long time);
boolean hasEvent(void);
private:
unsigned long timerCounter;
unsigned long timerTimeValue;
int8_t eventCounter;
};
#endif
MyTimer.cpp
/*
Original code by Sebastian Wallin
http://popdevelop.com/2010/04/mastering-timer-interrupts-on-the-arduino/
Adapted and Optimized by WilliamK @ Wusik Dot Com (c) 2010
http://arduino.wusik.com
See MyTimer.h for instructions
*/
#include "WConstants.h"
#include "MyTimer.h"
// ------------------------------------------------------------------------------------------- //
MyTimer::MyTimer()
{
eventCounter = 0;
timerCounter = 0;
timerTimeValue = 125000;
}
// ------------------------------------------------------------------------------------------- //
void MyTimer::start(void)
{
TIMSK2 &= ~(1<<TOIE2);
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
ASSR &= ~(1<<AS2);
TIMSK2 &= ~(1<<OCIE2A);
TCCR2B |= (1<<CS22) | (1<<CS20);
TCCR2B &= ~(1<<CS21);
eventCounter = 0;
timerCounter = 0;
TCNT2 = 255;
TIMSK2 |= (1<<TOIE2);
}
// ------------------------------------------------------------------------------------------- //
void MyTimer::stop(void)
{
TIMSK2 &= ~(1<<TOIE2);
eventCounter = 0;
timerCounter = 0;
}
// ------------------------------------------------------------------------------------------- //
boolean MyTimer::hasEvent(void)
{
if (eventCounter > 0)
{
eventCounter--;
return true;
}
return false;
}
// ------------------------------------------------------------------------------------------- //
void MyTimer::setTime(unsigned long time)
{
timerTimeValue = time;
}
MyTimerExample.pde
/*
Original code by Sebastian Wallin
Adapted and Optimized by WilliamK @ Wusik Dot Com
http://arduino.wusik.com
*/
#include <WProgram.h>
#include <MyTimer.h>
int8_t secondsCounter = 0;
int8_t minutesCounter = 0;
int8_t hoursCounter = 0;
int8_t daysCounter = 0;
MyTimer timer = MyTimer();
ISR(TIMER2_OVF_vect) { timer.tick(); }
void setup()
{
Serial.begin(115200);
timer.setTime(125000); // one second //
timer.start();
}
void loop()
{
while (timer.hasEvent())
{
secondsCounter++;
if (secondsCounter == 60) { secondsCounter = 0; minutesCounter++; }
if (minutesCounter == 60) { minutesCounter = 0; hoursCounter++; }
if (hoursCounter == 24) { hoursCounter = 0; daysCounter++; }
Serial.print("Day: ");
Serial.print(daysCounter, DEC);
Serial.print(" Time: ");
Serial.print(hoursCounter, DEC);
Serial.print(":");
Serial.print(minutesCounter, DEC);
Serial.print(":");
Serial.println(secondsCounter, DEC);
}
}