【STM32F746 Nucleo】 MxCube develops serial communication
[Copy link]
- CubeMX configuration development driver
- Chip Search
Because this development board is officially designed and launched, there are development board resources in the tool software, and you can directly select the development board currently in use.
-
- Clock Configuration
You can choose the constant frequency according to your actual situation. The maximum constant frequency is 216MHz
-
- Clock source configuration
This environment has already been configured, so just check it.
-
- SYS Settings Debug
The SW mode is selected
-
- Serial port configuration
Involves baud rate setting, pin setting and interrupt setting
1.6 Code Generation
This is a foreshadowing and the next article will give a brief description.
2. Keil Project
2.1. (MDK) keil project generated by CubeMX
2.2 Schematic Pinout
2.3、Serial port implementation
CubeMX software generates HAL library program code in a graphical way. The HAL library program code is very different from the standard library.
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
For example, this line of code is the function of the serial port putting back data. The bottom layer uses interrupts to process, but the standard library uses direct interrupt calls, while the HAL library uses callback functions. The design concept of HAL is actually more practical.
1. Add receiving and sending function code
/* USER CODE BEGIN 1 */
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] Usart1Send
* @param pUSARTx: Specified serial port
* @param str:Send data content
* @param strlen:Send data len
* @retval None
*/
voidUsart2_Send( char*str,uint8_tstrlen)
{
unsignedintk=0;
do
{
HAL_UART_Transmit(&huart2,(uint8_t*)(str+k),1,1000);
k++;
} while(k<strlen);
}
/**
* @brief Usart1Send
* @param pUSARTx: Specified serial port
* @param str:Send data content
* @param strlen:Send data len
* @retval None
*/
voidUsart3_Send( char*str,uint8_tstrlen)
{
unsignedintk=0;
do
{
HAL_UART_Transmit(&huart3,(uint8_t*)(str+k),1,1000);
k++;
} while(k<strlen);
}
/**
* @brief HAL_UART_RxCpltCallback
* @param huart: Specified serial port
* @retval None
*/
voidHAL_UART_RxCpltCallback(UART_HandleTypeDef*huart)
{
if(huart->Instance==USART2)
{
if(Uart2_Rx_Cnt>254)
{
Uart2_Rx_Cnt=0;
memset(RxBuffer_2,0x00,sizeof(RxBuffer_2));
}
else
{
RxBuffer_2[Uart2_Rx_Cnt++] =aRxBuffer_2;
}
HAL_UART_Receive_IT(&huart2, (uint8_t*)&aRxBuffer_2, 1);
}elseif(huart->Instance==USART3)
{
if(Uart3_Rx_Cnt>254)
{
Uart3_Rx_Cnt=0;
memset(RxBuffer_3,0x00,sizeof(RxBuffer_3));
}
else
{
RxBuffer_3[Uart3_Rx_Cnt++] =aRxBuffer_3;
}
HAL_UART_Receive_IT(&huart3, (uint8_t*)&aRxBuffer_3, 1);
}
UNUSED(huart);
}
- Add H file declaration code
externvoidUsart2_Send( char*str,uint8_tstrlen);
externvoidUsart2_Send( char*str,uint8_tstrlen);
- Add definition number code
uint8_taRxBuffer_2=0;
uint8_taRxBuffer_3=0;
unsignedcharaRxBuffer_hlp=0;
unsignedcharUart2_Rx_Cnt=0;
unsignedcharUart3_Rx_Cnt=0;
unsignedcharRxBuffer_2[RXBUFFERSIZE];
unsignedcharRxBuffer_3[RXBUFFERSIZE];
unsignedcharhlpUart1_Rx_Cnt=0;
unsignedcharRxBuffer_hlp[RXBUFFERSIZE];
2.4. Serial communication test
|