stm32中如何進行printf重定向用於串口調試輸出


1 在main中包含stdio.h 文件

2 Target選項框里選Use MicroLib 選項

3 在main中添加UART1_Configuration()初始化的代碼

Uart1初始化,
void UART1_Configuration(void)
{
          USART_InitTypeDef USART_InitStructure;
        USART_ClockInitTypeDef  USART_ClockInitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_USART1, ENABLE  );
        
        USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;                        // 時鍾低電平活動
        USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;                                // 時鍾低電平
        USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;                                // 時鍾第二個邊沿進行數據捕獲
        USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;                // 最后一位數據的時鍾脈沖不從SCLK輸出
        /* Configure the USART1 synchronous paramters */
        USART_ClockInit(USART1, &USART_ClockInitStructure);                                        // 時鍾參數初始化設置
                                                                                                                                                 
        USART_InitStructure.USART_BaudRate =9600;                                                  // 波特率為:115200
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;                          // 8位數據
        USART_InitStructure.USART_StopBits = USART_StopBits_1;                                  // 在幀結尾傳輸1個停止位
        USART_InitStructure.USART_Parity = USART_Parity_No ;                                  // 奇偶失能
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;        // 硬件流控制失能
        
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;                  // 發送使能+接收使能
        /* Configure USART1 basic and asynchronous paramters */
        USART_Init(USART1, &USART_InitStructure);
            
          /* Enable USART1 */
        USART_ClearFlag(USART1, USART_IT_RXNE);                         //清中斷,以免一啟用中斷后立即產生中斷
        USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);                //使能USART1中斷源
        USART_Cmd(USART1, ENABLE);                                                        //USART1總開關:開啟

}
4 子函數代碼添加,  可以也加在main函數后面,也可以單獨寫一個uart.h,然后包含在main中。(親測不加入這兩個函數也可以printf重定向輸出


//int fputc(int ch, FILE *f)
//{
// /* 發送一個字節數據到USART1 */
// USART_SendData(USART1, (u8) ch);
//
// /* 等待發送完畢 */
// while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
//
// return (ch);
//}
//
///// 重定向c庫函數scanf到USART1
//int fgetc(FILE *f)
//{
// /* 等待串口1輸入數據 */
// while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
//
// return (int)USART_ReceiveData(USART1);
//}
//


這個是printf函數調用串口輸出字符的
5 添加串口中斷程序,在stm32XX_it.c中,先定義幾個用到的變量
#define TxBufferSize   (countof(TxBuffer) - 1)
#define RxBufferSize   0x20

/* Private macro -------------------------------------------------------------*/
#define countof(a)   (sizeof(a) / sizeof(*(a)))

/* Private variables ---------------------------------------------------------*/
u8 TxBuffer[] = "\n\rUSART Hyperterminal Interrupts Example: USART-Hyperterminal\
communication using Interrupt\n\r";
u8 RxBuffer[RxBufferSize];
u8 NbrOfDataToTransfer = TxBufferSize;
u8 NbrOfDataToRead = RxBufferSize;
u8 TxCounter = 0; 
u16 RxCounter = 0;

然后是中斷程序
/*******************************************************************************
* Function Name  : USART1_IRQHandler
* Description    : This function handles USART1 global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    /* Read one byte from the receive data register */
    RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0x7F);
   if(RxCounter == NbrOfDataToRead)
    {
      /* Disable the USART Receive interrupt */
          RxCounter = 0;
      //USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
    }
  }

  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {   
    /* Write one byte to the transmit data register */
    USART_SendData(USART1, TxBuffer[TxCounter++]);                    

   if(TxCounter == NbrOfDataToTransfer)
    {
      /* Disable the USART1 Transmit interrupt */
      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
    }    
  }           
}

這部分不加好像也可以實現,有待我后續驗證。
6上邊ok之后,就可以隨便printf()輸出了,軟件debug,
就可以在MDK自帶的模擬串口中看到激動人心的輸出了,本人親測, 串口函數部分有些不同

串口調試非常實用的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM