STM32 is one of the mainstream products based on the ARM Cortex-M3 core launched by STMicroelectronics (ST). It is designed by ST specifically for embedded applications that require high performance, low power consumption and low cost. It has been widely used in various fields. SIM900A is a compact GSM/GPRS dual-band module product promoted by SIMCom. It is favored by engineers for its stable performance, exquisite appearance and high cost performance.
This article introduces the main underlying configuration of STM32 through the study of STM32 underlying configuration and data transmission, and focuses on the implementation of data transmission. Through the introduction of the program source code of the key steps, the details and precautions of implementing data transmission are explained. This method has certain implementation value and reference value for other projects or chips, and it is simple, reliable, universal and general.
1 STM32 underlying configuration
In order to realize the transmission of data commands between the STM32 microcontroller and the SIM900A module, this article takes the serial port as an example, first builds a development platform, adds corresponding library functions and configuration files to the project, and then configures the clock and the corresponding input and output GPIO interface of the serial port. During configuration, you need to write according to your own schematic diagram to ensure that the configuration is correct. In this way, the basic development platform is built.
1.1 Serial port configuration
After the development platform is built, you can configure the serial port. Configure the rate to 115 200 b/s, the word length to 8 bits, 1 bit stop bit, the serial port mode to input and output mode, and finally initialize the corresponding serial port. After initializing the serial port, open the interrupt response function of the serial port, that is, USART_ITConfig (USART2, USART_IT_RXNE, ENABLE) (taking serial port 2 as an example), and then enable the corresponding serial port, so that the serial port function is basically configured. It should be noted that some programs may lose the first bit during transmission. This problem involves the mechanism of USART. After the hardware is reset, the status bit of USART is set (set to 1, indicating that it has been sent), and the data can be sent normally at this time. When a frame of data is sent, the hardware sets this bit. The TC bit is cleared (set to 0) by software, which is cleared by reading USART_SR first and then writing USART_DR. However, when the program sends the first frame of data, it does not read USART_SR, but directly writes USART_DR, so the TC flag is still set to 1 and is not cleared. After sending the first frame of data, the status returned by USART_GetFlagStatus() is that the transmission is complete, and the program will immediately send the next frame of data, so the first frame of data will be overwritten by the second frame of data, so the first data cannot be seen. According to this situation, the transmission completion flag can be cleared before or after each transmission, that is, USART_ClearFlag (USART2, USART_FLAG_TC).
1.2 Interrupt configuration
After configuring the serial port, NVIC will be configured. First configure the interrupt group, and then select the interrupt of the serial port, that is, NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn (based on the definition of the firmware library used).
Then set the preemptive interrupt priority and responsive interrupt priority, and then enable interrupts and initialize. The above configuration must be combined with its own situation to design the optimal interrupt grouping and priority to ensure the speed of the program responding to the interrupt. The content done after the interrupt is configured in the stm32f10x_it.c file, which will be explained in detail below.
2 Implementation details
The principle of implementing GPRS data transmission is: STM32 parses a string of data or commands, and then sends it to the SIM900A module character by character through the serial port or other means. After SIM900A receives the data, it sends it to the server through the SIM card. When SIM900A receives the data, it immediately responds to the interrupt and processes the data according to the method set by the interrupt. At this time, it is necessary to control the transmission of data through the sending check and receiving check.
2.1 Sending check
Since STM32 sends data to the SIM900A module character by character, the correctness and coherence of the data must be guaranteed. If you respond to the interrupt or perform task scheduling during the sending, the sending will be invalid, resulting in program errors, so developers must be vigilant against such errors.
When sending data or commands, the data can be passed to the sending function through parameters, which will be controlled uniformly by the sending function. After the sending is completed, a sending completion flag will be returned to inform the caller of the function that the sending is completed. The source program is as follows:
void USART_Send_Byte(char MyData){ //Send character function
USART_ClearFlag(USART2, USART_FLAG_TC);
//Clear flag bit, as mentioned above
USART_SendData(USART2, MyData); //Send data
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET); //Wait for sending to complete
}
void USART_Send_Str(char*s){ //Send string
int i;
int len = strlen(s)-1; //String length
for(int i=0; i
if(s[i]==0x0a){ //Judge whether the sending is completed
SendCFFlag=TRUE;
//If true, the sending completion flag position is true
}else{
USART_Send_Byte(s[i]); //If false, send it out
}
}[page]
2.2 Receiving verification
When SIM900A returns data or data is received by SIM900A to the lower computer, STM32 will immediately respond to the interrupt to receive the data. At this time, a series of processing must be performed in the interrupt function. Taking SIM900A as an example, the commands returned by the SIM900A module all end with "r"+"n"+"", so the end of the transmission can be judged based on it. In the interrupt response function (i.e. in the stm32f10x_it.c file), the USART2_IRQHandler function can be set as follows:
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE)!=RESET){
//Store the received characters in the receive buffer RxBuffer
RxBuffer[ReceCounter++]=(char)USART_ReceiveData(USART2);
//Judge whether the reception is completed
if(RxBuffer[ReceCounter]==′′&& RxBuffer[ReceCounter-1]==0x0A &&
RxBuffer[ReceCounter-2]==0x0D){
ReceCFFlag=TRUE;
}
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
}
The basic idea of this function is: store the characters received by USART into the buffer one by one, and then determine whether the last 3 characters in the buffer are the end identifier of SIM900A. If it is false, continue to receive; if it is true, set the receive completion identifier to true. When the receive completion identifier is true, it means that the reception is completed, and then data processing can be performed.
2.3 Command function implementation method
The following will take AT+CIPSEND as an example to explain the details of sending data. After initializing the module, opening the network, establishing an access point and establishing a TCP connection, you can start sending data. The implementation source code is as follows:
u8 GPRS_Send(void){
u8 i=0;
u8*p;
USART_SendToGPRS("AT+CIPSENDrn");//Send command
Delay_ms(500);//Delay 500 ms
p=LookFor_Str(RxBuffer,">");
//Check whether there is ">" symbol. If so, you can send dataif
(p!=0){
p=0;
memset(RxBuffer,0,BufferSize);//Clear receive bufferUSART_SendToGPRS
(GPRSSendData);//Send data
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
p=LookFor_Str(RxBuffer,"SEND OK");
if(p!=0){//Judge whether the sending is successful
//Sending successful operation
return 1;
}else {
//Sending failed operation
return 0;
}
}
}
The basic idea of this function is: first send the command, then check whether there is a ">" symbol. If there is, it means that data can be sent. After a delay, check whether there is the word "SEND OK" in the receiving buffer. If there is, it means that the sending is successful, otherwise it means that the sending failed. Further operations can be performed based on the judgment. For detailed usage of the command, please refer to the AT command manual supporting SIM900A. There are three points to note:
(1) In the test program of this article, it is necessary to obtain the IP before establishing a TCP connection. This is determined by the SIM900A mechanism. Therefore, if the developer cannot establish a TCP connection, in addition to testing whether the network is normal and whether the server is correctly configured, it is also necessary to obtain the IP in the program first. The command is AT+CIFSR.
(2) You can first obtain the status of SIM900A. The command is AT+CIPSTATUS. Determining which operations to perform based on the status can reduce the amount of running, simplify the code, thereby reducing the running time and improving the running efficiency. For details, please refer to the AT command manual supporting SIM900A.
(3) The setting of the delay requires specific analysis of specific problems. For example, when initializing the SIM900A module, it only takes 500 ms to delay the information returned by the module. When receiving information from the server, sometimes it may take a longer delay due to signal problems or huge amounts of data, and developers need to test it themselves. Accurate delay setting can reduce the delay time while ensuring data correctness, thereby improving the running efficiency of the program.
This article explains the underlying configuration of the STM32 microprocessor by setting the serial port of the STM32 microprocessor and configuring the interrupt, and then implements the GPRS data transmission technology through the sending and receiving data of SIM900A, thereby enabling the STM32 microprocessor to access the Internet. In the implementation of the reception test, only one judgment can be made based on whether the reception is completed, thereby reducing the interrupt running time. SIM900A is a GSM/GPRS dual-band module, which can also realize many functions such as calling, sending and receiving text messages, HTTP and FTP transmission, etc. Through more in-depth research, the practical value of the module can be maximized, thereby providing more application functions for electronic products.
Previous article:Design of STM32 debugging platform based on LabVIEW
Next article:Design of portable mobile phone Bluetooth attendance machine system based on STM32
Recommended ReadingLatest update time:2024-11-16 22:32
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Is there anyone familiar with Trinamic TMC5160/5130/2160?
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board evaluation] MDK development environment test LCD Demo
- [Environmental Expert's Smart Watch] Part 13: Storing various data in EEPROM
- Behavioral modeling sequential logic circuit (two-input AND gate) source code and test code
- Hardware testing method for TI-CC chip
- Is the digital-to-analog configuration of a certain pin of STM32 related to the output? Do I need to set the output register of this pin when outputting?
- EEWORLD University ---- Microprocessor and Embedded System Design University of Electronic Science and Technology of China
- MSP430G2553 Study Notes
- [Silicon Labs BG22-EK4108A Bluetooth Development Review] Unboxing
- [STM32WB55 Review] + Wireless Firmware Update