main.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. /*******************************************************************************
  2. *
  3. * sw
  4. * Soft version: switch_v1.0
  5. * File Name: main.c
  6. * Author : zzw (zhangzw_3@163.com)
  7. * creation date: 2023-01-20
  8. * module description:
  9. * Copyright (C) reserve
  10. *
  11. *
  12. ********************************************************************************/
  13. #include <REGtenxTM52F1386.h>
  14. #include "TM52F1386_bsp.h"
  15. #include <intrins.h>
  16. #include "GLOBAL.h"
  17. #include "time2.h"
  18. #include "sTimeout.h"
  19. #include "uart.h"
  20. #include "gpio_ctrl.h"
  21. #include "zigbee.h"
  22. #include "EEPROM.h"
  23. #include "HLW8110.h"
  24. #include "uart2.h"
  25. #define NETWORK_CONFIGURE_TRIGGER_TIME 500 // 长按统计,10ms为单位,设置5秒
  26. void relay_ctl(void); // 普通继电器控制
  27. void WDGT_Init(void); // 看门口初始化
  28. void WDGT_Feed(void); // 喂狗
  29. static byte bKey;
  30. static byte bKeyl;
  31. static word wKeyTime;
  32. static byte readkey;
  33. static byte readkey1;
  34. /**********************************************************************************************************
  35. **函数名称 :button1LongPressCheck()
  36. **函数描述 :长按键扫描处理任务,10ms调用
  37. **输 入 :None
  38. **输 出 :None
  39. **********************************************************************************************************/
  40. void button1LongPressCheck(void)
  41. {
  42. /*if (KEY1_READ() == 0) // 按键1被按下
  43. {
  44. if (switchState.KeyPress == 0)
  45. { // 原来没按下
  46. switchState.KeyPress = 1; // 更新按键状态为按下
  47. switchState.buttonPressTime = 0; // 按下计数器清理
  48. }
  49. else // 长按统计
  50. { // 长按5s 触发入网启动
  51. if (switchState.buttonPressTime >= NETWORK_CONFIGURE_TRIGGER_TIME) // 长按超过5s
  52. {
  53. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
  54. {
  55. ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
  56. ZG_JoinCounter = 0;
  57. switchState.buttonPressTime = 0;
  58. ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
  59. }
  60. // switchState.KeyPress = 0; // 按键释放
  61. if (ZG_Joining == CONDITION_BEFOR_JOINING_TO_JOINING) // 入网倒计时180秒内再次长按退出配网
  62. {
  63. ZG_JoinCounter = 18000; //
  64. switchState.buttonPressTime = 0;
  65. }
  66. }
  67. }
  68. }
  69. else
  70. {
  71. switchState.KeyPress = 0; // 按键释放
  72. }*/
  73. }
  74. /**********************************************************************************************************
  75. **函数名称 :Scankey()
  76. **函数描述 :键盘扫描,10ms调用
  77. **输 入 :None
  78. **输 出 :None
  79. **********************************************************************************************************/
  80. void Scankey(void)
  81. {
  82. char i = 0;
  83. ScanKeyn(); // 按键值扫码任务
  84. bKey = GetKey(&wKeyTime); // 读取键值与长按时间
  85. if (bKey != NO_KEY_MSG)
  86. {
  87. if ((bKey & LONG_KEY_MASK) == LONG_KEY_MASK) // 长按键处理
  88. {
  89. bKeyl = bKey & SHORT_KEY_MASK;
  90. switch (bKeyl)
  91. {
  92. case KEY1: // 长按1号键
  93. if (wKeyTime >= 5000) // 5S
  94. {
  95. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
  96. {
  97. ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
  98. ZG_JoinCounter = 0;
  99. ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
  100. }
  101. }
  102. break;
  103. case KEY2: // 长按2号键
  104. if (wKeyTime >= 5000) // 5S
  105. {
  106. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
  107. {
  108. ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
  109. ZG_JoinCounter = 0;
  110. ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
  111. }
  112. }
  113. break;
  114. case KEY3: // 长按3号键
  115. if (wKeyTime >= 5000) // 5S
  116. {
  117. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
  118. {
  119. ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
  120. ZG_JoinCounter = 0;
  121. ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
  122. }
  123. }
  124. break;
  125. case KEY4: // 长按4号键
  126. if (wKeyTime >= 5000) // 5S
  127. {
  128. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
  129. {
  130. ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
  131. ZG_JoinCounter = 0;
  132. ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
  133. }
  134. }
  135. break;
  136. case KEY5: // 长按5号键
  137. if (wKeyTime >= 5000) // 5S
  138. {
  139. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
  140. {
  141. ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
  142. ZG_JoinCounter = 0;
  143. ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
  144. }
  145. }
  146. break;
  147. case KEY6: // 长按6号键
  148. if (wKeyTime >= 5000) // 5S
  149. {
  150. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 设备入网状态,长按后触发
  151. {
  152. ZG_Joining = CONDITION_NORMAL_TO_BEFOR_JOINING; // 配网前状态,待短按确认
  153. ZG_JoinCounter = 0;
  154. ledSetParam(50, 100); // 1S一次,所有指示灯1Hz闪烁(表现为三位开关三个指示灯都闪烁)
  155. }
  156. }
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. else // 短按键
  163. {
  164. switch (bKey & SHORT_KEY_MASK) // 短按键处理
  165. {
  166. case KEY1:
  167. if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
  168. {
  169. // mcu_join_zigbee(); // 设备入网函数
  170. ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
  171. ZG_JoinCounter = 0;
  172. ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
  173. }
  174. else
  175. {
  176. // switchState.KeyIn[0] = 0x01; // 按键被按下
  177. switchState.step[0] = 0x02; // 按键触发背光灯状态
  178. if (switchState.mod[0] == 0x01) // 场景开关
  179. {
  180. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  181. mcu_dp_enum_update(DPID_SCENE_1, 0); // 上报场景值
  182. #endif
  183. }
  184. else
  185. {
  186. #ifdef ZIGBEE_ZTU_T1_SW
  187. switchState.step[1] = 0x02; // 按键触发背光灯状态
  188. if ((gLedState & 0x02) == 0x00) // 原状态灯灭
  189. {
  190. gLedState = gLedState | 0x02; // // 按键2开启
  191. gRelayOnOff |= 0x02; // 继电器动作开启
  192. if (switchState.plus_mod[1] == 0x01) // 点动开启
  193. {
  194. switchState.plus_en[1] = 0x01;
  195. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  196. // write_mileage[1] = switchState.plus_en;
  197. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  198. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  199. write_mileage[15] = switchState.plus_en[1];
  200. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  201. }
  202. }
  203. else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
  204. {
  205. gLedState = gLedState & (~0x02); // 按键2清除
  206. gRelayOnOff &= (~0x02); // 继电器关闭
  207. }
  208. #else
  209. if ((gLedState & 0x01) == 0x00) // 原状态灯灭
  210. {
  211. gLedState = gLedState | 0x01; // 按键1开启
  212. gRelayOnOff |= 0x01; // 继电器动作开启
  213. if (switchState.plus_mod[0] == 0x01) // 点动开启
  214. {
  215. switchState.plus_en[0] = 0x01;
  216. switchState.plus_counter[0] = switchState.plus_auto_time[0];
  217. // write_mileage[0] = switchState.plus_en;
  218. // iap_eeprom_write(14, write_mileage, 1); // 写入1字节
  219. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  220. write_mileage[14] = switchState.plus_en[0];
  221. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  222. }
  223. }
  224. else if ((gLedState & 0x01) == 0x01) // 原状态灯亮
  225. {
  226. gLedState = gLedState & (~0x01); // 按键1清除
  227. gRelayOnOff &= (~0x01); // 继电器关闭
  228. }
  229. #endif
  230. }
  231. }
  232. break;
  233. case KEY2:
  234. if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
  235. {
  236. // mcu_join_zigbee(); // 设备入网函数
  237. ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
  238. ZG_JoinCounter = 0;
  239. ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
  240. }
  241. else
  242. {
  243. // switchState.KeyIn[1] = 0x01; // 按键被按下
  244. switchState.step[1] = 0x02; // 按键触发背光灯状态
  245. if (switchState.mod[1] == 0x01)
  246. {
  247. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  248. mcu_dp_enum_update(DPID_SCENE_2, 0);
  249. #endif
  250. }
  251. else
  252. {
  253. #ifdef ZIGBEE_ZTU_T1_SW
  254. switchState.step[1] = 0x02; // 按键触发背光灯状态
  255. if ((gLedState & 0x02) == 0x00) // 原状态灯灭
  256. {
  257. gLedState = gLedState | 0x02; // // 按键2开启
  258. gRelayOnOff |= 0x02; // 继电器动作开启
  259. if (switchState.plus_mod[1] == 0x01) // 点动开启
  260. {
  261. switchState.plus_en[1] = 0x01;
  262. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  263. // write_mileage[1] = switchState.plus_en;
  264. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  265. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  266. write_mileage[15] = switchState.plus_en[1];
  267. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  268. }
  269. }
  270. else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
  271. {
  272. gLedState = gLedState & (~0x02); // 按键2清除
  273. gRelayOnOff &= (~0x02); // 继电器关闭
  274. }
  275. #else
  276. if ((gLedState & 0x02) == 0x00) // 原状态灯灭
  277. {
  278. gLedState = gLedState | 0x02; // // 按键2开启
  279. gRelayOnOff |= 0x02; // 继电器动作开启
  280. if (switchState.plus_mod[1] == 0x01) // 点动开启
  281. {
  282. switchState.plus_en[1] = 0x01;
  283. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  284. // write_mileage[1] = switchState.plus_en;
  285. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  286. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  287. write_mileage[15] = switchState.plus_en[1];
  288. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  289. }
  290. }
  291. else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
  292. {
  293. gLedState = gLedState & (~0x02); // 按键2清除
  294. gRelayOnOff &= (~0x02); // 继电器关闭
  295. }
  296. #endif
  297. }
  298. }
  299. break;
  300. case KEY3:
  301. if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
  302. {
  303. // mcu_join_zigbee(); // 设备入网函数
  304. ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
  305. ZG_JoinCounter = 0;
  306. ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
  307. }
  308. else
  309. {
  310. // switchState.KeyIn[2] = 0x01; // 按键被按下
  311. switchState.step[2] = 0x02; // 按键触发背光灯状态
  312. if (switchState.mod[2] == 0x01)
  313. {
  314. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  315. mcu_dp_enum_update(DPID_SCENE_3, 0);
  316. #endif
  317. }
  318. else
  319. {
  320. #ifdef ZIGBEE_ZTU_T1_SW
  321. switchState.step[1] = 0x02; // 按键触发背光灯状态
  322. if ((gLedState & 0x02) == 0x00) // 原状态灯灭
  323. {
  324. gLedState = gLedState | 0x02; // // 按键2开启
  325. gRelayOnOff |= 0x02; // 继电器动作开启
  326. if (switchState.plus_mod[1] == 0x01) // 点动开启
  327. {
  328. switchState.plus_en[1] = 0x01;
  329. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  330. // write_mileage[1] = switchState.plus_en;
  331. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  332. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  333. write_mileage[15] = switchState.plus_en[1];
  334. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  335. }
  336. }
  337. else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
  338. {
  339. gLedState = gLedState & (~0x02); // 按键2清除
  340. gRelayOnOff &= (~0x02); // 继电器关闭
  341. }
  342. #else
  343. if ((gLedState & 0x04) == 0x00) // 原状态灯灭
  344. {
  345. gLedState = gLedState | 0x04; // // 按键3开启
  346. gRelayOnOff |= 0x04; // 继电器动作开启
  347. if (switchState.plus_mod[2] == 0x01) // 点动开启
  348. {
  349. switchState.plus_en[2] = 0x01;
  350. switchState.plus_counter[2] = switchState.plus_auto_time[2];
  351. // write_mileage[2] = switchState.plus_en;
  352. // iap_eeprom_write(16, write_mileage, 1); // 写入1字节
  353. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  354. write_mileage[16] = switchState.plus_en[2];
  355. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  356. }
  357. }
  358. else if ((gLedState & 0x04) == 0x04) // 原状态灯亮
  359. {
  360. gLedState = gLedState & (~0x04); // // 按键3清除
  361. gRelayOnOff &= (~0x04); // 继电器关闭
  362. }
  363. #endif
  364. }
  365. }
  366. break;
  367. case KEY4:
  368. if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
  369. {
  370. // mcu_join_zigbee(); // 设备入网函数
  371. ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
  372. ZG_JoinCounter = 0;
  373. ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
  374. }
  375. else
  376. {
  377. // switchState.KeyIn[3] = 0x01; // 按键被按下
  378. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  379. switchState.step[3] = 0x02; // 按键触发背光灯状态
  380. mcu_dp_enum_update(DPID_SCENE_4, 0);
  381. #endif
  382. #if ((defined ZIGBEE_ZTU_T3_SW) && (defined ZIGBEE_3SWITCH_SW))|| (defined ZIGBEE_ZTU_T2_SW)
  383. switchState.step[2] = 0x02; // 按键触发背光灯状态
  384. if ((gLedState & 0x04) == 0x00) // 原状态灯灭
  385. {
  386. gLedState = gLedState | 0x04; // // 按键3开启
  387. gRelayOnOff |= 0x04; // 继电器动作开启
  388. if (switchState.plus_mod[2] == 0x01) // 点动开启
  389. {
  390. switchState.plus_en[2] = 0x01;
  391. switchState.plus_counter[2] = switchState.plus_auto_time[2];
  392. // write_mileage[2] = switchState.plus_en;
  393. // iap_eeprom_write(16, write_mileage, 1); // 写入1字节
  394. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  395. write_mileage[16] = switchState.plus_en[2];
  396. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  397. }
  398. }
  399. else if ((gLedState & 0x04) == 0x04) // 原状态灯亮
  400. {
  401. gLedState = gLedState & (~0x04); // // 按键3清除
  402. gRelayOnOff &= (~0x04); // 继电器关闭
  403. }
  404. #endif
  405. #ifdef ZIGBEE_ZTU_T1_SW
  406. switchState.step[1] = 0x02; // 按键触发背光灯状态
  407. if ((gLedState & 0x02) == 0x00) // 原状态灯灭
  408. {
  409. gLedState = gLedState | 0x02; // // 按键2开启
  410. gRelayOnOff |= 0x02; // 继电器动作开启
  411. if (switchState.plus_mod[1] == 0x01) // 点动开启
  412. {
  413. switchState.plus_en[1] = 0x01;
  414. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  415. // write_mileage[1] = switchState.plus_en;
  416. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  417. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  418. write_mileage[15] = switchState.plus_en[1];
  419. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  420. }
  421. }
  422. else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
  423. {
  424. gLedState = gLedState & (~0x02); // 按键2清除
  425. gRelayOnOff &= (~0x02); // 继电器关闭
  426. }
  427. #endif
  428. }
  429. break;
  430. case KEY5:
  431. if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
  432. {
  433. // mcu_join_zigbee(); // 设备入网函数
  434. ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
  435. ZG_JoinCounter = 0;
  436. ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
  437. }
  438. else
  439. {
  440. // switchState.KeyIn[4] = 0x01; // 按键被按下
  441. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  442. switchState.step[4] = 0x02; // 按键触发背光灯状态
  443. mcu_dp_enum_update(DPID_SCENE_5, 0);
  444. #endif
  445. #if ((defined ZIGBEE_ZTU_T3_SW) && (defined ZIGBEE_3SWITCH_SW))|| (defined ZIGBEE_ZTU_T2_SW)
  446. switchState.step[1] = 0x02; // 按键触发背光灯状态
  447. if ((gLedState & 0x02) == 0x00) // 原状态灯灭
  448. {
  449. gLedState = gLedState | 0x02; // // 按键2开启
  450. gRelayOnOff |= 0x02; // 继电器动作开启
  451. if (switchState.plus_mod[1] == 0x01) // 点动开启
  452. {
  453. switchState.plus_en[1] = 0x01;
  454. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  455. // write_mileage[1] = switchState.plus_en;
  456. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  457. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  458. write_mileage[15] = switchState.plus_en[1];
  459. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  460. }
  461. }
  462. else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
  463. {
  464. gLedState = gLedState & (~0x02); // 按键2清除
  465. gRelayOnOff &= (~0x02); // 继电器关闭
  466. }
  467. #endif
  468. #ifdef ZIGBEE_ZTU_T1_SW
  469. switchState.step[1] = 0x02; // 按键触发背光灯状态
  470. if ((gLedState & 0x02) == 0x00) // 原状态灯灭
  471. {
  472. gLedState = gLedState | 0x02; // // 按键2开启
  473. gRelayOnOff |= 0x02; // 继电器动作开启
  474. if (switchState.plus_mod[1] == 0x01) // 点动开启
  475. {
  476. switchState.plus_en[1] = 0x01;
  477. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  478. // write_mileage[1] = switchState.plus_en;
  479. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  480. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  481. write_mileage[15] = switchState.plus_en[1];
  482. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  483. }
  484. }
  485. else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
  486. {
  487. gLedState = gLedState & (~0x02); // 按键2清除
  488. gRelayOnOff &= (~0x02); // 继电器关闭
  489. }
  490. #endif
  491. }
  492. break;
  493. case KEY6:
  494. if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) //&& (switchState.KeyPress == 0)) // 长按触发了配网,需在10秒操作
  495. {
  496. // mcu_join_zigbee(); // 设备入网函数
  497. ZG_Joining = CONDITION_BEFOR_JOINING_TO_JOINING; // 配网倒计时
  498. ZG_JoinCounter = 0;
  499. ledSetParam(6, 12); // 高速闪烁,所有指示灯8Hz闪烁(表现为三位开关三个指示灯都闪烁)
  500. }
  501. else
  502. {
  503. // switchState.KeyIn[5] = 0x01; // 按键被按下
  504. #ifdef ZIGBEE_ZTU_T6_SCENCE_SW
  505. switchState.step[5] = 0x02; // 按键触发背光灯状态
  506. mcu_dp_enum_update(DPID_SCENE_6, 0);
  507. #endif
  508. #if ((defined ZIGBEE_ZTU_T3_SW) && (defined ZIGBEE_3SWITCH_SW))|| (defined ZIGBEE_ZTU_T2_SW)
  509. switchState.step[0] = 0x02; // 按键触发背光灯状态
  510. if ((gLedState & 0x01) == 0x00) // 原状态灯灭
  511. {
  512. gLedState = gLedState | 0x01; // 按键1开启
  513. gRelayOnOff |= 0x01; // 继电器动作开启
  514. if (switchState.plus_mod[0] == 0x01) // 点动开启
  515. {
  516. switchState.plus_en[0] = 0x01;
  517. switchState.plus_counter[0] = switchState.plus_auto_time[0];
  518. // write_mileage[0] = switchState.plus_en;
  519. // iap_eeprom_write(14, write_mileage, 1); // 写入1字节
  520. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  521. write_mileage[14] = switchState.plus_en[0];
  522. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  523. }
  524. }
  525. else if ((gLedState & 0x01) == 0x01) // 原状态灯亮
  526. {
  527. gLedState = gLedState & (~0x01); // 按键1清除
  528. gRelayOnOff &= (~0x01); // 继电器关闭
  529. }
  530. #endif
  531. #ifdef ZIGBEE_ZTU_T1_SW
  532. switchState.step[1] = 0x02; // 按键触发背光灯状态
  533. if ((gLedState & 0x02) == 0x00) // 原状态灯灭
  534. {
  535. gLedState = gLedState | 0x02; // // 按键2开启
  536. gRelayOnOff |= 0x02; // 继电器动作开启
  537. if (switchState.plus_mod[1] == 0x01) // 点动开启
  538. {
  539. switchState.plus_en[1] = 0x01;
  540. switchState.plus_counter[1] = switchState.plus_auto_time[1];
  541. // write_mileage[1] = switchState.plus_en;
  542. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  543. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  544. write_mileage[15] = switchState.plus_en[1];
  545. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  546. }
  547. }
  548. else if ((gLedState & 0x02) == 0x02) // 原状态灯亮
  549. {
  550. gLedState = gLedState & (~0x02); // 按键2清除
  551. gRelayOnOff &= (~0x02); // 继电器关闭
  552. }
  553. #endif
  554. }
  555. break;
  556. default:
  557. break;
  558. }
  559. }
  560. }
  561. }
  562. /*******************************************************************************
  563. Function: Keyinit()
  564. Description: °′?ü?μ3?ê??ˉ
  565. Calls:
  566. Called By:
  567. Input: no
  568. Output: no
  569. Return: no
  570. Others: no
  571. ********************************************************************************/
  572. void Keyinit(void)
  573. {
  574. int i = 0;
  575. for (i = 0; i < KEYBUFLENGTH; i++)
  576. {
  577. KeyCodeBuf[i].bKey = 0xff; //按键值初始化,避免误动作
  578. KeyCodeBuf[i].wKeyTime = 0;
  579. }
  580. }
  581. /*******************************************************************************
  582. Function: sysInit()
  583. Description: system initialize entry
  584. Calls:
  585. Called By: no
  586. Input: no
  587. Output: no
  588. Return: no
  589. Others: no
  590. ********************************************************************************/
  591. void sysInit(void)
  592. {
  593. bsp_clock_init(); // 系统快时钟 18.432 div 2 = 9.216Mhz
  594. gpio_light_init(); // LED初始化
  595. pwm0_init(); // pwm0
  596. pwm1_init(); // pwm1
  597. pwm2_init(); // pwm2
  598. pwm3_init(); // PWM3
  599. gpio_key_init(); // 按键IO口初始化
  600. gpio_control_init(); // 雷达感应,继电器初始化
  601. tim2_init(); // 1ms定时器,定时器2
  602. uart0_timer1_init(); // Uart1(9600),采用time1定时器
  603. zigbee_protocol_init(); // zigbee初始化
  604. uart2_gpio_init();
  605. uart2_init();
  606. Keyinit(); ///按键值初始化,避免误动作
  607. }
  608. /*******************************************************************************
  609. Function: taskInit()
  610. Description: system Parameter Initialization
  611. Calls:
  612. Called By: no
  613. Input: no
  614. Output: no
  615. Return: no
  616. Others: no
  617. ********************************************************************************/
  618. void taskInit(void)
  619. {
  620. u08 i = 0;
  621. sys10msFlag = 0; /* 10ms*/
  622. // sTimeout(&SystemTimer, 0);
  623. // 全局初始化参数
  624. switchState.human = 0; // 默认关闭雷达,按键背光常亮
  625. for (i = 0; i < 3; i++)
  626. {
  627. switchState.step[i] = 0x01;
  628. switchState.relay_status[i] = 0x00;
  629. switchState.relay_new_status[i] = 0x00;
  630. switchState.mod[i] = 0x00; // 默认3路开关,//0:开关,1:场景
  631. switchState.plus_en[i] = 0x00; // 默认无点动触发
  632. switchState.plus_mod[i] = 0xff; // 默认未设置点动开关,测试点动开启
  633. switchState.plus_auto_time[i] = 0xffff; // 默认ffff
  634. }
  635. // switchState.relay_status[0] = 0x00;//测试继电器断电保存状态,默认关
  636. // switchState.relay_status[1] = 0x01;//测试继电器断电保存状态,默认开
  637. // switchState.relay_status[2] = 0x02;//测试继电器断电保存状态,默认与开机前一致
  638. switchState.step[3] = 0x01;
  639. switchState.step[4] = 0x01;
  640. switchState.step[5] = 0x01;
  641. switchState.led_level = PWMLEVEL1; // 默认最低亮度
  642. // switchState.gRadarCheckNull = 0; // 默认雷达有人,按键背光亮起
  643. // 全局初始化参数
  644. appControlLedEnable = 1; // 默认APP控制继电器动作联动背光亮10秒
  645. ZG_Joining = CONDITION_JOINGING_TO_NORMAL; // ZIGBEE正常模式状态
  646. switchState.zigbee_work_state = 0; // 默认ZIGBEE脱网运行
  647. EA = 1; // 总中断允许
  648. ledSetParam(0, 0); // 背光不闪
  649. pwm_set_prd(255); // PWM 周期设定
  650. /*
  651. 参数数据(10):0-9:人感1字节-0,开机状态(通电勿扰)全局设置1字节-1,三个继电器开机状态3个字节-2-3-4,背光等级1个字节-5
  652. 实时数据(6):10:19:当前继电器状态3个字节-10-11-12,雷达状态1字节-13,点动倒计时未结束1个字节-14-15-16,ota状态1字节-19(最后一字节)
  653. 点动开关参数(9):,电动开关3个字节(使能1,时间2)-20-21-22,-23-24-25,-26-27-28
  654. */
  655. iap_eeprom_read(0, read_mileage, 30); // 读取30字节,状态参数
  656. iap_eeprom_read_backup(0, read_mileage_backup, 30); // 读取30字节,配置参数
  657. /***************配置参数******************************/
  658. /*通电反应 通电勿扰开启 通过app操作使开关关闭的情况下,设备重新上电,设备处于关闭状态关闭 断电记忆 恢复到断电前使用的状态
  659. 开关上电状态设置 枚举范围:off,on,memory
  660. 通电勿扰开启 = off==0,关闭=memory==2,1:强制开机
  661. */
  662. if (read_mileage_backup[1] != 0xff)
  663. switchState.relay_allstatus = read_mileage_backup[1]; // 开关上电状态设置,全局
  664. if (read_mileage_backup[2] != 0xff)
  665. switchState.relay_status[0] = read_mileage_backup[2]; // 开关上电状态设置,1-3键
  666. if (read_mileage_backup[3] != 0xff)
  667. switchState.relay_status[1] = read_mileage_backup[3];
  668. if (read_mileage_backup[4] != 0xff)
  669. switchState.relay_status[2] = read_mileage_backup[4];
  670. if (read_mileage_backup[5] != 0xff)
  671. switchState.led_level = read_mileage_backup[5]; // 指示灯亮度
  672. if (read_mileage_backup[6] != 0xff)
  673. switchState.mod[0] = read_mileage_backup[6]; //// 开关、场景设置
  674. if (read_mileage_backup[7] != 0xff)
  675. switchState.mod[1] = read_mileage_backup[7]; //// 开关、场景设置
  676. if (read_mileage_backup[8] != 0xff)
  677. switchState.mod[2] = read_mileage_backup[8]; //// 开关、场景设置
  678. if (read_mileage_backup[9] != 0xff) //
  679. {
  680. switchState.human = read_mileage_backup[9]; // 人感使能开关
  681. }
  682. if (read_mileage_backup[20] != 0xff)
  683. switchState.plus_mod[0] = read_mileage_backup[20]; // 点动开关被配置
  684. if (read_mileage_backup[23] != 0xff)
  685. switchState.plus_mod[1] = read_mileage_backup[23]; // 点动开关被配置
  686. if (read_mileage_backup[26] != 0xff)
  687. switchState.plus_mod[2] = read_mileage_backup[26]; // 点动开关被配置
  688. if (read_mileage_backup[21] != 0xff)
  689. {
  690. switchState.plus_auto_time[0] = (read_mileage_backup[21] * 256 + read_mileage_backup[22]) * 10; // 高字节在前,秒转100ms为基数
  691. }
  692. else
  693. {
  694. switchState.plus_auto_time[0] = 50; // 默认5S,可控制门禁
  695. }
  696. if (read_mileage_backup[24] != 0xff)
  697. {
  698. switchState.plus_auto_time[1] = (read_mileage_backup[24] * 256 + read_mileage_backup[25]) * 10; // 高字节在前,秒转100ms为基数
  699. }
  700. else
  701. {
  702. switchState.plus_auto_time[1] = 50; // 默认5S,可控制门禁
  703. }
  704. if (read_mileage_backup[27] != 0xff)
  705. {
  706. switchState.plus_auto_time[2] = (read_mileage_backup[27] * 256 + read_mileage_backup[28]) * 10; // 高字节在前,秒转100ms为基数
  707. }
  708. else
  709. {
  710. switchState.plus_auto_time[2] = 50; // 默认5S,可控制门禁
  711. }
  712. /***************配置参数******************************/
  713. /***************关机前实时状态数据******************************/
  714. if (read_mileage[10] != 0xff)
  715. switchState.relay_new_status[0] = read_mileage[10]; // 关机前最新继电器状态,1-3键
  716. if (read_mileage[11] != 0xff)
  717. switchState.relay_new_status[1] = read_mileage[11];
  718. if (read_mileage[12] != 0xff)
  719. switchState.relay_new_status[2] = read_mileage[12];
  720. // 取消断电前的人感状态读取
  721. /*if (read_mileage[13] != 0xff)
  722. switchState.gRadarCheckNull = read_mileage[13]; // 断电前人感状态,1为无人*/
  723. if (read_mileage[14] != 0xff) // 取消该部分逻辑,根据APP设置的上电状态来启动点动
  724. switchState.plus_en[0] = read_mileage[14]; // 断电前点动开关未结束
  725. if (read_mileage[15] != 0xff)
  726. switchState.plus_en[1] = read_mileage[15]; // 断电前点动开关未结束
  727. if (read_mileage[16] != 0xff)
  728. switchState.plus_en[2] = read_mileage[16]; // 断电前点动开关未结束
  729. /***************关机前实时状态数据******************************/
  730. // if (switchState.plus_mod[0] == 0x01) // 1号通道被设置点动开关
  731. // {
  732. // if (switchState.relay_status[0] == 0x01) // 通电启动,其他设置不启动
  733. // {
  734. // switchState.plus_en[0] = 0x01;
  735. // switchState.plus_counter[0] = switchState.plus_auto_time[0];
  736. // gLedState = gLedState | 0x01; // 按键1开启
  737. // gRelayOnOff |= 0x01; // 继电器动作开启
  738. // }
  739. // else
  740. // {
  741. // switchState.plus_en[0] = 0x00; // 关闭
  742. // switchState.plus_counter[0] = 0;
  743. // gLedState = gLedState & (~0x01); // 按键1清除
  744. // gRelayOnOff &= (~0x01); // 继电器关闭
  745. // gRelayState |= 0x01;
  746. // }
  747. // }
  748. // else // 非点动,按照上电配置状态控制逻辑
  749. // {
  750. if ((switchState.mod[0] == 0x00) || (switchState.mod[1] == 0x00) || (switchState.mod[2] == 0x00))// 设置为开关时有效
  751. {
  752. // key1
  753. // if (switchState.relay_status[0] == 0x00) // 断电
  754. // {
  755. // gLedState = gLedState & (~0x01); // 按键1清除
  756. // gRelayOnOff &= (~0x01); // 继电器关闭
  757. // gRelayState |= 0x01;
  758. // }
  759. // else if (switchState.relay_status[0] == 0x01) // 通电
  760. // {
  761. // gLedState = gLedState | 0x01; // 按键1开启
  762. // gRelayOnOff |= 0x01; // 继电器动作开启
  763. // }
  764. // else // 断电记忆
  765. // {
  766. #ifdef ZIGBEE_ZTU_T3_SW
  767. if (switchState.relay_status[0] == 0x00) // 断电
  768. {
  769. gLedState = gLedState & (~0x04); // 按键1清除
  770. gRelayOnOff &= (~0x04); // 继电器关闭
  771. gRelayState |= 0x04;
  772. }
  773. else if (switchState.relay_status[0] == 0x01) // 通电
  774. {
  775. gLedState = gLedState | 0x04; // 按键1开启
  776. gRelayOnOff |= 0x04; // 继电器动作开启
  777. }
  778. else // 断电记忆
  779. {
  780. if (switchState.relay_new_status[2] == 0x01)
  781. {
  782. gLedState = gLedState | 0x04; // 按键1开启
  783. gRelayOnOff |= 0x04; // 继电器动作开启
  784. }
  785. else
  786. {
  787. gLedState = gLedState & (~0x04); // 按键1清除
  788. gRelayOnOff &= (~0x04); // 继电器关闭
  789. gRelayState |= 0x04;
  790. }
  791. }
  792. #endif
  793. #ifdef ZIGBEE_ZTU_T2_SW
  794. if (switchState.relay_status[0] == 0x00) // 断电
  795. {
  796. gLedState = gLedState & (~0x04); // 按键1清除
  797. gRelayOnOff &= (~0x04); // 继电器关闭
  798. gRelayState |= 0x04;
  799. }
  800. else if (switchState.relay_status[0] == 0x01) // 通电
  801. {
  802. gLedState = gLedState | 0x04; // 按键1开启
  803. gRelayOnOff |= 0x04; // 继电器动作开启
  804. }
  805. else // 断电记忆
  806. {
  807. if (switchState.relay_new_status[1] == 0x01)
  808. {
  809. gLedState = gLedState | 0x04; // 按键1开启
  810. gRelayOnOff |= 0x04; // 继电器动作开启
  811. }
  812. else
  813. {
  814. gLedState = gLedState & (~0x04); // 按键1清除
  815. gRelayOnOff &= (~0x04); // 继电器关闭
  816. gRelayState |= 0x04;
  817. }
  818. }
  819. #endif
  820. // }
  821. }
  822. // }
  823. // if (switchState.plus_mod[1] == 0x01) // 2号通道被设置点动开关
  824. // {
  825. // if (switchState.relay_status[1] == 0x01) // 通电
  826. // {
  827. // switchState.plus_en[1] = 0x01;
  828. // switchState.plus_counter[1] = switchState.plus_auto_time[1];
  829. // gLedState = gLedState | 0x02; // // 按键2开启
  830. // gRelayOnOff |= 0x02; // 继电器动作开启
  831. // }
  832. // else
  833. // {
  834. // switchState.plus_en[1] = 0x00;
  835. // switchState.plus_counter[1] = 0;
  836. // gLedState = gLedState & (~0x02); // 按键2清除
  837. // gRelayOnOff &= (~0x02); // 继电器关闭
  838. // gRelayState |= 0x02;
  839. // }
  840. // }
  841. // else // 非点动,按照上电配置状态控制逻辑
  842. // {
  843. if ((switchState.mod[0] == 0x00) || (switchState.mod[1] == 0x00) || (switchState.mod[2] == 0x00))// 设置为开关时有效
  844. {
  845. // key2
  846. // if (switchState.relay_status[1] == 0x00) // 断电
  847. // {
  848. // gLedState = gLedState & (~0x02); // 按键2清除
  849. // gRelayOnOff &= (~0x02); // 继电器关闭
  850. // gRelayState |= 0x02;
  851. // }
  852. // else if (switchState.relay_status[1] == 0x01) // 通电
  853. // {
  854. // gLedState = gLedState | 0x02; // // 按键2开启
  855. // gRelayOnOff |= 0x02; // 继电器动作开启
  856. // }
  857. // else // 断电记忆
  858. // {
  859. #ifdef ZIGBEE_ZTU_T3_SW
  860. if (switchState.relay_status[1] == 0x00) // 断电
  861. {
  862. gLedState = gLedState & (~0x02); // 按键2清除
  863. gRelayOnOff &= (~0x02); // 继电器关闭
  864. gRelayState |= 0x02;
  865. }
  866. else if (switchState.relay_status[1] == 0x01) // 通电
  867. {
  868. gLedState = gLedState | 0x02; // // 按键2开启
  869. gRelayOnOff |= 0x02; // 继电器动作开启
  870. }
  871. else // 断电记忆
  872. {
  873. if (switchState.relay_new_status[1] == 0x01)
  874. {
  875. gLedState = gLedState | 0x02; // // 按键2开启
  876. gRelayOnOff |= 0x02; // 继电器动作开启
  877. }
  878. else
  879. {
  880. gLedState = gLedState & (~0x02); // 按键2清除
  881. gRelayOnOff &= (~0x02); // 继电器关闭
  882. gRelayState |= 0x02;
  883. }
  884. }
  885. #endif
  886. #ifdef ZIGBEE_ZTU_T1_SW
  887. if (switchState.relay_status[0] == 0x00) // 断电
  888. {
  889. gLedState = gLedState & (~0x02); // 按键2清除
  890. gRelayOnOff &= (~0x02); // 继电器关闭
  891. gRelayState |= 0x02;
  892. }
  893. else if (switchState.relay_status[0] == 0x01) // 通电
  894. {
  895. gLedState = gLedState | 0x02; // // 按键2开启
  896. gRelayOnOff |= 0x02; // 继电器动作开启
  897. }
  898. else // 断电记忆
  899. {
  900. if (switchState.relay_new_status[2] == 0x01)
  901. {
  902. gLedState = gLedState | 0x02; // // 按键2开启
  903. gRelayOnOff |= 0x02; // 继电器动作开启
  904. }
  905. else
  906. {
  907. gLedState = gLedState & (~0x02); // 按键2清除
  908. gRelayOnOff &= (~0x02); // 继电器关闭
  909. gRelayState |= 0x02;
  910. }
  911. }
  912. #endif
  913. // }
  914. }
  915. // }
  916. // if (switchState.plus_mod[2] == 0x01) // 3号通道被设置点动开关
  917. // {
  918. // if (switchState.relay_status[2] == 0x01) // 通电
  919. // {
  920. // switchState.plus_en[2] = 0x01;
  921. // switchState.plus_counter[2] = switchState.plus_auto_time[2];
  922. // gLedState = gLedState | 0x04; // // 按键3开启
  923. // gRelayOnOff |= 0x04; // 继电器动作开启
  924. // }
  925. // else
  926. // {
  927. // switchState.plus_en[2] = 0x00;
  928. // switchState.plus_counter[2] = 0;
  929. // gLedState = gLedState & (~0x04); // // 按键3清除
  930. // gRelayOnOff &= (~0x04); // 继电器关闭
  931. // gRelayState |= 0x04;
  932. // }
  933. // }
  934. // else // 非点动,按照上电配置状态控制逻辑
  935. // {
  936. if ((switchState.mod[0] == 0x00) || (switchState.mod[1] == 0x00) || (switchState.mod[2] == 0x00))// 设置为开关时有效
  937. {
  938. // key3
  939. #ifdef ZIGBEE_ZTU_T2_SW
  940. if (switchState.relay_status[1] == 0x00) // 断电
  941. {
  942. gLedState = gLedState & (~0x01); // // 按键3清除
  943. gRelayOnOff &= (~0x01); // 继电器关闭
  944. gRelayState |= 0x01;
  945. }
  946. else if (switchState.relay_status[1] == 0x01) // 通电
  947. {
  948. gLedState = gLedState | 0x01; // // 按键3开启
  949. gRelayOnOff |= 0x01; // 继电器动作开启
  950. }
  951. else // 断电记忆
  952. {
  953. if (switchState.relay_new_status[2] == 0x01)
  954. {
  955. gLedState = gLedState | 0x01; // // 按键3开启
  956. gRelayOnOff |= 0x01; // 继电器动作开启
  957. }
  958. else
  959. {
  960. gLedState = gLedState & (~0x01); // // 按键3清除
  961. gRelayOnOff &= (~0x01); // 继电器关闭
  962. gRelayState |= 0x01;
  963. }
  964. }
  965. #endif
  966. #ifdef ZIGBEE_ZTU_T3_SW
  967. if (switchState.relay_status[2] == 0x00) // 断电
  968. {
  969. gLedState = gLedState & (~0x01); // // 按键3清除
  970. gRelayOnOff &= (~0x01); // 继电器关闭
  971. gRelayState |= 0x01;
  972. }
  973. else if (switchState.relay_status[2] == 0x01) // 通电
  974. {
  975. gLedState = gLedState | 0x01; // // 按键3开启
  976. gRelayOnOff |= 0x01; // 继电器动作开启
  977. }
  978. else // 断电记忆
  979. {
  980. if (switchState.relay_new_status[0] == 0x01)
  981. {
  982. gLedState = gLedState | 0x01; // // 按键3开启
  983. gRelayOnOff |= 0x01; // 继电器动作开启
  984. }
  985. else
  986. {
  987. gLedState = gLedState & (~0x01); // // 按键3清除
  988. gRelayOnOff &= (~0x01); // 继电器关闭
  989. gRelayState |= 0x01;
  990. }
  991. }
  992. #endif
  993. }
  994. // }
  995. // 取消断电前人感状态读取及应用
  996. /*if (switchState.gRadarCheckNull == 1) // 断电前无人
  997. {
  998. gRadarCheckNull = 1;
  999. gRadarKeepTimer = 0; // 雷达无人,按键灯熄灭
  1000. }*/
  1001. if (switchState.human) // 开启雷达的情况下有效
  1002. {
  1003. gRadarCheckNull = 1;
  1004. gRadarKeepTimer = 0; // 雷达无人,按键灯熄灭
  1005. }
  1006. sTimeout(&SystemTimer, 0);
  1007. #ifdef REALSE // 生产模式,开启看门狗
  1008. WDGT_Init();
  1009. #endif
  1010. }
  1011. /**********************************************************************************************************
  1012. **函数名称 :main()
  1013. **函数描述 :主函数
  1014. **输 入 :None
  1015. **输 出 :None
  1016. **********************************************************************************************************/
  1017. void main(void)
  1018. {
  1019. sysInit(); // 系统初始化
  1020. taskInit(); // 初始化任务
  1021. while (1)
  1022. {
  1023. zigbee_uart_service(); // zigbee接收处理
  1024. //增加循环读取电量数据 add by qys hlw
  1025. ///// 系统时钟控制
  1026. if (sys10msFlag) // 10ms任务
  1027. {
  1028. sys10msFlag = 0;
  1029. Scankey(); // 按键扫描处理任务
  1030. // button1LongPressCheck(); // 长按键处理任务
  1031. relay_plus_ctl(); // 磁保持继电器控制任务
  1032. if (ZG_Joining == CONDITION_JOINGING_TO_NORMAL) // 正常模式下按键灯控制任务
  1033. {
  1034. // 脱网指示灯闪烁
  1035. if (switchState.zigbee_work_state == 0x02) // 脱网报警(离线状态) 慢闪60s 3s一闪烁
  1036. {
  1037. if (--switchState.time)
  1038. {
  1039. ledCtrlProc(); // 脱网闪烁处理
  1040. }
  1041. else
  1042. {
  1043. ledSetParam(0, 0);
  1044. switchState.zigbee_work_state = 0; // 离线工作,指示灯恢复正常
  1045. }
  1046. }
  1047. else
  1048. {
  1049. key_led_pwm_ctl(); // 按键灯PWM控制任务
  1050. }
  1051. }
  1052. /*****************zigbee任务--10ms******************/
  1053. if (ZG_Joining == CONDITION_NORMAL_TO_BEFOR_JOINING) // zigbee配网前准备
  1054. {
  1055. if (++ZG_JoinCounter <= 1000) // 配网前确认闪烁10秒
  1056. {
  1057. ledCtrlProc(); // 按键led控制闪烁
  1058. }
  1059. else
  1060. {
  1061. ZG_Joining = CONDITION_JOINGING_TO_NORMAL; // 超过10秒未确认,从配网前恢复正常状态
  1062. ledSetParam(0, 0);
  1063. }
  1064. }
  1065. else if (ZG_Joining == CONDITION_BEFOR_JOINING_TO_JOINING) // 已进入配网状态
  1066. {
  1067. if (++ZG_JoinCounter <= 18000) // 配网快速闪烁180秒
  1068. {
  1069. ledCtrlProc(); ////按键led控制闪烁
  1070. if (ZG_JoinCounter == 0x05) // 进入配网模式,5秒后发送配网指令
  1071. {
  1072. // if (switchState.zigbee_work_state != 0x01) // 已入网不发送指令
  1073. //{
  1074. mcu_join_zigbee(); // 只要触发入网必须发送入网指令,满足从已入网主机1切换到新主机2的切换
  1075. //}
  1076. }
  1077. else if (ZG_JoinCounter % 900 == 0x00) // 过90秒再次发送一次入网-取消,01配网的逻辑是先离线在入网,不可二次发送
  1078. {
  1079. // mcu_join_zigbee();//每90秒再次发送入网指令
  1080. }
  1081. else
  1082. {
  1083. }
  1084. }
  1085. else
  1086. {
  1087. ZG_Joining = CONDITION_JOINGING_TO_NORMAL; ////超过180秒,从配网前恢复正常状态
  1088. ledSetParam(0, 0);
  1089. }
  1090. }
  1091. else
  1092. {
  1093. ZG_Joining = CONDITION_JOINGING_TO_NORMAL; // 正常状态
  1094. }
  1095. /*****************zigbee任务--10ms******************/
  1096. if (++sysTickfor100ms >= 10) // 100ms
  1097. {
  1098. sysTickfor100ms = 0;
  1099. /**********************10ms喂狗任务************************/
  1100. #ifdef REALSE // 生产模式
  1101. WDGT_Feed(); // 定时喂狗
  1102. #endif
  1103. /**********************10ms喂狗任务************************/
  1104. /*****************人感任务--100ms******************/
  1105. if (switchState.human) // 人感使能
  1106. {
  1107. if (RADAR_READ()) // 读取人感状态,高电平有效
  1108. {
  1109. gRadarCheckNull = 0; // 有人检测到
  1110. gRadarKeepTimer = 100; // 12s//传感器自带2S+软件倒计时10S
  1111. }
  1112. else // 下降沿清空有人标志
  1113. {
  1114. gRadarCheckNull = 1;
  1115. }
  1116. if (gRadarKeepTimer)
  1117. {
  1118. gRadarKeepTimer--;
  1119. // 取消人感状态变化存入eeprom
  1120. /*if (gRadarKeepTimer == 1) // 等于1时,触发存eeprom
  1121. {
  1122. switchState.gRadarCheckNull = 1; // 清空有人标志
  1123. // write_mileage[0] = switchState.gRadarCheckNull;
  1124. // iap_eeprom_write(13, write_mileage, 1); // 写入1字节
  1125. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  1126. write_mileage[13] = switchState.gRadarCheckNull;
  1127. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  1128. }
  1129. else if (gRadarKeepTimer == 90) // 等于90时,表示刚触发了有人,存eeprom
  1130. {
  1131. switchState.gRadarCheckNull = 0; // 设置有人标志
  1132. // write_mileage[0] = switchState.gRadarCheckNull;
  1133. // iap_eeprom_write(13, write_mileage, 1); // 写入1字节
  1134. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  1135. write_mileage[13] = switchState.gRadarCheckNull;
  1136. iap_eeprom_write(0, write_mileage, 30); // 写入1字节
  1137. }
  1138. else
  1139. {
  1140. }*/
  1141. }
  1142. }
  1143. else // 无人感,按键指示灯常亮
  1144. {
  1145. gRadarKeepTimer = 100; // 10s ,无人感设备一致默认有人
  1146. }
  1147. /*****************人感任务--100ms******************/
  1148. /*****************点动开关任务-100ms*******************/
  1149. if ((switchState.plus_en[0] == 0x01) && (switchState.plus_mod[0] == 0x01)) // 点动开关触发
  1150. {
  1151. if (switchState.plus_counter[0] == 0x00)
  1152. {
  1153. gLedState = gLedState & (~0x01); // 按键1清除
  1154. gRelayOnOff &= (~0x01); // 继电器关闭
  1155. // write_mileage[0] = 0;
  1156. // iap_eeprom_write(14, write_mileage, 1); // 写入1字节
  1157. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  1158. write_mileage[14] = 0;
  1159. iap_eeprom_write(0, write_mileage, 30); // 先读,在写入1字节
  1160. switchState.plus_en[0] = 0;
  1161. }
  1162. switchState.plus_counter[0]--;
  1163. }
  1164. if ((switchState.plus_en[1] == 0x01) && (switchState.plus_mod[1] == 0x01)) // 点动开关触发
  1165. {
  1166. if (switchState.plus_counter[1] == 0x00)
  1167. {
  1168. gLedState = gLedState & (~0x02); // 按键2清除
  1169. gRelayOnOff &= (~0x02); // 继电器关闭
  1170. // write_mileage[0] = 0;
  1171. // iap_eeprom_write(15, write_mileage, 1); // 写入1字节
  1172. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  1173. write_mileage[15] = 0;
  1174. iap_eeprom_write(0, write_mileage, 30); // 先读,在写入1字节
  1175. switchState.plus_en[1] = 0;
  1176. }
  1177. switchState.plus_counter[1]--;
  1178. }
  1179. if ((switchState.plus_en[2] == 0x01) && (switchState.plus_mod[2] == 0x01)) // 点动开关触发
  1180. {
  1181. if (switchState.plus_counter[2] == 0x00)
  1182. {
  1183. gLedState = gLedState & (~0x04); // // 按键3清除
  1184. gRelayOnOff &= (~0x04); // 继电器关闭
  1185. // write_mileage[0] = 0;
  1186. // iap_eeprom_write(16, write_mileage, 1); // 写入1字节
  1187. iap_eeprom_read(0, write_mileage, 30); // 读取30字节
  1188. write_mileage[16] = 0;
  1189. iap_eeprom_write(0, write_mileage, 30); // 先读,在写入1字节
  1190. switchState.plus_en[2] = 0;
  1191. }
  1192. switchState.plus_counter[2]--;
  1193. }
  1194. /**********************点动开关任务************************/
  1195. Read_Power_Data_handle(); //5s定时获取一次电量数据
  1196. if (++sysTickfor1000ms >= 10) // 1s任务
  1197. {
  1198. sysTickfor1000ms = 0;
  1199. //Start_Send_UartData(2);
  1200. Save_data_nun++; //电量数据备份存储计数器
  1201. }
  1202. }
  1203. }
  1204. }
  1205. }
  1206. /**********************************************************************************************************
  1207. **函数名称 :relay_ctl()
  1208. **函数描述 :继电器控制任务,10ms调用,普通继电器,电平控制
  1209. **输 入 :None
  1210. **输 出 :None
  1211. **********************************************************************************************************/
  1212. void relay_ctl(void)
  1213. {
  1214. /*if (gRelayState != gRelayOnOff)
  1215. {
  1216. //继电器1
  1217. if (gRelayOnOff & 0x01) // 继电器1开启
  1218. {
  1219. relay1_control(1);
  1220. gRelayState |= 0x01;
  1221. mcu_dp_bool_update(DPID_SWITCH_1, 1);
  1222. }
  1223. else if ((gRelayOnOff & 0x01) == 0x00) // 继电器1关闭
  1224. {
  1225. relay1_control(0);
  1226. gRelayState &= (~0x01);
  1227. mcu_dp_bool_update(DPID_SWITCH_1, 0);
  1228. }
  1229. //继电器2
  1230. if (gRelayOnOff & 0x02) // 继电器2开启
  1231. {
  1232. relay2_control(1);
  1233. gRelayState |= 0x02;
  1234. mcu_dp_bool_update(DPID_SWITCH_2, 1);
  1235. }
  1236. else if ((gRelayOnOff & 0x02) == 0x00) // 继电器3关闭
  1237. {
  1238. relay2_control(0);
  1239. gRelayState &= (~0x02);
  1240. mcu_dp_bool_update(DPID_SWITCH_2, 0);
  1241. }
  1242. //继电器3
  1243. if (gRelayOnOff & 0x04) // 继电器3开启
  1244. {
  1245. relay3_control(1);
  1246. gRelayState |= 0x04;
  1247. mcu_dp_bool_update(DPID_SWITCH_3, 1);
  1248. }
  1249. else if ((gRelayOnOff & 0x04) == 0x00) // 继电器3关闭
  1250. {
  1251. relay3_control(0);
  1252. gRelayState &= (~0x04);
  1253. mcu_dp_bool_update(DPID_SWITCH_3, 0);
  1254. }
  1255. }
  1256. // add by zzw all_data_update APP状态同步
  1257. if ((gLedState & 0x01) == 0x01)
  1258. {
  1259. switchState.SWITCH[0] = 1;
  1260. }
  1261. else
  1262. {
  1263. switchState.SWITCH[0] = 0;
  1264. }
  1265. if ((gLedState & 0x02) == 0x02)
  1266. {
  1267. switchState.SWITCH[1] = 1;
  1268. }
  1269. else
  1270. {
  1271. switchState.SWITCH[1] = 0;
  1272. }
  1273. if ((gLedState & 0x04) == 0x04) //
  1274. {
  1275. switchState.SWITCH[2] = 1;
  1276. }
  1277. else
  1278. {
  1279. switchState.SWITCH[2] = 0;
  1280. }*/
  1281. }
  1282. /******************************************************************************************
  1283. ** 函数名称: wdg_init
  1284. ** 函数描述: 看门狗初始化函数
  1285. ** 输入参数: 无
  1286. ** 输出参数: 无
  1287. *******************************************************************************************/
  1288. void WDGT_Init(void)
  1289. {
  1290. SET_REG_BITS(OPTION, WDTPSC, 0); // 240MS
  1291. SET_REG_BITS(AUX2, WDTE, 2); // 看门狗在快钟和慢钟模式下使能,空闲/停止/暂停模式下禁止
  1292. SET_REG_BITS(AUX1, CLRWDT, 0);
  1293. CLR_WDT; // 设置以清除看门狗定时器
  1294. }
  1295. /*******************************************************************************
  1296. Function: WDGT_Feed()
  1297. Description: 喂狗
  1298. Calls:
  1299. Called By: no
  1300. Input: no
  1301. Output: no
  1302. Return: no
  1303. Others: no
  1304. ********************************************************************************/
  1305. void WDGT_Feed(void)
  1306. {
  1307. CLRWDT = 1; // 喂狗
  1308. }