陶晶驰串口屏
资料
陶晶驰串口屏是本质是一个MCU,屏幕是MCU的模块,一般是一块TFT屏幕。在串口屏厂商提供的软件上面编写界面,然后通过串口直接烧到串口屏的MCU上,然后在屏幕上展示出来。
串口屏资料中心传送门入口
使用方法
串口屏资料中心传送门入口(详细使用方法请关注资料中心)
YXY通信
YXY通信是专门为串口屏开发的一种通信方法
YXY通信把数据分为了三个部分,分别是数据id、数据、数据尾,没错就这三个简单吧,甚至没有打包解包的过程,比市面上的一众通信原理都简单。
数据id是用来区分是哪个模块,发过来的数据,比如我要做一个智能家居,智能家居,要做空调,要做窗帘,要做扫地机器人,那我就分别给他定义数据id,比如空调是0x11,风扇是0x12,扫地机器人是0x13,当串口屏给外部MCU发送0x11时,我就知道,用户在串口屏上操作了空调部分,获取了操作后的数据,我再到对应的地方去编写对应的代码,就可以实现由串口屏到模块的直接控制。
串口屏软件上数据发送部分
直接上例子
就这几个例子吧,后面之恶极上工程
cubemx配置
1.开一个串口(开串口中断)
代码部分
我是直接把我之前做的工程的代码拿过来了,最后面附上整个工程的源码,这里仅供参考。
UART_Screen.c
#include "UART_Screen.h"
#include "O_redirect.h"
#include "yxy_delay.h"
#include "usart.h"
#include "string.h"
#include "stdbool.h"
#include "UserHand.h"
#include "esp8266.h"
#include "errorType.h"
HMI_Struct HMI_InitStr;
HMI_SetTime hmiSetTime;
void HMI_SendData(uint8_t *ConData, uint8_t DataType, void * Data)
{
U_Printf(HMI_print, "%s", ConData);
switch(DataType)
{
case HMI_NUM:
{
uint16_t Num = *((uint16_t *)Data);
U_Printf(HMI_print, "%d", Num);
}break;
case HMI_STRING:
{
uint8_t * text = (uint8_t *)Data;
uint8_t sz = 0;
while(*(text+sz) != 255)
{
sz++;
}
U_Printf(U_UART1, "sz : %d\r\n", sz);
U_Printf(HMI_print, "\"");
for(int i=0; i<sz; i++)
{
U_Printf(HMI_print, "%c", text[i]);
}
U_Printf(HMI_print, "\"");
}break;
}
U_Printf(HMI_print, "\xff\xff\xff");
}
void HMI_SendData(uint8_t* Data)
{
U_Printf(HMI_print, "page %s\xff\xff\xff", Data);
}
void HMI_Init(void)
{
HMI_InitStr.HMI_cnt = 0;
HMI_InitStr.a_HMI_buf=0;
HMI_InitStr.HMI_cntPre=0;
memset(HMI_InitStr.HMI_buf, 0, sizeof(HMI_InitStr.HMI_buf));
HAL_UART_Receive_IT(&HMI_huart, (uint8_t *)&(HMI_InitStr.a_HMI_buf), 1);
}
void HMI_Clear(void)
{
memset(HMI_InitStr.HMI_buf, 0, sizeof(HMI_InitStr.HMI_buf));
HMI_InitStr.a_HMI_buf = 0;
HMI_InitStr.HMI_cnt = 0;
HMI_InitStr.HMI_cntPre = 0;
}
static void LoginDataProcess(void);
static void M_C_DataProcess(void);
static void DisinfectDataProcess(void);
static void CurtainDataProcess(void);
static void InternetDataProcess(void);
void HMI_UnPack(void)
{
switch(HMI_InitStr.HMI_buf[0])
{
case HMI_MAIN:
U_Printf(U_UART1, "这里是main界面\r\n");
break;
case HMI_LOGIN:
U_Printf(U_UART1, "这里是login界面\r\n");
LoginDataProcess();
break;
case HMI_PASS_CHANGE:
U_Printf(U_UART1, "这里是password change界面\r\n");
LoginDataProcess();
break;
case HMI_LOGIN_SUCC:
U_Printf(U_UART1, "这里是login succeed界面\r\n");
break;
case HMI_LOGIN_FAIL:
U_Printf(U_UART1, "这里是login fail界面\r\n");
break;
case HMI_MEDICINE_CHEST:
U_Printf(U_UART1, "这里是mediciness chest界面\r\n");
M_C_DataProcess();
break;
case HMI_DISINFECT:
U_Printf(U_UART1, "这里是disinfect界面\r\n");
DisinfectDataProcess();
break;
case HMI_CURTAIN:
U_Printf(U_UART1, "这里是curtain界面\r\n");
CurtainDataProcess();
break;
case HMI_INTERNET:
U_Printf(U_UART1, "这里是internet界面\r\n");
InternetDataProcess();
break;
default:
break;
}
HMI_Clear();
}
static void LoginDataProcess(void)
{
uint8_t tx[] = "va0.txt=";
uint8_t UserPass[PASS_LENG_MAX+1];
uint8_t i = 2;
memset(UserPass, 255, sizeof(UserPass));
switch(HMI_InitStr.HMI_buf[1])
{
case Get_PASS:
{
U_Printf(U_UART1, "login中 的 Get_PASS\r\n");
UserGetPassword(UserPass);
UserPass[PASS_LENG_MAX] = '\0';
U_Printf(U_UART1, "UserPass:%s\r\n", UserPass);
HMI_SendData(tx, HMI_STRING, (void *)(UserPass));
}break;
case CHANGE_PASS:
{
U_Printf(U_UART1, "login中 的 CHANGE_PASS\r\n");
curPassLen=0;
while(*(HMI_InitStr.HMI_buf + i) != 0xff)
{
curPassLen++;
i++;
}
Wrinte_EEPOM(PASS_LEN, curPassLen);
U_Printf(U_UART1, "UserPass:%s, Cnt : %d \r\n", HMI_InitStr.HMI_buf+2, curPassLen);
UserSetPassword(((uint8_t *)(HMI_InitStr.HMI_buf+2)), curPassLen);
}break;
case RESET_PASS:
{
U_Printf(U_UART1, "login中 的 RESET_PASS\r\n");
uint8_t ResPass[] = "1111";
UserSetPassword((uint8_t *)ResPass, 4);
curPassLen = 4;
Wrinte_EEPOM(PASS_LEN, curPassLen);
}break;
default:
U_Printf(U_UART1, "0x%x\r\n", HMI_InitStr.HMI_buf[1]);
break;
}
}
static void M_C_DataProcess(void)
{
switch(HMI_InitStr.HMI_buf[1])
{
case MC_SW_ON_OFF:
U_Printf(U_UART1, "mediciness chest 的 MC_SW_ON_OFF\r\n");
switch(HMI_InitStr.HMI_buf[2])
{
case 1:
U_Printf(U_UART1, "va0 : %d\r\n", HMI_InitStr.HMI_buf[3]);
if(HMI_InitStr.HMI_buf[3] == MC_Close)
{
SG90_Ch_Degrees(sg90_Ch1, Degrees135);
}
else if(HMI_InitStr.HMI_buf[3] == MC_Open)
{
SG90_Ch_Degrees(sg90_Ch1, Degrees0);
}
break;
case 2:
U_Printf(U_UART1, "va1 : %d\r\n", HMI_InitStr.HMI_buf[3]);
if(HMI_InitStr.HMI_buf[3] == MC_Close)
{
SG90_Ch_Degrees(sg90_Ch2, Degrees135);
}
else if(HMI_InitStr.HMI_buf[3] == MC_Open)
{
SG90_Ch_Degrees(sg90_Ch2, Degrees0);
}
break;
default:
break;
}
break;
case Get_MC_Val:
{
U_Printf(U_UART1, "mediciness chest 的 Get_MC_Val\r\n");
uint8_t tx0[] = "va0.val=";
uint16_t Num = MC_Close;
if(Get_MC_flag(sg90_Ch1) == MC_Open)
{
Num = MC_Open;
}
HMI_SendData(tx0, HMI_NUM, &Num);
uint8_t tx2[] = "h0.val=";
uint8_t tx3[] = "h1.val=";
uint8_t tx4[] = "h2.val=";
uint8_t tx5[] = "t5.txt=";
uint8_t tx6[30];
sprintf((char *)tx6, "定时时间:%2d:%2d:%2d", hmiSetTime.hmi_hour[HMI_MC_Num],
hmiSetTime.hmi_min[HMI_MC_Num],
hmiSetTime.hmi_sec[HMI_MC_Num]);
HMI_SendData(tx2, HMI_NUM, &hmiSetTime.hmi_hour[HMI_MC_Num]);
HMI_SendData(tx3, HMI_NUM, &hmiSetTime.hmi_min[HMI_MC_Num]);
HMI_SendData(tx4, HMI_NUM, &hmiSetTime.hmi_sec[HMI_MC_Num]);
HMI_SendData(tx5, HMI_STRING, (void *)tx6);
}break;
case Set_Med_Time:
{
U_Printf(U_UART1, "mediciness chest 的 Set_Med_Time\r\n");
hmiSetTime.hmi_hour[HMI_MC_Num] = HMI_InitStr.HMI_buf[2];
hmiSetTime.hmi_min[HMI_MC_Num] = HMI_InitStr.HMI_buf[3];
hmiSetTime.hmi_sec[HMI_MC_Num] = HMI_InitStr.HMI_buf[4];
hmiSetTime.hmi_flag &= ~(1<<(HMI_MC_Num));
U_Printf(U_UART1, "Set_Med_Time: %d:%d:%d\r\n", hmiSetTime.hmi_hour[HMI_MC_Num],
hmiSetTime.hmi_min[HMI_MC_Num], hmiSetTime.hmi_sec[HMI_MC_Num]);
}break;
case In_Med_Interface:
{
U_Printf(U_UART1, "mediciness chest 的 In_Med_Interface\r\n");
switch(HMI_InitStr.HMI_buf[2])
{
case 0x01:
BeepRing(1);
yxy_delay_ms(200);
BeepRing(0);
break;
case 0x02:
hmiSetTime.hmi_flag |= 1<<(HMI_MC_Num);
hmiSetTime.hmi_lastTime = esp_time.esp_hour*60 + esp_time.esp_min;
break;
}
}break;
default:
break;
}
}
static void DisinfectDataProcess(void)
{
switch(HMI_InitStr.HMI_buf[1])
{
case D_SW_ON_OFF:
switch(HMI_InitStr.HMI_buf[2])
{
case 0x00:
No_Disinfection;
TIM5->CCR1 = 10000;
break;
case 0x01:
LowDisinfection;
TIM5->CCR1 = 6666;
break;
case 0x02:
ModDisinfection;
TIM5->CCR1 = 3333;
break;
case 0x03:
HigDisinfection;
TIM5->CCR1 = 0;
break;
default:
No_Disinfection;
TIM5->CCR1 = 0;
break;
}
break;
case Get_LED_Val:
{
uint8_t tx[] = "h0.val=";
uint16_t Num;
Num = UV2_State();
HMI_SendData(tx, HMI_NUM, &Num);
}break;
default:
break;
}
}
static void CurtainDataProcess(void)
{
switch(HMI_InitStr.HMI_buf[1])
{
case C_SW_ON_OFF:
{
U_Printf(U_UART1, "curtain 的 C_SW_ON_OFF\r\n");
switch(HMI_InitStr.HMI_buf[2])
{
case 0x01:
uln2003_MotorRotation(FORWARD);
uln_flag = 1;
break;
case 0x02:
uln2003_MotorRotation(REVERSE);
uln_flag = 2;
break;
default:
uln2003_MotorRotation(STOP_IT);
uln_flag = 0;
break;
}
}break;
case Get_Cur_Val:
{
U_Printf(U_UART1, "curtain 的 Get_Cur_Val\r\n");
uint8_t tx2[] = "h0.val=";
uint8_t tx3[] = "h1.val=";
uint8_t tx4[] = "h2.val=";
uint8_t tx5[] = "t5.txt=";
uint8_t tx6[30];
sprintf((char *)tx6, "定时时间:%2d:%2d:%2d", hmiSetTime.hmi_hour[HMI_Cur_Num],
hmiSetTime.hmi_min[HMI_Cur_Num], hmiSetTime.hmi_sec[HMI_Cur_Num]);
HMI_SendData(tx2, HMI_NUM, &hmiSetTime.hmi_hour[HMI_Cur_Num]);
HMI_SendData(tx3, HMI_NUM, &hmiSetTime.hmi_min[HMI_Cur_Num]);
HMI_SendData(tx4, HMI_NUM, &hmiSetTime.hmi_sec[HMI_Cur_Num]);
HMI_SendData(tx5, HMI_STRING, (void *)tx6);
}break;
case Set_Cur_Time:
{
U_Printf(U_UART1, "curtain 的 Set_Cur_Time\r\n");
hmiSetTime.hmi_hour[HMI_Cur_Num] = HMI_InitStr.HMI_buf[2];
hmiSetTime.hmi_min[HMI_Cur_Num] = HMI_InitStr.HMI_buf[3];
hmiSetTime.hmi_sec[HMI_Cur_Num] = HMI_InitStr.HMI_buf[4];
U_Printf(U_UART1, "Set_Cur_Time: %d:%d:%d\r\n", hmiSetTime.hmi_hour[HMI_Cur_Num],
hmiSetTime.hmi_min[HMI_Cur_Num], hmiSetTime.hmi_sec[HMI_Cur_Num]);
U_Printf(HMI_print, "page Curtain\xff\xff\xff");
}break;
default:
break;
}
}
static void InternetDataProcess(void)
{
uint16_t Len = 0;
uint8_t ssid[20];
uint8_t wifiPassword[20];
switch(HMI_InitStr.HMI_buf[1])
{
case NETWORKING:
{
uint8_t tx[] = "main.p0.pic=";
uint16_t Num = 40;
ESP_flag &= ~ESP_IN_NETWORK;
HMI_SendData(tx, HMI_NUM, &Num);
while(Len < HMI_InitStr.HMI_buf[2])
{
ssid[Len] = HMI_InitStr.HMI_buf[3+Len];
Len++;
}
ssid[Len] = '\0';
Len = 0;
while(Len < HMI_InitStr.HMI_buf[3 + HMI_InitStr.HMI_buf[2]])
{
wifiPassword[Len] = HMI_InitStr.HMI_buf[4 + HMI_InitStr.HMI_buf[2] + Len];
Len++;
}
wifiPassword[Len] = '\0';
U_Printf(U_UART1, "ssidLen : %d password : %d\r\n", HMI_InitStr.HMI_buf[2], HMI_InitStr.HMI_buf[3 + HMI_InitStr.HMI_buf[2]]);
U_Printf(U_UART1, "ssid : %s\r\npassword : %s\r\n", ssid, wifiPassword);
ssidPass_wifi(ssid, wifiPassword);
U_Printf(HMI_print, "page main\xff\xff\xff");
if((ESP_flag&ESP_IN_NETWORK) == ESP_IN_NETWORK)
{
Num = 41;
}
else
{
Num = 40;
}
HMI_SendData(tx, HMI_NUM, &Num);
}break;
default:
break;
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == HMI_UART)
{
if(HMI_InitStr.HMI_cnt >= HMI_BUF_SIZE)
{
HMI_InitStr.HMI_cnt = 0;
memset(HMI_InitStr.HMI_buf, 0, sizeof(HMI_InitStr.HMI_buf));
U_Printf(U_UART1, "HMI data overflow\r\n");
}
else
{
HMI_InitStr.HMI_buf[HMI_InitStr.HMI_cnt] = HMI_InitStr.a_HMI_buf;
HMI_InitStr.HMI_cnt++;
if(HMI_InitStr.a_HMI_buf == HMI_PackTail)
HMI_flag = 1;
}
HAL_UART_Receive_IT(&HMI_huart, (uint8_t *)&(HMI_InitStr.a_HMI_buf), 1);
}
}
UART_Screen.h
#ifndef __UART_SCREEN_H_
#define __UART_SCREEN_H_
#include "stdint.h"
#define HMI_BUF_SIZE 127
#define HMI_huart huart2
#define HMI_UART USART2
#define HMI_print U_UART2
#define HMI_PackTail 0xff
#define TimeNumber 16
#define TimeRInit 0x70
typedef enum
{
HMI_MAIN = 0x01,
HMI_LOGIN = 0x70,
HMI_PASS_CHANGE = 0x71,
HMI_LOGIN_SUCC = 0x72,
HMI_LOGIN_FAIL = 0x73,
HMI_MEDICINE_CHEST = 0x74,
HMI_DISINFECT = 0x75,
HMI_CURTAIN = 0x76,
HMI_INTERNET = 0x77,
MAX_MODULE_ID = 0x80
}EnumPackID;
#define HMI_MC_Num (HMI_MEDICINE_CHEST - TimeRInit)
#define HMI_Cur_Num (HMI_CURTAIN - TimeRInit)
typedef enum
{
Get_PASS = 0x11,
CHANGE_PASS = 0x12,
RESET_PASS = 0x13
}PasswordChangeID;
typedef enum
{
MC_SW_ON_OFF = 0x11,
Get_MC_Val = 0x12,
Set_Med_Time = 0x13,
In_Med_Interface= 0x14
}MedicineChestID;
typedef enum
{
D_SW_ON_OFF = 0x11,
Get_LED_Val = 0x12
}DisinfectID;
typedef enum
{
C_SW_ON_OFF = 0x11,
Get_Cur_Val = 0x12,
Set_Cur_Time= 0x13,
}CurtainID;
typedef enum
{
NETWORKING = 0x11
}InternetID;
typedef enum
{
HMI_NUM = 0,
HMI_STRING
}HMI_Data_Type;
typedef struct HMI_struct
{
uint8_t HMI_buf[HMI_BUF_SIZE];
uint8_t a_HMI_buf;
uint16_t HMI_cnt;
uint8_t HMI_cntPre;
}HMI_Struct;
typedef struct hmiSet_Time
{
uint8_t hmi_hour[TimeNumber];
uint8_t hmi_min[TimeNumber];
uint8_t hmi_sec[TimeNumber];
uint16_t hmi_flag;
uint16_t hmi_lastTime;
}HMI_SetTime;
extern HMI_SetTime hmiSetTime;
void HMI_Init(void);
void HMI_UnPack(void);
void HMI_SendData(uint8_t *ConData, uint8_t DataType, void * Data);
#endif
初始化
HMI_Init();
效果展示
源码
链接:https://pan.baidu.com/s/1FYY1S2K6p81wKKVA_KCcxg?pwd=nkxs 提取码:nkxs
|