#include <16F877.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES RESERVED                 //Used to set the reserved FUSE bits
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <lcd.c>
#include <keypad.c>   //Libreria de teclado 4x3
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int16 test;
float var,ADC0;
int key,tecla=0,cont0=0,cont1=0,leer=0;
char ch;
void accion (void);



void main()
{
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RB);
   enable_interrupts(INT_RDA);      //Habilita interrupción por recepción RS232
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_adc_ports(ALL_ANALOG);
   setup_adc(adc_clock_internal); // configuro ADC con frecuencia interna
   setup_adc(ADC_CLOCK_DIV_8);
   init_keypad();
   lcd_init();
   lcd_gotoxy(1,1);
   printf(lcd_putc,"Sistema Activado");
   tecla=0;

   // TODO: USER CODE!!
   while (true)
   {
//!   set_adc_channel(0);
//!   test=READ_ADC();
 //  PRINTF("V""%1.2F""VAC""!",ADC0);
   if (tecla==1)
   {
   accion();
   }
//!   if (tecla==0)
//!   {
//!   if (test>=50)
//!   {
//!   output_high(PIN_D3);
//!   }
//!   if (test<50)
//!   {
//!   output_low(PIN_D3);
//!   }
//!   set_adc_channel(1);
//!   test=READ_ADC();
//!   
//!   if (test<50)
//!   {
//!   output_low(PIN_C4);
//!   }
//!   if (test>=50 && test<=150)
//!   {
//!   output_high(PIN_C4);
//!   }
//!   }
   
   }
}

void accion(void)
{
if (key=='1')
{
set_adc_channel(0);
test=READ_ADC();
delay_us(10);
VAR=0.0049*test;
ADC0=VAR;
LCD_GOTOXY(3,1);
PRINTF(LCD_PUTC,"\f%LU   ",test);
PRINTF("V""%LU   ""VAC""!",test);
LCD_GOTOXY(3,2);
PRINTF(LCD_PUTC,"%1.2F",ADC0);
//PRINTF("V""%1.2F""VAC""!",ADC0);
delay_ms(3000);
lcd_gotoxy(1,1);
printf(lcd_putc,"\fSistema Activado");
tecla=0;
}

if (key=='2')
{
set_adc_channel(1);
test=READ_ADC();
delay_us(10);
VAR=28.6*test;
ADC0=VAR;
LCD_GOTOXY(3,1);
PRINTF(LCD_PUTC,"\f%LU   ",test);
PRINTF("C""%LU   ""mA""!",test);
//!LCD_GOTOXY(1,2);
//!PRINTF(LCD_PUTC,"C=%1.2F""mA",ADC0); 
//!PRINTF("%1.2F",ADC0);
delay_ms(3000);
lcd_gotoxy(1,1);
printf(lcd_putc,"\fSistema Activado");
tecla=0;
}

if (key=='4')
{
OUTPUT_TOGGLE(PIN_D3);
tecla=0;
}

if (key=='5')
{
OUTPUT_TOGGLE(PIN_C4);

}

if (key=='6')
{
cont0++;
write_eeprom(cont0,cont0);
lcd_gotoxy(1,1);
PRINTF(LCD_PUTC,"\f%u",cont0);
delay_ms(1000);
lcd_gotoxy(1,1);
printf(lcd_putc,"\fSistema Activado");
tecla=0;
}

if (key=='7')
{
cont1++;
leer=read_eeprom(cont1);
lcd_gotoxy(1,1);
PRINTF(LCD_PUTC,"\f%u",leer);
delay_ms(1000);
lcd_gotoxy(1,1);
printf(lcd_putc,"\fSistema Activado");
tecla=0;
}

}

#INT_RB
void interrupt_keypad()
{
key=read_keypad();         //Asigna la tecla pulsada
tecla=1;                     //levanto bandera de tecla
clear_interrupt(INT_RB);
}

#INT_RDA

void serial_isr(void) 
{
  if(kbhit())
  {
    ch=getchar();
    tecla=1;
    key=ch;
  }
}
