12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397 |
- /*******************************************************************************
- *
- * sw
- * Soft version: switch_v1.0
- * File Name: main.c
- * Author : zzw (zhangzw_3@163.com)
- * creation date: 2023-01-20
- * module description:
- * Copyright (C) reserve
- *
- *
- ********************************************************************************/
- #include <REGtenxTM52F1386.h>
- #include "TM52F1386_bsp.h"
- #include <intrins.h>
- #include "GLOBAL.h"
- #include "time2.h"
- #include "sTimeout.h"
- #include "uart.h"
- #include "gpio_ctrl.h"
- #include "zigbee.h"
- #include "EEPROM.h"
- #include "HLW8110.h"
- #include "uart2.h"
- #define NETWORK_CONFIGURE_TRIGGER_TIME 500 // 长按统计,10ms为单位,设置5秒
- void relay_ctl(void); // 普通继电器控制
- void WDGT_Init(void); // 看门口初始化
- void WDGT_Feed(void); // 喂狗
- static byte bKey;
- static byte bKeyl;
- static word wKeyTime;
- static byte readkey;
- static byte readkey1;
- /**********************************************************************************************************
- **函数名称 :button1LongPressCheck()
- **函数描述 :长按键扫描处理任务,10ms调用
- **输 入 :None
- **输 出 :None
- **********************************************************************************************************/
- void button1LongPressCheck(void)
- {
- /*if (KEY1_READ() == 0) // 按键1被按下
- {
- if (switchState.KeyPress == 0)
- { // 原来没按下
- switchState.KeyPress = 1; // 更新按键状态为按下
- switchState.buttonPressTime = 0; // 按下计数器清理
- }
- else // 长按统计
- { // 长按5s 触发入网启动
- if (switchState.buttonPressTime >= NETWORK_CONFIGURE_TRIGGER_TIME) // 长按超过5s
- {
- if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
- {
- ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
- ZG_JoinCounter = 0;
- switchState.buttonPressTime = 0;
- ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- // switchState.KeyPress = 0; // 按键释放
- if (ZG_Joining == CONDITION_BEFOR_JOINING_TO_JOINING) // 入网倒计时180秒内再次长按退出配网
- {
- ZG_JoinCounter = 18000; //
- switchState.buttonPressTime = 0;
- }
- }
- }
- }
- else
- {
- switchState.KeyPress = 0; // 按键释放
- }*/
- }
- /**********************************************************************************************************
- **函数名称 :Scankey()
- **函数描述 :键盘扫描,10ms调用
- **输 入 :None
- **输 出 :None
- **********************************************************************************************************/
- void Scankey(void)
- {
- char i = 0;
- ScanKeyn(); // 按键值扫码任务
- bKey = GetKey(&wKeyTime); // 读取键值与长按时间
- if (bKey != NO_KEY_MSG)
- {
- if ((bKey & LONG_KEY_MASK) == LONG_KEY_MASK) // 长按键处理
- {
- bKeyl = bKey & SHORT_KEY_MASK;
- switch (bKeyl)
- {
- case KEY1: // 长按1号键
- if (wKeyTime >= 5000) // 5S
- {
- if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
- {
- ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
- ZG_JoinCounter = 0;
- ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- }
- break;
- case KEY2: // 长按2号键
- if (wKeyTime >= 5000) // 5S
- {
- if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
- {
- ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
- ZG_JoinCounter = 0;
- ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- }
- break;
- case KEY3: // 长按3号键
- if (wKeyTime >= 5000) // 5S
- {
- if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
- {
- ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
- ZG_JoinCounter = 0;
- ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- }
- break;
- case KEY4: // 长按4号键
- if (wKeyTime >= 5000) // 5S
- {
- if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
- {
- ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
- ZG_JoinCounter = 0;
- ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- }
- break;
- case KEY5: // 长按5号键
- if (wKeyTime >= 5000) // 5S
- {
- if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
- {
- ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
- ZG_JoinCounter = 0;
- ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- }
- break;
- case KEY6: // 长按6号键
- if (wKeyTime >= 5000) // 5S
- {
- if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
- {
- ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
- ZG_JoinCounter = 0;
- ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- }
- break;
- default:
- break;
- }
- }
- else // 短按键
- {
- switch (bKey & SHORT_KEY_MASK) // 短按键处理
- {
- case KEY1:
- if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
- {
- // mcu_join_zigbee(); // 设备入网函数
- ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
- ZG_JoinCounter = 0;
- ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- else
- {
- // switchState.KeyIn[0] = 0x01; // 按键被按下
- switchState.step[0] = 0x02; // 按键触发背光灯状态
-
- if (switchState.mod[0] == 0x01) // 场景开关
- {
- #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
- mcu_dp_enum_update(DPID_SCENE_1, 0); // 上报场景值
- #endif
- }
- else
- {
- #ifdef ZIGBEE_ZTU_T1_SW
- switchState.step[1] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x02) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- if (switchState.plus_mod[1] == 0x01) // 点动开启
- {
- switchState.plus_en[1] = 0x01;
- switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // write_mileage[1] = switchState.plus_en;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = switchState.plus_en[1];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- }
- #else
- if ((gLedState & 0x01) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x01; // 按键1开启
- gRelayOnOff |= 0x01; // 继电器动作开启
- if (switchState.plus_mod[0] == 0x01) // 点动开启
- {
- switchState.plus_en[0] = 0x01;
- switchState.plus_counter[0] = switchState.plus_auto_time[0];
- // write_mileage[0] = switchState.plus_en;
- // iap_eeprom_write(14, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[14] = switchState.plus_en[0];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x01) == 0x01) // 原状态灯亮
- {
- gLedState = gLedState & (~0x01); // 按键1清除
- gRelayOnOff &= (~0x01); // 继电器关闭
- }
- #endif
- }
- }
- break;
- case KEY2:
- if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
- {
- // mcu_join_zigbee(); // 设备入网函数
- ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
- ZG_JoinCounter = 0;
- ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- else
- {
- // switchState.KeyIn[1] = 0x01; // 按键被按下
- switchState.step[1] = 0x02; // 按键触发背光灯状态
- if (switchState.mod[1] == 0x01)
- {
- #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
- mcu_dp_enum_update(DPID_SCENE_2, 0);
- #endif
- }
- else
- {
- #ifdef ZIGBEE_ZTU_T1_SW
- switchState.step[1] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x02) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- if (switchState.plus_mod[1] == 0x01) // 点动开启
- {
- switchState.plus_en[1] = 0x01;
- switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // write_mileage[1] = switchState.plus_en;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = switchState.plus_en[1];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- }
- #else
- if ((gLedState & 0x02) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- if (switchState.plus_mod[1] == 0x01) // 点动开启
- {
- switchState.plus_en[1] = 0x01;
- switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // write_mileage[1] = switchState.plus_en;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = switchState.plus_en[1];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- }
- #endif
- }
- }
- break;
- case KEY3:
- if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
- {
- // mcu_join_zigbee(); // 设备入网函数
- ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
- ZG_JoinCounter = 0;
- ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- else
- {
- // switchState.KeyIn[2] = 0x01; // 按键被按下
- switchState.step[2] = 0x02; // 按键触发背光灯状态
- if (switchState.mod[2] == 0x01)
- {
- #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
- mcu_dp_enum_update(DPID_SCENE_3, 0);
- #endif
- }
- else
- {
- #ifdef ZIGBEE_ZTU_T1_SW
- switchState.step[1] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x02) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- if (switchState.plus_mod[1] == 0x01) // 点动开启
- {
- switchState.plus_en[1] = 0x01;
- switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // write_mileage[1] = switchState.plus_en;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = switchState.plus_en[1];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- }
- #else
- if ((gLedState & 0x04) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x04; // // 按键3开启
- gRelayOnOff |= 0x04; // 继电器动作开启
- if (switchState.plus_mod[2] == 0x01) // 点动开启
- {
- switchState.plus_en[2] = 0x01;
- switchState.plus_counter[2] = switchState.plus_auto_time[2];
- // write_mileage[2] = switchState.plus_en;
- // iap_eeprom_write(16, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[16] = switchState.plus_en[2];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x04) == 0x04) // 原状态灯亮
- {
- gLedState = gLedState & (~0x04); // // 按键3清除
- gRelayOnOff &= (~0x04); // 继电器关闭
- }
- #endif
- }
- }
- break;
- case KEY4:
- if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
- {
- // mcu_join_zigbee(); // 设备入网函数
- ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
- ZG_JoinCounter = 0;
- ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- else
- {
- // switchState.KeyIn[3] = 0x01; // 按键被按下
-
- #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
- switchState.step[3] = 0x02; // 按键触发背光灯状态
- mcu_dp_enum_update(DPID_SCENE_4, 0);
- #endif
- #if ((defined ZIGBEE_ZTU_T3_SW) && (defined ZIGBEE_3SWITCH_SW))|| (defined ZIGBEE_ZTU_T2_SW)
- switchState.step[2] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x04) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x04; // // 按键3开启
- gRelayOnOff |= 0x04; // 继电器动作开启
- if (switchState.plus_mod[2] == 0x01) // 点动开启
- {
- switchState.plus_en[2] = 0x01;
- switchState.plus_counter[2] = switchState.plus_auto_time[2];
- // write_mileage[2] = switchState.plus_en;
- // iap_eeprom_write(16, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[16] = switchState.plus_en[2];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x04) == 0x04) // 原状态灯亮
- {
- gLedState = gLedState & (~0x04); // // 按键3清除
- gRelayOnOff &= (~0x04); // 继电器关闭
- }
- #endif
- #ifdef ZIGBEE_ZTU_T1_SW
- switchState.step[1] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x02) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- if (switchState.plus_mod[1] == 0x01) // 点动开启
- {
- switchState.plus_en[1] = 0x01;
- switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // write_mileage[1] = switchState.plus_en;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = switchState.plus_en[1];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- }
- #endif
- }
- break;
- case KEY5:
- if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
- {
- // mcu_join_zigbee(); // 设备入网函数
- ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
- ZG_JoinCounter = 0;
- ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- else
- {
- // switchState.KeyIn[4] = 0x01; // 按键被按下
-
- #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
- switchState.step[4] = 0x02; // 按键触发背光灯状态
- mcu_dp_enum_update(DPID_SCENE_5, 0);
- #endif
- #if ((defined ZIGBEE_ZTU_T3_SW) && (defined ZIGBEE_3SWITCH_SW))|| (defined ZIGBEE_ZTU_T2_SW)
- switchState.step[1] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x02) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- if (switchState.plus_mod[1] == 0x01) // 点动开启
- {
- switchState.plus_en[1] = 0x01;
- switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // write_mileage[1] = switchState.plus_en;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = switchState.plus_en[1];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- }
- #endif
- #ifdef ZIGBEE_ZTU_T1_SW
- switchState.step[1] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x02) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- if (switchState.plus_mod[1] == 0x01) // 点动开启
- {
- switchState.plus_en[1] = 0x01;
- switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // write_mileage[1] = switchState.plus_en;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = switchState.plus_en[1];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- }
- #endif
- }
- break;
- case KEY6:
- if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
- {
- // mcu_join_zigbee(); // 设备入网函数
- ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
- ZG_JoinCounter = 0;
- ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
- }
- else
- {
- // switchState.KeyIn[5] = 0x01; // 按键被按下
-
- #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
- switchState.step[5] = 0x02; // 按键触发背光灯状态
- mcu_dp_enum_update(DPID_SCENE_6, 0);
- #endif
- #if ((defined ZIGBEE_ZTU_T3_SW) && (defined ZIGBEE_3SWITCH_SW))|| (defined ZIGBEE_ZTU_T2_SW)
-
- switchState.step[0] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x01) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x01; // 按键1开启
- gRelayOnOff |= 0x01; // 继电器动作开启
- if (switchState.plus_mod[0] == 0x01) // 点动开启
- {
- switchState.plus_en[0] = 0x01;
- switchState.plus_counter[0] = switchState.plus_auto_time[0];
- // write_mileage[0] = switchState.plus_en;
- // iap_eeprom_write(14, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[14] = switchState.plus_en[0];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x01) == 0x01) // 原状态灯亮
- {
- gLedState = gLedState & (~0x01); // 按键1清除
- gRelayOnOff &= (~0x01); // 继电器关闭
- }
- #endif
- #ifdef ZIGBEE_ZTU_T1_SW
- switchState.step[1] = 0x02; // 按键触发背光灯状态
- if ((gLedState & 0x02) == 0x00) // 原状态灯灭
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- if (switchState.plus_mod[1] == 0x01) // 点动开启
- {
- switchState.plus_en[1] = 0x01;
- switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // write_mileage[1] = switchState.plus_en;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = switchState.plus_en[1];
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- }
- else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- }
- #endif
- }
- break;
- default:
- break;
- }
- }
- }
- }
- /*******************************************************************************
- Function: Keyinit()
- Description: °′?ü?μ3?ê??ˉ
- Calls:
- Called By:
- Input: no
- Output: no
- Return: no
- Others: no
- ********************************************************************************/
- void Keyinit(void)
- {
- int i = 0;
- for (i = 0; i < KEYBUFLENGTH; i++)
- {
- KeyCodeBuf[i].bKey = 0xff; //按键值初始化,避免误动作
- KeyCodeBuf[i].wKeyTime = 0;
- }
- }
- /*******************************************************************************
- Function: sysInit()
- Description: system initialize entry
- Calls:
- Called By: no
- Input: no
- Output: no
- Return: no
- Others: no
- ********************************************************************************/
- void sysInit(void)
- {
- bsp_clock_init(); // 系统快时钟 18.432 div 2 = 9.216Mhz
- gpio_light_init(); // LED初始化
- pwm0_init(); // pwm0
- pwm1_init(); // pwm1
- pwm2_init(); // pwm2
- pwm3_init(); // PWM3
- gpio_key_init(); // 按键IO口初始化
- gpio_control_init(); // 雷达感应,继电器初始化
- tim2_init(); // 1ms定时器,定时器2
- uart0_timer1_init(); // Uart1(9600),采用time1定时器
- zigbee_protocol_init(); // zigbee初始化
-
- uart2_gpio_init();
- uart2_init();
- Keyinit(); ///按键值初始化,避免误动作
- }
- /*******************************************************************************
- Function: taskInit()
- Description: system Parameter Initialization
- Calls:
- Called By: no
- Input: no
- Output: no
- Return: no
- Others: no
- ********************************************************************************/
- void taskInit(void)
- {
- u08 i = 0;
- sys10msFlag = 0; /* 10ms*/
- // sTimeout(&SystemTimer, 0);
- // 全局初始化参数
- switchState.human = 0; // 默认关闭雷达,按键背光常亮
- for (i = 0; i < 3; i++)
- {
- switchState.step[i] = 0x01;
- switchState.relay_status[i] = 0x00;
- switchState.relay_new_status[i] = 0x00;
- switchState.mod[i] = 0x00; // 默认3路开关,//0:开关,1:场景
- switchState.plus_en[i] = 0x00; // 默认无点动触发
- switchState.plus_mod[i] = 0xff; // 默认未设置点动开关,测试点动开启
- switchState.plus_auto_time[i] = 0xffff; // 默认ffff
- }
- // switchState.relay_status[0] = 0x00;//测试继电器断电保存状态,默认关
- // switchState.relay_status[1] = 0x01;//测试继电器断电保存状态,默认开
- // switchState.relay_status[2] = 0x02;//测试继电器断电保存状态,默认与开机前一致
- switchState.step[3] = 0x01;
- switchState.step[4] = 0x01;
- switchState.step[5] = 0x01;
- switchState.led_level = PWMLEVEL1; // 默认最低亮度
- // switchState.gRadarCheckNull = 0; // 默认雷达有人,按键背光亮起
- // 全局初始化参数
- appControlLedEnable = 1; // 默认APP控制继电器动作联动背光亮10秒
- ZG_Joining = CONDITION_JOINGING_TO_NORMAL; // ZIGBEE正常模式状态
- switchState.zigbee_work_state = 0; // 默认ZIGBEE脱网运行
- EA = 1; // 总中断允许
- ledSetParam(0, 0); // 背光不闪
- pwm_set_prd(255); // PWM 周期设定
- /*
- 参数数据(10):0-9:人感1字节-0,开机状态(通电勿扰)全局设置1字节-1,三个继电器开机状态3个字节-2-3-4,背光等级1个字节-5
- 实时数据(6):10:19:当前继电器状态3个字节-10-11-12,雷达状态1字节-13,点动倒计时未结束1个字节-14-15-16,ota状态1字节-19(最后一字节)
- 点动开关参数(9):,电动开关3个字节(使能1,时间2)-20-21-22,-23-24-25,-26-27-28
- */
- iap_eeprom_read(0, read_mileage, 30); // 读取30字节,状态参数
- iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节,配置参数
- /***************配置参数******************************/
- /*通电反应 通电勿扰开启 通过app操作使开关关闭的情况下,设备重新上电,设备处于关闭状态关闭 断电记忆 恢复到断电前使用的状态
- 开关上电状态设置 枚举范围:off,on,memory
- 通电勿扰开启 = off==0,关闭=memory==2,1:强制开机
- */
- if (read_mileage_backup[1] != 0xff)
- switchState.relay_allstatus = read_mileage_backup[1]; // 开关上电状态设置,全局
- if (read_mileage_backup[2] != 0xff)
- switchState.relay_status[0] = read_mileage_backup[2]; // 开关上电状态设置,1-3键
- if (read_mileage_backup[3] != 0xff)
- switchState.relay_status[1] = read_mileage_backup[3];
- if (read_mileage_backup[4] != 0xff)
- switchState.relay_status[2] = read_mileage_backup[4];
- if (read_mileage_backup[5] != 0xff)
- switchState.led_level = read_mileage_backup[5]; // 指示灯亮度
- if (read_mileage_backup[6] != 0xff)
- switchState.mod[0] = read_mileage_backup[6]; //// 开关、场景设置
- if (read_mileage_backup[7] != 0xff)
- switchState.mod[1] = read_mileage_backup[7]; //// 开关、场景设置
- if (read_mileage_backup[8] != 0xff)
- switchState.mod[2] = read_mileage_backup[8]; //// 开关、场景设置
- if (read_mileage_backup[9] != 0xff) //
- {
- switchState.human = read_mileage_backup[9]; // 人感使能开关
- }
- if (read_mileage_backup[20] != 0xff)
- switchState.plus_mod[0] = read_mileage_backup[20]; // 点动开关被配置
- if (read_mileage_backup[23] != 0xff)
- switchState.plus_mod[1] = read_mileage_backup[23]; // 点动开关被配置
- if (read_mileage_backup[26] != 0xff)
- switchState.plus_mod[2] = read_mileage_backup[26]; // 点动开关被配置
- if (read_mileage_backup[21] != 0xff)
- {
- switchState.plus_auto_time[0] = (read_mileage_backup[21] * 256 + read_mileage_backup[22]) * 10; // 高字节在前,秒转100ms为基数
- }
- else
- {
- switchState.plus_auto_time[0] = 50; // 默认5S,可控制门禁
- }
- if (read_mileage_backup[24] != 0xff)
- {
- switchState.plus_auto_time[1] = (read_mileage_backup[24] * 256 + read_mileage_backup[25]) * 10; // 高字节在前,秒转100ms为基数
- }
- else
- {
- switchState.plus_auto_time[1] = 50; // 默认5S,可控制门禁
- }
- if (read_mileage_backup[27] != 0xff)
- {
- switchState.plus_auto_time[2] = (read_mileage_backup[27] * 256 + read_mileage_backup[28]) * 10; // 高字节在前,秒转100ms为基数
- }
- else
- {
- switchState.plus_auto_time[2] = 50; // 默认5S,可控制门禁
- }
- /***************配置参数******************************/
- /***************关机前实时状态数据******************************/
- if (read_mileage[10] != 0xff)
- switchState.relay_new_status[0] = read_mileage[10]; // 关机前最新继电器状态,1-3键
- if (read_mileage[11] != 0xff)
- switchState.relay_new_status[1] = read_mileage[11];
- if (read_mileage[12] != 0xff)
- switchState.relay_new_status[2] = read_mileage[12];
- // 取消断电前的人感状态读取
- /*if (read_mileage[13] != 0xff)
- switchState.gRadarCheckNull = read_mileage[13]; // 断电前人感状态,1为无人*/
- if (read_mileage[14] != 0xff) // 取消该部分逻辑,根据APP设置的上电状态来启动点动
- switchState.plus_en[0] = read_mileage[14]; // 断电前点动开关未结束
- if (read_mileage[15] != 0xff)
- switchState.plus_en[1] = read_mileage[15]; // 断电前点动开关未结束
- if (read_mileage[16] != 0xff)
- switchState.plus_en[2] = read_mileage[16]; // 断电前点动开关未结束
- /***************关机前实时状态数据******************************/
- // if (switchState.plus_mod[0] == 0x01) // 1号通道被设置点动开关
- // {
- // if (switchState.relay_status[0] == 0x01) // 通电启动,其他设置不启动
- // {
- // switchState.plus_en[0] = 0x01;
- // switchState.plus_counter[0] = switchState.plus_auto_time[0];
- // gLedState = gLedState | 0x01; // 按键1开启
- // gRelayOnOff |= 0x01; // 继电器动作开启
- // }
- // else
- // {
- // switchState.plus_en[0] = 0x00; // 关闭
- // switchState.plus_counter[0] = 0;
- // gLedState = gLedState & (~0x01); // 按键1清除
- // gRelayOnOff &= (~0x01); // 继电器关闭
- // gRelayState |= 0x01;
- // }
- // }
- // else // 非点动,按照上电配置状态控制逻辑
- // {
- if ((switchState.mod[0] == 0x00) || (switchState.mod[1] == 0x00) || (switchState.mod[2] == 0x00))// 设置为开关时有效
- {
- // key1
- // if (switchState.relay_status[0] == 0x00) // 断电
- // {
- // gLedState = gLedState & (~0x01); // 按键1清除
- // gRelayOnOff &= (~0x01); // 继电器关闭
- // gRelayState |= 0x01;
- // }
- // else if (switchState.relay_status[0] == 0x01) // 通电
- // {
- // gLedState = gLedState | 0x01; // 按键1开启
- // gRelayOnOff |= 0x01; // 继电器动作开启
- // }
- // else // 断电记忆
- // {
- #ifdef ZIGBEE_ZTU_T3_SW
- if (switchState.relay_status[0] == 0x00) // 断电
- {
- gLedState = gLedState & (~0x04); // 按键1清除
- gRelayOnOff &= (~0x04); // 继电器关闭
- gRelayState |= 0x04;
- }
- else if (switchState.relay_status[0] == 0x01) // 通电
- {
- gLedState = gLedState | 0x04; // 按键1开启
- gRelayOnOff |= 0x04; // 继电器动作开启
- }
- else // 断电记忆
- {
- if (switchState.relay_new_status[2] == 0x01)
- {
- gLedState = gLedState | 0x04; // 按键1开启
- gRelayOnOff |= 0x04; // 继电器动作开启
- }
- else
- {
- gLedState = gLedState & (~0x04); // 按键1清除
- gRelayOnOff &= (~0x04); // 继电器关闭
- gRelayState |= 0x04;
- }
- }
- #endif
- #ifdef ZIGBEE_ZTU_T2_SW
- if (switchState.relay_status[0] == 0x00) // 断电
- {
- gLedState = gLedState & (~0x04); // 按键1清除
- gRelayOnOff &= (~0x04); // 继电器关闭
- gRelayState |= 0x04;
- }
- else if (switchState.relay_status[0] == 0x01) // 通电
- {
- gLedState = gLedState | 0x04; // 按键1开启
- gRelayOnOff |= 0x04; // 继电器动作开启
- }
- else // 断电记忆
- {
- if (switchState.relay_new_status[1] == 0x01)
- {
- gLedState = gLedState | 0x04; // 按键1开启
- gRelayOnOff |= 0x04; // 继电器动作开启
- }
- else
- {
- gLedState = gLedState & (~0x04); // 按键1清除
- gRelayOnOff &= (~0x04); // 继电器关闭
- gRelayState |= 0x04;
- }
- }
- #endif
- // }
- }
- // }
- // if (switchState.plus_mod[1] == 0x01) // 2号通道被设置点动开关
- // {
- // if (switchState.relay_status[1] == 0x01) // 通电
- // {
- // switchState.plus_en[1] = 0x01;
- // switchState.plus_counter[1] = switchState.plus_auto_time[1];
- // gLedState = gLedState | 0x02; // // 按键2开启
- // gRelayOnOff |= 0x02; // 继电器动作开启
- // }
- // else
- // {
- // switchState.plus_en[1] = 0x00;
- // switchState.plus_counter[1] = 0;
- // gLedState = gLedState & (~0x02); // 按键2清除
- // gRelayOnOff &= (~0x02); // 继电器关闭
- // gRelayState |= 0x02;
- // }
- // }
- // else // 非点动,按照上电配置状态控制逻辑
- // {
- if ((switchState.mod[0] == 0x00) || (switchState.mod[1] == 0x00) || (switchState.mod[2] == 0x00))// 设置为开关时有效
- {
- // key2
- // if (switchState.relay_status[1] == 0x00) // 断电
- // {
- // gLedState = gLedState & (~0x02); // 按键2清除
- // gRelayOnOff &= (~0x02); // 继电器关闭
- // gRelayState |= 0x02;
- // }
- // else if (switchState.relay_status[1] == 0x01) // 通电
- // {
- // gLedState = gLedState | 0x02; // // 按键2开启
- // gRelayOnOff |= 0x02; // 继电器动作开启
- // }
- // else // 断电记忆
- // {
- #ifdef ZIGBEE_ZTU_T3_SW
- if (switchState.relay_status[1] == 0x00) // 断电
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- gRelayState |= 0x02;
- }
- else if (switchState.relay_status[1] == 0x01) // 通电
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- }
- else // 断电记忆
- {
- if (switchState.relay_new_status[1] == 0x01)
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- }
- else
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- gRelayState |= 0x02;
- }
- }
- #endif
- #ifdef ZIGBEE_ZTU_T1_SW
- if (switchState.relay_status[0] == 0x00) // 断电
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- gRelayState |= 0x02;
- }
- else if (switchState.relay_status[0] == 0x01) // 通电
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- }
- else // 断电记忆
- {
- if (switchState.relay_new_status[2] == 0x01)
- {
- gLedState = gLedState | 0x02; // // 按键2开启
- gRelayOnOff |= 0x02; // 继电器动作开启
- }
- else
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- gRelayState |= 0x02;
- }
- }
- #endif
- // }
- }
- // }
- // if (switchState.plus_mod[2] == 0x01) // 3号通道被设置点动开关
- // {
- // if (switchState.relay_status[2] == 0x01) // 通电
- // {
- // switchState.plus_en[2] = 0x01;
- // switchState.plus_counter[2] = switchState.plus_auto_time[2];
- // gLedState = gLedState | 0x04; // // 按键3开启
- // gRelayOnOff |= 0x04; // 继电器动作开启
- // }
- // else
- // {
- // switchState.plus_en[2] = 0x00;
- // switchState.plus_counter[2] = 0;
- // gLedState = gLedState & (~0x04); // // 按键3清除
- // gRelayOnOff &= (~0x04); // 继电器关闭
- // gRelayState |= 0x04;
- // }
- // }
- // else // 非点动,按照上电配置状态控制逻辑
- // {
- if ((switchState.mod[0] == 0x00) || (switchState.mod[1] == 0x00) || (switchState.mod[2] == 0x00))// 设置为开关时有效
- {
- // key3
- #ifdef ZIGBEE_ZTU_T2_SW
- if (switchState.relay_status[1] == 0x00) // 断电
- {
- gLedState = gLedState & (~0x01); // // 按键3清除
- gRelayOnOff &= (~0x01); // 继电器关闭
- gRelayState |= 0x01;
- }
- else if (switchState.relay_status[1] == 0x01) // 通电
- {
- gLedState = gLedState | 0x01; // // 按键3开启
- gRelayOnOff |= 0x01; // 继电器动作开启
- }
- else // 断电记忆
- {
- if (switchState.relay_new_status[2] == 0x01)
- {
- gLedState = gLedState | 0x01; // // 按键3开启
- gRelayOnOff |= 0x01; // 继电器动作开启
- }
- else
- {
- gLedState = gLedState & (~0x01); // // 按键3清除
- gRelayOnOff &= (~0x01); // 继电器关闭
- gRelayState |= 0x01;
- }
- }
- #endif
- #ifdef ZIGBEE_ZTU_T3_SW
- if (switchState.relay_status[2] == 0x00) // 断电
- {
- gLedState = gLedState & (~0x01); // // 按键3清除
- gRelayOnOff &= (~0x01); // 继电器关闭
- gRelayState |= 0x01;
- }
- else if (switchState.relay_status[2] == 0x01) // 通电
- {
- gLedState = gLedState | 0x01; // // 按键3开启
- gRelayOnOff |= 0x01; // 继电器动作开启
- }
- else // 断电记忆
- {
- if (switchState.relay_new_status[0] == 0x01)
- {
- gLedState = gLedState | 0x01; // // 按键3开启
- gRelayOnOff |= 0x01; // 继电器动作开启
- }
- else
- {
- gLedState = gLedState & (~0x01); // // 按键3清除
- gRelayOnOff &= (~0x01); // 继电器关闭
- gRelayState |= 0x01;
- }
- }
- #endif
- }
- // }
-
-
- // 取消断电前人感状态读取及应用
- /*if (switchState.gRadarCheckNull == 1) // 断电前无人
- {
- gRadarCheckNull = 1;
- gRadarKeepTimer = 0; // 雷达无人,按键灯熄灭
- }*/
- if (switchState.human) // 开启雷达的情况下有效
- {
- gRadarCheckNull = 1;
- gRadarKeepTimer = 0; // 雷达无人,按键灯熄灭
- }
- sTimeout(&SystemTimer, 0);
- #ifdef REALSE // 生产模式,开启看门狗
- WDGT_Init();
- #endif
- }
- /**********************************************************************************************************
- **函数名称 :main()
- **函数描述 :主函数
- **输 入 :None
- **输 出 :None
- **********************************************************************************************************/
- void main(void)
- {
- sysInit(); // 系统初始化
- taskInit(); // 初始化任务
- while (1)
- {
- zigbee_uart_service(); // zigbee接收处理
- //增加循环读取电量数据 add by qys hlw
-
- ///// 系统时钟控制
- if (sys10msFlag) // 10ms任务
- {
- sys10msFlag = 0;
- Scankey(); // 按键扫描处理任务
- // button1LongPressCheck(); // 长按键处理任务
- relay_plus_ctl(); // 磁保持继电器控制任务
- if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 正常模式下按键灯控制任务
- {
- // 脱网指示灯闪烁
- if (switchState.zigbee_work_state == 0x02) // 脱网报警(离线状态) 慢闪60s 3s一闪烁
- {
- if (--switchState.time)
- {
- ledCtrlProc(); // 脱网闪烁处理
- }
- else
- {
- ledSetParam(0, 0);
- switchState.zigbee_work_state = 0; // 离线工作,指示灯恢复正常
- }
- }
- else
- {
- key_led_pwm_ctl(); // 按键灯PWM控制任务
- }
- }
- /*****************zigbee任务--10ms******************/
- if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) // zigbee配网前准备
- {
- if (++ZG_JoinCounter <= 1000) // 配网前确认闪烁10秒
- {
- ledCtrlProc(); // 按键led控制闪烁
- }
- else
- {
- ZG_Joining = CONDITION_JOINGING_TO_NORMAL; // 超过10秒未确认,从配网前恢复正常状态
- ledSetParam(0, 0);
- }
- }
- else if (ZG_Joining == CONDITION_BEFOR_JOINING_TO_JOINING) // 已进入配网状态
- {
- if (++ZG_JoinCounter <= 18000) // 配网快速闪烁180秒
- {
- ledCtrlProc(); ////按键led控制闪烁
- if (ZG_JoinCounter == 0x05) // 进入配网模式,5秒后发送配网指令
- {
- // if (switchState.zigbee_work_state != 0x01) // 已入网不发送指令
- //{
- mcu_join_zigbee(); // 只要触发入网必须发送入网指令,满足从已入网主机1切换到新主机2的切换
- //}
- }
- else if (ZG_JoinCounter % 900 == 0x00) // 过90秒再次发送一次入网-取消,01配网的逻辑是先离线在入网,不可二次发送
- {
- // mcu_join_zigbee();//每90秒再次发送入网指令
- }
- else
- {
- }
- }
- else
- {
- ZG_Joining = CONDITION_JOINGING_TO_NORMAL; ////超过180秒,从配网前恢复正常状态
- ledSetParam(0, 0);
- }
- }
- else
- {
- ZG_Joining = CONDITION_JOINGING_TO_NORMAL; // 正常状态
- }
- /*****************zigbee任务--10ms******************/
- if (++sysTickfor100ms >= 10) // 100ms
- {
- sysTickfor100ms = 0;
- /**********************10ms喂狗任务************************/
- #ifdef REALSE // 生产模式
- WDGT_Feed(); // 定时喂狗
- #endif
- /**********************10ms喂狗任务************************/
- /*****************人感任务--100ms******************/
- if (switchState.human) // 人感使能
- {
- if (RADAR_READ()) // 读取人感状态,高电平有效
- {
- gRadarCheckNull = 0; // 有人检测到
- gRadarKeepTimer = 100; // 12s//传感器自带2S+软件倒计时10S
- }
- else // 下降沿清空有人标志
- {
- gRadarCheckNull = 1;
- }
- if (gRadarKeepTimer)
- {
- gRadarKeepTimer--;
- // 取消人感状态变化存入eeprom
- /*if (gRadarKeepTimer == 1) // 等于1时,触发存eeprom
- {
- switchState.gRadarCheckNull = 1; // 清空有人标志
- // write_mileage[0] = switchState.gRadarCheckNull;
- // iap_eeprom_write(13, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[13] = switchState.gRadarCheckNull;
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- else if (gRadarKeepTimer == 90) // 等于90时,表示刚触发了有人,存eeprom
- {
- switchState.gRadarCheckNull = 0; // 设置有人标志
- // write_mileage[0] = switchState.gRadarCheckNull;
- // iap_eeprom_write(13, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[13] = switchState.gRadarCheckNull;
- iap_eeprom_write(0, write_mileage, 30); // 写入1字节
- }
- else
- {
- }*/
- }
- }
- else // 无人感,按键指示灯常亮
- {
- gRadarKeepTimer = 100; // 10s ,无人感设备一致默认有人
- }
- /*****************人感任务--100ms******************/
- /*****************点动开关任务-100ms*******************/
- if ((switchState.plus_en[0] == 0x01) && (switchState.plus_mod[0] == 0x01)) // 点动开关触发
- {
- if (switchState.plus_counter[0] == 0x00)
- {
- gLedState = gLedState & (~0x01); // 按键1清除
- gRelayOnOff &= (~0x01); // 继电器关闭
- // write_mileage[0] = 0;
- // iap_eeprom_write(14, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[14] = 0;
- iap_eeprom_write(0, write_mileage, 30); // 先读,在写入1字节
- switchState.plus_en[0] = 0;
- }
- switchState.plus_counter[0]--;
- }
- if ((switchState.plus_en[1] == 0x01) && (switchState.plus_mod[1] == 0x01)) // 点动开关触发
- {
- if (switchState.plus_counter[1] == 0x00)
- {
- gLedState = gLedState & (~0x02); // 按键2清除
- gRelayOnOff &= (~0x02); // 继电器关闭
- // write_mileage[0] = 0;
- // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[15] = 0;
- iap_eeprom_write(0, write_mileage, 30); // 先读,在写入1字节
- switchState.plus_en[1] = 0;
- }
- switchState.plus_counter[1]--;
- }
- if ((switchState.plus_en[2] == 0x01) && (switchState.plus_mod[2] == 0x01)) // 点动开关触发
- {
- if (switchState.plus_counter[2] == 0x00)
- {
- gLedState = gLedState & (~0x04); // // 按键3清除
- gRelayOnOff &= (~0x04); // 继电器关闭
- // write_mileage[0] = 0;
- // iap_eeprom_write(16, write_mileage, 1); // 写入1字节
- iap_eeprom_read(0, write_mileage, 30); // 读取30字节
- write_mileage[16] = 0;
- iap_eeprom_write(0, write_mileage, 30); // 先读,在写入1字节
- switchState.plus_en[2] = 0;
- }
- switchState.plus_counter[2]--;
- }
- /**********************点动开关任务************************/
- Read_Power_Data_handle(); //5s定时获取一次电量数据
- if (++sysTickfor1000ms >= 10) // 1s任务
- {
- sysTickfor1000ms = 0;
- //Start_Send_UartData(2);
- Save_data_nun++; //电量数据备份存储计数器
- }
- }
- }
- }
- }
- /**********************************************************************************************************
- **函数名称 :relay_ctl()
- **函数描述 :继电器控制任务,10ms调用,普通继电器,电平控制
- **输 入 :None
- **输 出 :None
- **********************************************************************************************************/
- void relay_ctl(void)
- {
- /*if (gRelayState != gRelayOnOff)
- {
- //继电器1
- if (gRelayOnOff & 0x01) // 继电器1开启
- {
- relay1_control(1);
- gRelayState |= 0x01;
- mcu_dp_bool_update(DPID_SWITCH_1, 1);
- }
- else if ((gRelayOnOff & 0x01) == 0x00) // 继电器1关闭
- {
- relay1_control(0);
- gRelayState &= (~0x01);
- mcu_dp_bool_update(DPID_SWITCH_1, 0);
- }
- //继电器2
- if (gRelayOnOff & 0x02) // 继电器2开启
- {
- relay2_control(1);
- gRelayState |= 0x02;
- mcu_dp_bool_update(DPID_SWITCH_2, 1);
- }
- else if ((gRelayOnOff & 0x02) == 0x00) // 继电器3关闭
- {
- relay2_control(0);
- gRelayState &= (~0x02);
- mcu_dp_bool_update(DPID_SWITCH_2, 0);
- }
- //继电器3
- if (gRelayOnOff & 0x04) // 继电器3开启
- {
- relay3_control(1);
- gRelayState |= 0x04;
- mcu_dp_bool_update(DPID_SWITCH_3, 1);
- }
- else if ((gRelayOnOff & 0x04) == 0x00) // 继电器3关闭
- {
- relay3_control(0);
- gRelayState &= (~0x04);
- mcu_dp_bool_update(DPID_SWITCH_3, 0);
- }
- }
- // add by zzw all_data_update APP状态同步
- if ((gLedState & 0x01) == 0x01)
- {
- switchState.SWITCH[0] = 1;
- }
- else
- {
- switchState.SWITCH[0] = 0;
- }
- if ((gLedState & 0x02) == 0x02)
- {
- switchState.SWITCH[1] = 1;
- }
- else
- {
- switchState.SWITCH[1] = 0;
- }
- if ((gLedState & 0x04) == 0x04) //
- {
- switchState.SWITCH[2] = 1;
- }
- else
- {
- switchState.SWITCH[2] = 0;
- }*/
- }
- /******************************************************************************************
- ** 函数名称: wdg_init
- ** 函数描述: 看门狗初始化函数
- ** 输入参数: 无
- ** 输出参数: 无
- *******************************************************************************************/
- void WDGT_Init(void)
- {
- SET_REG_BITS(OPTION, WDTPSC, 0); // 240MS
- SET_REG_BITS(AUX2, WDTE, 2); // 看门狗在快钟和慢钟模式下使能,空闲/停止/暂停模式下禁止
- SET_REG_BITS(AUX1, CLRWDT, 0);
- CLR_WDT; // 设置以清除看门狗定时器
- }
- /*******************************************************************************
- Function: WDGT_Feed()
- Description: 喂狗
- Calls:
- Called By: no
- Input: no
- Output: no
- Return: no
- Others: no
- ********************************************************************************/
- void WDGT_Feed(void)
- {
- CLRWDT = 1; // 喂狗
- }
|