
#include <18F14K50.h>

#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV19                
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOMCLR                   //Master Clear pin disabled (Set as Digital Input)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES HFOFST                
#FUSES NOWRT0                
#FUSES NOWRT1                
#FUSES USBDIV1               
#FUSES BBSIZ2K                  //2K words Boot Block size
#FUSES PLLEN                 
#FUSES CPUDIV4                  //System Clock by 4
#FUSES PCLKEN                

#use delay(clock=12000000)

#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,bits=8,stream=SERIAL_SIM900)
#use rs232(baud=9600,parity=N,xmit=PIN_C3,rcv=PIN_C2,bits=8,stream=SERIAL_XBeePRO)



/*
Version 1
Ejemplo para la placa GSM
Fecha:15/11/2011
Compilador a utilizar: CCS 4.104
Al presionar el pulsador1 enciende el led RC6
Al presionar el pulsador2  se envia un SMS al telefono asignado
ATENCION: No olvidar que el led NEt debe tener un destello de 64ms on y 3000ms off
Detalle de estado del led de NET
 OFF  Módulo Apagado.
 64ms on/ 800ms no encuentra la red.
 64ms on/3000ms red encontrada.
 64ms on/300ms GPRS comunicación
*/

#define PIN_ON  output_high
#define PIN_OFF output_low
#define RELAY1  PIN_C0
#define RELAY2  PIN_C1
//#define PULSADOR1  PIN_C2
//#define PULSADOR2  PIN_C3
#define PULSADOR3  PIN_B6
#define PWM_LED  PIN_C5
#define LED1  PIN_C6
#define LED2  PIN_C7
#define TEMP PIN_B4
#define POWERKEYGSM  PIN_C4


//************************************************************************************************************************************* 
//--------------------------------------Funciones 
//************************************************************************************************************************************* 


char valor=0;
boolean x=0;









#int_EXT2                    //interrupción externa ligada al Stream XBeePRO puerto COM por software.
void  EXT2_isr(void)
{

   valor=getc(SERIAL_XBeePRO);
   x=1;
   
   
   return;

}





void inicializacionSIM900(void)
{
   int cont;
   delay_ms(1000);
   puts("A",SERIAL_SIM900); //para sincronizar el baudrate del SIM 900
   delay_ms(300);
   putchar(0x0d,SERIAL_SIM900);
   delay_ms(1000);
   for(cont=0;cont<5;cont++)              // mando varios AT para el autobauding
         {
         puts("AT",SERIAL_SIM900);
         delay_ms(200);
         putchar(0x0d,SERIAL_SIM900);
         delay_ms(200);
         PIN_ON(LED1);
         delay_ms(200);
         PIN_OFF(LED1);
         }
//-----------------------------------seteos de configuracion del SIM900------------------------         
   puts("AT+CMGF=1\r",SERIAL_SIM900);                  // configuro para que trabaje en modo texto y no PDU
   delay_ms(100);
   putchar(0x0d,SERIAL_SIM900);
   delay_ms(100);
   return;
}







//*************************************************************************************************************************************
//************************************************************************************************************************************* 
//------------------------------------Programa Principal
//************************************************************************************************************************************* 
void main()
{
   int8 i;
   //disable_interrupts(GLOBAL);
   enable_interrupts(INT_EXT2_H2L);
   //enable_interrupts(GLOBAL);
   //disable_interrupts(int_EXT2);
   //ext_int_edge(2,H_to_L);   
    
   set_tris_b(0b00001100);
   set_tris_c(0b01110100);   
   
   setup_adc_ports(NO_ANALOGS);
                        //setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);

   



   
   PIN_OFF(RELAY1);
   PIN_OFF(RELAY2);
   PIN_ON(LED1);
   PIN_ON(LED2);
   
   
   PIN_OFF(POWERKEYGSM);    //Enciendo modulo GSM
   delay_ms(1000);
   inicializacionSIM900();  //Inicializo el SIM900

   for(i=0;i<50;i++)
      {
       OUTPUT_TOGGLE(LED2);
       delay_ms(50);  
      };
      
      PIN_ON(LED1);
      PIN_OFF(LED2);
      
   
   
   
   enable_interrupts(GLOBAL);
   //interrupt_active(int_EXT2);
     
     PIN_OFF(LED1);
     

for(;;){



  if (x==1)    
     {
     disable_interrupts(GLOBAL);
     //disable_interrupts(int_EXT2);
     PIN_ON(LED1);
     PIN_ON(LED2);
   
 
     fprintf(SERIAL_SIM900,"AT+CMGS=\"0380154631193\"\r"); // aca poner el numero de celular que recibe el SMS
     delay_ms(300);
   
     PIN_OFF(LED2);
      
     fprintf(SERIAL_SIM900,"Valor recibido: %c",valor);
   
     delay_ms(300);
    
     PIN_ON(LED2);
      
     fprintf(SERIAL_SIM900,"%c",0x1a);
   
     PIN_OFF(LED2);
     delay_ms(200);
     PIN_ON(LED2);
     delay_ms(200);
     PIN_OFF(LED2);
     delay_ms(200);
     PIN_ON(LED2);
     delay_ms(200);
     PIN_OFF(LED2);
     delay_ms(200);
     
     x=0;
     
     //enable_interrupts(int_EXT2);
     enable_interrupts(GLOBAL);
     }
  
  
  else
     {
     Output_toggle(LED1);
     Output_toggle(LED2);
     delay_ms(100);
     } 



}





}

