STM32 serial port interrupt receiving data

Publisher:量子心跳Latest update time:2017-11-02 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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;

}


Keywords:STM32 Reference address:STM32 serial port interrupt receiving data

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

Introduction to STM32 online upgrade IAP-IAP
IAP (In Application Programming) is in-application programming. IAP is the process of burning part of the User Flash area during the operation of the user's own program. The purpose is to easily update the firmware program in the product through the reserved communication port after the product is released. Usually, w
[Microcontroller]
[STM32 Motor FOC] Record 17——Edge trigger of Hall timer
Hall sensor placed at 120 degrees: The idea of ​​changing the 120 degree trigger to a 60 degree trigger Originally, when triggered, the three signals of the Hall sensor are XORed into one signal, and the falling edge of this signal is used for triggering, and then the corresponding interruption, that is, the measurem
[Microcontroller]
[STM32 Motor FOC] Record 17——Edge trigger of Hall timer
Solution to the problem that the stm32 microcontroller cannot download the program after entering the sleep mode
By using the sleep mode of the stm32 microcontroller, the microcontroller can be put into sleep intermittently to achieve low power consumption. It often fails to wake up after entering sleep mode, resulting in the next program not being able to be burned in. The usual solution is: the general development board or mic
[Microcontroller]
Solution to the problem that the stm32 microcontroller cannot download the program after entering the sleep mode
STM32 configuration PC13-PC15
In the pinout diagram of the STM32 data sheet, you can see that PC14 and OSC32_IN share a pin, and PC15 and OSC32_OUT share a pin. Their usage is as follows:   When LSE (low-speed external clock signal) is turned on, the functions of these two common pins are OSC32_IN and OSC32_OUT. When LSE (low-speed external cl
[Microcontroller]
STM32 configuration PC13-PC15
STM32 USB slave HID analysis
Overview Initialize the STM32 USB as a USB slave and use the standard HID protocol. The control board comes with VBUS power supply, so VBUS and GND pins are not required. Just connect 2 data cables to the computer. Source code analysis When connected to the computer via USB cable, a USB reset packet is receive
[Microcontroller]
STM32 Practice 4. Use timer and serial port to receive instructions to control LED brightness
#include "sys.h" #include "led.h" #include "delay.h" #include "string.h" #include "USART3_TIM2.h"     char *str_open = "open"; char *str_close = "close";   int main(void) { delay_init(); Led_Heat(); USART3_TIM2_Init(115200);   while(1) { if(RX_stat == 1) // indicates that the reception is completed { if(
[Microcontroller]
How to enter sleep mode in STM32 μC/OS system
   Some time ago, I was doing development based on μC/OS system and encountered a problem. When running a task, I needed to put the CPU into sleep state for a few seconds, but directly calling __WFI() had no effect. Later, I looked up information and found that it needed to be called in the idle task.    Here is an
[Microcontroller]
Visible steps to create a new STM32 project
1. Create a new folder                DOC folder:            can store project documents such as readme.txt     Library folder:    can store ST library source code, directly copy the ST library source code to the Library folder, and startup only needs to copy the .s file corresponding to the chip , which is
[Microcontroller]
Visible steps to create a new STM32 project
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号