STM32利用串口标准库发送字节,发送数组,发送字符串,发送数字,实现printf功能。

早晨到现在刚刚完成的功能:发送字节,发送数组,发送字符串,发送数字,实现printf功能。

当然这是建立在昨天学习使用串口发送数据的基础上,新建立的功能函数,咱们先来看看这次实验的结果吧:

都是通过串口发送到电脑端的数据,废话不多说了,自己看上篇文章再看这篇,就差不多理解了,不理解就从头开始看吧,每节的代码敲10遍就理解了,我这都敲了5遍了才勉强记住,我们不是神,我们只是一个熟练工,塌下心来不停地敲代码,总有一天会明白的,坚持就是胜利。

下面是serial.c文件:

cs 复制代码
#include "stm32f10x.h"                  // Device header
#include <stdio.h>

void Serial_Init(void)
{
	//1:RCC
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	
	//2:GPIO_Init
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	//3:USART_Init
	USART_InitTypeDef USART_InitStruct;
	USART_InitStruct.USART_BaudRate = 9600;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStruct.USART_Mode = USART_Mode_Tx;
	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);
	
	//4:USART_Cmd
	USART_Cmd(USART1, ENABLE);
	
}

//发送一个字节
void Send_Byte(uint8_t Dat)
{
	USART_SendData(USART1, Dat);
	while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

//发送一个数组
void Send_array(uint8_t *arr, uint8_t len)
{
		uint8_t i=0;
	for(i=0; i<len; i++)
	{
		Send_Byte(arr[i]);
	}
}

//发送一个字符串
void Send_Strint(char *str)
{
	while(*str != 0)   //只要解引用str不是0
	{
		Send_Byte(*str);  //发送解引用str
		str++;             //地址加加
	}
}
//求一个数的几次方
uint32_t CiFang(uint32_t dat, uint8_t num)  //比如要求2的3次方  
{
	uint32_t result =1;
	while(num)    //  num=3   每次减1  运行3次条件不满足
	{
		result *=dat;  // 1: 1*2=2  2:2*2=4  3:4*2=8 
		num--;        // 1:num=2 2:num=1  3:num=0
	}
	return result;   //返回结果8
		
}


//发送一个数字(参数1:要发送的数字, 参数2:这个数字的位数)
void Send_Number(uint32_t Number, uint8_t len)  //123456  6
{
	uint8_t i=0;
	for(i=0; i<len; i++)     // i=0
	{
		Send_Byte(Number/CiFang(10,(len-i-1)) % 10 + '0');    //
	}
}

//从写printf函数
int fputc(int ch, FILE *f)
{
	Send_Byte(ch);			//将printf的底层重定向到自己的发送字节函数
	return ch;
}

下面是serial.h文件:

cs 复制代码
#ifndef __SERIAL_H
#define __SERIAL_H
#include<stdio.h>

void Serial_Init(void);
void Send_Byte(uint8_t Dat);
void Send_array(uint8_t *arr, uint8_t len);

void Send_Strint(char *str);

void Send_Number(uint32_t Number, uint8_t len);
#endif

下面就是主函数main.c文件了:

cs 复制代码
#include "stm32f10x.h"                  // Device header
#include "OLED.h"
#include "Serial.h"

int main(void)
{
	OLED_Init();       //oled  屏幕初始化
	Serial_Init();     //串口初始化
	
	Send_Byte(0x41);   //发送一个字节
	Send_Byte('\n');   //发送一个回车
	
	uint8_t arr[]={0x61,0x62,0x63,0x64};
	Send_array(arr, sizeof(arr));  //发送一个数组
	Send_Byte('\n');  //发送一个回车
	
	Send_Strint("ABCDEFG");  //发送字符串
	
	Send_Byte('\n');  //发送一个回车
	Send_Number(123654987, 9);  //发送一串数字
	
	Send_Byte('\n');  //发送一个回车
	printf("Num=%d", 123456);
	
	while(1)
	{

	}
}

就这样编译后下载到STM32中,每次按下复位键,电脑的串口助手都能收到单片机发来的数据。

相关推荐
来自晴朗的明天1 分钟前
16、电压跟随器(缓冲器)电路
单片机·嵌入式硬件·硬件工程
钰珠AIOT16 分钟前
在同一块电路板上同时存在 0805 0603 不同的封装有什么利弊?
嵌入式硬件
代码游侠17 分钟前
复习——Linux设备驱动开发笔记
linux·arm开发·驱动开发·笔记·嵌入式硬件·架构
代码游侠11 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
xuxg200513 小时前
4G 模组 AT 命令解析框架课程正式发布
stm32·嵌入式·at命令解析框架
CODECOLLECT15 小时前
京元 I62D Windows PDA 技术拆解:Windows 10 IoT 兼容 + 硬解码模块,如何降低工业软件迁移成本?
stm32·单片机·嵌入式硬件
BackCatK Chen15 小时前
STM32+FreeRTOS:嵌入式开发的黄金搭档,未来十年就靠它了!
stm32·单片机·嵌入式硬件·freertos·低功耗·rtdbs·工业控制
全栈游侠18 小时前
STM32F103XX 02-电源与备份寄存器
stm32·单片机·嵌入式硬件
Lsir10110_18 小时前
【Linux】中断 —— 操作系统的运行基石
linux·运维·嵌入式硬件
深圳市九鼎创展科技20 小时前
瑞芯微 RK3399 开发板 X3399 评测:高性能 ARM 平台的多面手
linux·arm开发·人工智能·单片机·嵌入式硬件·边缘计算