博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STM32 keil printf的使用
阅读量:4356 次
发布时间:2019-06-07

本文共 1775 字,大约阅读时间需要 5 分钟。

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,
          "**%");

之类的输出函数

 

转载于:https://www.cnblogs.com/BMYC/p/10768591.html

你可能感兴趣的文章
LeetCode--二分查找相关算法
查看>>
RobotFramework自动化测试框架系统关键字之断言
查看>>
《Node.js In Action》笔记之流程控制
查看>>
通俗易懂云计算
查看>>
zigbee首次开通
查看>>
Hibernate查询-hql
查看>>
QT Creator 快速入门教程 读书笔记(三)
查看>>
bzoj 3811: 玛里苟斯【线性基+期望dp】
查看>>
Android的“再按一次返回键退出”的实现
查看>>
题目1014:排名
查看>>
团队开发1
查看>>
返回顶部
查看>>
Ext DateField控件 - 只选择年月
查看>>
Codeforces Round #342 (Div. 2) A. Guest From the Past(贪心)
查看>>
.Net基础
查看>>
Vue.js
查看>>
L2-001 紧急救援 [Dijkstra]
查看>>
【洛谷4219】[BJOI2014]大融合(线段树分治)
查看>>
深度学习面试题19:1*1卷积核的作用
查看>>
OData services入门----使用ASP.NET Web API描述
查看>>