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’.
Here are some of the important AT commands used in SIM900A :
- Get model number: AT+CGMM
- Get IMEI number (International Mobile Equipment Identity): AT+CGSN
- mobile phone activity status: AT+CPAS
- mobile network registration status: AT+CREG
- radio signal strength: AT+CSQ
- Make a Call to a number: ATDP+ PHONE_NUMBER (in international format)
- Hang up a call: ATH
- Receive an incoming call: ATA
- set the SIM900 to text mode : AT+CMGF=1\r
- send SMS to a number : AT+CMGS = PHONE_NUMBER (in international format)
- Read the first SMS in the inbox: AT+CMGR=1\r
- Read the second SMS in the inbox: AT+CMGR=2\r
- Read all SMS in the inbox: AT+CMGR=ALL\r
For more AT commands see the AT commands manual here
Interfacing AT89S52 with SIM900A
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.
Circuit Diagram for Interfacing GSM(SIM900A) Module with 8051
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.
- PCON: Power Control Register.
- SCON: SCON is an 8-bit register for configuring serial control register.
- TCON: Timer Control Register for Baud Rate Generator (For more details refer to chapter for Timer)
- TMOD: Timer Mode Control for Baud Rate Generator (For more details refer to chapter for Timer)
- SBUFF: Serial Buffer holds the data to be transmitted and the data received
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.
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