Hi, I'm currently working on a project. I'm using a PIC16f887 microcontroller to help me send a sms to the user. It is supposed to allow the user to enter their phone number and it will send a sms to the user. The keypad is working fine but I have problems with the GSM.
The code I've attached below is my main code.
The code I've attached below is the code for the GSM.
The code is supposed to allow for 115200 baud rate at 8Mhz
Can anyone help me check my code. Thanks, really appreciate it.
The code I've attached below is my main code.
Code:
void main(void)
{
int t,a,b;
t = a = b = 0;
static char Duration[3];
static char PHnum[11];
char Key = 'n';
LCD_Begin();
InitKeypad();
InitGSM();
LCD_Cmd(LCD_CLEAR);
LCD_Print("Enter Phone No.");
while(a<10){
Key = switch_press_scan_num();
if ((Key == '#')&&(t>0))
{
PHnum[a-1]=' ';
LCD_Goto(1, 2);
LCD_Printr(PHnum);
__delay_ms(200);
a--;
}
else if (Key != '#')
{
LCD_Goto(1, 2);
PHnum[a] = Key;
LCD_Printr(PHnum);
__delay_ms(200);
a++;
}
};
LCD_Cmd(LCD_CLEAR);
while (b<10){
phoneno[b]=PHnum[b];
b++;
}
sms("Hello World");
}
The code I've attached below is the code for the GSM.
Code:
void InitGSM(){
TRISCbits.TRISC6 = 0; //Set RC6 (TX) as output
TRISCbits.TRISC7 = 1; //Set RC7 (RX) as input
TXSTA = 0b00100100;
RCSTA = 0b10010000;
BAUDCTL |= 0b00001000;
SPBRG = 16; //Baud rate 115200 @ 8 MHz
TXIF = RCIF = 0; //Disable interrupt(For reseting purpose)
}
void sms(unsigned char *msg){
tx_str("AT\r\n");
__delay_ms(500);
tx_str("AT+CMGF=1\r\n"); //Enter GSM SMS TEXT MODE
__delay_ms(500);
tx_str("AT+CMGS="); //SEND SMS to Phone number
tx('"');
for(int l = 0; l <= 9; l++){
tx(phoneno[l]); //Enter phone number with size 10 array
}
tx('"');
tx_str("\r\n"); //After enter will be entering SMS
__delay_ms(1000);
while(*msg)
tx(*msg++); //Send SMS
tx(0x1A); //CTRL Z ASCII code to send
__delay_ms(500);
}
void tx(char a) //Function to send one byte of data to UART
{
while(TXIF==0) continue; //Hold the program until TX buffer is free to transmit
TXREG=a;
}
unsigned char rx(void){
while(PIR1bits.RCIF == 0); //Hold the program until RX buffer is free to receive
return RCREG;
}
void tx_str(const char *s) //Function to convert string to byte
{
while(*s)
tx(*s++);
}
The code is supposed to allow for 115200 baud rate at 8Mhz
Can anyone help me check my code. Thanks, really appreciate it.