微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 在MDK下面怎样用printf对stm32进行调试

在MDK下面怎样用printf对stm32进行调试

时间:10-02 整理:3721RD 点击:
大家好,参照网上的例子整了一下,发现串口1还是发不出数来,另外串口配置是没有问题,我用USART_SendData()函数发送是没问题的,有做过的给说说怎么回事?
参照网址:http://www.amobbs.com/thread-5532403-1-1.html

自己先顶一下啦啦啦啦
原帖是这样的
1 添加stdio.h 文件
2 Target选项框里选Use MicroLib 选项
3 代码添加,比如要用到Uart1,则给Uart1初始化,void UART1_Configuration(void) 我的代码供参考
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 代码添加,  
int fputc(int ch, FILE *f)
{
    //USART_SendData(USART1, (u8) ch);
    USART1->DR = (u8) ch;
     
    /* Loop until the end of transmission */
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
    {
    }
    return ch;
}
这个是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自带的模拟串口中看到激动人心的输出了,本人亲测, 串口函数部分有些不同
串口调试非常实用的

直接用printf是不行的 可以重定向printf到USARTx

使用putchar函数 包含stdio.h文件,然后定义STDIO变量 主要是实现printf的重定向

顶一下!

printf重定向,需要修改驱动函数的,参考下http://home.eeworld.com.cn/my/space-uid-320632-blogid-112464.html

支持一下,大家都给看看。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top