STM32cube HAL library UART serial port interrupt mode to send and receive arbitrary length debugging notes

Publisher:雅致小筑Latest update time:2019-03-25 Source: eefocusKeywords:STM32cube Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STM32Cube can save a lot of time for the development of new projects, freeing you from the tedious chip initialization



1. STM32 UART initialization part, configure the project, use STM32cube to generate code, and the initialization is completed.


2. The serial port interrupt mode is triggered: HAL_UART_Transmit_IT


Without any processing, we use printf in the program to print information to the display screen. At this time, we need to redirect printf to print the printf message to the serial port. Add the following function in the main function:


int fputc(int ch, FILE *f) 

{

    HAL_UART_Transmit_IT(&huart1, (uint8_t *)&ch, 1);

    osDelay(1); 

    return 0;

}


//The redirection is completed, and executing printf can send characters of any length to the serial port.



3. Receive action of serial port interrupt: HAL_UART_Receive_IT


HAL_UART_Receive_IT(&huart1,(uint8_t *)&value,1);//The program will not stop at this statement, and will directly store the received data in value according to the interrupt method. However, this statement can only enable the serial port interrupt once. Therefore, it is necessary to re-enable it in the interrupt service function or callback function.

Code:


// Initialize the receive buffer

#define MAX_RECV_LEN 1024 

uint8_t msg_buff[MAX_RECV_LEN] = {0};

uint8_t * msg = msg_buff;

static int flag = false;


//Receive interrupt callback function

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)  

{  

    uint8_t right = HAL_OK;

    msg++;

    if( msg == msg_buff + MAX_RECV_LEN)

    {

        msg = msg_buff;

    }

    do  

    {  

        ret = HAL_UART_Receive_IT(UartHandle,(uint8_t *)msg,1);            

    }while(ret != HAL_OK);

    

    if(*(msg-1) == '\n') //Receive a string ending with \n, which means the reception is complete

    {

        flag  = true;

    }



void uart_main(void)

{

    extern UART_HandleTypeDef huart1;

    HAL_UART_Receive_IT(&huart1, (uint8_t *)msg, 1); // Enable the first interrupt


    while(1)

    {

        if (flag == true)

        {

            printf("msg_buff = %s\r\n",msg_buff);

            memset(msg_buff, 0, sizeof(msg_buff)); //Clear the buffer

            // Point to the head of the receive buffer

            msg = msg_buff;

            (&huart1)->pRxBuffPtr = msg;

            flag = false;

        }

        osDelay(10);

    }

}

Keywords:STM32cube Reference address:STM32cube HAL library UART serial port interrupt mode to send and receive arbitrary length debugging notes

Previous article:STM32 Interrupts and Events --- An example of using GPIO as an external interrupt
Next article:HAL library-STM32F4 external interrupt-delay

Latest Microcontroller Articles
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号