江科大STM32学习记录
在数据位后面可以加一位奇偶校验位,那么数据位就是九位
1:代表数据位(不包含校验位)偶数个1
0:代表数据位(不包含校验位)奇数个1
·· 串口时序
TX输出定时翻转的高低电平,RX定时读取引脚的高低电平,每个字节的数据加上起始位、停止位、可选的校验位,打包成数据帧,依次输出在TX引脚,另外一端RX引脚依次接收
TXE:发送寄存器空
RXNE:接收寄存器非空
USART基本结构
移位寄存器:低位先行
数据帧
数据帧
起始位侦测
数据采样
波特率发生器
#include "USART.h"
void Usart_Init(void)
{ //开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//初始化GPIO
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结构体
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
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_Cmd(USART1,ENABLE);
}
void Serial_SendByte(uint8_t data)//发送一个字节
{
USART_SendData(USART1,data);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);//硬件自动清零
}
void Serial_SendArray(uint8_t *array,uint16_t len)//发送数组
{
uint16_t i;
for(i=0;i<len;i++){
Serial_SendByte(array[i]);
}
}
\
void Serial_SendString(char *str)//发送字符串
{
while(*str != '\0'){
Serial_SendByte(*str);
str++;
}
}
uint32_t Serial_Pow(uint16_t X,uint16_t Y)
{
uint32_t Result = 1;
while(Y--){
Result *= X;
}
return Result;
}
void Serial_SendNumber(uint32_t Number,uint16_t len)//发送文本数字
{
uint8_t i;
for(i=0;i<len;i++){
Serial_SendByte((Number/Serial_Pow(10,len-i-1)%10) + '0');//提取每位转换为文本发送
}
}
关于使用printf,串口重定向的问题
1.下面要打开
2.重写fputc函数
#include
int fputc(int ch,FILE *f)//重定向到串口
{
Serial_SendByte(ch);
return ch;
}
封装sprintf
#include
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);
}
发送汉字的问题
添加下面文本
--no-multibyte-chars
查询法:
配置:
void Usart_Init(void)
{ //开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//初始化GPIO
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);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//配置USART结构体
USART_InitTypeDef USART_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_Cmd(USART1,ENABLE);
}
while(1){
//查询USART_FLAG_RXNE这个标志位,
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == SET){
Rx_data = USART_ReceiveData(USART1);//读取DR,查询USART_FLAG_RXNE自动清零
OLED_ShowHexNum(2,1,Rx_data,2);
}
}
中断方法:
uint8_t Rx_data;
uint8_t Rx_Flag;
void Usart_Init(void)
{ //开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//初始化GPIO
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);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//配置USART结构体
USART_InitTypeDef USART_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_InitTypeDef NVIC_InitStructure;
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);
}
uint8_t Serial_GetRxFlag(void)
{
if(Rx_Flag == 1){
Rx_Flag = 0;
return 1;
}else {
return 0;
}
}
uint8_t Serial_GetRxData(void)
{
return Rx_data;
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET){
Rx_data = USART_ReceiveData(USART1);
Rx_Flag = 1;
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
while(1){
if(Serial_GetRxFlag() == 1){
temp = Serial_GetRxData();
OLED_ShowHexNum(2,3,temp,2);
}
}
uint8_t Serial_GetRxFlag(void)
{
if(Rx_Flag == 1){
Rx_Flag = 0;
return 1;
}else {
return 0;
}
}
void Receive_HexPacket(void)
{
Rx_data = USART_ReceiveData(USART1);
switch(Rx_StateFlag){
case 0://等待包头
if(Rx_data == 0xFF){
Rx_StateFlag = 1;
}
break;
case 1://接收数据
Rx_Packet[Num] = Rx_data;
Num++;
if(Num >= 4){
Rx_StateFlag = 2;
Num = 0;
}
break;
case 2://等待包尾
if(Rx_data == 0xFE){
Rx_Flag = 1;
Rx_StateFlag = 0;
}
break;
}
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET){
Receive_HexPacket();
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
void Receive_HexPacket(void)
{
Rx_data = USART_ReceiveData(USART1);
switch(Rx_StateFlag){
case 0://等待包头
if(Rx_data == '@'){
Rx_StateFlag = 1;
Num = 0;
}
break;
case 1://接收数据
if(Rx_data != '\r'){
Rx_Packet[Num] = Rx_data;
Num++;
}else {
Rx_StateFlag = 2;
}
break;
case 2://等待包尾
if(Rx_data == '\n'){
Rx_Flag = 1;
Rx_StateFlag = 0;
Rx_Packet[Num] = '\0';
}
break;
}
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE) == SET){
Receive_HexPacket();
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}