blackpearl87
Newbie level 5
- Joined
- Jun 8, 2014
- Messages
- 9
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 91
Hi, I'm trying to interface SIM300S GSM module with PIC18F4550 (which comes with the PicDem 2 Plus Board). So far I have developed the below code, but I'm still unable to send a SMS. Could anyone help me to find the error in this program?
Code:
/********************************************************************/
#include "Lcd.h"
#include "General.h"
#include "DisplayMacros.h"
#include <p18f4550.h>
#include <delays.h>
enum State_Machine
{
ST_INIT = 0, ///< Application's state just after Start/Reset
ST_IMEI = 1,
ST_CALL = 2,
ST_ATCMD = 3, ///< App is testing the simple AT Command
ST_SENDSMS = 4 ///< App is sending a text message to a mobile number
};
// Used to prevent buffer overflow
#define FREQ 12000000
#define BAUD 9600
#define SPBRG_VALUE (((FREQ/64)/BAUD)-1)
#define PB1 PORTAbits.RA4
#define PB2 PORTBbits.RB0
UINT8_T nState = ST_INIT;
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 *at_cmd="AT";
unsigned char *call_cmd="ATD**********"; // Provide a 10-Digit Mobile Number
unsigned char *sms_format="AT+CMGF=1";
unsigned char *sms_write="AT+CMGS=\"**********\""; // Provide 10-Digit Mobile Number
unsigned char *sms="Hello";
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)
{
gsmInit();
LCDInit();
LATB = 0;
gsmCode();
} // End of main function
void gsmCode(void)
{
while(1) // Main Infinite loop
{
StateInitial();
if(!PB1)
{
LATB = 0;
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();
//tx_data(enter); // Send ASCII code for 'Enter' key
gsm_cmd(sms);
output();
tx_data(sms_terminate); // Send ASCII code for 'Ctrl+z' key
Wait1s();
}
} // Main infinite loop
}
void gsmInit(void)
{
TRISA = 0xFF;
TRISB = 0xF1;
TRISC = 0xBF;
TRISD = 0x00;
SPBRG = SPBRG_VALUE; // Fill SPBRG register to set the baud rate
RCSTAbits.SPEN = ENABLE; // To activate serial port (Tx and Rx pins)
TXSTAbits.TXEN = ENABLE; // Activate Transmissiom
RCSTAbits.CREN = ENABLE; // Activate Reception
PIE1bits.RCIE = ENABLE; // Enable Reception interrupt
INTCONbits.GIEL = ENABLE; // Peripheral Interrupt enable
INTCONbits.GIEH = ENABLE; // General Interrupt enable
}
void gsm_cmd(unsigned char *string)
{
i = 0; j = 0;
while(string[i] != '\0')
{
temp = 0;
if(string[i] == 0x5C) // Not to send '\' cahracter
i++;
tx_data(string[i]); // Send by serial communication
i++;
while(temp != 1);
}
temp = 0;
tx_data(enter); // Send ASCII code for 'Enter' key
while(temp != 1);
}
void output(void) // To print data on LCD
{
LCDClear(); // Clear LCD for next display
i = -1; flag = 0;
while(i < j)
{
if(flag > 1)
{
flag = 0;
Wait1s();
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++;
Wait1s();
}
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()
{
data[j] = RCREG; // Store the data into array when Reception interrupt occurs
value = RCREG;
j++;
temp = 1;
}
void StateInitial (void)
{
DisplaySplashText(); // Welcome message
Wait2s(); // Leave splash screen for 2 s
LCDClear(); // Clear LCD for next display
}
void DisplaySplashText(void)
{
LCDClear();
WELCOME();
}
void Wait2s(void)
{
Delay10KTCYx(40);
}
void Wait1s(void)
{
Delay10KTCYx(20);
}