51 microcontroller + SIM800C (GSM module) realizes SMS sending function

I. Introduction

This project uses 51 microcontroller and SIM800C GSM module to realize the SMS sending function. As a widely used communication method, SMS plays an important role in many fields, such as the Internet of Things, security systems, remote monitoring, etc. By combining the 51 microcontroller with the SIM800C GSM module, the SMS communication function can be realized in various application scenarios.

The core component of this project is the 51 microcontroller, which is a low-cost, low-power microcontroller that is widely used in embedded system development. The serial port function of the 51 microcontroller is used to control the communication of the SIM800C GSM module. SIM800C is a powerful GSM module that supports GSM/GPRS communication and has the ability to send and receive text messages.

In this project, the hardware connection between 51 microcontroller and SIM800C GSM module is established. Use C language to write a program to implement communication control with SIM800C on the 51 microcontroller. Send AT commands to SIM800C through serial communication to realize the sending function of SMS.

In order to implement the SMS sending function, you need to be familiar with the AT command set of SIM800C and understand how to set SMS parameters, write SMS content and send it. It is also necessary to process the response returned by SIM800C to ensure the success of sending the text message.

image-20230810164852517

image-20230810164922749

2. SIM800C hardware introduction

SIM800C is a powerful, flexible and reliable GSM/GPRS module that is widely used in various communication and control scenarios, especially in IoT applications. By rationally using the AT commands of SIM800C, functions such as sending and receiving text messages can be easily realized.

2.1 Features of SIM800C

【1】Supports multiple communication methods: SIM800C supports GSM, GPRS, SMS, MMS, TCP/IP and other communication methods, and can realize functions such as voice calls, SMS sending and receiving, and data transmission.

【2】A large number of interfaces: SIM800C provides UART, SPI and I2C interfaces to facilitate communication and control with other devices.

【3】Low power consumption design: SIM800C has a low power consumption mode, which can greatly reduce power consumption during standby.

【4】Compact size: The SIM800C module is compact and easy to embed in various devices.

【5】Rich operating temperature range: SIM800C is suitable for a wide operating temperature range and can work normally in harsh environmental conditions.

2.2 Usage scenarios

SIM800C has a wide range of usage scenarios, mainly including the following aspects:

[1] Internet of Things applications: SIM800C can transmit data through GPRS and is used for remote monitoring, remote control, data collection and transmission of Internet of Things devices.

【2】Security system: SIM800C can be used in alarm systems to notify users of security incidents through SMS or voice.

【3】Remote control application: Through the SIM800C module, remote control equipment can be realized, such as remote switches, access control systems, etc.

【4】Mobile payment terminal: SIM800C can be integrated with the mobile payment system to realize the function of mobile payment terminal.

2.3 Introduction to AT commands

SIM800C uses AT commands for communication and control.

The following are some commonly used AT commands related to text messages:

【1】AT+CMGF: Set the SMS mode, used to select the format of SMS. For example, AT+CMGF=1 means sending and receiving text messages in text mode.

【2】AT+CMGS: Send SMS. You need to specify the phone number of the recipient, and press Ctrl+Z (ASCII code is 0x1A) after the input is completed to indicate that the input of the text message content is completed. For example, AT+CMGS="+123456789" means sending a text message to the number +123456789.

【3】AT+CMGR: Read text messages. Received text messages stored in the module can be read and information including the sender number and text message content is returned.

【4】AT+CMGD: Delete text messages. Used to delete text messages at the specified index. For example, AT+CMGD=1 means to delete the text message with index 1.

【5】AT+CNMI: Set new short message indication. The module can be configured to give notifications when new text messages are received so that they can be processed in a timely manner.

3. Code implementation

3.1 STC89C52 hardware configuration

【1】Serial port: STC89C52 has two serial ports, namely UART0 and UART1. Can be used for asynchronous serial communication with other devices.

【2】Timer: STC89C52 has three timers, namely Timer0, Timer1 and Timer2. Can be used to generate scheduled interrupts, timing and other functions.

【3】GPIO: STC89C52 has 32 I/O ports, and each I/O port can be configured as input or output. Among them, the pins on Port 0 (Port 0) and Port 2 (Port 2) can be used as GPIO pins of UART0, while the pins on Port 3 (Port 3) can be used as GPIO pins of UART1.

The GPIO port numbers corresponding to the serial port are as follows:

【A】UART0:

  • TXD: Corresponds to P0.0 port
  • RXD: Corresponds to P0.1 port

【B】UART1:

  • TXD: Corresponds to P3.1 port
  • RXD: Corresponds to P3.0 port

In STC89C52, the TXD pin of UART0 corresponds to the P0.0 port, and the RXD pin corresponds to the P0.1 port; the TXD pin of UART1 corresponds to the P3.1 port, and the RXD pin corresponds to the P3.0 port.

3.2 SMS sending code implementation

#include <reg52.h>

// 定义SIM800C的串口引脚
sbit SIM_RX = P3^0;  // SIM800C的串口接收引脚
sbit SIM_TX = P3^1;  // SIM800C的串口发送引脚

// 定义波特率常量
#define BAUDRATE 9600

// 定义发送函数
void sendATCommand(char* command) {
    
    
    // 发送AT指令
    for (int i = 0; command[i] != '\0'; i++) {
    
    
        SBUF = command[i];
        while (TI == 0);  // 等待发送完成
        TI = 0;  // 清除发送完成标志
    }
}

// 主函数
void main() {
    
    
    // 初始化串口
    TMOD = 0x20;  // 设置定时器1为模式2
    TH1 = 256 - BAUDRATE / 9600;  // 设置波特率
    TL1 = TH1;
    TR1 = 1;  // 启动定时器1
    SCON = 0x50;  // 设置串口为模式1,允许接收

    // 发送AT指令初始化SIM800C模块
    sendATCommand("AT\r\n");  // 发送AT指令,检测模块是否正常
    sendATCommand("AT+CMGF=1\r\n");  // 设置短信模式为文本模式
    sendATCommand("AT+CNMI=1,2,0,0,0\r\n");  // 设置接收新短信时的提示方式

    // 发送短信
    sendATCommand("AT+CMGS=\"+1234567890\"\r\n");  // 设置短信接收号码
    sendATCommand("Hello, this is a test message.\x1A");  // 发送短信内容,以Ctrl+Z作为结束符

    while (1);
}

3.3 SMS sending and phone calling functions-encapsulated sub-functions

#include <reg51.h>

// 定义串口1的引脚连接
sbit UART1_TX = P3^1;
sbit UART1_RX = P3^0;

// 初始化串口1
void UART1_Init() {
    
    
    TMOD |= 0x20;  // 设置定时器1为模式2(8位自动重载)
    SCON = 0x50;  // 设置串口1为工作方式1,并允许接收
    TH1 = 0xFD;   // 设置波特率9600,对应12MHz晶振
    TL1 = 0xFD;
    TR1 = 1;      // 启动定时器1
}

// 发送一个字符到串口1
void UART1_SendChar(unsigned char c) {
    
    
    SBUF = c;
    while(!TI);  // 等待发送完成
    TI = 0;      // 清除发送标志
}

// 发送字符串到串口1
void UART1_SendString(const unsigned char *str) {
    
    
    while (*str) {
    
    
        UART1_SendChar(*str++);
    }
}

// 发送AT指令到SIM800C模块
void SIM800C_SendATCommand(const unsigned char *atCmd) {
    
    
    UART1_SendString(atCmd);
    UART1_SendChar('\r');
    UART1_SendChar('\n');
}

// 发送短信
void SIM800C_SendSMS(const unsigned char *phoneNumber, const unsigned char *message) {
    
    
    SIM800C_SendATCommand("AT+CMGF=1"); // 设置为文本模式
    // 等待回复
    // ...
    SIM800C_SendATCommand("AT+CMGS=\"");
    UART1_SendString(phoneNumber); // 接收方手机号
    UART1_SendChar('"');
    UART1_SendChar('\r');
    UART1_SendString(message); // 短信内容
    UART1_SendChar(0x1A); // 发送Ctrl+Z结束短信
}

// 拨打电话
void SIM800C_MakeCall(const unsigned char *phoneNumber) {
    
    
    SIM800C_SendATCommand("ATD"); // 拨号命令
    UART1_SendString(phoneNumber); // 目标手机号
    UART1_SendChar(';'); // 发送分号以拨号
}

void main() {
    
    
    UART1_Init(); // 初始化串口1

    // 等待SIM800C模块初始化完成
    // ...

    // 发送短信
    SIM800C_SendSMS("手机号", "短信内容");

    // 拨打电话
    // SIM800C_MakeCall("目标手机号");

    while(1);
}

In the code, the "mobile phone number" and "target mobile phone number" need to be filled with actual phone numbers.

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/134941365
Recommended