一、任务目标
基于STM32F103ZET6单片机+蓝牙模块无线接收发送来的数据,通过485通信,使用中航ZH-E3L字符控制卡及其通信协议,使得蓝牙无线接收到数据显示在LED屏上。
二、使用模块
1、STM32F103ZET6的正点原子STM32精英开发板
2、串口通信的蓝牙模块
3、中航ZH-E3L字符控制卡
4、LED显示屏
三、实现思路
单片机搭载蓝牙模块接收另一端蓝牙无线发送过来的数据信息,单片机将蓝牙收到数据信息按照中航字符卡的485通信协议重新组包,然后通过485通信传输给中航字符控制卡,中航字符控制卡则通过HUB08接口将数据显示在与之相连的LED屏上。
四、中航字符卡通信协议解析
1、中航字符卡使用说明文档中协议说明如下:
由协议可知,单片机需要发送的数据格式整理如下:
void SetData1_toLED( )
{
tempbuff[0]=0x78;//包头
tempbuff[1]=0x34;//协议版本
tempbuff[2]=0x01;
tempbuff[3]=0x00;//字符分区 00 01
tempbuff[4]=0x29;//命令:更新
tempbuff[5]=0x12;//识别标志
tempbuff[6]=0xF2;//识别标志
tempbuff[7]=0x00;//识别标志
tempbuff[8]=0x00;//识别标志
tempbuff[9]=0x00;//帧计数
tempbuff[10]=0x00;//帧计数
tempbuff[11]=0x00;//帧计数
tempbuff[12]=0x00;//帧计数
}
void SetData2_toLED( )
{
tempbuff1[0]=0x01;//
tempbuff1[1]=0x00;//字符分区ID
tempbuff1[2]=0x01;//GB2312编码方式
tempbuff1[3]=0x02;//立即显示模式
tempbuff1[4]=0x00;//字符串索引
tempbuff1[5]=0x01;//颜色序号:红色
}
五、开发实现过程
1、建立cubemx工程
2、蓝牙通信使用串口1
3、485通信使用串口2
4、485发送使能引脚 PD7配置
六、程序代码片段
1、usart1.c
#include "usart1.h"
#include "usart.h"
/**********重定义函数**********/
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART2->SR&0X40)==0);//循环发送,直到发送完毕
USART2->DR = (uint8_t) ch;
return ch;
}
//串口1发送串口屏数据send_data
void UserUart2Send(uint8_t *send_data,uint8_t send_len)
{
while(send_len--)
{
HAL_UART_Transmit(&huart2,send_data++,1,20);
}
}
/*
78 34 01 00 29 BC FD 00 00 00 00 00 00 14 00
01 00 01 02 06 01 0C 00 CE D2 B0 AE D6 D0 BA
BD C8 ED BC FE 45 8C A5
*/
uint8_t tempbuff[13];//定义发送数据变量数组
uint8_t tempbuff1[6];//定义发送数据变量数组
uint8_t tempbuff2[33];//全部数据
uint8_t tempbuff3[10];
uint8_t tempbuff4[36];
//组成向LED屏发送数据的数组tempbuff
//void SetData_toLED(uint8_t highAdd,uint8_t lowAdd,uint8_t highData,uint8_t lowData)
//{
//// uint16_t tempbuff[8];
//// uint8_t t;
void SetData1_toLED( )
{
tempbuff[0]=0x78;//包头
tempbuff[1]=0x34;//协议版本
tempbuff[2]=0x01;
tempbuff[3]=0x00;//字符分区 00 01
tempbuff[4]=0x29;//命令:更新
tempbuff[5]=0x12;//识别标志
tempbuff[6]=0xF2;//识别标志
tempbuff[7]=0x00;//识别标志
tempbuff[8]=0x00;//识别标志
tempbuff[9]=0x00;//帧计数
tempbuff[10]=0x00;//帧计数
tempbuff[11]=0x00;//帧计数
tempbuff[12]=0x00;//帧计数
}
void SetData2_toLED( )
{
tempbuff1[0]=0x01;//
tempbuff1[1]=0x00;//字符分区ID
tempbuff1[2]=0x01;//GB2312编码方式
tempbuff1[3]=0x02;//立即显示模式
tempbuff1[4]=0x00;//字符串索引
tempbuff1[5]=0x01;//颜色序号:红色
}
void SetData3_toLED( )
{
tempbuff3[0]=blue_receive_buf[4];
tempbuff3[1]=blue_receive_buf[5];
tempbuff3[2]=blue_receive_buf[6];
tempbuff3[3]=blue_receive_buf[7];
tempbuff3[4]=blue_receive_buf[8];
tempbuff3[5]=blue_receive_buf[9];
tempbuff3[6]=blue_receive_buf[10];
tempbuff3[7]=blue_receive_buf[11];
tempbuff3[8]=blue_receive_buf[12];
tempbuff3[9]=blue_receive_buf[13];
}
2、main.c的while循环
if(blue_transmite_flag==1)//如果蓝牙收到命令数据
{
RS485_Ctr_TX;//控制485发送使能
//填充协议前一段固定字段
SetData1_toLED( );//填充通信协议中固定数据部分到tempbuff数组
//计算协议的数据长度
lenth1=blue_receive_buf[2]+8;
lenth1_high=lenth1%256;//低字节在前
lenth1_low=lenth1/256;//高字节在后
//填充协议的数据中的固定字段到tempbuff1数组
SetData2_toLED( );
//计算协议的数据中的实际数据的长度
lenth2=blue_receive_buf[2];
lenth2_high=lenth2%256;//低字节在前
lenth2_low=lenth2/256;//高字节在后
//填充协议的实际的数据内容到tempbuff3数组
SetData3_toLED( );
//填充协议的整包数据从开始到CRC前的数据的CRC16校验值到tempbuff2数组
SetDataAll1_toLED( );
checkcrc16=GetCRC(tempbuff2,33);
crc16_high=checkcrc16%256;//低字节在前
crc16_low=checkcrc16/256;//高字节在后
//填充整包通信协议数据到tempbuff4数组
SetDataAll2_toLED( );
UserUart2Send(tempbuff4,sizeof(tempbuff4));//通过485串口将整包数据发送给中航字符控制卡从而在LED屏显示数据内容
blue_transmite_flag=0;//蓝牙接收标志位置0
}
七、实物测试结果
八、程序工程源码下载链接