码迷,mamicode.com
首页 > 其他好文 > 详细

8、printf

时间:2015-05-27 20:52:16      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

1、调用头文件stdio.h, 然后重定义printf函数,如下: 

技术分享
1 int fputc(int ch,FILE *F)
2 {
3     
4     USART_SendData(USART1,(u8)ch);
5     
6     while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位
7     
8     return ch;
9 }
View Code

 


2、例程如下:

技术分享
  1 /*************************************************************************************************************************************
  2 *  
  3 *  文件名称:main.c
  4 *  文件功能:主函数文件
  5 *
  6 ***************************************************************************************************************************************/
  7 
  8 #include "pbdata.h"//调用自定义公共函数库
  9 
 10 
 11 /*********************************************************************************
 12 *
 13 * 初始化操作
 14 *
 15 *********************************************************************************/
 16 void RCC_Configuration(void);//系统时钟初始化函数声明
 17 void GPIO_Configuration(void);//GPIO初始化函数声明
 18 void NVIC_Configuration(void);//中断优先级配置函数声明
 19 void USART_Configuration(void);//串口配置函数声明
 20 
 21 
 22 
 23 
 24 /********************************************************************************
 25 *
 26 *  函数名称:main(void)
 27 *  函数功能:主函数
 28 *  函数说明:不能用void定义主函数
 29 *
 30 ********************************************************************************/
 31 int main(void)//void不能void定义主函数
 32 {
 33         
 34     RCC_Configuration();    //系统时钟初始化
 35     
 36     GPIO_Configuration();//端口初始化
 37     
 38     USART_Configuration();//串口配置
 39     
 40     NVIC_Configuration();//中断优先级配置
 41     
 42     
 43     
 44     while(1)
 45     {
 46         printf("liubo\n");
 47         
 48         delay_ms(3000);
 49     }
 50     
 51 }
 52     
 53     
 54 
 55 
 56 /********************************************************************************
 57 *
 58 *  函数名称:RCC_Configuration(void)
 59 *  函数功能:系统时钟高初始化函数
 60 *
 61 ********************************************************************************/
 62     void RCC_Configuration(void)//系统时钟高初始化函数
 63   {
 64         
 65     SystemInit();//系统初始化
 66     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//串口对应GPIO时钟使能
 67         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能
 68         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//引脚复用
 69 
 70     }
 71     
 72     
 73 
 74 /*******************************************************************************
 75 *
 76 * 函数名称:GPIO_Configuration(void)
 77 * 函数功能:GPIO初始化函数
 78 *
 79 ********************************************************************************/    
 80     
 81     void GPIO_Configuration(void)//GPIO初始化函数
 82   {
 83               
 84         
 85 /*串口引脚配置*/
 86         GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量
 87 
 88 /*输出引脚配置*/        
 89         /*结构体变量赋值*/
 90       GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;////引脚配置TX
 91       GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率
 92         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出
 93         /*对应的GPIO初始化*/
 94       GPIO_Init(GPIOA,&GPIO_InitStructure);
 95         
 96     
 97 /*输入引脚配置*/        
 98         /*结构体变量赋值*/
 99       GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;////引脚配置RX
100         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//接收引脚配置成浮空输入
101         /*对应的GPIO初始化*/
102       GPIO_Init(GPIOA,&GPIO_InitStructure);        
103 
104         
105   }
106     
107 
108 /****************************************************************************
109 *
110 *  函数名称:NVIC_Configuration(void)
111 *  函数功能:配置中断优先级
112 *
113 ****************************************************************************/
114     
115 void NVIC_Configuration(void)
116 {
117   NVIC_InitTypeDef NVIC_InitStructure; //定义一个优先级配置结构体变量
118     
119   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组
120 
121   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
122   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级
123   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级
124   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能
125     
126   NVIC_Init(&NVIC_InitStructure);//初始化
127 }
128     
129 
130 /*********************************************************************************
131 *
132 *  函数名称:
133 *  函数功能:串口配置函数
134 *
135 *********************************************************************************/
136 void USART_Configuration(void)
137 {
138 /*定义串口配置结构体变量*/
139         USART_InitTypeDef USART_InitStructure;//定义一个串口配置结构体变量
140     
141     
142 /*结构体变量赋值*/
143     USART_InitStructure.USART_BaudRate = 9600;//波特率9600
144     USART_InitStructure.USART_WordLength = USART_WordLength_8b;//位宽,8位
145     USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1
146     USART_InitStructure.USART_Parity = USART_Parity_No;//不奇偶校验
147     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控禁止
148     USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送使能
149 
150     
151 /*发送串口配置初始化*/
152     USART_Init(USART1, &USART_InitStructure);
153     
154 
155 /*打开串口接收中断*/
156       USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//当接收到数据时,会产生中断
157     
158     
159 /*打开串口*/
160     USART_Cmd(USART1,ENABLE);//串口使能,打开
161         
162 /*清空中断标志位*/
163     USART_ClearFlag(USART1,USART_FLAG_TC);
164 }
165     
166     
167     
168     
169     
170 
171     
172     
173 
174     
175 
176     
177     
178     
179     
180     
181     
182     
183     
184     
185     
186     
187     
188     
189     
190     
191     
192     
193     
194     
195     
196     
197     
198     
199     
200     
View Code
技术分享
  1 /**
  2   ******************************************************************************
  3   * @file GPIO/IOToggle/stm32f10x_it.c 
  4   * @author  MCD Application Team
  5   * @version  V3.0.0
  6   * @date  04/06/2009
  7   * @brief  Main Interrupt Service Routines.
  8   *         This file provides template for all exceptions handler and 
  9   *         peripherals interrupt service routine.
 10   ******************************************************************************
 11   * @copy
 12   *
 13   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
 14   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
 15   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
 16   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
 17   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
 18   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 19   *
 20   * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
 21   */
 22 
 23 /* Includes ------------------------------------------------------------------*/
 24 #include "stm32f10x_it.h"     
 25 #include "stm32f10x_exti.h"
 26 #include "stm32f10x_rcc.h"
 27 #include "stm32f10x_tim.h"
 28 #include "misc.h"
 29 #include "pbdata.h"
 30 
 31 void NMI_Handler(void)
 32 {
 33 }
 34 
 35 
 36 
 37 /**
 38   * @brief  This function handles Hard Fault exception.
 39   * @param  None
 40   * @retval : None
 41   */
 42 void HardFault_Handler(void)
 43 {
 44   /* Go to infinite loop when Hard Fault exception occurs */
 45   while (1)
 46   {
 47   }
 48 }
 49 
 50 /**
 51   * @brief  This function handles Memory Manage exception.
 52   * @param  None
 53   * @retval : None
 54   */
 55 void MemManage_Handler(void)
 56 {
 57   /* Go to infinite loop when Memory Manage exception occurs */
 58   while (1)
 59   {
 60   }
 61 }
 62 
 63 /**
 64   * @brief  This function handles Bus Fault exception.
 65   * @param  None
 66   * @retval : None
 67   */
 68 void BusFault_Handler(void)
 69 {
 70   /* Go to infinite loop when Bus Fault exception occurs */
 71   while (1)
 72   {
 73   }
 74 }
 75 
 76 /**
 77   * @brief  This function handles Usage Fault exception.
 78   * @param  None
 79   * @retval : None
 80   */
 81 void UsageFault_Handler(void)
 82 {
 83   /* Go to infinite loop when Usage Fault exception occurs */
 84   while (1)
 85   {
 86   }
 87 }
 88 
 89 /**
 90   * @brief  This function handles SVCall exception.
 91   * @param  None
 92   * @retval : None
 93   */
 94 void SVC_Handler(void)
 95 {
 96 }
 97 
 98 /**
 99   * @brief  This function handles Debug Monitor exception.
100   * @param  None
101   * @retval : None
102   */
103 void DebugMon_Handler(void)
104 {
105 }
106 
107 /**
108   * @brief  This function handles PendSV_Handler exception.
109   * @param  None
110   * @retval : None
111   */
112 void PendSV_Handler(void)
113 {
114 }
115 
116 /**
117   * @brief  This function handles SysTick Handler.
118   * @param  None
119   * @retval : None
120   */
121 void SysTick_Handler(void)
122 {
123 }
124 
125 
126 
127 /****************************************************************************
128 *
129 * 函数名称:void TIM3_IRQHandler(void)
130 * 外部中断处理函数
131 *
132 ****************************************************************************/
133 
134 
135 void TIM3_IRQHandler(void)
136 {
137     TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
138 
139     if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET)
140     {
141         if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET)
142         {
143             //LED 熄灭
144            GPIO_SetBits(GPIOB,GPIO_Pin_5);    
145         }
146         else
147         {
148            //LED 发光
149            GPIO_ResetBits(GPIOB,GPIO_Pin_5);
150         }
151     }
152 
153 }
154 
155 
156 
157 
158 /****************************************************************************
159 *
160 * 函数名称:void EXTI9_5_IRQHandler(void)
161 * 外部中断处理函数
162 *
163 ****************************************************************************/
164 void EXTI9_5_IRQHandler(void)
165 {
166   if(EXTI_GetITStatus(EXTI_Line5)==SET)  //判断是否产生了中断
167   {
168        EXTI_ClearITPendingBit(EXTI_Line5);//中断清除
169 
170       delay_ms(10);//延时消抖
171 
172       if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET) //再次判断,消抖
173       {
174           if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET)//如果LED是亮
175           {
176            GPIO_SetBits(GPIOB,GPIO_Pin_5);    //则熄灭
177           }
178           else
179           {
180               GPIO_ResetBits(GPIOB,GPIO_Pin_5);//如果是熄灭的,就亮
181           }
182       }
183   }    
184 }
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 /****************************************************************************
195 * ?    ?úvoid EXTI1_IRQHandler(void)
196 * ?    ?úEXTI2???m??
197 * ????ú?
198 * ????ú?
199 * ?    ?ú
200 * ????ú? 
201 ****************************************************************************/
202 void EXTI1_IRQHandler(void)
203 {
204     
205 }
206 
207 /****************************************************************************
208 * ?    ?úvoid EXTI2_IRQHandler(void)
209 * ?    ?úEXTI2???m??
210 * ????ú?
211 * ????ú?
212 * ?    ?ú
213 * ????ú? 
214 ****************************************************************************/
215 void EXTI2_IRQHandler(void)
216 {
217     
218 }
219 
220 /****************************************************************************
221 *
222 * 函数名称:void EXTI3_IRQHandler(void)
223 * 函数功能:外部中断XTI3中断处理函数
224 * 
225 ****************************************************************************/
226 void EXTI3_IRQHandler(void)
227 {
228 
229      
230 }
231 
232 
233 /****************************************************************************
234 *
235 * 函数名称:USART1_IRQHandler(void)
236 * 函数功能:串口中断1处理函数
237 * 
238 ****************************************************************************/
239 void USART1_IRQHandler(void)
240 {
241     
242      if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//判断是否发生了串口中断
243      {
244          
245          USART_SendData(USART1,USART_ReceiveData(USART1));//从串口中读取数据,然后发送出去
246          
247          while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位
248          
249      }
250  }
View Code
技术分享
/****************************************************************************************************************
*
* 文件名称:pbdata.c
* 文件功能:自定义函数或者全局变量的初始化
*
****************************************************************************************************************/

/*头文件声明*/
#include "pbdata.h"




/********************************************************************************************
*
* 自定义全局变量
*
********************************************************************************************/
u8 dt=0;





/******************************************************************************************
*
* 自定义函数
*
******************************************************************************************/



/**************************************************
*
*  函数名称:delay_us(u32 nus)
*  函数功能:微秒延时函数
*  输入参数:输入值为延时us
*
***************************************************/
void delay_us(u32 nus)
{
    u32 temp;
    SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9次
    SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值
    SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟
    
    do
    {
          temp=SysTick->CTRL;//标志位,等到一直减到0
         }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达
    
    SysTick->CTRL=0x00; //关闭计数器
    SysTick->VAL =0X00; //清空计数器
}







/***************************************************
*
* 函数名称:delay_ms(u16 nms)
* 函数功能:毫秒级延时
* 输入参数:输入值位延时ms
*
****************************************************/
void delay_ms(u16 nms)
{
    u32 temp;
    SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9000次
    SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值
    SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟
    
    do
    {
          temp=SysTick->CTRL;//标志位,等到一直减到0
         }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达
    
    SysTick->CTRL=0x00; //关闭计数器
    SysTick->VAL =0X00; //清空计数器
}



/****************************************************
*
* 重定义printf函数部分
*
****************************************************/
int fputc(int ch,FILE *F)
{
    
    USART_SendData(USART1,(u8)ch);
    
    while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位
    
    return ch;
}
View Code
技术分享
 1 /*pbdata.h*/
 2 /***************************************************************************************************
 3 *
 4 * 文件名称:pbdata.h
 5 * 文件功能:自定义的函数和全局变量的声明头文件
 6 *
 7 ***************************************************************************************************/
 8 
 9 #ifndef _pbdata_H
10 #define _pbdata_H
11 
12 
13 
14 
15 
16 /********************************************************************
17 *
18 *  调用的头文件放在这里
19 *
20 ********************************************************************/
21 #include "stm32f10x.h"
22 #include "misc.h"
23 #include "stm32f10x_exti.h"
24 #include "stm32f10x_tim.h"
25 #include "stm32f10x_usart.h"
26 #include "stdio.h"
27 
28 
29 
30 
31 /********************************************************************
32 *
33 *  自定义全局变量声明
34 *
35 ********************************************************************/
36 extern u8 dt;
37 
38 
39 
40 
41 
42 /********************************************************************
43 *
44 *  自定义全函数声明
45 *
46 ********************************************************************/
47 void delay(u32 nCount);
48 void delay_us(u32 nus);
49 void delay_ms(u16 nms);
50 int fputc(int ch,FILE *F);
51 
52 
53 
54 #endif
View Code

 

3、工程下载:

http://download.csdn.net/detail/a1181803348/8745749

 

8、printf

标签:

原文地址:http://www.cnblogs.com/STM32bo/p/4534336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!