#include <Arduino.h>
#include "timer_System.h"
void SetTimer1()
{
cli(); //stop interrupts
//set timer1 interrupt at 120Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 60hz increments
OCR1A = 10000; // = (16*10^6) / (120 * 8) - 1 (must be <65536) //60Hz 16683
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
//TCCR1B |= (1 << CS12) | (1 << CS10);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); //allow interrupts
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
ISR(TIMER1_COMPA_vect)
{
flagTickBase = 1;
TimersSys_Tick();
}
void setup() {
// put your setup code here, to run once:
// put your main code here, to run repeatedly:
SetTimer1();
/////////////////////////////////////////////////////////////////////
TimersSys_Init(); // Inicializa la estructuras de los 4 timers del sistema, en el header de la libreria
TimersSys_Start(0, 5); // Inicializa 0 de TimerSys, 50 ms
TimersSys_Start(1, 10); // Inicializa 1 de TimersSys: 100 ms
TimersSys_Start(2, 25); // Inicializa 2 de TimersSys: 250 ms
TimersSys_Start(3, 100); // Inicializa 2 de TimersSys: 1 seg
/////////////////////////////////////////////////////////////////////
Serial.begin(115200);
}
void loop()
{
//////////////////////////////////////////////
if (TIMERSYS_CHECK_EVENT(0)) // 50 ms
{
TIMERSYS_CLEAR_EVENT(0);
Serial.println("t0");
//Serial.println("");
}
//////////////////////////////////////////////
if (TIMERSYS_CHECK_EVENT(1)) // 100 ms
{
TIMERSYS_CLEAR_EVENT(1);
}
//////////////////////////////////////////////
if (TIMERSYS_CHECK_EVENT(2)) //250ms
{
TIMERSYS_CLEAR_EVENT(2);
}
//////////////////////////////////////////////
if (TIMERSYS_CHECK_EVENT(3)) //1 seg
{
TIMERSYS_CLEAR_EVENT(3);
}
}