(stm32f103學習總結)—printf重定向


一、printf重定向簡介

  我們知道C語言中printf函數默認輸出設備是顯示器,如果要實現在 串口或者LCD上顯示,必須重定義標准庫函數里調用的與輸出設備相關的函數。比如使用printf輸出到串口,需要將fputc里面的輸出指向串口, 這一過程就叫重定向。 那么如何讓STM32使用printf函數呢?

int fputc(int ch,FILE *p) //函數默認的,在使用printf函數時自動調用

{

  USART_SendData(USART1,(u8)ch);

  while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);

  return ch;

}

二、printf函數格式

printf("<格式化字符串>", <參量表>);

 

 

 

//.h文件
1
#ifndef __usart_H 2 #define __usart_H 3 4 #include "system.h" 5 #include "stdio.h" 6 7 void USART1_Init(u32 bound); 8 9 10 #endif

 

 

 
         
//.c文件
 1 #include "usart.h"         
 2 
 3 int fputc(int ch,FILE *p)  //函數默認的,在使用printf函數時自動調用
 4 {  5  USART_SendData(USART1,(u8)ch);  6     while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);  7     return ch;  8 }  9 
10 /******************************************************************************* 11 * 函 數 名 : USART1_Init 12 * 函數功能 : USART1初始化函數 13 * 輸 入 : bound:波特率 14 * 輸 出 : 無 15 *******************************************************************************/ 
16 void USART1_Init(u32 bound) 17 { 18    //GPIO端口設置
19  GPIO_InitTypeDef GPIO_InitStructure; 20  USART_InitTypeDef USART_InitStructure; 21  NVIC_InitTypeDef NVIC_InitStructure; 22     
23  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 24  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); 25     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);     //打開時鍾
26  
27     
28     /* 配置GPIO的模式和IO口 */
29     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口輸出PA9
30     GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; 31     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;        //復用推挽輸出
32     GPIO_Init(GPIOA,&GPIO_InitStructure);  /* 初始化串口輸入IO */
33     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口輸入PA10
34     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;          //模擬輸入
35     GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */
36     
37 
38    //USART1 初始化設置
39     USART_InitStructure.USART_BaudRate = bound;//波特率設置
40     USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數據格式
41     USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
42     USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗位
43     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數據流控制
44     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    //收發模式
45     USART_Init(USART1, &USART_InitStructure); //初始化串口1
46     
47     USART_Cmd(USART1, ENABLE);  //使能串口1 
48     
49  USART_ClearFlag(USART1, USART_FLAG_TC); 50         
51     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啟相關中斷 52 
53     //Usart1 NVIC 配置
54     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中斷通道
55     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//搶占優先級3
56     NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;        //子優先級3
57     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //IRQ通道使能
58     NVIC_Init(&NVIC_InitStructure);    //根據指定的參數初始化VIC寄存器、 
59 } 60 
61 /******************************************************************************* 62 * 函 數 名 : USART1_IRQHandler 63 * 函數功能 : USART1中斷函數 64 * 輸 入 : 無 65 * 輸 出 : 無 66 *******************************************************************************/ 
67 void USART1_IRQHandler(void)                    //串口1中斷服務程序
68 { 69  u8 r; 70     if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中斷
71  { 72         r =USART_ReceiveData(USART1);//(USART1->DR); //讀取接收到的數據
73  USART_SendData(USART1,r); 74         while(USART_GetFlagStatus(USART1,USART_FLAG_TC) != SET); 75  } 76  USART_ClearFlag(USART1,USART_FLAG_TC); 77 }     

 

 

 

 // main文件
1
#include "system.h" 2 #include "SysTick.h" 3 #include "led.h" 4 #include "usart.h" 5 6 7 /******************************************************************************* 8 * 函 數 名 : main 9 * 函數功能 : 主函數 10 * 輸 入 : 無 11 * 輸 出 : 無 12 *******************************************************************************/ 13 int main() 14 { 15 u8 i=0; 16 u16 data=1234; 17 float fdata=12.34; 18 char str[]="Hello World!"; 19 SysTick_Init(72); 20 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中斷優先級分組 分2組 21 LED_Init(); 22 USART1_Init(9600); 23 24 while(1) 25 { 26 i++; 27 if(i%20==0) 28 { 29 led1=!led1; 30 31 printf("輸出整型數data=%d\r\n",data); 32 printf("輸出浮點型數fdata=%0.2f\r\n",fdata); 33 printf("輸出十六進制數data=%X\r\n",data); 34 printf("輸出八進制數data=%o\r\n",data); 35 printf("輸出字符串str=%s\r\n",str); 36 37 } 38 delay_ms(10); 39 } 40 }

 


免責聲明!

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



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