80% found this document useful (10 votes)
9K views

Program To Interface GSM Module With 8051 Micro Controller

This program interfaces a GSM module with an 8051 microcontroller to send and receive data via serial communication without using a PC. It initializes the serial port and LCD, then transmits the character 'A' followed by 'T' and a carriage return. It receives incoming serial data, checks for newline or carriage return, and displays the characters on the LCD by shifting the array.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
80% found this document useful (10 votes)
9K views

Program To Interface GSM Module With 8051 Micro Controller

This program interfaces a GSM module with an 8051 microcontroller to send and receive data via serial communication without using a PC. It initializes the serial port and LCD, then transmits the character 'A' followed by 'T' and a carriage return. It receives incoming serial data, checks for newline or carriage return, and displays the characters on the LCD by shifting the array.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

// Program to Interface GSM Module with 8051

microcontroller (AT89C51) without using PC


#include<reg51.h> #define port P1 #define dataport P2 sbit rs = port^2; sbit rw = port^3; sbit en = port^4; int count,i; unsigned char check,str[15]; bit check_space;

// Data port for LCD

void init_serial() // Initialize serial port { TMOD=0x20; // Mode2 TH1=0xfd; // 9600 baud SCON=0x50; // Serial mode=1 ,8-Bit data,1 Stop bit ,1 Start bit, Receiving on TR1=1; // Start timer } void delay(unsigned int msec) // Function for delay { int i,j; for(i=0;i<msec;i++) for(j=0; j<1275; j++); } void lcd_cmd(unsigned char item) command on LCD { dataport = item; rs= 0; rw=0; en=1; delay(1); en=0; return; } void lcd_data(unsigned char item) display character on LCD { dataport = item; rs= 1; rw=0; en=1; // Function to send

// Function to

delay(1); en=0; return; } void lcd_data_string(unsigned char *str) display string on LCD { int i=0; while(str[i]!='\0') { lcd_data(str[i]); i++; delay(10); } return; } void lcd() { lcd_cmd(0x38); using 8-bit 2 row LCD delay(5); lcd_cmd(0x0F); display on cursor blinking delay(5); lcd_cmd(0x80); the cursor on first position of LCD delay(5); } void transmit_data(unsigned char str) // Function to transmit data through serial port { SBUF=str; //Store data in SBUF while(TI==0); //Wait till data transmits TI=0; } void receive_data() interrupt 4 // Function to recieve data serialy from RS232 into microcontroller { RI=0; // Function to

// For

// For

// Set

str[++count]=SBUF; }

//Read SBUF

unsigned char byte_check() // Function to check carraige return and new line character { switch(str[0]) { case 0x0a: { // Return 0x00 for new line return 0x00; break ; } case 0x0d: { // Return 0x01 for carriage return return 0x01; break ; } default :return 0x02 ; // Return 0x02 for characters except new line and carriage return } } void main() { lcd(); Initialize LCD init_serial(); // Initialize serial port count=(-1); delay(500); lcd_data_string("Ready"); delay(10); lcd_cmd(0x01); IE=0x94; transmit_data('A'); 'A' to serial port delay(1); transmit_data('T'); 'T' to serial port delay(1); transmit_data(0x0d); Transmit carriage return to serial port delay(50);

//

// Transmit // Transmit //

while(1) { if(count>=0) { check=byte_check(); Check the character if(check!=0x00) { if(check==0x01) { if(check_space==1) Check previous character { lcd_data(0x20); check_space=0; } } else { lcd_data(str[0]); check_space=1; } } count--; for(i=0;i<count;i++) Shift the whole array to one left { str[i]=str[i+1]; } } } }

//

//

//

// Program to get latitude and longitude value from GPS modem and display it on LCD:
/* LCD DATA port----PORT A signal port------PORT B rs-------PB0 rw-------PB1 en-------PB2 */ #define F_CPU 12000000UL

#include<avr/io.h> #include<util/delay.h> #define USART_BAUDRATE 4800 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) #define LCD_DATA PORTA #define #define #define #define void void void void ctrl PORTB en PB2 rw PB1 rs PB0 //LCD data port //enable signal //read/write signal //resister select signal

LCD_cmd(unsigned char cmd); init_LCD(void); LCD_write(unsigned char data); LCD_write_string(unsigned char *str);

void usart_init(); unsigned int usart_getch(); unsigned char value,i,lati_value[15],lati_dir, longi_value[15], longi_dir, alti[5] ; int main(void) { DDRA=0xff; //LCD_DATA port as out put port DDRB=0x07; //ctrl as out put init_LCD(); //initialization of LCD _delay_ms(50); // delay of 50 mili seconds LCD_write_string("we at"); LCD_cmd(0xC0); usart_init(); // initialization of USART while(1) { value=usart_getch(); if(value=='$') { value=usart_getch(); if(value=='G') { value=usart_getch(); if(value=='P') { value=usart_getch(); if(value=='G')

{ value=usart_getch(); if(value=='G') { value=usart_getch(); if(value=='A') { value=usart_getch(); if(value==',') { value=usart_getch(); while(value!=',') { value=usart_getch(); } lati_value[0]=usart_getch(); value=lati_value[0]; for(i=1;value!=',';i++) { lati_value[i]=usart_getch(); value=lati_value[i]; } lati_dir=usart_getch(); value=usart_getch(); while(value!=',') { value=usart_getch(); } longi_value[0]=usart_getch(); value=longi_value[0]; for(i=1;value!=',';i++) { longi_value[i]=usart_getch();

value=longi_value[i]; } longi_dir=usart_getch(); LCD_cmd(0x01); _delay_ms(1); LCD_cmd(0x80); _delay_ms(1000); i=0; while(lati_value[i]!='\0') { LCD_write(lati_value[j]); j++; } LCD_write(lati_dir); LCD_cmd(0xC0); _delay_ms(1000); i=0; while(longi_value[i]!='\0') { LCD_write(longi_value[i]); i++; } LCD_write(longi_dir); _delay_ms(1000); } } } } } } }

} } void init_LCD(void) { LCD_cmd(0x38); 8bit mode _delay_ms(1); LCD_cmd(0x01); _delay_ms(1); LCD_cmd(0x0E); _delay_ms(1); LCD_cmd(0x80); is for 0th position _delay_ms(1); return; }

//initialization of 16X2 LCD in

//clear LCD //cursor ON // ---8 go to first line and --0

void LCD_cmd(unsigned char cmd) { LCD_DATA=cmd; ctrl =(0<<rs)|(0<<rw)|(1<<en); _delay_us(40); ctrl =(0<<rs)|(0<<rw)|(0<<en); //_delay_ms(50); return; } void LCD_write(unsigned char data) { LCD_DATA= data; ctrl = (1<<rs)|(0<<rw)|(1<<en); _delay_us(40); ctrl = (1<<rs)|(0<<rw)|(0<<en); //_delay_ms(50); return ; } void usart_init() {

UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register } unsigned int usart_getch() { while ((UCSRA & (1 << RXC)) == 0); // Do nothing until data have been recieved and is ready to be read from UDR return(UDR); // return the byte } void LCD_write_string(unsigned char *str) of the string in pionter *str { int i=0; while(str[i]!='\0') till the NULL charaters is soon in string { LCD_write(str[i]); CD byte by byte i++; } return; } //take address vaue

// loop will go on // sending data on

Program to Interface GSM Module with 8051 microcontroller (AT89C51) without using PC
// #include<reg51.h> #define port P1 #define dataport P2 sbit rs = port^2; sbit rw = port^3;

// Data port for LCD

sbit en = port^4; int count,i; unsigned char check,str[15]; bit check_space; void init_serial() // Initialize serial port { TMOD=0x20; // Mode2 TH1=0xfd; // 9600 baud SCON=0x50; // Serial mode=1 ,8-Bit data,1 Stop bit ,1 Start bit, Receiving on TR1=1; // Start timer } void delay(unsigned int msec) // Function for delay { int i,j; for(i=0;i<msec;i++) for(j=0; j<1275; j++); } void lcd_cmd(unsigned char item) command on LCD { dataport = item; rs= 0; rw=0; en=1; delay(1); en=0; return; } void lcd_data(unsigned char item) display character on LCD { dataport = item; rs= 1; rw=0; en=1; delay(1); en=0; return; } void lcd_data_string(unsigned char *str) display string on LCD { // Function to send

// Function to

// Function to

int i=0; while(str[i]!='\0') { lcd_data(str[i]); i++; delay(10); } return; } void lcd() { lcd_cmd(0x38); using 8-bit 2 row LCD delay(5); lcd_cmd(0x0F); display on cursor blinking delay(5); lcd_cmd(0x80); the cursor on first position of LCD delay(5); } void transmit_data(unsigned char str) // Function to transmit data through serial port { SBUF=str; //Store data in SBUF while(TI==0); //Wait till data transmits TI=0; } void receive_data() interrupt 4 // Function to recieve data serialy from RS232 into microcontroller { RI=0; str[++count]=SBUF; //Read SBUF } unsigned char byte_check() // Function to check carraige return and new line character { switch(str[0])

// For

// For

// Set

{ case 0x0a: { line return 0x00; break ; } case 0x0d: { // Return 0x01 for carriage return return 0x01; break ; } default :return 0x02 ; // Return 0x02 for characters except new line and carriage return } } void main() { lcd(); Initialize LCD init_serial(); // Initialize serial port count=(-1); delay(500); lcd_data_string("Ready"); delay(10); lcd_cmd(0x01); IE=0x94; transmit_data('A'); 'A' to serial port delay(1); transmit_data('T'); 'T' to serial port delay(1); transmit_data(0x0d); Transmit carriage return to serial port delay(50); while(1) { if(count>=0) { check=byte_check(); Check the character if(check!=0x00) { // Return 0x00 for new

//

// Transmit // Transmit //

//

if(check==0x01) { if(check_space==1) Check previous character { lcd_data(0x20); check_space=0; } } else { lcd_data(str[0]); check_space=1; } } count--; for(i=0;i<count;i++) Shift the whole array to one left { str[i]=str[i+1]; } } } }

//

//

MAIN SOURCE CODE


#include <REGX51.H> #include"SERIAL.c" #include"LCD.c" sbit rf=P2^0; sbit relay=P0^1; sbit motor=P0^0; unsigned char asc,ok[2],r[4],a,n[15],e[15],i,oxoa, a,m[20],j,temp[10],temp1[10]; void cmdwrt_lcd(unsigned char cmd); void serial_transmit(unsigned char *srstr); void command(unsigned char d[]); void serial_tx(unsigned char srda);

You might also like