操作步骤

01、在项目中,加上以下函数即可(作用:重写fputc)

           库函数版

                int fputc( int ch, FILE *f ){
                      USART_SendData(USART1,(u8) ch );
                      while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
                      return ch;
               }

           寄存器版

              int fputc(int ch, FILE *f){ 
                 while((USART2->SR&0X40)==0);//循环发送,直到发送完毕   
                 USART2->DR = (u8)ch;      
                 return ch;
             }

02、在使用printf函数的.C文件张工包含头文件stdio.h

      #include<stdio.h>

03、勾选Target中的use MicroLIB
STM32重写fputc

 04、HAL库重写fputc

int fputc(int ch,FILE *f)
{
    uint8_t temp[1]={ch};
    HAL_UART_Transmit(&UartHandle,temp,1,2);
}

         说明

               HAL库与库函数接口有所不同

          参数说明

               UartHandle为定义的串口全局变量

                        例

                               UART_HandleTypeDef UartHandle;

              temp为数组名

                       例

                             uint8_t temp[3];

           1为传输的大小

           2为超时时间

相关文章:

  • 2021-10-27
  • 2022-02-08
  • 2022-12-23
  • 2021-12-18
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
相关资源
相似解决方案