Gitee
https://gitee.com/WangXi_Chn/PcComTestApp
应用版本V1.0
应用功能
- 作为应用程序编写范例,可作为模板参考
- 可与上位机通信(遵从板级串口通信模块的通信协议)
- 支持JC24B无线串口透传模块
- 支持HMI界面数据显示
- 支持与其他STM32单片机通信(遵从板级串口通信模块的通信协议)
- 支持LED频闪显示自身ID
开发环境
CubeMX
MDK5 IDE
STM32F103C8T6 芯片
Stm32最小系统板
RT-Thread操作系统
面向对象模块接口设计
陶晶驰HMI串口屏
JC24B无线串口应用特性
模块化设计自由裁剪
多线程工作
通信模块通用,分自身ID和目标ID,可迅速部署通信网络
数据表大小0xFF个int类型数据源文件
App_PcComTest.c
```c /*- Copyright (c) 2020 - ~, HIT_HERO Team *
- PCCOMTEST APP SOUCE FILE
- Used in RT-Thread Operate System *
- Change Logs:
- Date Author Notes Mail
- 2020-09-06 WangXi first version WangXi_chn@foxmail.com */
include “App_PcComTest.h”
include
static void APP_PcComTestInit(APP_PCCOMTEST Application); static void APP_PcComTestRun(APP_PCCOMTEST Application);
static void PcComTestLed_entry(void parameter); static void PcComTestUpdate_entry(void parameter); static void PcComTestDeal_entry(void *parameter);
void APP_PcComTest_Config(APP_PCCOMTEST *Application) { if( Application->Method_Init == NULL && Application->Method_Run == NULL
){
/* Link the Method */
Application->Method_Init = APP_PcComTestInit;
Application->Method_Run = APP_PcComTestRun;
}
else{
rt_kprintf("Warning: Module Led is Configed twice\n");
return;
}
/* Device Init */
Application->Method_Init(Application);
/* Module Config */
Module_UartCom_Config(&(Application->dev_UartBsp));
Module_Led_Config(&(Application->dev_Led));
Module_UartHMI_Config(&(Application->dev_UartHMI));
return;
}
static void APP_PcComTestInit(APP_PCCOMTEST Application) { / Module param list ————————————————————————————————————- / / LED Pin device / / Pin: PC13 Low power enable */ Application->dev_Led.Property_pin = GET_PIN(C, 13); Application->dev_Led.Property_Mode = FLASH_LED_MODE; Application->dev_Led.LED_TIME_CYCLE = 1500; Application->dev_Led.LED_TIME_OUTPUT = 150;
/* JC24B 2.4G Uart communication device */
/* Pin: PA2 Uart2_TX */
/* Pin: PA3 Uart2_RX */
/* Pin: PB12 Set Pin */
Application->dev_UartBsp.Property_UartDevName = "uart2";
Application->dev_UartBsp.Property_BaudRate = 9600;
Application->dev_UartBsp.Property_MyID = MYID;
Application->dev_UartBsp.Property_NetID = 0x10;
Application->dev_UartBsp.Property_Device = JC24B;
Application->dev_UartBsp.Property_JC24BsetPin = GET_PIN(B, 12);
/* HMI Uart communication device */
/* Pin: PB10 Uart3_TX */
/* Pin: PB11 Uart3_RX */
Application->dev_UartHMI.Property_UartDevName = "uart3";
/* Data Sheet Default Value ------------------------------------------------------------------ */
}
static void APP_PcComTestRun(APP_PCCOMTEST Application)
{
/ Let the LED flash module show the machine ID */
Application->dev_Led.Method_Set(&Application->dev_Led,MYID);
rt_thread_t PcComTestLed_thread = rt_thread_create("PcComTestLed", PcComTestLed_entry,
Application,512, RT_THREAD_PRIORITY_MAX - 2, 20);
if (PcComTestLed_thread != RT_NULL){
rt_thread_startup(PcComTestLed_thread);
}
rt_thread_t PcComTestUpdate_thread = rt_thread_create("PcComTestUpdate", PcComTestUpdate_entry,
Application,512, RT_THREAD_PRIORITY_MAX - 3, 20);
if (PcComTestUpdate_thread != RT_NULL){
rt_thread_startup(PcComTestUpdate_thread);
}
rt_thread_t PcComTestDeal_thread = rt_thread_create("PcComTestDeal", PcComTestDeal_entry,
Application,1024, RT_THREAD_PRIORITY_MAX - 3, 20);
if (PcComTestDeal_thread != RT_NULL){
rt_thread_startup(PcComTestDeal_thread);
}
}
/ thread entry list ——————————————————————————————————————- /
static void PcComTestLed_entry(void parameter)
{
APP_PCCOMTEST Application = (APP_PCCOMTEST *)parameter;
while(1)
{
rt_thread_mdelay(1);
Application->dev_Led.Method_Handle(&(Application->dev_Led));
}
}
static void PcComTestUpdate_entry(void parameter) { APP_PCCOMTEST Application = (APP_PCCOMTEST *)parameter;
Application->dev_UartBsp.Method_Feed( &(Application->dev_UartBsp),
&(Application->Value_PacketID),
Application->Value_DataSheet);
}
static void PcComTestDeal_entry(void parameter) { APP_PCCOMTEST Application = (APP_PCCOMTEST *)parameter; char str[50]; while (1) { rt_thread_mdelay(1); sprintf(str, “tempreture.txt=\”%d\””, Application->Value_DataSheet[gytempreture_ID]); Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "offsetdata.txt=\"%d\"", Application->Value_DataSheet[gylingdata_ID]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "origindata.txt=\"%d\"", Application->Value_DataSheet[qystartdata_ID]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "dealdata.txt=\"%d\"", Application->Value_DataSheet[gylvdata_ID]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "groAngle.txt=\"%d\"", Application->Value_DataSheet[gyangle_ID]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "sampleNum.txt=\"%d\"", Application->Value_DataSheet[maNumber]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "magXorigin.txt=\"%d\"", Application->Value_DataSheet[ma]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "magXdeal.txt=\"%d\"", Application->Value_DataSheet[malvX]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "magYorigin.txt=\"%d\"", Application->Value_DataSheet[mastartY]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "magYdeal.txt=\"%d\"", Application->Value_DataSheet[malvY]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "magXoffset.txt=\"%d\"", Application->Value_DataSheet[malingX]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "magYoffset.txt=\"%d\"", Application->Value_DataSheet[malingY]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "magCircle.txt=\"%d\"", Application->Value_DataSheet[macircle]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "magAngle.txt=\"%d\"", Application->Value_DataSheet[maAngle]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "AimX.txt=\"%d\"", Application->Value_DataSheet[coX]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "AimY.txt=\"%d\"", Application->Value_DataSheet[coY]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "AimR.txt=\"%d\"", Application->Value_DataSheet[coangle]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "Mode.txt=\"%d\"", Application->Value_DataSheet[mo360]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "GPSX.txt=\"%d\"", Application->Value_DataSheet[allX]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "GPSY.txt=\"%d\"", Application->Value_DataSheet[allY]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
sprintf(str, "GPSAng.txt=\"%d\"", Application->Value_DataSheet[allziangle]);
Application->dev_UartHMI.Method_Send(&Application->dev_UartHMI,str);
}
}
/** (C) COPYRIGHT 2020 WANGXI **END OF FILE**/
- 方法API解释
- 在Config全局函数中,实现对应用对象的初始化
- 绑定函数方法,调用Init初始化方法(配置成员模块的必要参数,初始化数据表默认数值)
- 调用各个模块的Config全局函数,实现对各个模块的初始化
- Run方法被main调用,指示应用开始运行
- 调用成员模块的方法,执行外设功能
- 创建线程,启动线程
- 初始化所需信号量等
- 线程解释
- PcComTestLed
- 应用状态灯线程,通过频闪次数指示自身在网中的ID号
- PcComTestUpdate
- 数据更新线程,接收串口数据更新数据表
- PcComTestDeal
- 数据处理线程,这里仅将数据表中的信息对应显示在了串口屏中
<a name="hACgS"></a>
#### App_PcComTest.h
```c
/*
* Copyright (c) 2020 - ~, HIT_HERO Team
*
* PCCOMTEST APP HEAD FILE
* Used in RT-Thread Operate System
*
* Change Logs:
* Date Author Notes Mail
* 2020-09-06 WangXi first version WangXi_chn@foxmail.com
*/
#ifndef _APP_PCCOMTEST_H_
#define _APP_PCCOMTEST_H_
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "Module_UartCom.h"
#include "Module_LED.h"
#include "Module_UartHMI.h"
struct _APP_PCCOMTEST
{
/* Property */
MODULE_UARTCOM dev_UartBsp;
MODULE_LED dev_Led;
MODULE_UARTHMI dev_UartHMI;
/* Value */
UARTCOM_PACKETID Value_PacketID;
rt_int32_t Value_DataSheet[0xFF];
/* Method */
void (*Method_Init)(struct _APP_PCCOMTEST *Application);
void (*Method_Run)(struct _APP_PCCOMTEST *Application);
};
typedef struct _APP_PCCOMTEST APP_PCCOMTEST;
void APP_PcComTest_Config(APP_PCCOMTEST *Application);
//Machine_ID define--------------------------------------------------------------
#define MYID 0x01
#define PCID 0x11
//Packet_ID define---------------------------------------------------------------
//陀螺仪
//陀螺仪芯片温度 gytempreture 0X01
#define gytempreture_ID 0x01
//陀螺仪Z轴零偏数据 gylingdata 0X02
#define gylingdata_ID 0x02
//陀螺仪Z轴原始数据 qystartdata 0X03
#define qystartdata_ID 0X03
//陀螺仪Z轴滤波数据 gylvdata 0X04
#define gylvdata_ID 0X04
//陀螺仪计算角度 gyangle 0X05
#define gyangle_ID 0X05
//陀螺仪Kalman滤波器参数 估算协方差P gyP 0X06
#define gyP 0x06
//陀螺仪Kalman滤波器参数 过程噪声协方差Q gyQ 0X07
#define gyQ 0X07
//陀螺仪Kalman滤波器参数 观测噪声协方差R gyR 0X08
#define gyR 0X08
//磁力计
//磁位计X轴原始数据 ma 0X11
#define ma 0X11
//磁位计X轴滤波数据 malvX 0X12
#define malvX 0X12
//磁位计Y轴原始数据 mastartY 0X13
#define mastartY 0X13
//磁位计Y轴滤波数据 malvY 0X14
#define malvY 0X14
//磁位计计算角度 maAngle 0X15
#define maAngle 0X15
//设置零偏采样值个数 maNumber 0X16
#define maNumber 0X16
//磁位计X轴零偏数据 malingX 0X17
#define malingX 0X17
//磁位计Y轴零偏数据 malingY 0X18
#define malingY 0X18
//磁位计圆化率 macircle 0X19
#define macircle 0X19
//磁位计Kalman滤波器参数 估算协方差P maP 0X1A
#define maP 0X1A
//磁位计Kalman滤波器参数 过程噪声协方差Q maQ 0X1B
#define maQ 0X1B
//磁位计Kalman滤波器参数 观测噪声协方差R maR 0X1C
#define maR 0X1C
//整体部分读取
//互补滤波角度 allangle 0X21
#define allangle 0X21
//全局定位X坐标值 allX 0X22
#define allX 0x22
//全局定位Y坐标值 allY 0X23
#define allY 0x23
//全局定位姿态角值 allziangle 0X24
#define allziangle 0X24
//模式部分
//选择360° mo360 0X31
#define mo360 0X31
//±180°模式 mo180 0X32
#define mo180 0X32
//控制部分读取
//装置目标位置值 cogoallocation 0X41
#define cogoallocation 0X41
//装置实际位置值 coactuallocation 0X42
#define coactuallocation 0X42
//装置目标角度值 cogoalangle 0X43
#define cogoalangle 0X43
//装置实际位置值 coactualangle 0X44
#define coactualangle 0X44
//装置坐标值X coX 0X45
#define coX 0X45
//装置坐标值Y coY 0X46
#define coY 0X46
//装置姿态角 coangle 0X47
#define coangle 0X47
#endif
/************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/
- 将帧的ID作为数据表(数组)的检索,对数据表读取或存储
使用main.c
```c /*- Copyright (c) 2020 - ~, HIT_HERO Team *
- STM32F103C8T6 MIN SYSTEM MAIN SOUCE FILE
- Used in RT-Thread Operate System *
- Change Logs:
- Date Author Notes Mail
- 2020-09-03 WangXi first version WangXi_chn@foxmail.com *
- Note:
- Finsh: uart1
- uart1_tx —> PA9
- uart1_rx —> PA10 */
include
include
include
include “App_PcComTest.h”
APP_PCCOMTEST app_pccomtest;
int main(void) { APP_PcComTest_Config(&app_pccomtest); app_pccomtest.Method_Run(&app_pccomtest);
return 0;
}
/** (C) COPYRIGHT 2020 WANGXI **END OF FILE**/
屏幕界面
显示屏程序
TJC3224T022_011_TESTMACH.rar
补充说明
- 该应用程序并非正式投入使用的程序,只是各项功能的先行验证版本
- 同时也是通过应用封装各个模块的模板说明,为如何组织各个模块工作提供范例
- 将视情况选择是否继续更新
应用版本V2.0
功能更新
- 赋予项目名称 Ratel(蜜獾)
- 寓意勇敢尝试,实验新的功能
- 试验了无线串口通信的组网功能
- 试验了通过单片机自定义ID的功能
- 试验了OLED显示功能