/********************************************************************/
#include "Lcd.h"
#include "General.h"
#include "DisplayMacros.h"
#include <p18f4550.h>
#include <delays.h>
#define FREQ 12000000
#define BAUD 9600
#define SPBRG_VALUE (((FREQ/64)/BAUD)-1)
#define PB1 PORTAbits.RA4
#define PB2 PORTBbits.RB0
void Wait2s(void); ///< 2s delay used to display splash screen
void Wait1s(void);
void DisplaySplashText(void); ///< Display the welcome Text
void StateInitial(void); ///< Go to initial state
void gsmCode(void);
/*void gsmInit(void);
void gpsCode(void);
void gpsInit(void); */
unsigned char rx_data(void);
void tx_data(unsigned char);
void gsm_cmd(unsigned char *);
void output(void);
void interrupt(void);
unsigned char value = 0;
int i = 0,j,k,temp,flag;
unsigned char *starting_text = "Enter Choice =";
unsigned char *dial_text = "Dialing...";
unsigned char *at_cmd = "AT";
unsigned char *imei_cmd = "AT+GSN";
unsigned char *call_cmd = "ATDxxxxxxxxxx"; // Provide a 10-Digit Mobile Number
unsigned char *sms_format = "AT+CMGF=1";
unsigned char *sms_write = "AT+CMGS=\"xxxxxxxxxx\""; // 10-Digit Mobile Number
unsigned char *sms = "Hello";
unsigned char *sms_report = "SMS Sent...";
unsigned char sms_terminate = 0x1A;
unsigned char enter = 0x0D;
unsigned char *data;
unsigned char longi_data[12];
unsigned char lati_data[12];
void main(void)
{
TRISA = 0xFF;
TRISB = 0xF1;
TRISC = 0x80;
TRISD = 0x00;
SPBRG = SPBRG_VALUE; // Fill SPBRG register to set the baud rate
RCSTAbits.SPEN = 1; // To activate serial port (Tx and Rx pins)
TXSTAbits.TXEN = 1; // Activate Transmissiom
RCSTAbits.CREN = 1; // Activate Reception
PIE1bits.RCIE = 1; // Enable Reception interrupt
INTCONbits.GIE = 1; // Peripheral Interrupt enable
INTCONbits.PEIE = 1; // General Interrupt enable
LATA = 0; LATB = 0; LATC = 0; LATD = 0;
LCDInit();
gsmCode();
} // End of main function
void gsmCode(void)
{
while(1) // Main Infinite loop
{
StateInitial();
if(!PB1)
{
PORTBbits.RB1 = 1;
gsm_cmd(at_cmd);
output();
Wait1s();
}
if(!PB2)
{
LATB = 0;
PORTBbits.RB2 = 1;
gsm_cmd(sms_format);
output();
Wait1s();
gsm_cmd(sms_write);
output();
Wait1s();
gsm_cmd(sms);
output();
tx_data(sms_terminate); // Send ASCII code for 'Ctrl+z' key
Wait1s();
}
} // Main infinite loop
}
void gsm_cmd(unsigned char *string)
{
i = 0; j = 0; temp = 0;
while(string[i] != '\0')
{
temp = 0;
if(string[i] == 0x5C) // Not to send '\' character
i++;
tx_data(string[i]); // Send by serial communication
i++;
while(temp != 1);
}
tx_data(enter); // Send ASCII code for 'Enter' key
interrupt();
while(temp != 1);
}
void output(void) // To print data on LCD
{
i = 0; flag = 0;
LCDClear(); // Clear LCD for next display
while(i < j)
{
if(flag > 1)
{
flag = 0;
Delay10KTCYx(10); // 500ms delay
LCDClear(); // Clear LCD for next display
LCDGoto(0,0);
}
/* This condition is to avoid double Enter during execution of a command */
if(data[i] == 0x0A)
{
flag++;
LCDGoto(0,1);
}
/* Not to print this character */
if(data[i]=='>' || data[i]=='"')
{
i++;
LCDGoto(0,1);
}
/* Condition to print the data, except 'Enter','New line' and 'Submit' */
if(data[i]!=0x0D && data[i]!=0x0A && data[i]!=0x1A)
{
LCDPutChar(data[i]);
i++;
}
else
i++;
Delay10KTCYx(6); // 300ms delay
}
LCDClear(); // Clear LCD for next display
}
void tx_data(unsigned char serial_data) // Transmit data function
{
TXREG = serial_data;
while(PIR1bits.TXIF == 0);
}
void interrupt(void)
{
data[j] = RCREG; // Store the data into array when Reception interrupt occurs
value = data[j];
j++;
temp = 1;
}
void StateInitial(void)
{
DisplaySplashText(); // Welcome message
Wait2s(); // Leave splash screen for 2 s
}
void DisplaySplashText(void)
{
LCDClear();
WELCOME();
}
void Wait2s(void)
{
Delay10KTCYx(40);
}
void Wait1s(void)
{
Delay10KTCYx(20);
}