/*-------------------------------------------------------------------------------------- * @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>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; }