ZigbeeNet.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*--------------------------------------------------------------------------------------
  2. * @file ZigbeeNet.c
  3. * @author ZhangJing
  4. * @version base on stm32f0x
  5. * @date 2015.09.11
  6. * @brief zigbee模块初始化
  7. ---------------------------------------------------------------------------------------*/
  8. #include "stm32f10x_gpio.h"
  9. #include "TypeDefine.h"
  10. #include "USARTSetup.h"
  11. #include "ZigbeeNet.h"
  12. uint16_t g_uiCurZigCmd=0;//zigbee当前命令
  13. uint8_t g_ucZigbeeCmdResLen=0;//zigbee接收命令长度
  14. uint8_t g_ucZigbeeCmdRes=0;//zigbee命令长度
  15. uint8_t g_ucCurZigState=0;//zigbee当前状态
  16. /***************************************************************************************
  17. * Function: ZigXor
  18. * Object: 异或
  19. * 输入: uint8_t *ucBuf, uint8_t ucLen
  20. * 输出: 返回结果
  21. * 备注: 计算zigbee校验值根据 ucLen长度对 ucBuf中数据进行异或运算
  22. ****************************************************************************************/
  23. uint8_t ZigXor(uint8_t *ucBuf, uint8_t ucLen)
  24. {
  25. uint8_t i;
  26. uint8_t ucRes=0;
  27. for(i=0;i<ucLen;i++)
  28. {
  29. ucRes=ucRes^(*(ucBuf+i));
  30. }
  31. return(ucRes);
  32. }
  33. /***************************************************************************************
  34. * Function: SendZigbeeCmd
  35. * Object: 发送zigbee命令
  36. * 输入: uint8_t *ucCmdBuf,uint8_t ucCmdLen,uint8_t ucResLen
  37. * 输出: 返回发送状态
  38. * 备注: 1、uint8_t *ucCmdBuf,uint8_t ucCmdLen,uint8_t ucResLen 发送数据指针,长度及接收数据长度
  39. ****************************************************************************************/
  40. uint8_t SendZigbeeCmd(uint8_t *ucCmdBuf,uint8_t ucCmdLen,uint8_t ucResLen)
  41. {
  42. // uint16_t i;
  43. g_ucZigbeeCmdRes=0;
  44. USART2Send(ucCmdBuf,ucCmdLen);
  45. // g_ucZigbeeCmdResLen=ucResLen;
  46. // for(i=0;i<1000;i++)
  47. // {
  48. // if(g_ucZigbeeCmdRes==1)
  49. // {
  50. // g_ucZigbeeCmdRes=0;
  51. // if(USART2_RX_BUF[1]==ucCmdBuf[1])
  52. // {
  53. // if(g_uiCurZigCmd==READZIGSTATE)
  54. // {
  55. // g_ucCurZigState=USART2_RX_BUF[4];
  56. // }
  57. // return(1);
  58. // }
  59. // }
  60. // Delay_ms(1);
  61. // }
  62. return(0);
  63. }
  64. /***************************************************************************************
  65. * Function: ZigSetID
  66. * Object: 设置网络ID
  67. * 输入: uint16_t uiID
  68. * 输出: 无
  69. * 备注: 1、uint8_t ucComdBuf[7]={0xFC,0x06,0x02,0x00}; 为设置网络ID命令
  70. ****************************************************************************************/
  71. uint8_t ZigSetPainID(uint16_t uiID)
  72. {
  73. uint8_t ucComdBuf[7]={0xFC,0x06,0x02,0x00};
  74. uint8_t ucRes;
  75. ucComdBuf[4]=(uint8_t)(uiID&0xff);
  76. ucComdBuf[5]=(uint8_t)((uiID>>8)&0xff);
  77. ucComdBuf[6]=ZigXor(ucComdBuf,6);
  78. g_uiCurZigCmd=SETPINID;
  79. //发送ZIGBEE 命令
  80. ucRes=SendZigbeeCmd(ucComdBuf,7,7);
  81. return(ucRes);
  82. }
  83. /***************************************************************************************
  84. * Function: ZigSetZGType
  85. * Object: 设置路由、协调器
  86. * 输入: uint8_t ucChannel
  87. * 输出: 无
  88. * 备注: 1、ucComdBuf[16]={0xFC,0x06,0x09,0x00}; 为设置路由、协调器命令
  89. ****************************************************************************************/
  90. uint8_t ZigSetChannel(uint8_t ucChannel)
  91. {
  92. uint8_t ucComdBuf[16]={0xFC,0x06,0x09,0x00};
  93. uint8_t ucRes;
  94. ucComdBuf[4]=ucChannel; //0
  95. ucComdBuf[5]=0x00;
  96. ucComdBuf[6]=ZigXor(ucComdBuf,6);
  97. g_uiCurZigCmd=SETCHANNEL;
  98. //发送ZIGBEE 命令
  99. ucRes=SendZigbeeCmd(ucComdBuf,7,7);
  100. return(ucRes);
  101. }
  102. /***************************************************************************************
  103. * Function: ZigReadPainID
  104. * Object: 读取网络ID
  105. * 输入: 无
  106. * 输出: 返回网络ID
  107. * 备注: 1�int8_t ucComdBuf[16]={0xFC,0x03,0x02,0x00,0x00,0x00};为读取网络ID命令
  108. ****************************************************************************************/
  109. uint8_t ZigReadPainID(void)
  110. {
  111. uint8_t ucComdBuf[16]={0xFC,0x03,0x02,0x00,0x00,0x00};//FC 03 02 00 00 00 X0R
  112. uint8_t ucRes;
  113. ucComdBuf[6]=ZigXor(ucComdBuf,6);
  114. g_uiCurZigCmd=READPAINID;
  115. //发送ZIGBEE 命令
  116. ucRes=SendZigbeeCmd(ucComdBuf,7,7);
  117. return(ucRes);
  118. }
  119. /***************************************************************************************
  120. * Function: ZigReadPainID
  121. * Object: 读取PainID
  122. * 输入: 无
  123. * 输出: 无
  124. * 备注: 1�int8_t ucComdBuf[16]=0xFC,0x03,0x08,0x00,0x00,0x00};为读取PainID命令
  125. ****************************************************************************************/
  126. uint8_t ZigReadState(void)
  127. {
  128. uint8_t ucComdBuf[16]={0xFC,0x03,0x08,0x00,0x00,0x00};//FC 03 02 00 00 00 X0R
  129. uint8_t ucRes;
  130. ucComdBuf[6]=ZigXor(ucComdBuf,6);
  131. g_uiCurZigCmd=READZIGSTATE;
  132. //发送ZIGBEE 命令
  133. ucRes=SendZigbeeCmd(ucComdBuf,7,7);
  134. return(ucRes);
  135. }
  136. /***************************************************************************************
  137. * Function: ZigDefaultInit
  138. * Object: 恢复出厂设置
  139. * 输入: 无
  140. * 输出: 返回状态值
  141. * 备注: 1、uint8_t ucComdBuf[16]={0xFC,0x06,0x01,0x00,0x00,0x00}; 恢复出厂设置命令
  142. ****************************************************************************************/
  143. uint8_t ZigDefaultInit(void)
  144. {
  145. uint8_t ucComdBuf[16]={0xFC,0x06,0x01,0x00,0x00,0x00};//FC 03 02 00 00 00 X0R
  146. uint8_t ucRes;
  147. ucComdBuf[6]=ZigXor(ucComdBuf,6);
  148. g_uiCurZigCmd=DEFAULTSTATE;
  149. //发送ZIGBEE 命令
  150. ucRes=SendZigbeeCmd(ucComdBuf,7,7);
  151. return(ucRes);
  152. }
  153. /***************************************************************************************
  154. * Function: ZigClearNetInfo
  155. * Object: 清除结节信息
  156. * 输入: 无
  157. * 输出: 状态值
  158. * 备注: 1、uint8_t ucComdBuf[7]={0xFC,0x06,0x18,0x00,0x00,0x00};清除结节信息命令
  159. ****************************************************************************************/
  160. uint8_t ZigClearNetInfo(void)
  161. {
  162. uint8_t ucComdBuf[7]={0xFC,0x06,0x18,0x00,0x00,0x00};//FC 03 02 00 00 00 X0R
  163. uint8_t ucRes;
  164. ucComdBuf[6]=ZigXor(ucComdBuf,6);
  165. g_uiCurZigCmd=ZIGCLEARNET;
  166. //发送ZIGBEE 命令
  167. ucRes=SendZigbeeCmd(ucComdBuf,7,7);
  168. return(ucRes);
  169. }
  170. /***************************************************************************************
  171. * Function: ZigReadPainID
  172. * Object: 设置路由器或者协调器 ucType=0 协调器,ucType=1 路由器
  173. * 输入: uint8_t ucType
  174. * 输出: 状态值
  175. * 备注: 1、uint8_t ucComdBuf[7]={0xFC,0x06,0x11,0x00,0x00,0x00};设置路由或节点命令
  176. ****************************************************************************************/
  177. uint8_t ZigSetDevType(uint8_t ucType)
  178. {
  179. uint8_t ucComdBuf[7]={0xFC,0x06,0x11,0x00,0x00,0x00};//FC 06 11 00 XX 00 X0R
  180. uint8_t ucRes;
  181. ucComdBuf[4]=ucType;
  182. ucComdBuf[6]=ZigXor(ucComdBuf,6);
  183. g_uiCurZigCmd=SETDEVTYPE;
  184. //发送ZIGBEE 命令
  185. ucRes=SendZigbeeCmd(ucComdBuf,7,7);
  186. return(ucRes);
  187. }
  188. /***************************************************************************************
  189. * Function: ZigbeeInit
  190. * Object: 初始化zigbee模块,ucType=0 协调器,ucType=1 路由器
  191. * 输入: uint8_t ucType
  192. * 输出: 返回状态值
  193. * 备注:
  194. ****************************************************************************************/
  195. uint8_t ZigbeeInit(uint8_t ucType)
  196. {
  197. uint8_t ucRes;
  198. if(ucType==1) //协调器
  199. {
  200. //恢复出厂设置
  201. ucRes=ZigDefaultInit();
  202. //设置为协调器模式
  203. ucRes=ZigSetDevType(COORDINATOR);
  204. }
  205. else
  206. {
  207. //设置为路由模式
  208. ucRes=ZigSetDevType(ROUTTER);
  209. Delay_ms(100);
  210. //清除网络信息
  211. ucRes=ZigClearNetInfo();
  212. Delay_ms(100);
  213. //设置PAINID 为0xffff
  214. ucRes=ZigSetPainID(0xffff);
  215. }
  216. return ucRes;
  217. }