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


    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    波特率:约定的传输速率,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){
    		     
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    效果:

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

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    完整代码:

    #include "stm32f10x.h"    // Device header
    #include "Delay.h"
    #include "OLED.h"
    #include 
    
    
    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){
    		     
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
  • 相关阅读:
    解决GD32F207串口可以接收但发送00的问题
    MySql5.1+版本主从同步配置(bin_log)模式
    学习ASP.NET Core Blazor编程系列六——初始化数据
    你应该知道的数仓安全:都是同名Schema惹的祸
    毫米波传感器原理介绍:测距
    微信小程序修改vant组件样式
    蓝牙 5.1 高精度定位 XaaS 方案发布,蓝牙定位产品快速增长
    Ansible最佳实践之委派任务和事实
    小程序源码:独立后台带分销功能月老办事处交友盲盒-多玩法安装简单
    AE特效解读
  • 原文地址:https://blog.csdn.net/xyint/article/details/133833237