STM32串口标准库实现发送字节、数组、字符串和数字,实现printf功能

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

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

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

下面是serial.c文件:

#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文件:

#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文件了:

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

作者:尚久龙

物联沃分享整理
物联沃-IOTWORD物联网 » STM32串口标准库实现发送字节、数组、字符串和数字,实现printf功能

发表评论