
#include <18F2550.h> 

#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,NOPBADEN,USBDIV,PLL1,CPUDIV1,VREGEN 
#use delay(clock=20M) 
#use rs232(stream=PC, baud=4800, parity=N, bits=8, xmit=PIN_C6, rcv=PIN_C7, ERRORS) 
#use rs232(stream=GSM, baud=9600, parity=N, bits=8, xmit=PIN_C1, rcv=PIN_C2,FORCE_SW)

#include <LCD.C>
#include <STDLIB.H> 
#include <MATH.H> 

char strSentance[6];  // String to hold the sentance name 
char strSentance_gsm[6];

#define BUFFER_SIZE 100 
int8 buffer[BUFFER_SIZE]; 
int8 next_in = 0; 
int8 next_out = 0;
int8 buffer_gsm[BUFFER_SIZE]; 
int8 next_in_gsm = 0; 
int8 next_out_gsm = 0;

#INT_RDA 
void serial_isr() {       // Serial Interrupt 
   int8 t;
    int8 t_gsm;
   buffer[next_in]=getc(PC); 
   t=next_in; 
   next_in=(next_in+1) % BUFFER_SIZE; 
   if(next_in==next_out) 
     next_in=t;           // Buffer full !!
     
   buffer_gsm[next_in_gsm]=getc(GSM); 
   t_gsm=next_in_gsm; 
   next_in_gsm=(next_in_gsm+1) % BUFFER_SIZE; 
  if(next_in_gsm==next_out_gsm) 
     next_in_gsm=t_gsm;           // Buffer _gsm full !!  
} 

#define bkbhit (next_in!=next_out) 
#define bkbhit_gsm (next_in_gsm!=next_out_gsm)

int8 bgetc() { 
   int8 c; 
   WHILE(!bkbhit) ; 
   c=buffer[next_out]; 
   next_out=(next_out+1) % BUFFER_SIZE; 
   return(c); 
}

int8 bgetc_gsm() { 
   int8 d; 
   WHILE(!bkbhit_gsm) ; 
   d=buffer_gsm[next_out_gsm]; 
   next_out_gsm=(next_out_gsm+1) % BUFFER_SIZE; 
   return(d);
}

void getsentance() { 
strSentance[0] = bgetc(); 
strSentance[1] = bgetc(); 
strSentance[2] = bgetc(); 
strSentance[3] = bgetc(); 
strSentance[4] = bgetc(); 
strSentance[5] = '\0'; 
} 

void getsentance_gsm() { 
strSentance_gsm[0] = bgetc_gsm(); 
strSentance_gsm[1] = bgetc_gsm(); 
strSentance_gsm[2] = bgetc_gsm(); 
strSentance_gsm[3] = bgetc_gsm();  
strSentance_gsm[4] = '\0'; 
} 

void main() { 
SETUP_ADC(ADC_OFF); 
   SETUP_ADC_PORTS(NO_ANALOGS); 
   SETUP_COMPARATOR(NC_NC_NC_NC); 
   SETUP_TIMER_0(RTCC_OFF); 
   SETUP_TIMER_1(T1_DISABLED);               // Disable Timer 1 
   SETUP_TIMER_2(T2_DISABLED, 127, 1);       // Disable Timer 2 
   SETUP_TIMER_3(T3_DISABLED);               // Disable Timer 3 
   SETUP_CCP1(CCP_OFF);                      // Disable CCP1 
   SETUP_CCP2(CCP_OFF);                      // Disable CCP2 
   ENABLE_INTERRUPTS(GLOBAL); 
   ENABLE_INTERRUPTS(INT_RDA); 
   
   DELAY_MS(50);
   lcd_init();
   lcd_gotoxy(4,2);
   DELAY_MS(50);
   printf(lcd_putc,"start");
   DELAY_MS(500); 
    lcd_init();

   while (TRUE) {
         WHILE (bgetc()!= '$'){            // ha nem akkor mehet a kijelzõre
                    getsentance();         //  
               lcd_gotoxy(10,1); 
               printf(lcd_putc,strsentance); 
             }
         WHILE (bgetc_gsm()!= 'R'){            //  ha nem R mehet a kijelzõre
                    getsentance_gsm();         //  
               lcd_gotoxy(10,2); 
               printf(lcd_putc,strsentance_gsm); 
             }
      }  // End While(1) 
   }  // End Main 
