STM32的USART外设:
自带波特率发生器,最高可达4.5Mbits/s
可配置数据位长度(8/9),停止位长度(0.5/1/1.5/2)
可选校验位(无校验/奇校验/偶校验)
STM32F103C8T6的USART资源:USART1(APB2总线),USAR2(APB1总线),USART3(APB1总线)。
硬件部分:
执行过程:
波特率发生器产生约定的通信速率,产生的时钟通向发送控制器和接收控制器,用来控制发送移位和接收移位。之后由发送数据寄存器和发送移位寄存器配合,移出数据(右移,低位先行),通过GPIO复用输出,输出到TX引脚。RX引脚通过GPIO复用输入,移动到接收寄存器(右移,低位先行),然后转移到RDR,同时置一个RXNE标志位(既可检查是否收到,也可申请中断)。
数据采样:
以波特率的16倍频率采样,在某个位置,突然采集到0,说明出现了下降沿,然后进行连续16位采样,在下降沿的第3,5,7次进行一批采样,8,9,10次进行一批采样,这两批,,每3位里都要有至少2个0。若有一个1,会置一个NE,即噪声标志位。若通过检测,之后每次都在8,9,10位进行采样(正好在正中心采样)。
波特率发生器(寄存器):
波特率由波特率寄存器BRR里的DIV来确定。
波特率 =(一般为72M) /(16*DIV)
举例:若要配置波特率为9600,9600 = 72M/(16*DIV) 即 DIV = 72M/9600/16=468.75,然后转换成二进制,468.75 = 11101 0100.11
所以写入BRR寄存器,DIV15~0:0000 11101 0100 1100 (整数部分前面补0,小数部分后面补0)
串口发送+接收:
接线:
USB转串口模块:VCC和3V3用跳线帽接在一起,TXD接PA10,RXD接PA9。、
(因为USART1_TX是PA9,USART1_RX是PA10,所以TX接RX,RX接TX,交叉连接)
串口发送:
一些SendByte的拓展函数:
串口接收+发送:
#include "Device/Include/stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
uint8_t Serial_RxData;
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;//发送+接收
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
//使能或者失能指定的 USART 中断,USART_IT_TXE:发送中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//该参数设置优先级分组位长度
//NVIC_PriorityGroup_2:先占优先级 2 位,从优先级 2 位
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
//该参数用以使能或者失能指定的 IRQ 通道
//USART1_IRQChannel USART1 全局中断
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//该参数指定了在成员 NVIC_IRQChannel 中定义的 IRQ 通道被使能还是失能。
//这个参数取值为 ENABLE 或者 DISABLE。
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
//该参数设置了成员 NVIC_IRQChannel 中的先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
//该参数设置了成员 NVIC_IRQChannel 中的从优先级
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1,ENABLE);
//使能或者失能 USART 外设
}
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 Length)//串口1发送数组,(首地址,长度)
{
uint16_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *String)//串口1发送字符串(地址)
{
uint8_t i;
for(i=0;String[i] !='\0';i++)
{
Serial_SendByte(String[i]);
}
}
uint32_t Serial_Pow(uint32_t x,uint32_t y)//转化次方 (x的y次方)
{
uint32_t Result = 1;
while(y--)
{
Result *= x;
}
return Result;
}
void Serial_SendNumber(uint32_t Number,uint8_t Length)//串口1发送数字(数字,长度)
{
uint8_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte(Number / Serial_Pow(10,Length - i - 1) %10 + 0x30);
}
}
int fputc(int ch,FILE *f)
{
Serial_SendByte(ch);
return ch;
}
void Serial_Printf(char *format,...)
{
char String[100];
va_list arg;
va_start(arg,format);
vsprintf(String,format,arg);
va_end(arg);
Serial_SendString(String);
}
uint8_t Serial_GetRxFlag(void)//清除引出标志位
{
if(Serial_RxFlag == 1)
{
Serial_RxFlag = 0;
return 1;
}
return 0;
}
uint8_t Serial_GetRxData(void)//引出收到的数据
{
return Serial_RxData;
}
void USART1_IRQHandler(void)
{
if(USART_GetFlagStatus(USART1,USART_IT_RXNE) == SET)//判断RXNE标志位是否置1
{
Serial_RxData = USART_ReceiveData(USART1);
//引出收到的数据
Serial_RxFlag = 1;
//引出完成标志位
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
//清除RXNE标志位
}
}
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
uint8_t RxData;
int main(void)
{
OLED_Init(); //OLED初始化
OLED_ShowString(1,1,"RxData:");
Serial_Init();//串口初始化
while(1)
{
if(Serial_GetRxFlag() == 1)
{
RxData = Serial_GetRxData();
Serial_SendByte(RxData);
OLED_ShowHexNum(1,8,RxData,2);
}
}
}
串口收发数据包:
HEX数据包:
如果载荷容易与包头包尾重复,尽量选择固定包长。如果载荷不会和包头包尾重复,可选择可变包长。(包头包尾都是可以人为规定的)
文本数据包:
文本数据包存在大量字符可以作为包头包尾,有效避免载荷与包头包尾重复。
发送数据包:
很简单,可参考“串口发送”,直接调用封装的函数即可。
接收数据包:
添加一个状态机。定义三个状态,状态变量为S,三个状态分别为S=0,S=1,S=2。(类似于置标志位)
文本数据包的接收也是使用状态机:
代码实现(HEX数据包):
Serial.c:
#include "Device/Include/stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
uint8_t Serial_TxPacket[4];//发送的数据载荷(4个字节)
uint8_t Serial_RxPacket[4];//接收的数据载荷(4个字节)
uint8_t Serial_RxFlag;//如果收到一个数据包,就置RxFlag标志位
void Serial_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
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 Length)//串口1发送数组,(首地址,长度)
{
uint16_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *String)//串口1发送字符串(地址)
{
uint8_t i;
for(i=0;String[i] !='\0';i++)
{
Serial_SendByte(String[i]);
}
}
uint32_t Serial_Pow(uint32_t x,uint32_t y)//转化次方 (x的y次方)
{
uint32_t Result = 1;
while(y--)
{
Result *= x;
}
return Result;
}
void Serial_SendNumber(uint32_t Number,uint8_t Length)//串口1发送数字(数字,长度)
{
uint8_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte(Number / Serial_Pow(10,Length - i - 1) %10 + 0x30);
}
}
int fputc(int ch,FILE *f)
{
Serial_SendByte(ch);
return ch;
}
void Serial_Printf(char *format,...)
{
char String[100];
va_list arg;
va_start(arg,format);
vsprintf(String,format,arg);
va_end(arg);
Serial_SendString(String);
}
void Serial_SendPacket(void)//发送数据包
{
Serial_SendByte(0xff);
Serial_SendArray(Serial_TxPacket,4);
Serial_SendByte(0xfe);
}
uint8_t Serial_GetRxFlag(void)
{
if(Serial_RxFlag == 1)
{
Serial_RxFlag = 0;
return 1;
}
return 0;
}
void USART1_IRQHandler(void)
{
static uint8_t RxState = 0;//状态机变量(S)
static uint8_t pRxPacket = 0;
if(USART_GetFlagStatus(USART1,USART_IT_RXNE) == SET)
{
uint8_t RxData = USART_ReceiveData(USART1);
if(RxState == 0)
{
if(RxData == 0xff)//等待包头
{
RxState = 1;
pRxPacket = 0;
}
else
{
RxState = 0;
}
}
else if(RxState == 1)//等待接收数据
{
Serial_RxPacket[pRxPacket] =RxData;
pRxPacket ++;
if(pRxPacket >= 4)
{
RxState = 2;
}
}
else if(RxState == 2)//等待包尾
{
if(RxData == 0xfe)
{
RxState = 0;
Serial_RxFlag = 1;
}
else;
}
}
}
Serial.h:
#ifndef __SERIAL_H
#define __SERIAL_H
#include <stdio.h>
extern uint8_t Serial_TxPacket[];//发送缓存区数组
extern uint8_t Serial_RxPacket[];//接收缓存区数数组
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);
void Serial_Printf(char *format,...);
uint8_t Serial_GetRxFlag(void);
void USART1_IRQHandler(void);
void Serial_SendPacket(void);
#endif
main.c:
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "KEY.h"
uint8_t KeyNum;
int main(void)
{
OLED_Init(); //OLED初始化
Key1_Init();
Serial_Init();//串口初始化
OLED_ShowString(1,1,"TxPacket:");
OLED_ShowString(3,1,"RxPacket:");
Serial_TxPacket[0] = 0x01;
Serial_TxPacket[1] = 0x02;
Serial_TxPacket[2] = 0x03;
Serial_TxPacket[3] = 0x04;
while(1)
{
if (Serial_GetRxFlag() == 1)
{
OLED_ShowHexNum(1,1,Serial_RxPacket[0],2);
OLED_ShowHexNum(1,4,Serial_RxPacket[1],2);
OLED_ShowHexNum(1,7,Serial_RxPacket[2],2);
OLED_ShowHexNum(1,10,Serial_RxPacket[3],2);
}
}
}
代码实现(文本数据包):
Serial.c:
#include "Device/Include/stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
char Serial_RxPacket[100];
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
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 Length)//串口1发送数组,(首地址,长度)
{
uint16_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *String)//串口1发送字符串(地址)
{
uint8_t i;
for(i=0;String[i] !='\0';i++)
{
Serial_SendByte(String[i]);
}
}
uint32_t Serial_Pow(uint32_t x,uint32_t y)//转化次方 (x的y次方)
{
uint32_t Result = 1;
while(y--)
{
Result *= x;
}
return Result;
}
void Serial_SendNumber(uint32_t Number,uint8_t Length)//串口1发送数字(数字,长度)
{
uint8_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte(Number / Serial_Pow(10,Length - i - 1) %10 + 0x30);
}
}
int fputc(int ch,FILE *f)
{
Serial_SendByte(ch);
return ch;
}
void Serial_Printf(char *format,...)
{
char String[100];
va_list arg;
va_start(arg,format);
vsprintf(String,format,arg);
va_end(arg);
Serial_SendString(String);
}
void USART1_IRQHandler(void)
{
static uint8_t RxState = 0;
static uint8_t pRxPacket = 0;
if(USART_GetFlagStatus(USART1,USART_IT_RXNE) == SET)
{
uint8_t RxData = USART_ReceiveData(USART1);
if(RxState == 0)
{
if(RxData == '@' && Serial_RxFlag == 0)
{
RxState = 1;
pRxPacket = 0;
}
else
{
RxState = 0;
}
}
else if(RxState == 1)
{
if(RxData == '\r')
{
RxState = 2;
}
else
{
Serial_RxPacket[pRxPacket] =RxData;
pRxPacket ++;
}
}
else if(RxState == 2)
{
if(RxData == '\n')
{
RxState = 0;
Serial_RxPacket[pRxPacket] = '\0';
Serial_RxFlag = 1;
}
else;
}
}
}
Serial.h:
#ifndef __SERIAL_H
#define __SERIAL_H
#include <stdio.h>
extern char Serial_RxPacket[];
extern uint8_t Serial_RxFlag;
void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array,uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number,uint8_t Length);
void Serial_Printf(char *format,...);
void USART1_IRQHandler(void);
#endif
main.c:
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "LED.h"
#include <string.h>
int main(void)
{
OLED_Init(); //OLED初始化
LED_Init();
Serial_Init();//串口初始化
OLED_ShowString(1,1,"TxPacket:");
OLED_ShowString(3,1,"RxPacket:");
while(1)
{
if(Serial_RxFlag == 1)
{
OLED_ShowString(4,1," ");
OLED_ShowString(4,1,Serial_RxPacket);
if(strcmp(Serial_RxPacket,"led_on") == 0)
{
LED_G_ON();
Serial_SendString("led_on_ok\r\n");
OLED_ShowString(2,1," ");
OLED_ShowString(2,1,"led_on_ok");
}
else if(strcmp(Serial_RxPacket,"led_off") == 0)
{
LED_G_OFF();
Serial_SendString("led_off_ok\r\n");
OLED_ShowString(2,1," ");
OLED_ShowString(2,1,"led_off_ok");
}
else
{
Serial_SendString("ERROR_COMMAND\r\n");
OLED_ShowString(2,1," ");
OLED_ShowString(2,1,"ERROR_COMMAND");
}
Serial_RxFlag = 0;
}
}
}