STM32 keil printf的使用


http://blog.sina.com.cn/s/blog_6dad298b0100tp20.html

請在MDK(keil)工程屬性的“Target“-》”Code Generation“中勾選”Use MicroLIB

 

前提是你有一個完整keil的工程 比如ADC的

調試的時候很多時候用到串口 這里教你怎么樣使用Printf 函數

紅色字句為重點!!!!!

在程序中添加Printf
1,
#include <stdio.h>
2,
下添加

void USART_Configuration(void);

#ifdef __GNUC__

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

3,添加如下2個函數 usart配置 和 重定向 C庫的printf函數
void USART_Configuration()
{

USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);


USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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;

USART_Init(USART1, &USART_InitStructure);

USART_Cmd(USART1, ENABLE);
}


PUTCHAR_PROTOTYPE
{


USART_SendData(USART1, (uint8_t) ch);


while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}

return ch;
}
4,

void RCC_Configuration(void) 添加

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);

5,
STM32F10x.CONF.H

去掉 的注釋
6,
在Main()中添加

void USART_Configuration()

然后就可以在main()調用

printf("The is a example!" );

printf("%s%c%c%c%c%c%s",
           "#**",
           Value/256,Value%6,
          '&',
           Value_2/256,Value_2%6,
          "**%");

之類的輸出函數

 


免責聲明!

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



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