波特率:约定的传输速率,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){
}
}