Stm32_标准库_12_串口_发送数据

文章详细描述了如何在STM32F10x开发板上使用USART1进行串口通信,包括初始化GPIO和USART、发送单个字节、数组、字符串以及数字的方法。展示了如何通过`Serial_Init`、`Serial_SendByte`等函数实现基本的数据传输功能。
摘要由CSDN通过智能技术生成

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
波特率:约定的传输速率,1000bps,1s发1000位

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
引脚

在这里插入图片描述
在这里插入图片描述
结构

在这里插入图片描述
数据帧的传输特点

在这里插入图片描述
在这里插入图片描述
代码:

#include "stm32f10x.h"    // Device header
#include "Delay.h"
#include "OLED.h"

GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruture;

void Serial_Init(void){
	   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//开启USART1的时钟
	   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA的时钟
	  
	   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
	   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;//TX
	   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	   GPIO_Init(GPIOA, &GPIO_InitStruct);
     //初始化USART
	   USART_InitStruture.USART_BaudRate = 9600;//波特率
	   USART_InitStruture.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不使用
	   USART_InitStruture.USART_Mode = USART_Mode_Tx;//需要发送功能
	   USART_InitStruture.USART_Parity = USART_Parity_No;//不需要校验位
	   USART_InitStruture.USART_StopBits = USART_StopBits_1;//停止位1
	   USART_InitStruture.USART_WordLength = USART_WordLength_8b;//八位字长
	   USART_Init(USART1, &USART_InitStruture);
	
	   USART_Cmd(USART1, ENABLE);
}
 

void Serial_SendByte(uint8_t Byte){
	   USART_SendData(USART1, Byte);//传递数据至TDR
	   while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待TDR数据传送完
}
 
 
int main(void){
	Serial_Init();
	Serial_SendByte(0x41);
	while(1){
		     
	}
}

效果:

在这里插入图片描述
扩充函数

void Serial_SendByte(uint8_t Byte){
	   USART_SendData(USART1, Byte);//传递数据至TDR
	   while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待TDR数据传送完
}

uint32_t Serial_Pow(uint32_t x, uint32_t y){
	       uint32_t result = 1;
	       while(y --){
					    result = result * x;
				 }
				 return result;
}

void Serial_SendArray(uint8_t *Array, uint16_t length){//传输数组
	   uint16_t i = 0;
	   for(i = 0; i < length; i ++){
			   Serial_SendByte(Array[i]);
		 }
}

void Serial_SendString(char *String){//传输字符串
	   uint8_t i;
	   for(i = 0; String[i] != '\0'; i ++){
			   Serial_SendByte(String[i]); 
		 }
}

void Serial_SendNumber(uint32_t Number, uint8_t length){
	   uint8_t i;
	   for(i = 0; i < length; i ++){
			   Serial_SendByte(Number / Serial_Pow(10, length - 1 - i) %10 + '0');//拆分成字符发送
		 }
}
int fputc(int ch, FILE *f){
	   Serial_SendByte(ch); 
	   return ch;
}

完整代码:

#include "stm32f10x.h"    // Device header
#include "Delay.h"
#include "OLED.h"
#include <stdio.h>


GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruture;

void Serial_Init(void){
	   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//开启USART1的时钟
	   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA的时钟
	  
	   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
	   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;//TX
	   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	   GPIO_Init(GPIOA, &GPIO_InitStruct);
     //初始化USART
	   USART_InitStruture.USART_BaudRate = 9600;//波特率
	   USART_InitStruture.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//不使用
	   USART_InitStruture.USART_Mode = USART_Mode_Tx;//需要发送功能
	   USART_InitStruture.USART_Parity = USART_Parity_No;//不需要校验位
	   USART_InitStruture.USART_StopBits = USART_StopBits_1;//停止位1
	   USART_InitStruture.USART_WordLength = USART_WordLength_8b;//八位字长
	   USART_Init(USART1, &USART_InitStruture);
	
	   USART_Cmd(USART1, ENABLE);
}
 

void Serial_SendByte(uint8_t Byte){
	   USART_SendData(USART1, Byte);//传递数据至TDR
	   while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待TDR数据传送完
}

uint32_t Serial_Pow(uint32_t x, uint32_t y){
	       uint32_t result = 1;
	       while(y --){
					    result = result * x;
				 }
				 return result;
}

void Serial_SendArray(uint8_t *Array, uint16_t length){//传输数组
	   uint16_t i = 0;
	   for(i = 0; i < length; i ++){
			   Serial_SendByte(Array[i]);
		 }
}

void Serial_SendString(char *String){//传输字符串
	   uint8_t i;
	   for(i = 0; String[i] != '\0'; i ++){
			   Serial_SendByte(String[i]); 
		 }
}

void Serial_SendNumber(uint32_t Number, uint8_t length){
	   uint8_t i;
	   for(i = 0; i < length; i ++){
			   Serial_SendByte(Number / Serial_Pow(10, length - 1 - i) %10 + '0');//拆分成字符发送
		 }
}
int fputc(int ch, FILE *f){
	   Serial_SendByte(ch); 
	   return ch;
}
 
int main(void){
	uint8_t a[] = {0x42, 0x43, 0x44, 0x45};
	Serial_Init();
	Serial_SendByte(0x41);
	 
	Serial_SendArray(a, 4);
	Serial_SendString("hello");//程序自动补结束符
	Serial_SendNumber(123, 3);
	printf("NUm = %d\r\n", 666);
	while(1){
		     
	}
}

下面是使用STM32标准库进行串口发送数据的基本步骤: 1. 初始化串口:在程序中引用STM32标准库的头文件,然后配置串口的波特率、数据位、停止位、校验位等参数。 2. 发送数据:使用库函数向串口发送数据,可以使用printf()函数格式化数据,也可以使用send()函数直接发送数据。 示例代码: ```c #include "stm32f10x.h" #include <stdio.h> #define USART1_TX_PIN GPIO_Pin_9 #define USART1_RX_PIN GPIO_Pin_10 #define USART1_GPIO GPIOA #define USART1 USART1 void USART1_Init(void) { USART_InitTypeDef USART_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); //配置USART1的GPIO GPIO_InitStruct.GPIO_Pin = USART1_TX_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(USART1_GPIO, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = USART1_RX_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(USART1_GPIO, &GPIO_InitStruct); //配置USART1 USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Tx; USART_Init(USART1, &USART_InitStruct); USART_Cmd(USART1, ENABLE); } int main(void) { USART1_Init(); while (1) { char buffer[20]; sprintf(buffer, "Hello World!\r\n"); USART_SendData(USART1, buffer, strlen(buffer)); delay(1000); } } ``` 在上面的代码中,我们使用了USART_SendData()函数发送了一条“Hello World”的消息。可以看到,我们使用了sprintf()函数对数据进行了格式化,然后再将其发送串口中。我们还使用了一个简单的延时函数来控制发送的时间间隔。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值