aravinl
Junior Member level 1
- Joined
- Mar 6, 2017
- Messages
- 18
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 125
Hi,
Here's my code
Please help me correct any errors present in it
Here's my code
Code:
#include "p18f4580.h"
#include "xc.h"
#include "htc.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
// #include <delays.h>
#define FREQ 12000000
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1)
#define rs LATAbits.LATA0
#define rw LATAbits.LATA1
#define en LATAbits.LATA2
#define lcdport LATD
#define _XTAL_FREQ 20000000
void tx_data(unsigned char);
unsigned char rx_data();
void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void gsm_cmd(unsigned char *);
void led_blink(char * ,char *,char *);
void output(void);
int i=0,j=0,k,temp,flag,choice,id;
unsigned char *starting_text = "Welcome";
unsigned char *at_cmd = "AT";
unsigned char *echo_off = "ATE0&W";
unsigned char *imei_cmd = "AT+GSN";
unsigned char *sms_format = "AT+CMGF=1";
unsigned char *sms_inc = "AT+CNMI=\"1,1,0,0,0\""; // checks if message is received
unsigned char *sms_send = "AT+CMGS=\"xxxxxxxxxx\""; // 10-Digit Mobile Number
unsigned char *sms = "Hello 0x1A";
unsigned char *gps_on = "AT+CGPSPWR=1";
unsigned char *gps_rst = "AT+CGPSRST=2";
unsigned char *gps_sts ="AT+CGPSSTATUS?";
unsigned char *gps_inf = "AT+CGPSINF=0"; // to send command GPS info from pic to gsm
unsigned char *store_loc = "AT+CPMS=\"<SM,SM>\""; //Received msg location
unsigned char enter=0x0D; // carriage return
unsigned char l_feed=0x0A; // line feed
volatile unsigned char *data;
void main()
{
TRISA = 0;
LATA = 0;
TRISD = 0; //Set port D for LCD interface
LATD = 0;
TRISB = 0;// Set port B for LED interface/CAN interface(RB2 & RB3)
LATB = 0;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 1;
TXSTAbits.SYNC = 0; // Enable UART comm
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
TXSTAbits.BRGH = 1; // high baud rate mode
RCSTAbits.CREN=1; // Activate Reception
PIE1bits.RCIE=1; // Enable Reception interrupt
INTCONbits.GIE=1; // Enable Global interrupt
INTCONbits.PEIE=1; // Enable Peripheral interrupt
char adr[30];
char strtype[10];
char stoloc[10];
char adr[30];
char strtype[10];
char stoloc[10];
lcd_ini();
k=0;
lcdcmd(0x80);
while(starting_text[k]!='\0')
{
lcddata(starting_text[k]);
k++;
}
gsm_cmd(at_cmd);
__delay_ms(1000);
output();
__delay_ms(1000);
gsm_cmd(echo_off);
__delay_ms(1000);
output();
__delay_ms(1000);
gsm_cmd(imei_cmd);
__delay_ms(1000);
output();
__delay_ms(1000);
gsm_cmd(gps_on);
__delay_ms(1000);
output();
__delay_ms(1000);
gsm_cmd(gps_rst);
__delay_ms(1000);
output();
check: gsm_cmd(gps_sts);
if(strcmp(data,"+CGPSSTATUS:Location 2D Fix ")==0)
{
output();
__delay_ms(1000);
}
else if(strcmp(data,"+CGPSSTATUS:Location 3D Fix")==0)
{
output();
__delay_ms(1000);
}
else
{
goto check;
}
gsm_cmd(store_loc);
output();
__delay_ms(1000);
gsm_cmd(sms_format);
output();
__delay_ms(1000);
gsm_cmd(sms_send);
output();
__delay_ms(1000);
if(RCREG=">")
{
gsm_cmd(sms);
output();
__delay_ms(1000);
}
gsm_cmd(sms_inc);
output();
__delay_ms(1000);
int k;
for(k=0;k<=300000;k++)
{
if(strncmp(data,"+CMTI: \"SM\",",12)==0)
break;
}
strcpy(adr,data);
strtok(adr, ",");
strcpy(strtype, strtok(NULL, ","));
strcpy(stoloc, strtok(NULL, ","));
id= atoi(stoloc);
}
void gsm_cmd(unsigned char *string)
{
i=0;j=0;
while(string[i]!='\0')
{
if(string[i]==0x5C) // Not to send '\' character
i++;
tx_data(string[i]); // Send by serial communication
i++;
}
tx_data(enter); // Send ASCII code for 'Enter' key
tx_data(l_feed);
}
void output(void) // To print data on LCD
{
lcdcmd(0x01);
i=0;flag=0;
while(i<j)
{
if(flag>1)
{
flag=0;
__delay_ms(500);
lcdcmd(0x01);
lcdcmd(0x80);
}
if(data[i]==0x0A) // This condition is to avoid double Enter
// during execution of a command
{
flag++;
lcdcmd(0xc0);
}
if(data[i]=='>'||data[i]=='"') // Not to print this character
{
i++;
lcdcmd(0xc0);
}
if(data[i]!=0x0D&&data[i]!=0x0A&&data[i]!=0x1A) // Condition to print the data
// except 'Enter','New line' and 'Submit'
{
lcddata(data[i]);
i++;
}
else
i++;
__delay_ms(300);
}
j=0; // after reading set j=0
lcdcmd(0x01);
}
void tx_data(unsigned char serial_data) // Transmit data function
{
while(PIR1bits.TXIF==0);
TXREG=serial_data;
}
void interrupt SerialRxPinInterrupt()
{
while(!PIR1bits.RCIF);
data[j]= RCREG; // Store the data into array when Reception interrupt occurs
j++;
}
void lcd_ini()
{
lcdcmd(0x38); // Configure the LCD in 8-bit mode, 2 line and 5x7 font
lcdcmd(0x0C); // Display On and Cursor Off
lcdcmd(0x01); // Clear display screen
lcdcmd(0x06); // Increment cursor
lcdcmd(0x80); // Set cursor position to 1st line, 1st column
}
void lcdcmd(unsigned char cmdout)
{
lcdport = cmdout; //Send command to lcdport=PORTB
rs = 0;
rw = 0;
en = 1;
__delay_ms(10);
en =0;
}
void lcddata(unsigned char dataout)
{
lcdport=dataout; //Send data to lcdport=PORTB
rs = 1;
rw = 0;
en = 1;
__delay_ms(10);
en = 0;
}