STM32
直播中

王彬

8年用户 1100经验值
私信 关注
[问答]

stm32f7系列使用printf打印在串口上显示不出来怎么解决?

  #include "STM32f7xx.h"
#include  "stm32f7xx_hal.h"

#include         
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

static void Error_Handler(void);
static void CPU_CACHE_Enable(void);
void systemclockinit(int plln,int pllm,int pllp,int pllq);
void uartinit(void);

static UART_HandleTypeDef UARThandle;

int main(void)
{
  /* Enable the CPU Cache */
  CPU_CACHE_Enable();

  HAL_Init();
        systemclockinit(432,25,2,9);
        uartinit();
        printf("hello");
//        HAL_icInit();
// printf("my printf test,compile time:%s %srn",__DATE__,__TIME__);
        printf("hello");
        while(1)
  {
/*        if(TIM5CH1_Capture_STA 0X40)
               
        {
        temp=TIM5CH1_Capture_STA;
        temp*=0xFFFFFFFF;
        temp+=TIM5CH1_Capture_VAL;
        sprintf(text, "AD value = 0x%04X", temp);
        
        }                                
                */
  }
}

static void Error_Handler(void)
{
  /* User may add here some code to deal with this error */
  while(1)
  {
  }
}


static void CPU_CACHE_Enable(void)
{
  /* Enable I-Cache */
  SCB_EnableICache();

  /* Enable D-Cache */
  SCB_EnableDCache();
}


//串口初始化
void uartinit(void)
{
        UARThandle.Instance=USART1;
        UARThandle.Init.BaudRate=115200;
        UARThandle.Init.WordLength=UART_WORDLENGTH_8B;
        UARThandle.Init.StopBits=UART_STOPBITS_1;
        UARThandle.Init.HwFlowCtl=UART_HWCONTROL_NONE;
        UARThandle.Init.Parity=UART_PARITY_NONE;   
        UARThandle.Init.Mode=UART_MODE_TX_RX;
        if        (HAL_UART_Init( UARThandle)!=HAL_OK)
        {
  Error_Handler();        
        }
}


//串口回调函数
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  GPIO_InitTypeDef  GPIO_InitStruct;

  RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;

  /*##-1- Enable peripherals and GPIO Clocks #################################*/
  /* Enable GPIO TX/RX clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /* Select SysClk as source of USART1 clocks */
  RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  RCC_PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
  HAL_RCCEx_PeriphCLKConfig( RCC_PeriphClkInit);

  /* Enable USARTx clock */
  __HAL_RCC_USART1_CLK_ENABLE();

  /*##-2- Configure peripheral GPIO ##########################################*/
  /* UART TX GPIO pin configuration  */
  GPIO_InitStruct.Pin       = GPIO_PIN_9;
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART1;

  HAL_GPIO_Init(GPIOA,  GPIO_InitStruct);


  /* UART RX GPIO pin configuration  */
  GPIO_InitStruct.Pin = GPIO_PIN_10;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART1;

  HAL_GPIO_Init(GPIOA,  GPIO_InitStruct);

}


//printf重定向
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit( UARThandle, (uint8_t *) ch, 1, 0xFFFF);

  return ch;
}




回帖(1)

王丹青

2024-4-11 09:52:53
从代码上看是正常的,检查一下串口的连接,RX接TX。如果还是不确定,用示波器看一下TX的波形。注意开发板的串口连接引脚和实际的是否一致。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分