In this tutorial, you will learn the GSM module Interfacing with 8051. With the GSM module, we can connect our microcontroller with the GSM network. By connecting to the GSM network, Our microcontroller will be able to make calls, send SMS, etc. In this tutorial, we will be using the GSM module, SIM900A.
The microcontroller communicates with the GSM module serially using AT commands and there will be required response from the GSM module. The most simple command is ‘AT’ for which the GSM module will respond ‘OK’.
For more AT commands see the AT commands manual here
So here, we will interface 8051 based AT89S52 microcontroller with the GSM module, SIM900A. We will also interface a 16X2 character LCD with 8051 to see the output. For a complete tutorial on interfacing LCD visit: LCD Interfacing with 8051 microcontroller. To make the learning simple, for now, we will write a program to send the command ‘AT’ to SIM900A and display the response received on the LCD. The response should be ‘OK’. But with the same procedure, you can extend the code to use all the facilities of the GSM module.
We are using a microcontroller serial port to communicate with the GSM module. So, here I had connected RX of SIM900A to TX (PIN 11) of 8051 and TX of SIM900A to RX (PIN 10) of 8051. You should also connect your character LCD to the required port. TX and RX of SIM900A are pin numbers 9 and 10 respectively. All the necessary circuit for the controller are also made.
These are the registers that must be configured for UART peripheral.
For more details on these registers visit: UART om 8051 microcontroller
Let’s understand the functions used specifically for the GSM module. There are three functions :
This function is used to make a complete initialization serial port for the GSM module. Here, First, the baud rate is set to 9600 to match the baud rate of 8051 to the Baud rate of the GSM module This is done by putting the value 0xfd to the register TH1. To use Timer 1 in Mode 2 (8-bit auto-reload mode) TMOD register is set to 0X20. With the SCON register set to 0x50, the Mode of serial communication is set to Mode1. Which is 8-bit UART with receiving enabled.
void gsm_init() { TMOD=0x20; // Timer 1 in Mode 2 TH1=0xfd; // 9600 baudrate SCON=0x50; // 8-bit UART with receiving enabled mode. TR1=1; // Start timer }
SBUF (serial buffer special function register) act as the buffer for serial communication, when we want to pass any byte through serial pin, we put that byte in SBUF register. And then these bytes are transmitted through TX pin bit by bit.
After the complete byte is sent, then TI bit is set automatically by hardware. It indicates that byte has been sent successfully. So we will wait till the TI bit becomes 1 using a while loop. After which TI bit is manually cleared.
We will be sending “AT” followed by “\r” by using this function.
void gsm_write(unsigned char ch) // Function to send commands to GSM { SBUF=ch; // Transmitting the byte while(TI==0); //wait until the transmittion is complete TI=0; //clear TI }
similar to transmission, whenever we receive a byte from external devices, we can access that byte from SBUF register. whenever the complete byte has been received RI bit is set by hardware So we can wait till the RI bit becomes 1 to access the incoming data from the GSM module. RI is the first bit of the SCON register.
void gsm_read() // Function to read the response from GSM { while(RI==0); // Wait until the byte received str[i]=SBUF; //storing byte in str array RI=0; //clear RI }