14_USART串口数据包
USART串口数据包
HEX数据包
文本数据包
HEX数据包接收
文本数据包接收
串口收发HEX数据包
接线图
代码
Serial.c
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
uint8_t Serial_TxPacket[4];
uint8_t Serial_RxPacket[4];
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
//开启USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//开启GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//初始化GPIO引脚
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; //上拉输入模式
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//初始化USART
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate=9600; //波特率
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //硬件流控制
USART_InitStruct.USART_Mode=USART_Mode_Tx | USART_Mode_Rx; //发送模式
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_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(USART1,ENABLE);
}
uint8_t Serial_GetRxFlag(void)
{
if(Serial_RxFlag)
{
Serial_RxFlag=0;
return 1;
}
else
{
return 0;
}
}
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)
{
uint16_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *String)
{
uint16_t i;
for(i=0;String[i];i++)
{
Serial_SendByte(String[i]);
}
}
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);
}
void USART1_IRQHandler(void)
{
static uint8_t RxState=0;
static uint8_t index=0;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
uint8_t RxData=USART_ReceiveData(USART1);
switch(RxState)
{
case 0:
if(RxData==0xFF)
{
RxState=1;
}
break;
case 1:
Serial_RxPacket[index++]=RxData;
if(index==4)
{
index=0;
RxState=2;
}
break;
case 2:
if(RxData==0xFE)
{
RxState=0;
Serial_RxFlag=1;
}
break;
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
Serial.h
#ifndef __SERIAL_H__
#define __SERIAL_H__
#include <stdio.h>
extern uint8_t Serial_TxPacket[4];
extern uint8_t Serial_RxPacket[4];
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_Printf(char *format, ...);
uint8_t Serial_GetRxFlag(void);
void Serial_SendPacket(void);
#endif
Key.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
void Key_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_11;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
}
uint8_t Key_GetNum(void)
{
uint8_t KeyNum = 0;
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
{
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);
Delay_ms(20);
KeyNum = 1;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
{
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0);
Delay_ms(20);
KeyNum = 2;
}
return KeyNum;
}
Key.h
#ifndef __KEY_H__
#define __KEY_H__
void Key_Init(void);
uint8_t Key_GetNum(void);
#endif
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Key.h"
uint8_t KeyNum;
int main(void)
{
OLED_Init();
Serial_Init();
Key_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)
{
KeyNum=Key_GetNum();
if(KeyNum==1)
{
Serial_TxPacket[0]++;
Serial_TxPacket[1]++;
Serial_TxPacket[2]++;
Serial_TxPacket[3]++;
Serial_SendPacket();
OLED_ShowHexNum(2,1,Serial_TxPacket[0],2);
OLED_ShowHexNum(2,4,Serial_TxPacket[1],2);
OLED_ShowHexNum(2,7,Serial_TxPacket[2],2);
OLED_ShowHexNum(2,10,Serial_TxPacket[3],2);
}
if(Serial_GetRxFlag())
{
OLED_ShowHexNum(4,1,Serial_RxPacket[0],2);
OLED_ShowHexNum(4,4,Serial_RxPacket[1],2);
OLED_ShowHexNum(4,7,Serial_RxPacket[2],2);
OLED_ShowHexNum(4,10,Serial_RxPacket[3],2);
}
}
}
串口收发文本数据包
接线图
代码
Serial.c
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
char Serial_RxPacket[100];
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
//开启USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//开启GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//初始化GPIO引脚
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; //上拉输入模式
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//初始化USART
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate=9600; //波特率
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //硬件流控制
USART_InitStruct.USART_Mode=USART_Mode_Tx | USART_Mode_Rx; //发送模式
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_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_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 Length)
{
uint16_t i;
for(i=0;i<Length;i++)
{
Serial_SendByte(Array[i]);
}
}
void Serial_SendString(char *String)
{
uint16_t i;
for(i=0;String[i];i++)
{
Serial_SendByte(String[i]);
}
}
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 index=0;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
uint8_t RxData=USART_ReceiveData(USART1);
switch(RxState)
{
case 0:
if(RxData=='@' && Serial_RxFlag==0)
{
RxState=1;
}
break;
case 1:
if(RxData=='\r')
{
RxState=2;
}
else
{
Serial_RxPacket[index++]=RxData;
}
break;
case 2:
if(RxData=='\n')
{
Serial_RxPacket[index++]='\0';
index=0;
RxState=0;
Serial_RxFlag=1;
}
break;
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
Serial.h
#ifndef __SERIAL_H__
#define __SERIAL_H__
#include <stdio.h>
extern char Serial_RxPacket[4];
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_Printf(char *format, ...);
uint8_t Serial_GetRxFlag(void);
#endif
LED.c
#include "stm32f10x.h" // Device header
void LED_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}
void LED1_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
void LED1_OFF(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
void LED1_Turn(void)
{
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
}
void LED2_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
void LED2_OFF(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
void LED2_Turn(void)
{
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
}
LED.h
#ifndef __LED_H__
#define __LED_H__
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED1_Turn(void);
void LED2_Turn(void);
#endif
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "LED.h"
#include <string.h>
uint8_t KeyNum;
int main(void)
{
OLED_Init();
Serial_Init();
LED_Init();
OLED_ShowString(1,1,"TxPacket");
OLED_ShowString(3,1,"RxPacket");
while(1)
{
if(Serial_RxFlag)
{
OLED_ShowString(4,1," ");
OLED_ShowString(4,1,Serial_RxPacket);
if(strcmp(Serial_RxPacket,"LED_ON")==0)
{
LED1_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)
{
LED1_OFF();
Serial_SendString("LED_OFF_OK!\r\n");
OLED_ShowString(2,1," ");
OLED_ShowString(2,1,"LED_OFF_OK!");
}
else
{
Serial_SendString("COMMANT_ERROR!\r\n");
OLED_ShowString(2,1," ");
OLED_ShowString(2,1,"COMMANT_ERROR!");
}
Serial_RxFlag=0;
}
}
}