The data frame meets the following format:
Frame Header | Type | Length | Value | CRC Check |
2 bytes | 1 byte | 1 byte | X bytes | 2 bytes |
0xaa 0x55 | X |
void USART6_Init (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //Modify
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;//Modify
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);//Modify
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6);//Modify
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6); //Modify
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART6,&USART_InitStructure);
USART_ITConfig(USART6,USART_IT_RXNE,ENABLE);//Enable receive interrupt
USART_Cmd(USART6,ENABLE);
}
void USART6_IRQHandler()
{
unsigned char rCh;
static char rCnt = 0;
if (USART_GetITStatus(USART6,USART_IT_RXNE) != RESET)
{
rCh = USART_ReceiveData(USART6);
COM6_RecvBuf[rCnt] = rCh;
if (rCnt == 0) //frame header 0xAA
{
rCnt = (0xAA != rCh)?0:rCnt+1;
}
else if (rCnt == 1) //frame header 0x55
{
rCnt = (0x55 != rCh)?0:rCnt+1;
}
else if (rCnt == 2) //type type
{
//Here you can process the above according to the type range
rCnt++;
}
else if (rCnt == 3) //length len
{
rCnt++;
}
else if (rCnt > 3) //value
{
rCnt++;
if (rCnt == 6+COM6_RecvBuf[3])
{
rCnt = 0;
memcpy(COM6_RecvBufBck,COM6_RecvBuf,RECV_BUF_SZ);//buffer
COM6_RecvFin = 1; //Notify the main loop processing
}
}
}
}
int main(void)
{
int i;
// Code segment 1
while (1) //The loop cannot be too slow, otherwise the data buffer will be partially modified
{
// Code segment 2
if (COM6_RecvFin == 1)
{
COM6_RecvFin = 0;
CMD_Analysis(); //Analyze the received frame data
}
// Code segment 3
}
return 0;
}
Previous article:How to modify the STM32 clock (when HSE uses a 16M passive crystal oscillator)
Next article:STM32 BKP backup register introduction
Recommended ReadingLatest update time:2024-11-16 12:52
- 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
- Sub-library: Motion Algorithm Library
- TI radar technology is changing three trends in the in-cockpit sensing market
- Prize-giving event: Blind box delivery | Murata’s Pet Pink Month is officially launched!
- AD packaging for self-use HR911105A 100M and HR911130A 1000M network ports
- Serial port expansion
- HR made inappropriate remarks in the circle of friends, made a public apology, and was fired!
- 【AT-START-F425 Review】No.02 AT32F425 Development Environment Construction
- About the use of AWR1642BOOST development system
- Excellent "Internet of Things" (IoT) video recommendation, it explains it very thoroughly!
- Design of TPS53355 ripple injection circuit