STM32CubeMX serial port receives data (interrupt mode, HAL library)

Publisher:SHow111timeLatest update time:2018-06-19 Source: eefocusKeywords:STM32CubeMX Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

System: Linux Mint 18.3 xfce 64bit 
Software: 
STM32CubeMX 4.24 
SW4STM32 2.4


Development board chip: STM32F103RCT6, STM32F407VET6 
Implementation effect: Open two serial port assistant windows, one for USART1 and one for USART2. Data sent from any serial port (serial port 1 or serial port 2) will be immediately returned through this serial port 
.

Advantages of using STM32CubeMX and SW4STM32: 
(1) Advantage of cross-platform development, there are ready-made installation packages for both Windows and Linux systems, and the installation process of sw4stm32 will automatically install the STLINK driver 
(2) Version upgrades are active, which I think is a good phenomenon, indicating that developers attach great importance to it 
(3) The most important thing is free! ! ! No need to look for crack activation everywhere


(1) Use STM32CubeMX to configure TIM3, PA8, PD2 (LED light), USART1, USART2 in Pinout.

Write the picture description here

(2) Keep the clock configuration as default 
Write the picture description here

(3) Set the TIM1 clock to interrupt once every 1s 
Write the picture description here

(4) Keep the baud rates of USART1 and USART2 unchanged, set NVIC as shown below, pay attention to the priority 
Write the picture description here 
Write the picture description here 
(5) and then generate the SW4STM32 project 
Write the picture description here

(6) Use sw4stm32 to open the project and 
add three codes in main.c. Please refer to the prompts for the specific locations. The entire main.c file is too long to post.

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

uint8_t aTxBuffer[]="*********Serial port sends data ××××××××××××\r\n";

uint8_t aRxBuffer1[1]; // used to receive data sent by serial port 1

uint8_t aRxBuffer2[1]; // used to receive data sent by serial port 2

/* USER CODE END PV */


  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

    HAL_UART_Receive_IT(&huart1,aRxBuffer1,1);          // Enable the USART1 Interrupt

    HAL_UART_Receive_IT(&huart2,aRxBuffer2,1);          // Enable the USART2 Interrupt

    HAL_UART_Transmit(&huart1,aTxBuffer,sizeof(aTxBuffer),100);

    HAL_UART_Transmit(&huart2,aTxBuffer,sizeof(aTxBuffer),100);

    HAL_TIM_Base_Start_IT(&htim3); // Start timer 3

  while (1)

  {


  /* USER CODE END WHILE */


  /* USER CODE BEGIN 3 */


  }

  /* USER CODE END 3 */


/* USER CODE BEGIN 4 */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{


    if(huart->Instance == USART1) // Determine which serial port triggered the interrupt

    {

        HAL_UART_Transmit(&huart1,aRxBuffer1,1,100); // Send the received data immediately using serial port 1

        HAL_UART_Receive_IT(&huart1,aRxBuffer1,1); // Re-enable serial port 1 receive interrupt

    }

    if(huart->Instance == USART2)

    {

        HAL_UART_Transmit(&huart2,aRxBuffer2,1,100); // Send the received data immediately using serial port 1

        HAL_UART_Receive_IT(&huart2,aRxBuffer2,1); // Re-enable serial port 2 receive interrupt

    }

}


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

    if(htim->Instance == TIM3) // Determine which timer triggered the interrupt

    {

        HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_8);

    }


}

/* USER CODE END 4 */


If you find that the effect cannot be achieved, please check whether your hardware connection is correct, and whether the USB to TTL converter is normal. I encountered that my USB to TTL converter could receive data from STM32 but could not send data. I tossed it all night and changed the serial port line the next day to test it. It was normal



Keywords:STM32CubeMX Reference address:STM32CubeMX serial port receives data (interrupt mode, HAL library)

Previous article:Detailed explanation of STM32CubeMX clock configuration
Next article:STM32 serial port obtains ammonia sensor data

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
Guess you like

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号