——文档转载自电子发烧友
这次设计是用迪文屏+CH582来控制固态继电器控制家用取暖器恒温系统。CH582作为主控,接收迪文屏的开、关机以及预设温度值,同时通过AHT10来采集实时温度来控制固态继电器的导通与关闭。
1.DGUS开发
(1)制作迪文屏所需要的图片
(2)软件属性设置
2.编写CH582的主控程序,我这里只示例串口主要程序:
void task_uart0_rec(void) { int i , l; IF (SerialStr0.RecvLen > 0 ) { PRINT("RECV_LEN:%d",SerialStr0.RecvLen); if (SerialStr0.RecvLen == (SerialStr0.RecvBuff[2]+3) \ && SerialStr0.RecvBuff[0] == 0x5A \ && SerialStr0.RecvBuff[1] == 0xA5) { //接收到正确的数据 DMG85480.state = RECV_WAIT_EXPLAN; DMG85480.data_len = SerialStr0.RecvBuff[2]-3;
DMG85480.commad = SerialStr0.RecvBuff[3]; PRINT("recv_CMMD:%X\n",DMG85480.commad); DMG85480.address = (uint16_t)SerialStr0.RecvBuff[4]<<8 | SerialStr0.RecvBuff[5]; PRINT("recv_ADDRS:%X\n",DMG85480.address); i = SerialStr0.RecvBuff[2]-3; for (l = 0; l < i; ++l) { DMG85480.data[l] = SerialStr0.RecvBuff[l+6]; } rev_explain(); DMG85480.DMG_ERROR = RECV_OK; } else { DMG85480.DMG_ERROR = RECV_ERR; SerialStr0.RecvLen = 0;//丢弃无效的包 } } SerialStr0.RecvLen = 0; }
/* * 数据判断 * */ static void rev_explain(void) { uint8_t send_data[10]; switch (DMG85480.commad) { case WRIT_DMG_DATA: if (DMG85480.address == SWITCH) {//如果是开关按键 if (DMG85480.data[2] == 0x01) {//按下的是开键,转换到页面。 PRINT("OPEN\n"); Electric_Heater.work_state = OPEN; send_data[0] = 0x5A; send_data[1] = 0x01; send_data[2] = 0x00; send_data[3] = 0x01; DMG85480.state = IDEL; send_DMG(send_data, CHANG_PAGE, WRIT_DMG_CMD,4); //等待收到OK //设置开机模式 } else if (DMG85480.data[2] == 0x00) { //关断了 //关闭电加热丝 切换到0号页面 Electric_Heater.work_state = CLOSE; PRINT("CLOSE\n"); send_data[0] = 0x5A; send_data[1] = 0x01; send_data[2] = 0x00; send_data[3] = 0x00; DMG85480.state = IDEL; send_DMG(send_data, CHANG_PAGE, WRIT_DMG_CMD,4); } } else if (DMG85480.address == SET_TEMP) { //温度设定改变 //设置温度值 PRINT("CHANGE\n"); Electric_Heater.set_temp = DMG85480.data[2]; DMG85480.state = IDEL; } break; case WRIT_DMG_CMD: if (DMG85480.address == SEND_RECV_OK) {//返回写正确 DMG85480.DMG_ERROR = SEND_OK; DMG85480.state = IDEL;
} break; default: break; } } /* * 发送给串口屏 * 参数1:数据 * 参数2:指令 0x80-0x83 * 参数3:串口屏地址 * 参数4:数据长度 */ void send_DMG(uint8_t pdata[], uint16_t addr, uint8_t commd, uint8_t data_len) { uint8_t commd_len; while(DMG85480.state != IDEL); SerialStr0.TxBuff[0] = 0x5a; SerialStr0.TxBuff[1] = 0xa5; SerialStr0.TxBuff[2] = data_len + 3;//长度 SerialStr0.TxBuff[3] = commd; SerialStr0.TxBuff[4] = (uint8_t)(addr>>8); SerialStr0.TxBuff[5] = (uint8_t)addr; for (commd_len = 0; commd_len < data_len; ++commd_len) { SerialStr0.TxBuff[6+commd_len] = pdata[commd_len]; } DMG85480.state = SEND_WAIT; UART0_SendString(SerialStr0.TxBuff, 6+data_len); DMG85480.state = IDEL; }
3.实物效果展示
抖音视频:https://v.douyin.com/6uqhb9T/
快手视频:https://www.kuaishou.com/f/X5Dc3XhTPbEw2bg
|