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中,每次按下复位键,电脑的串口助手都能收到单片机发来的数据。

相关推荐
redcocal6 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
辰哥单片机设计10 小时前
门磁模块详解(防盗感应开关 STM32)
stm32·单片机·嵌入式硬件·传感器
夜间去看海10 小时前
基于51单片机的自动清洗系统(自动洗衣机)
嵌入式硬件·51单片机·proteus·洗衣机
yrx02030710 小时前
stm32 IIC总线busy解决方法
stm32·单片机·嵌入式硬件
YHPsophie12 小时前
ATGM331C-5T杭州中科微BDS/GNSS全星座定位授时模块应用领域
经验分享·笔记·单片机·信息与通信·交通物流
Archie_IT12 小时前
【STM32系统】基于STM32设计的SD卡数据读取与上位机显示系统(SDIO接口驱动、雷龙SD卡)——文末资料下载
arm开发·stm32·单片机·嵌入式硬件
辰哥单片机设计13 小时前
1×4矩阵键盘详解(STM32)
stm32·单片机·嵌入式硬件·矩阵·传感器
wmkswd13 小时前
CAN总线-STM32上CAN外设
stm32·单片机·嵌入式硬件
Ruohongxu13 小时前
LAN8720A-CP-TR-ABC QFN-24 以太网收发器芯片
单片机