基本信息
源码名称:串口通信---STM32F103数据帧接收与控制
源码大小:1.37M
文件格式:.rar
开发语言:C/C++
更新时间:2021-08-02
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 4 元×
微信扫码支付:4 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
:STM32F103通过串口来接收如GPRS等串口设备发送过来的数据,并进行解析
#include "stm32f10x.h"
#include "usart1.h"
#define BUF_MAX 32
void DataParse(void);
extern unsigned char uart1buff[BUF_MAX];
extern unsigned char FrameFlag;
extern unsigned char buf_address;
extern void UART1SendByte(char SendData);
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //通用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //高电平驱动,灭灯
SystemInit(); //配置系统时钟为 72M
USART1_Config();
Uart1SendStr("Uart1 Ok...\r\n");
Uart1SendStr("Demo of DataFrame!\r\n");
while (1)
{
DataParse();
}
}
void DataParse(void)//数据解析
{
unsigned char i;
if(FrameFlag==1)
{
FrameFlag=0;
for(i=0;i< buf_address;i ) UART1SendByte(uart1buff[i]);//接收到的数据帧按原样输出,主要用于调试用
Uart1SendStr("\r\n");
if((uart1buff[0]=='$') && (uart1buff[buf_address-1]=='*')) //是一个合法的数据帧
{
if ((uart1buff[1]=='1')&&(uart1buff[2]=='1')) //第1只LED灯亮
{
Uart1SendStr("Led1 is On\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
else if((uart1buff[1]=='2')&&(uart1buff[2]=='1')) //第2只LED灯亮
{
Uart1SendStr("Led2 is On\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_13);
}
else if((uart1buff[1]=='3')&&(uart1buff[2]=='1')) //第3只LED灯亮
{
Uart1SendStr("Led3 is On\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_14);
}
else if((uart1buff[1]=='4')&&(uart1buff[2]=='1')) //第4只LED灯亮
{
Uart1SendStr("Led4 is On\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_15);
}
else if((uart1buff[1]=='1')&&(uart1buff[2]=='0')) //第1只LED灯灭
{
Uart1SendStr("Led1 is Off\r\n");
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
else if((uart1buff[1]=='2')&&(uart1buff[2]=='0')) //第2只LED灯灭
{
Uart1SendStr("Led2 is Off\r\n");
GPIO_ResetBits(GPIOB, GPIO_Pin_13);
}
else if((uart1buff[1]=='3')&&(uart1buff[2]=='0')) //第3只LED灯灭
{
Uart1SendStr("Led3 is Off\r\n");
GPIO_ResetBits(GPIOB, GPIO_Pin_14);
}
else if((uart1buff[1]=='4')&&(uart1buff[2]=='0')) //第4只LED灯灭
{
Uart1SendStr("Led4 is Off\r\n");
GPIO_ResetBits(GPIOB, GPIO_Pin_15);
}
}
}
}
:STM32F103通过串口来接收如GPRS等串口设备发送过来的数据,并进行解析
#include "stm32f10x.h"
#include "usart1.h"
#define BUF_MAX 32
void DataParse(void);
extern unsigned char uart1buff[BUF_MAX];
extern unsigned char FrameFlag;
extern unsigned char buf_address;
extern void UART1SendByte(char SendData);
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //通用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //高电平驱动,灭灯
SystemInit(); //配置系统时钟为 72M
USART1_Config();
Uart1SendStr("Uart1 Ok...\r\n");
Uart1SendStr("Demo of DataFrame!\r\n");
while (1)
{
DataParse();
}
}
void DataParse(void)//数据解析
{
unsigned char i;
if(FrameFlag==1)
{
FrameFlag=0;
for(i=0;i< buf_address;i ) UART1SendByte(uart1buff[i]);//接收到的数据帧按原样输出,主要用于调试用
Uart1SendStr("\r\n");
if((uart1buff[0]=='$') && (uart1buff[buf_address-1]=='*')) //是一个合法的数据帧
{
if ((uart1buff[1]=='1')&&(uart1buff[2]=='1')) //第1只LED灯亮
{
Uart1SendStr("Led1 is On\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
else if((uart1buff[1]=='2')&&(uart1buff[2]=='1')) //第2只LED灯亮
{
Uart1SendStr("Led2 is On\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_13);
}
else if((uart1buff[1]=='3')&&(uart1buff[2]=='1')) //第3只LED灯亮
{
Uart1SendStr("Led3 is On\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_14);
}
else if((uart1buff[1]=='4')&&(uart1buff[2]=='1')) //第4只LED灯亮
{
Uart1SendStr("Led4 is On\r\n");
GPIO_SetBits(GPIOB, GPIO_Pin_15);
}
else if((uart1buff[1]=='1')&&(uart1buff[2]=='0')) //第1只LED灯灭
{
Uart1SendStr("Led1 is Off\r\n");
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
else if((uart1buff[1]=='2')&&(uart1buff[2]=='0')) //第2只LED灯灭
{
Uart1SendStr("Led2 is Off\r\n");
GPIO_ResetBits(GPIOB, GPIO_Pin_13);
}
else if((uart1buff[1]=='3')&&(uart1buff[2]=='0')) //第3只LED灯灭
{
Uart1SendStr("Led3 is Off\r\n");
GPIO_ResetBits(GPIOB, GPIO_Pin_14);
}
else if((uart1buff[1]=='4')&&(uart1buff[2]=='0')) //第4只LED灯灭
{
Uart1SendStr("Led4 is Off\r\n");
GPIO_ResetBits(GPIOB, GPIO_Pin_15);
}
}
}
}