【STM32】-CubeMX-HAL library-UART-serial communication-STM32F103C8T6-transmitting test

 CubeMX-UART serial port configuration method reference: CubeMx project creation reference

​​​​​​​​Understanding the UART serial port receive interrupt code reference: STM32CubeMX-HAL library-UART serial port receive interrupt callback function code analysis

        In order to learn the basic operation of CubeMx and the HAL library call, here is an attempt to use the HAL library based on STM32F103C8T6 to realize the sending and receiving test between the UART serial port and the PC. At the end, the project documents are given for your reference!

-------------------------------------------------- ---------------------Refer to the function code of the punctual atomic serial port experiment library --------------------- --------------------------------------------------

Main function part:

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
	HAL_UART_Receive_IT(&huart1, &Res, 1);//将Res作为UART中断接收数据的变量
	HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin,GPIO_PIN_RESET);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
			if(UART1_RX_STA & 0x8000)//判断UART接收是否完成
			{
				printf("Get data:");
                //正常发送,非中断发送
				HAL_UART_Transmit(&huart1,UART1_RX_Buffer,UART1_RX_STA&0x3fff, 0xffff);
				while(huart1.gState != HAL_UART_STATE_READY);//等待发送完成MCU->上位机
				printf("\r\n");
				UART1_RX_STA=0;
			}
	HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);
	printf("test\r\n");
	HAL_Delay(500);
  }
  /* USER CODE END 3 */
}

Callback function part:

/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//自定义回调函数 在UART_Receive_IT()中调用
{
	//判断是哪个串口触发的中断  huart1.Instance = USART1;定义在MX_USART1_UART_Init中
	if(huart==&huart1)//huart ->Instance == USART1两种判断条件等价
	{
		if((UART1_RX_STA & 0x8000)==0)//接收未完成&位运算符 &&短路与判断
		{
			if(UART1_RX_STA & 0x4000)//接收到 \r
			{
				if(Res==0x0a)//下一个必须是接收 \n
					UART1_RX_STA|=0x8000;
				else
					UART1_RX_STA=0;
			}
			else //未接收到\r
			{
				if(Res==0x0d)//Receive \r
				{
					UART1_RX_STA|=0x4000;
				}
				else
				{
					UART1_RX_Buffer[UART1_RX_STA&0X3FFF]=Res;
					UART1_RX_STA++;
					if(UART1_RX_STA>UART1_REC_LEN-1) UART1_RX_STA=0;//如果接收数据大于200Byte 重新开始接收
				}
			}
		}
		HAL_UART_Receive_IT(&huart1, &Res, 1);//完成一次接受,再此开启中断
	}
}
/* USER CODE END 4 */

Printf remapping: the custom fputc function will be called first in the printf function

/*printf函数重定义,使用用UART1打印到上位机*/
/* USER CODE BEGIN 0 */
int fputc(int ch, FILE *f)
{      
	unsigned char temp[1]={ch};
	HAL_UART_Transmit(&huart1,temp,1,0xffff);//循环发送,直到发送完毕   
	return ch;
}
/* USER CODE END 0 */

Precautions:


/* USER CODE BEGIN 4 */
    //自己编写的代码最好写在这个里面
    //不然CubeMx生成代码时,自己写的代码会被覆盖。
/* USER CODE END 4 */

Test situation:

working condition:

        Test will be printed to the terminal every 500ms, and the LED lights will flash together (please refer to the code for details)

Wiring situation:

        Since the STM32F103C8T6 minimum system does not have a CH340 chip, it is necessary to use the CH340 module to convert the USB level to the TTL level, so that the PC can establish communication with the STM32.

Wiring situation:

CH340 STM32F103C8T6
3V3     3V3    
GND GND
RXD PA9(UART1_TXD)
TXD PA10(UART1_RXD)

Engineering code: https://gitee.com/waynesuper/mygitee

If this blog is helpful to you, please like it before leaving, Respect all bloggers who have helped me!

Guess you like

Origin blog.csdn.net/weixin_44322104/article/details/125187420