#include "timer_System.h"
#include "calib_manager.h"
#include "ac_adq.h"
#include "inps_manager.h"
#include "led_manager.h"
#include "pot_manager.h"
#include "adjust_manager.h"
#include "rele_manager.h"
#include "tc_manager.h"
#include "global.h"
/*#define POT1 0
#define POT2 1
#define POT3 2*/
uint8_t REDLED = 10;
uint8_t GREENLED = 9;
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)
{
TimersSys_Tick();
inpManager_Refresh();
//Serial.println("Timer Test");
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(57200);
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
SetTimer1();
TimersSys_Init(); // Inicializa la estructuras de los 4 timers del sistema, en el header de la libreria
TimersSys_Start(0, 10); // Inicializa 0 de TimerSys, de calibración 100 ms
TimersSys_Start(1, 12); // Inicializa 1 de TimersSys: 120ms (12 * Tick de TMR2(10ms))
TimersSys_Start(2, 6); // Inicializa 2 de TimersSys: 60ms (6 * Tick de TMR2(10ms)) 16 por segundo
TimersSys_Start(3, 200); // Inicializa 3 de TimersSys: 2s (200 * Tick de TMR2(10ms))
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
relay_Init(RELAY_CNNDLY_DISABLE & RELAY_DCNNDLY_DISABLE & RELAY_POL_DIRECT & (RELAY_GPIO_MASK_PIN | 4),0,0);
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// put your setup code here, to run once:
//pinMode(PINLED, OUTPUT);
inpsManager_Init();
inpsManager_Cfg(0, 3, INPUT_PULLUP);
inpsManager_Cfg(1, 2, INPUT_PULLUP);
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
potManager_Init(); //inicializa las estructuras de potenciometro
potManager_CfgPot(POT1, A1); //
potManager_CfgPot(POT2, A2); //
potManager_CfgPot(POT3, A3); //
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
ledManager_CfgLed(0, REDLED);
ledManager_CfgLed(1, GREENLED);
ledManager_DefSecOut(LED_BLINK1, 0b11000000); //define la secuencias del estado BLINK1
ledManager_DefSecOut(LED_BLINK2, 0b11001100); //define la secuencias del estado BLINK1
ledManager_Update(0, LED_BLINK2);
ledManager_Update(1, LED_BLINK1);
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//lectura rapida de cada uno de los potenciometros de esta
//manera en el primer ciclo de procesamiento ya se tiene informacion
potManager_fastRefresh(POT1);
potManager_fastRefresh(POT2);
potManager_fastRefresh(POT3);
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
flags_adq.vector_adq = 0;
calibrate_Product();
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
adjust_Mode();
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
adjust_Escales(); // Ajusta las escalas de los pots según el producto:
llenar_ArregloGTC();
flagInitGTC = false;
}
void loop()
{
//////////////////////////////////////////////
if (TIMERSYS_CHECK_EVENT(2)) // 120 ms
{
TIMERSYS_CLEAR_EVENT(2);
Serial.println("T120");
ledManager_Refresh();
adjust_B_Reg(); // Ajusta los B que serán colocados en pot1 y pot2 (GSM-L/GSM-C)
adjust_Escales(); // Ajusta las escalas de los pots según el producto:
llenar_ArregloGTC();
processTimerGTC(); //se procesa el temporizador GTC
}
//llevar esta a 50 ms
if (TIMERSYS_CHECK_EVENT(1)) // 60 ms
{
TIMERSYS_CLEAR_EVENT(1);
potManager_Refresh();
}
if (TIMERSYS_CHECK_EVENT(3)) //2s
{
TIMERSYS_CLEAR_EVENT(3);
//adjust_Escales(); // Ajusta las escalas de los pots según el producto:
//llenar_ArregloGTC();
//Serial.println(tabla[0], DEC);
//Serial.println(tabla[1], DEC);
//Serial.println(pot1, DEC);
///Serial.println(pot2, DEC);
//Serial.println(pot3, DEC);
}
if(inpsInfo[0].b.change == 1)
{
inpsInfo[0].b.change = 0;
if(GET_INP_STATE(0))
{
Serial.println("Pin0, Alto");
}
else
{
Serial.println("Pin0, Bajo");
}
}
}