一。printf函數格式
printf函數具有強大的輸出功能
%表示格式化字符串輸出
目前printf支持以下格式的輸出,例如:
printf("%c",a);輸出單個字符。
printf("%d",a);輸出十進制整數。
printf("%f",a);輸出十進制浮點數.
printf("%o",a);輸出八進制數。
printf("%s",a);輸出字符串。
printf("%u",a);輸出無符號十進制數。
printf("%x",a);輸出十六進制數。
例如:
n = 15
printf("The result is %d", n); //通過屏幕輸出十進制數15
n = 15.2
printf("The result is %f", n); //通過屏幕輸出十進制浮點數15
二。實現方法
在uart.c文件中加入
#include "stdio.h"
//////////////////////////////////////////////////////////////////
//加入以下代碼,支持printf函數,而不需要選擇use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//標准庫需要的支持函數
struct __FILE
{
int handle;
};
FILE __stdout;
//定義_sys_exit()以避免使用半主機模式
_sys_exit(int x)
{
x = x;
}
//重定義fputc函數
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//把數據通過串口1循環發送,直到發送完畢 ,如果使用串口2,則改成 USART2
USART1->DR = (u8) ch;
return ch;
}
#endif
三。主函數中調用printf函數
int main(void)
{
u16 t;
u16 len;
u16 times=0;
delay_init(); //延時函數初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
uart_init(115200); //串口初始化為115200
LED_Init(); //LED端口初始化
KEY_Init(); //初始化與按鍵連接的硬件接口
while(1)
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3fff;//得到此次接收到的數據長度
printf("\r\n您發送的消息為:\r\n\r\n"); //\r\n為回車換行
for(t=0;t
{
USART_SendData(USART1, USART_RX_BUF[t]);//向串口1發送數據
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待發送結束
}
printf("\r\n\r\n");//插入換行
USART_RX_STA=0;
}else
{
times++;
if(timesP00==0)
{
printf("\r\n戰艦STM32開發板 串口實驗\r\n");
printf("正點原子@ALIENTEK\r\n\r\n");
}
if(times 0==0)printf("請輸入數據,以回車鍵結束\n");
if(times0==0)LED0=!LED0;//閃爍LED,提示系統正在運行.
delay_ms(10);
}
}
}

(stm32串口應用)
http://www.makeru.com.cn/live/1392_1164.html?s=45051
PWM脈寬調制技術
http://www.makeru.com.cn/live/4034_2146.html?s=45051
基於STM32講解串口操作
http://www.makeru.com.cn/live/1758_490.html?s=45051
通過Z-stack協議棧實現串口透傳
http://www.makeru.com.cn/live/1758_330.html?s=45051