STM32:串口发送

简介: STM32:串口发送
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
int main(void)
{
    OLED_Init();
    //注:以下各代码分别代表一个自定义函数的实现,只能同时实现一次,否则会有警告
    //Serial_Init(0x41);
    //Serial_SendByte('A');//发送0x41给电脑等其他串口通信接收端
    //uint16_t MyArray[]={0x41,0x42,0x43,0x44};
    //Serial_SendArray(MyArray,4);//将MyArray的值传递给Serial_SendArray
    //Serial_SendString("Hello Word");//自定义传递字符串
    Serial_SendNumber(12345,5);//自定义发送一个数字,在电脑上显示字符串形式的数字
    while(1)
    {
    }
}

2.serial.c代码:

#include "stm32f10x.h"                  // Device header
//1.开启时钟USART和GPIO
//2.GPIO初始化,配置TX输出,RX输入
//3.配置USART,结构体,
//4.开启USART即为发送功能
void Serial_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    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);
    USART_Cmd(USART1, ENABLE);
}
//自定义传递数据
void Serial_SendByte(uint8_t Byte)
{
    USART_SendData(USART1,Byte);
    while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
//自定义传递数组
void Serial_SendArray(uint8_t *Array,uint16_t Lenth)//数组名+长度
{
    uint16_t i;
    for(i=0;i<Lenth;i++)
    {
        USART_SendData(USART1,Array[i]);
    }
}
//自定义传递字符串
void Serial_SendString(char*String)
{
    uint16_t i;
    for(i=0;String[i]!='\0';i++)
    {
        USART_SendData(USART1,String[i]);
    }
}
//求x的y次方的函数
uint32_t Serial_Pow(uint32_t x,uint32_t y)
{
    uint32_t Result;
    while(y--)
    {
        Result*=x;
    }
    return Result;
}
//自定义发送一个数字,在电脑上显示字符串形式的数字
void Serial_SendNumber(uint32_t Number,uint8_t Lenth)
{
    uint16_t i;
    for(i=0;i<Lenth;i++)
    {
        USART_SendData(USART1,Number/Serial_Pow(10,Lenth-i-1)%10+'0');
    }
}
//void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);//发送数据,写DR寄存器
//uint16_t USART_ReceiveData(USART_TypeDef* USARTx);//接收数据,读DR寄存器


3.serial.h代码部分:


#ifndef  __SERIAL_H
#define  __SERIAL_H
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Lenth);
void Serial_SendString(char*String);
uint32_t Serial_Pow(uint32_t x,uint32_t y);
void Serial_SendNumber(uint32_t Number,uint8_t Lenth);
#endif


相关文章
|
11月前
STM32串口编程基础知识讲解
STM32串口编程基础知识讲解
82 0
|
2月前
stm32f407探索者开发板(十七)——串口寄存器库函数配置方法
stm32f407探索者开发板(十七)——串口寄存器库函数配置方法
344 0
|
2月前
STM32CubeMX 串口收发一帧数据
STM32CubeMX 串口收发一帧数据
43 9
|
2月前
|
芯片
STM32CubeMX 串口数据收发
STM32CubeMX 串口数据收发
34 2
|
2月前
|
监控
stm32f407探索者开发板(十八)——串口通信实验讲解(USART_RX_STA流程图详解)
stm32f407探索者开发板(十八)——串口通信实验讲解(USART_RX_STA流程图详解)
|
4月前
|
存储 缓存 芯片
STM32--USART串口
STM32--USART串口
|
4月前
|
Java C语言
STM32使用printf重定向到USART(串口)并打印数据到串口助手
STM32使用printf重定向到USART(串口)并打印数据到串口助手
196 0
STM32(HAL库)驱动GY30光照传感器通过串口进行打印
STM32(HAL库)驱动GY30光照传感器通过串口进行打印