学习记录(三) UART串口通信 中断式、printf发送 STM32CubeMX MDK5 ST-LinkV2 STM32G474VET6

3.1.1 依旧CubeMX创建工程,打开GPIO(这里从表格操作打开的引脚和原理图不符,以原理图为准)、RCC,USART1设置为异步(Asynchronous)模式,因为要使用非阻塞式发送,所以还要打开USART1的中断。

 

3.1.2 代码上与LED操作大同小异,只需改任务执行的函数,使用HAL库的HAL_UART_Transmit_IT()函数,附源码。运行效果为每秒发送一个字符串,打印在串口调试助手上。

static MAINAPP_COMPONENTS	theComps[] =
{
    {  0,	0,	1000,       MCM_AUTO,		USART_Send },
	{  0,   0,  1000,       MCM_AUTO,       App_EndProcess  },
};
void USART_Send(void)
{
		HAL_UART_Transmit_IT(&huart1, sendData, 10);    // u8 sendData[10] = "qwert12345";
}
/**
  * @brief Send an amount of data in interrupt mode.
  * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  *         the sent data is handled as a set of u16. In this case, Size must indicate the number
  *         of u16 provided through pData.
  * @param huart UART handle.
  * @param pData Pointer to data buffer (u8 or u16 data elements).
  * @param Size  Amount of data elements (u8 or u16) to be sent.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
{
  /* Check that a Tx process is not already ongoing */
  if (huart->gState == HAL_UART_STATE_READY)
  {
    if ((pData == NULL) || (Size == 0U))
    {
      return HAL_ERROR;
    }

    __HAL_LOCK(huart);

    huart->pTxBuffPtr  = pData;
    huart->TxXferSize  = Size;
    huart->TxXferCount = Size;
    huart->TxISR       = NULL;

    huart->ErrorCode = HAL_UART_ERROR_NONE;
    huart->gState = HAL_UART_STATE_BUSY_TX;

    /* Configure Tx interrupt processing */
    if (huart->FifoMode == UART_FIFOMODE_ENABLE)
    {
      /* Set the Tx ISR function pointer according to the data word length */
      if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
      {
        huart->TxISR = UART_TxISR_16BIT_FIFOEN;
      }
      else
      {
        huart->TxISR = UART_TxISR_8BIT_FIFOEN;
      }

      __HAL_UNLOCK(huart);

      /* Enable the TX FIFO threshold interrupt */
      SET_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
    }
    else
    {
      /* Set the Tx ISR function pointer according to the data word length */
      if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
      {
        huart->TxISR = UART_TxISR_16BIT;
      }
      else
      {
        huart->TxISR = UART_TxISR_8BIT;
      }

      __HAL_UNLOCK(huart);

      /* Enable the Transmit Data Register Empty interrupt */
      SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
    }

    return HAL_OK;
  }
  else
  {
    return HAL_BUSY;
  }
}

 

3.1.3 原理图看到MCU与串口调试接口的RX、TX并没有反接,所以接线时要将RXD接TX、TXD接RX,接线如下图。

3.1.4 在串口调试助手中验证成功。

 

3.2.1 再使用print()函数发送,先在 uart.c 中添加下段代码:(要包含stdio.h)

/* USER CODE BEGIN 1 */
int fputc(int ch, FILE *f)
{
	HAL_UART_Transmit(&huart1, (u8 *)&ch, 1, 0xFFFF);  //阻塞式发送
	return ch;
}
/* USER CODE END 1 */

即可使用:

void USART_Send(void)
{
	//中断式发送	
	//HAL_UART_Transmit_IT(&huart1, sendData, 10);
	
	//printf发送
	printf("hatjhrtj56j5\n");
}

 

3.2.2 可在串口调试助手中看到结果:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值