This article takes STM8S103F3P6 programming as an example to introduce STM8S103F3P6 serial port interrupt reception.
Computer platform: Windows 7 64-bit flagship
Compiler software: IAR
Hardware platform: STM8S103F3P6
1. The system clock is initialized to internal 16M
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); // Clock Initialization Clock is 16M divided by 1 and 8M divided by 2
2. Initialize the serial port
UART1_DeInit();
UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
UART1_Cmd(ENABLE);
enableInterrupts();
3. Write the main function
void main(void)
{
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
Heat_UART1();
enableInterrupts();
/* Infinite loop */
while (1)
{
if(flagpacker)
{
flagpacker = 0;
for(unsigned char num = 0; num < length;num++)
Send(Rxpacker[num]);
}
}
}
4. Write interrupt service function
unsigned char flagpacker; //global variable whether a data packet is completely received
unsigned char Rxpacker[255]; //global variable complete data packet
unsigned char length; //data length
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
unsigned char RxBuf; //temporary receiving
static unsigned char RxData[255]; //Receive data buffer
static unsigned char count; //Serial port receives data length
static unsigned char rec; // Determine whether data is being received
UART1_ClearITPendingBit(UART1_IT_RXNE);
RxBuf = UART1_ReceiveData8();
if(RxBuf == (0xcc)) //Frame header
{
rec= 1;
count = 0;
flagpacker = 0;
return ;
}
if(RxBuf == (0xcd)) // End
{
rec= 0;
//You can add a verification code here
length = count;
for(unsigned char i = 0; i Rxpacker[i] =RxData[i]; } flagpacker = 1; // Tell the system that a complete data packet has been received return ; } if(rec) //Judge whether it is in receiving state { RxData[count++] = RxBuf; } }
Previous article:STM8S103 tim4 ordinary timer
Next article:STM8S IO port basic input and output
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
- Does anyone have the IPM driver board?
- Oximeter Principle
- Those sneaky tech giants
- Playing with Zynq Serial 26 - Functional Simulation of PL in Vivado
- EMC testing of Bluetooth electronic products powered by lithium batteries
- Playing with Zynq serial 8 - Installing Linux cross compiler under Ubuntu
- About the disassembly and welding of this type of module
- [Repost] Introduction to the application of hard-flex PCB
- What is the output/input impedance value of CC2xxx?
- 【Project source code】The PC of FPGA transmits the image to FPGA based on USB3.0 and displays it on the TFT screen