嵌入式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");
	}		
}

串口显示

相关推荐
yuehuaxin015 小时前
LP8728A/LP8728B 芯茂微|SOP8L 副边反馈 PWM,18‑30W 适配器恒功率补偿方案解析
嵌入式硬件·智能家居·适配器模式·智能硬件
万物智能信息科技5 小时前
RGB并行屏输出—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
单片机·嵌入式硬件·华为·鸿蒙
AI职业加油站5 小时前
2026大数据运维行业趋势复盘:大数据运维工程师赋能发展
大数据·运维·人工智能·学习·数据分析·职场发展
小黄人软件5 小时前
享受独处、终身学习
学习
新晨单片机设计5 小时前
S012A-基于STM32单片机光强检测系统【Proteus仿真+Keil程序+原理图】
stm32·单片机·嵌入式硬件
辰域电子5 小时前
STM32项目开源:电子秤计价系统(代码+原理图+仿真)
stm32·单片机·嵌入式硬件·开源·毕业设计·proteus
马东185 小时前
冲突后的解决状态
学习
ysu_03146 小时前
基于 STM32 与 TinyML 的端侧手势识别系统
stm32·单片机·嵌入式硬件·mcu
CCC:CarCrazeCurator6 小时前
LeRobot 深度技术方案解析:Hugging Face 端到端机器人学习框架
学习·机器人
Brilliantwxx6 小时前
【STM32 】把 printf 搬到串口上 —— C 标准 IO 函数重定向与多文件工程搭建(实战串口电灯)
c语言·开发语言·stm32·单片机·嵌入式硬件·架构·ecmascript