Skip to content

Instantly share code, notes, and snippets.

@jha-adrs
Created April 12, 2023 13:01
Show Gist options
  • Save jha-adrs/b8ccd22428612456dc561c5734f5a00e to your computer and use it in GitHub Desktop.
Save jha-adrs/b8ccd22428612456dc561c5734f5a00e to your computer and use it in GitHub Desktop.
Project for interfacing GSM with 8051 using Proteus
#include<reg52.h>
#define display_port P1 //Data pins connected to port 2 on microcontroller
sbit rs = P2^0; //RS pin connected to pin 0 of port 2
sbit rw = P2^1; // RW pin connected to pin 1 of port 2
sbit e = P2^2; //E pin connected to pin 2 of port 2
int k;
int i;
unsigned char str[26];
unsigned char text_mode[9]="AT+CMGF=1";
unsigned char mob[9]="AT+CMGS=";
idata char mobile_no[14] = "+917991175416";
void GSM_init() // serial port initialization
{
TMOD=0x20; // Timer 1 selected, Mode 2(8-bit auto-reload mode)
TH1=0xfd; // 9600 baudrate
SCON=0x50; // Mode 1(8-bit UART), receiving enabled
TR1=1; // Start timer
}
void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned m,n ;
for(m=0;m<time;m++)
for(n=0;n<1275;n++);
}
void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
{
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x80); // bring cursor to beginning of first line
msdelay(10);
}
void lcd_string(unsigned char *str) // Function to display string on LCD
{
int i=0;
while(str[i]!='\0')
{
lcd_data(str[i]);
i++;
msdelay(10);
if(i==15) lcd_cmd(0xc2);
}
return;
}
void GSM_write(unsigned char ch) // Function to send commands to GSM
{
SBUF=ch; // Put byte in SBUF to send to GSM
while(TI==0); //wait until the byte trasmission
TI=0; //clear TI to send next byte.
}
void GSM_read() // Function to read the response from GSM
{
while(RI==0); // Wait until the byte received
str[k]=SBUF; //storing byte in str array
RI=0; //clear RI to receive next byte
}
void main()
{
k=0;
lcd_init();
GSM_init();
msdelay(150);
lcd_string("Welcome");
msdelay(150);
lcd_cmd(0x01);
lcd_string("Interfacing GSM with 8051");
msdelay(150);
lcd_cmd(0x01); // Clear LCD screen
msdelay(50);
GSM_write('A');lcd_data('A');msdelay(1);
GSM_write('T');lcd_data('T');msdelay(1);
GSM_write(0x0d); // Sending carriage return to GSM module
msdelay(150);
lcd_cmd(0x01); // Clear LCD screen
lcd_string("AT OK");
k=0;
// ATE0 to stop echo from GSM module
lcd_cmd(0x01);
GSM_write('A');lcd_data('A');msdelay(1);
GSM_write('T');lcd_data('T');msdelay(1);
GSM_write('E');lcd_data('E');msdelay(1);
GSM_write('0');lcd_data('0');msdelay(1);
GSM_write(0x0d);
msdelay(150);
lcd_cmd(0x01); // Clear LCD screen
lcd_string("ECHO OFF");
GSM_write(0x0d); //CARRAIGE RETURN TO GSM
msdelay(150);
lcd_cmd(0x01);
msdelay(150);
lcd_cmd(0x01); // Clear LCD screen
lcd_string("Waiting...");
GSM_write(0x0d); //CARRAIGE RETURN TO GSM
msdelay(150);
lcd_cmd(0x01);
lcd_string("This is a sample message");
msdelay(500);
lcd_cmd(0x01);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment