catalogue
3. Wiring mode of module and single chip microcomputer
2. SIM900A code for sending English SMS
3. SIM900A code for sending Chinese SMS
The first part is the preface
1. Preparation knowledge
The first one before [STM32 training - Item 1] Part 1: the computer's serial assistant drives SIM900A to send Chinese and English text messages_ Big fart peach blog - CSDN blog I have made preparations for the relevant knowledge and made a detailed introduction. If you don't see it, you can click this link to learn how to use the serial port assistant on the computer to debug SIM900A to send Chinese and English text messages, which is conducive to your understanding of my STM32 code. Ha, I won't BB here.
In this article, I want to introduce in detail how to use STM32 to send SIM900A short messages in Chinese and English. The relevant knowledge and configuration of serial port belong to the basic knowledge of STM32, so I will bring the subtitle of [STM32 learning - Project 1], which also caters to the order in which I write this column of STM32, If you think my article is messy, I suggest you read the opening work of my column: personal perception, you will understand my good intentions, which is convenient for others and yourself. [STM32 learning] personal perception_ Big fart peach blog - CSDN blog
2. Commissioning purpose
I debug this module in order to realize it, because this module is a part of my project 1. At that time, I will integrate all the modules I sent into a small project as a whole. I also mentioned this in the opening work of this column: personal perception. Those who are interested can see it.
3. Wiring mode of module and single chip microcomputer
1. The power supply of the single chip microcomputer is connected to the corresponding power supply
Single chip microcomputer PB10 | Corresponding to | Pin 5VR of SIM900A |
Single chip microcomputer PB11 | Corresponding to | Pin 5VT of SIM900A |
Single chip microcomputer GND | Corresponding to | Module GND |
Single chip microcomputer 5V | Corresponding to | Module VCC_MCU |
2. The module is powered separately
VCC (or VCC5V) of the module | Corresponding to | External DC5V |
GND of module | Corresponding to | External power GND |
Part II Engineering Code
1. Code function description (read carefully)
After downloading the program, the phenomenon is as follows: first, LED0 lights up for two seconds and sends an English short message. After successful sending, it closes. Then, LED1 lights up for two seconds and sends a Chinese short message. After successful sending, it closes
Finally, LED0 and LED1 flash after successful transmission. At the same time, the serial port will always print the response results sent by the message.
2. SIM900A code for sending English SMS
Why is the code written like this? You have to put the previous article [STM32 training - Item 1] Part 1: the computer's serial assistant drives SIM900A to send Chinese and English text messages_ Big fart peach blog - CSDN blog I can understand why I use the serial port to send instructions. I read the SIM900A routine code of punctual atom. It's really troublesome to open the door for trouble. It's troublesome.
/**************************************************************************/ //Function function: send English SMS function //Function name: sim900a_send_English_message(char *message,char *phonenumber)(uint8_t number); //Internal parameter: message phonenumber //Revision date: 2:40 am, January 26, 2022 //Author: (CSDN search) big fart peach /**************************************************************************/ void sim900a_send_English_message(char *message,char *phonenumber) { Usart_SendString2(USART3,"AT\r\n"); //Is SIM900A successfully connected with MCU delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("English_message_OK1\r\n"); Usart_SendString2(USART3,"AT&F\r\n"); //SIM900A reset delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); //String matching function printf("English_message_OK2\r\n"); Usart_SendString2(USART3,"AT+CSCS=\"GSM\"\r\n"); //English SMS instruction 1 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("English_message_OK3\r\n"); Usart_SendString2(USART3,"AT+CMGF=1\r\n"); //English SMS instruction 2 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("English_message_OK4\r\n"); sprintf(dispbuf,"AT+CMGS=\"%s\"\r\n",phonenumber); Usart_SendString2(USART3,dispbuf); //English SMS instruction 3 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("English_message_OK5\r\n"); Usart_SendString2(USART3,message); //English SMS instruction 4 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("English_message_OK6\r\n"); Usart_SendHalfWord(USART3,0x1a); //End instruction delay_ms(2000); //Delay two seconds while(Find_char((char*)Usart3_buff,"OK")); printf("English_message_OK7\r\n"); }
3. SIM900A code for sending Chinese SMS
The difference between sending Chinese and sending English is that the phone number and content should be encoded in Unicode. This requires conversion software. You can download it for free at the end of the previous article. Secondly, there is one more line of code USART than those in English_ SendString2(USART3,"AT+CSMP=17,167,0,8\r\n");
/**************************************************************************/ //Function function: send Chinese SMS function //Function name: sim900a_send_Chinese_message(char *message,char *phonenumber)(uint8_t number); //Internal parameter: message phonenumber //Revision date: 2:40 a.m. on January 26, 2022 //Author: (CSDN search) big fart peach /**************************************************************************/ void sim900a_send_Chinese_message(char *message,char *phonenumber) { Usart_SendString2(USART3,"AT\r\n"); //Is SIM900A successfully connected with MCU delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); //String matching function printf("Chinese_message_OK1\r\n"); Usart_SendString2(USART3,"AT&F\r\n"); //SIM900A reset delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("Chinese_message_OK2\r\n"); Usart_SendString2(USART3,"AT+CSCS=\"UCS2\"\r\n"); //Chinese SMS instruction 1 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("Chinese_message_OK3\r\n"); Usart_SendString2(USART3,"AT+CMGF=1\r\n"); //Chinese SMS instruction 2 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("Chinese_message_OK4\r\n"); Usart_SendString2(USART3,"AT+CSMP=17,167,0,8\r\n"); //Chinese SMS instruction 2 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("Chinese_message_OK5\r\n"); sprintf(dispbuf,"AT+CMGS=\"%s\"\r\n",phonenumber); Usart_SendString2(USART3,dispbuf); //Chinese SMS instruction 3 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("Chinese_message_OK6\r\n"); Usart_SendString2(USART3,message); //Chinese SMS instruction 4 delay_ms(200); while(Find_char((char*)Usart3_buff,"OK")); printf("Chinese_message_OK7\r\n"); Usart_SendHalfWord(USART3,0x1a); //Chinese end instruction delay_ms(2000); //Delay two seconds while(Find_char((char*)Usart3_buff,"OK")); printf("Chinese_message_OK8\r\n"); }
4,main.c document code
I don't introduce the code too much. I have notes for each code, which is easy to understand.
#include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" #Include "sim900a. H" / / sim900a SMS sending module //connection /************************************************** 1,The power supply of the single chip microcomputer is connected to the corresponding power supply MCU PB10 --- module 5VR MCU PB11 --- module 5VT Single chip microcomputer GND --- module GND Single chip microcomputer 5V --- module VCC_MCU 2,The module is powered separately The VCC (or VCC5V) of the module is connected to DC5V GND of the module is connected to the power ground *****************************************************/ /**************************************************************************/ //Function function: hardware initialization, initialization of MCU function and external equipment //Function name: Hardware_Init(); //External parameters: None //Revision date: 21:35, January 27, 2022 //Author: (CSDN search) big fart peach /**************************************************************************/ void Hardware_Init(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Interrupt controller grouping setting delay_init(); //systick initialization LED_Init(); Usart1_Init(115200); //Serial port 1, for printing information Usart3_Init(9600); //Serial port 3, used to drive SIM900A UsartPrintf(USART_DEBUG, " Hardware init OK\r\n"); } /**************************************************************************/ //Function function: main function //Function name: mian() //External parameters: None //Revision date: 21:35, January 27, 2022 //Author: (CSDN search) big fart peach //Function Description: /* After the single chip microcomputer RCT6 is put into the program, the phenomenon is as follows: First of all, LED0 will send an English text message after it is on for two seconds. It will be turned off after sending it successfully Then, LED1 will send Chinese text messages after it is on for two seconds, and turn off after successful sending Finally, LED0 and LED1 flash after successful transmission At the same time, the serial port will always print messages */ /**************************************************************************/ int main() { /*SIM900A SMS content and number*/ char English_message[]={"Hello!!"}; //English SMS content char phonenumber[]={"13225567263"}; //Number to receive SMS char Chinese_message[]={"4F60597D"}; //Chinese SMS content corresponds to hello char Unicode_phonenumber[]={"00310033003200320035003500360037003200360033"}; Hardware_Init(); //Initialize peripheral hardware LED0 = 0; //LED0 opens for two seconds and sends an English text message delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); /*SIM900A Send English SMS code*/ sim900a_send_English_message(English_message,phonenumber); //Function to send information LED0 = 1; LED1 = 0; //LED1 opens and sends Chinese text messages two seconds later delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); delay_ms(500); /*SIM900A Send Chinese SMS code*/ sim900a_send_Chinese_message(Chinese_message,Unicode_phonenumber); LED1 = 1; while(1) { LED1 = 0; //LED0 and LED1 flash after sending successfully LED0 = 0; delay_ms(500); delay_ms(500); LED1 = 1; LED0 = 1; delay_ms(500); delay_ms(500); } }
The third part is the summary
1. Effect pictures and videos
This is a screenshot of my mobile phone receiving a text message. I sent a short message to my mobile phone with my grandmother's mobile phone card. This is a video link. The video recording is a little skimpy 😁, Hands tremble: STM32 drives SIM900A to send short messages in Chinese and English_ Beep beep beep_ bilibili
2. Thank you
Without this Westernization L ✅ Blog_ CSDN blog - domain Blogger With the help of the blogger, I may have to waste more time in the pit. Thank yiboha!
3. Complete engineering code
Note: my MCU model is STM32F03RCT6, and the project link is as follows:
Baidu Yunbai whoring link: https://pan.baidu.com/s/1fyaLeTgnFUbamflhZ81axQ Extraction code: nny0
CSDN paid download link:
The contents of the two projects are old fellow. If the old iron is useful, remember to praise the collection. 🐶🐶🐶, If you have any questions, please send me a private letter at any time! Finally, I hope my project will help you!