| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- /*--------------------------------------------------------------------------------------
- * @file ZigbeeNet.c
- * @author ZhangJing
- * @version base on stm32f0x
- * @date 2015.09.11
- * @brief zigbee模块初始化
- ---------------------------------------------------------------------------------------*/
- #include "stm32f10x_gpio.h"
- #include "TypeDefine.h"
- #include "USARTSetup.h"
- #include "ZigbeeNet.h"
- uint16_t g_uiCurZigCmd=0;//zigbee当前命令
- uint8_t g_ucZigbeeCmdResLen=0;//zigbee接收命令长度
- uint8_t g_ucZigbeeCmdRes=0;//zigbee命令长度
- uint8_t g_ucCurZigState=0;//zigbee当前状态
- /***************************************************************************************
- * Function: ZigXor
- * Object: 异或
- * 输入: uint8_t *ucBuf, uint8_t ucLen
- * 输出: 返回结果
- * 备注: 计算zigbee校验值根据 ucLen长度对 ucBuf中数据进行异或运算
- ****************************************************************************************/
- uint8_t ZigXor(uint8_t *ucBuf, uint8_t ucLen)
- {
- uint8_t i;
-
- uint8_t ucRes=0;
-
- for(i=0;i<ucLen;i++)
- {
- ucRes=ucRes^(*(ucBuf+i));
- }
-
- return(ucRes);
-
- }
- /***************************************************************************************
- * Function: SendZigbeeCmd
- * Object: 发送zigbee命令
- * 输入: uint8_t *ucCmdBuf,uint8_t ucCmdLen,uint8_t ucResLen
- * 输出: 返回发送状态
- * 备注: 1、uint8_t *ucCmdBuf,uint8_t ucCmdLen,uint8_t ucResLen 发送数据指针,长度及接收数据长度
- ****************************************************************************************/
- uint8_t SendZigbeeCmd(uint8_t *ucCmdBuf,uint8_t ucCmdLen,uint8_t ucResLen)
- {
- // uint16_t i;
- g_ucZigbeeCmdRes=0;
-
-
- USART2Send(ucCmdBuf,ucCmdLen);
- // g_ucZigbeeCmdResLen=ucResLen;
- // for(i=0;i<1000;i++)
- // {
- // if(g_ucZigbeeCmdRes==1)
- // {
- // g_ucZigbeeCmdRes=0;
- // if(USART2_RX_BUF[1]==ucCmdBuf[1])
- // {
- // if(g_uiCurZigCmd==READZIGSTATE)
- // {
- // g_ucCurZigState=USART2_RX_BUF[4];
- // }
- // return(1);
- // }
- // }
- // Delay_ms(1);
- // }
- return(0);
-
- }
- /***************************************************************************************
- * Function: ZigSetID
- * Object: 设置网络ID
- * 输入: uint16_t uiID
- * 输出: 无
- * 备注: 1、uint8_t ucComdBuf[7]={0xFC,0x06,0x02,0x00}; 为设置网络ID命令
- ****************************************************************************************/
- uint8_t ZigSetPainID(uint16_t uiID)
- {
- uint8_t ucComdBuf[7]={0xFC,0x06,0x02,0x00};
- uint8_t ucRes;
- ucComdBuf[4]=(uint8_t)(uiID&0xff);
- ucComdBuf[5]=(uint8_t)((uiID>>8)&0xff);
- ucComdBuf[6]=ZigXor(ucComdBuf,6);
- g_uiCurZigCmd=SETPINID;
-
- //发送ZIGBEE 命令
- ucRes=SendZigbeeCmd(ucComdBuf,7,7);
- return(ucRes);
-
- }
- /***************************************************************************************
- * Function: ZigSetZGType
- * Object: 设置路由、协调器
- * 输入: uint8_t ucChannel
- * 输出: 无
- * 备注: 1、ucComdBuf[16]={0xFC,0x06,0x09,0x00}; 为设置路由、协调器命令
- ****************************************************************************************/
- uint8_t ZigSetChannel(uint8_t ucChannel)
- {
- uint8_t ucComdBuf[16]={0xFC,0x06,0x09,0x00};
- uint8_t ucRes;
- ucComdBuf[4]=ucChannel; //0
- ucComdBuf[5]=0x00;
- ucComdBuf[6]=ZigXor(ucComdBuf,6);
- g_uiCurZigCmd=SETCHANNEL;
- //发送ZIGBEE 命令
- ucRes=SendZigbeeCmd(ucComdBuf,7,7);
- return(ucRes);
-
- }
- /***************************************************************************************
- * Function: ZigReadPainID
- * Object: 读取网络ID
- * 输入: 无
- * 输出: 返回网络ID
- * 备注: 1�int8_t ucComdBuf[16]={0xFC,0x03,0x02,0x00,0x00,0x00};为读取网络ID命令
- ****************************************************************************************/
- uint8_t ZigReadPainID(void)
- {
- uint8_t ucComdBuf[16]={0xFC,0x03,0x02,0x00,0x00,0x00};//FC 03 02 00 00 00 X0R
- uint8_t ucRes;
- ucComdBuf[6]=ZigXor(ucComdBuf,6);
- g_uiCurZigCmd=READPAINID;
- //发送ZIGBEE 命令
- ucRes=SendZigbeeCmd(ucComdBuf,7,7);
- return(ucRes);
-
- }
- /***************************************************************************************
- * Function: ZigReadPainID
- * Object: 读取PainID
- * 输入: 无
- * 输出: 无
- * 备注: 1�int8_t ucComdBuf[16]=0xFC,0x03,0x08,0x00,0x00,0x00};为读取PainID命令
- ****************************************************************************************/
- uint8_t ZigReadState(void)
- {
- uint8_t ucComdBuf[16]={0xFC,0x03,0x08,0x00,0x00,0x00};//FC 03 02 00 00 00 X0R
- uint8_t ucRes;
- ucComdBuf[6]=ZigXor(ucComdBuf,6);
- g_uiCurZigCmd=READZIGSTATE;
- //发送ZIGBEE 命令
- ucRes=SendZigbeeCmd(ucComdBuf,7,7);
- return(ucRes);
-
- }
- /***************************************************************************************
- * Function: ZigDefaultInit
- * Object: 恢复出厂设置
- * 输入: 无
- * 输出: 返回状态值
- * 备注: 1、uint8_t ucComdBuf[16]={0xFC,0x06,0x01,0x00,0x00,0x00}; 恢复出厂设置命令
- ****************************************************************************************/
- uint8_t ZigDefaultInit(void)
- {
- uint8_t ucComdBuf[16]={0xFC,0x06,0x01,0x00,0x00,0x00};//FC 03 02 00 00 00 X0R
- uint8_t ucRes;
- ucComdBuf[6]=ZigXor(ucComdBuf,6);
- g_uiCurZigCmd=DEFAULTSTATE;
-
- //发送ZIGBEE 命令
- ucRes=SendZigbeeCmd(ucComdBuf,7,7);
- return(ucRes);
-
- }
- /***************************************************************************************
- * Function: ZigClearNetInfo
- * Object: 清除结节信息
- * 输入: 无
- * 输出: 状态值
- * 备注: 1、uint8_t ucComdBuf[7]={0xFC,0x06,0x18,0x00,0x00,0x00};清除结节信息命令
- ****************************************************************************************/
- uint8_t ZigClearNetInfo(void)
- {
- uint8_t ucComdBuf[7]={0xFC,0x06,0x18,0x00,0x00,0x00};//FC 03 02 00 00 00 X0R
- uint8_t ucRes;
- ucComdBuf[6]=ZigXor(ucComdBuf,6);
- g_uiCurZigCmd=ZIGCLEARNET;
- //发送ZIGBEE 命令
- ucRes=SendZigbeeCmd(ucComdBuf,7,7);
- return(ucRes);
-
- }
- /***************************************************************************************
- * Function: ZigReadPainID
- * Object: 设置路由器或者协调器 ucType=0 协调器,ucType=1 路由器
- * 输入: uint8_t ucType
- * 输出: 状态值
- * 备注: 1、uint8_t ucComdBuf[7]={0xFC,0x06,0x11,0x00,0x00,0x00};设置路由或节点命令
- ****************************************************************************************/
- uint8_t ZigSetDevType(uint8_t ucType)
- {
- uint8_t ucComdBuf[7]={0xFC,0x06,0x11,0x00,0x00,0x00};//FC 06 11 00 XX 00 X0R
- uint8_t ucRes;
- ucComdBuf[4]=ucType;
- ucComdBuf[6]=ZigXor(ucComdBuf,6);
- g_uiCurZigCmd=SETDEVTYPE;
- //发送ZIGBEE 命令
- ucRes=SendZigbeeCmd(ucComdBuf,7,7);
- return(ucRes);
-
- }
- /***************************************************************************************
- * Function: ZigbeeInit
- * Object: 初始化zigbee模块,ucType=0 协调器,ucType=1 路由器
- * 输入: uint8_t ucType
- * 输出: 返回状态值
- * 备注:
- ****************************************************************************************/
- uint8_t ZigbeeInit(uint8_t ucType)
- {
-
- uint8_t ucRes;
-
-
- if(ucType==1) //协调器
- {
- //恢复出厂设置
- ucRes=ZigDefaultInit();
-
- //设置为协调器模式
- ucRes=ZigSetDevType(COORDINATOR);
- }
- else
- {
- //设置为路由模式
- ucRes=ZigSetDevType(ROUTTER);
- Delay_ms(100);
-
- //清除网络信息
- ucRes=ZigClearNetInfo();
-
- Delay_ms(100);
- //设置PAINID 为0xffff
- ucRes=ZigSetPainID(0xffff);
- }
-
- return ucRes;
- }
|