嵌入式STM32学习——串口USART 2.0(printf重定义及串口发送)

printf重定义:

C语言里面的printf函数默认输出设备是显示器,如果要实现printf函数输出正在串口或者LCD显示屏上,必须要重定义标准库函数里调用的与输出设备相关的函数,比如printf输出到串口,需要将fputc里面的输出指向串口。

应用:

usart.h

复制代码
#ifndef USART_H_
#define USART_H_

	void my_usart_init(void);
	void My_Usart_Send_Byte( USART_TypeDef* USARTx, uint16_t Data);
	void My_Usart_Send_Sting( USART_TypeDef* USARTx, char * string);

#endif

usart.c

复制代码
#include"stm32f10x.h"
#include"usart.h"
#include "stdio.h"

void my_usart_init(void)
{
	GPIO_InitTypeDef GPIOInitstruct;
	USART_InitTypeDef Usart_initstruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
	
	//A9
	GPIOInitstruct.GPIO_Pin = GPIO_Pin_9;
	GPIOInitstruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIOInitstruct.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(GPIOA,&GPIOInitstruct);
	//A10
	GPIOInitstruct.GPIO_Pin =GPIO_Pin_10;
	GPIOInitstruct.GPIO_Mode =GPIO_Mode_IPU;
	GPIO_Init(GPIOA,&GPIOInitstruct);
	
	
	Usart_initstruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;									
	Usart_initstruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;		
	Usart_initstruct.USART_BaudRate = 115200;						
	Usart_initstruct.USART_Parity = USART_Parity_No;		
	Usart_initstruct.USART_StopBits = USART_StopBits_1;  		
	Usart_initstruct.USART_WordLength = USART_WordLength_8b;  		
	USART_Init(USART1,&Usart_initstruct);
	USART_Cmd(USART1,ENABLE);																											
	
}

void My_Usart_Send_Byte(USART_TypeDef* USARTx, uint16_t Data)
{
			USART_SendData(USARTx,  Data);
			while( USART_GetFlagStatus( USARTx,  USART_FLAG_TXE) == RESET);
		
}
	
void My_Usart_Send_Sting( USART_TypeDef* USARTx, char * string)
{
		uint16_t i=0;
		do
		{
				My_Usart_Send_Byte(USARTx,*(string+i));
				i++;
		}while(*(string+i) != '\0');
		
		while( USART_GetFlagStatus( USARTx,  USART_FLAG_TC) == RESET);
}


int fputc(int ch, FILE * p)									//printf重定义
{
		USART_SendData(USART1,  (u8)ch);
		while( USART_GetFlagStatus( USART1,  USART_FLAG_TXE) == RESET);
		return ch;
}

main.c

复制代码
#include "stm32f10x.h"
#include "main.h"
#include "led.h"
#include "bear.h"
#include "key.h"
#include "shake.h"
#include "usart.h"
#include "stdio.h"
void delay(uint16_t time) 
{
	uint16_t i=0;
	while(time--)
	{
		i =12000;
		while(i--);
	}

}
 
int  main() 
{

		my_usart_init();
//	My_Usart_Send_Byte( USART1,  'A');
//	My_Usart_Send_Byte( USART1,  'B');
//	My_Usart_Send_Byte( USART1,  'C');
//	My_Usart_Send_Sting( USART1,  "\r\n");
//	My_Usart_Send_Sting(USART1 , "fei \r\n");
	
	while(1)
	{
			printf("Hello,shi \r\n");
	}		
}

串口显示

相关推荐
励志不掉头发的内向程序员14 分钟前
【Linux系列】掌控 Linux 的脉搏:深入理解进程控制
linux·运维·服务器·开发语言·学习
三佛科技-1341638421220 分钟前
智能美甲灯方案,UV/LED美甲光疗机美甲烤灯MCU控制方案开发设计
单片机·嵌入式硬件·智能家居·pcb工艺
listhi52035 分钟前
基于STM32F407与FT245R芯片实现USB转并口通信时序控制
stm32·单片机·嵌入式硬件
光影少年1 小时前
云计算生态及学习方向和就业领域方向
学习·云计算
好奇龙猫1 小时前
[AI学习:SPIN -win-安装SPIN-工具过程 SPIN win 电脑安装=accoda 环境-第四篇:代码修复]
人工智能·学习
luckyPian2 小时前
学习go语言
开发语言·学习·golang
chenzhou__2 小时前
MYSQL学习笔记(个人)(第十五天)
linux·数据库·笔记·学习·mysql
JJJJ_iii5 小时前
【机器学习01】监督学习、无监督学习、线性回归、代价函数
人工智能·笔记·python·学习·机器学习·jupyter·线性回归
Han.miracle6 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
知识分享小能手7 小时前
uni-app 入门学习教程,从入门到精通,uni-app基础扩展 —— 详细知识点与案例(3)
vue.js·学习·ui·微信小程序·小程序·uni-app·编程