Zigbee组网程序.docx
- 文档编号:30037485
- 上传时间:2023-08-04
- 格式:DOCX
- 页数:29
- 大小:20.20KB
Zigbee组网程序.docx
《Zigbee组网程序.docx》由会员分享,可在线阅读,更多相关《Zigbee组网程序.docx(29页珍藏版)》请在冰豆网上搜索。
Zigbee组网程序
SappleApp.c
#include"OSAL.h"
#include"ZGlobals.h"
#include"AF.h"
#include"aps_groups.h"
#include"ZDApp.h"
#include"SampleApp.h"
#include"SampleAppHw.h"
#include"OnBoard.h"
/*HAL*/
#include"hal_lcd.h"
#include"hal_led.h"
#include"hal_key.h"
#include"string.h"
#include"MT_UART.h"
//#include"Lcd128X64.h"
#include"UtOled.h"
#include"sensor.h"
#include"HAL_ADC.h"
#include"exsensor.h"
#include"lcd128_64.h"
constcId_tSampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS]=
{
SAMPLEAPP_PERIODIC_CLUSTERID,
SAMPLEAPP_FLASH_CLUSTERID
};
constSimpleDescriptionFormat_tSampleApp_SimpleDesc=
{
SAMPLEAPP_ENDPOINT,//intEndpoint;SAMPLEAPP_PROFID,//uint16AppProfId[2];SAMPLEAPP_DEVICEID,//uint16AppDeviceId[2];SAMPLEAPP_DEVICE_VERSION,//intAppDevVer:
4;SAMPLEAPP_FLAGS,//intAppFlags:
4;SAMPLEAPP_MAX_CLUSTERS,//uint8AppNumInClusters;(cId_t*)SampleApp_ClusterList,//uint8*pAppInClusterList;SAMPLEAPP_MAX_CLUSTERS,//uint8AppNumInClusters;(cId_t*)SampleApp_ClusterList//uint8*pAppInClusterList;
};
endPointDesc_tSampleApp_epDesc;
uint8SampleApp_TaskID;
devStates_tSampleApp_NwkState;
uint8SampleApp_TransID;
uint8changeline[2]={0x0a,0x0d};
unionh{
uint8RxBuf[70];
Union_DATARXDATA;
}UartRxBuf;//串口接收缓冲区
unione{
uint8TxBuf[70];
Union_DATATXDATA;
}UartTxBuf;//串口发送缓冲区
unionf{
uint8RxBuf[70];
Union_DATARXDATA;
}RfRx;//无线接收缓冲区
uniong{
uint8TxBuf[70];
Union_DATATXDATA;
}RfTx;//无线接收缓冲区
voidSampleApp_MessageMSGCB(afIncomingMSGPacket_t*pckt);
voidSampleApp_SendData(unsignedcharbuff[],uint8length);
staticvoidrxCB(uint8port,uint8event);
voidSampleApp_Init(uint8task_id)
{
halUARTCfg_tuartConfig;
SampleApp_TaskID=task_id;
SampleApp_NwkState=DEV_INIT;
SampleApp_TransID=0;
SampleApp_epDesc.endPoint=SAMPLEAPP_ENDPOINT;SampleApp_epDesc.task_id=&SampleApp_TaskID;
SampleApp_epDesc.simpleDesc
=(SimpleDescriptionFormat_t*)&SampleApp_SimpleDesc;SampleApp_epDesc.latencyReq=noLatencyReqs;
afRegister(&SampleApp_epDesc);
RegisterForKeys(SampleApp_TaskID);
MT_UartRegisterTaskID(SampleApp_TaskID);
LCDInit();
ClearScreen();
Print(0,10,"COORDINATOR",1);
Print(2,5,"ZhangHang",1);
Print(4,5,"FengGuangWen",1);
Print(6,5,"YaoJiaDong",1);
uartConfig.configured=TRUE;
uartConfig.baudRate=HAL_UART_BR_115200;
uartConfig.flowControl=FALSE;
uartConfig.callBackFunc=rxCB;
//uartConfig.callBackFunc=NULL;
HalUARTOpen(0,&uartConfig);
}
uint16SampleApp_ProcessEvent(uint8task_id,uint16events)
{
afIncomingMSGPacket_t*MSGpkt;
(void)task_id;
if(events&SYS_EVENT_MSG)
{
MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(SampleApp_TaskID);
while(MSGpkt)
{
switch(MSGpkt->hdr.event)
{
caseZDO_STATE_CHANGE:
SampleApp_NwkState=(devStates_t)(MSGpkt->hdr.status);
HalLedBlink(0x02,0,50,500);
uint8*buff;
buff[0]='A';
buff[1]='B';
buff[2]='C';
HalUARTWrite(0,buff,3);
HalUARTWrite(0,changeline,2);
break;
caseAF_INCOMING_MSG_CMD:
HalLedBlink(0x01,0,50,500);
SampleApp_MessageMSGCB(MSGpkt);
break;
/*caseSPI_INCOMING_ZTOOL_PORT:
HalLedBlink(0x02,0,50,500);
rxCBX();*/
default:
break;
}
osal_msg_deallocate((uint8*)MSGpkt);
MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(SampleApp_TaskID);
}
return(events^SYS_EVENT_MSG);
}
return0;
}
staticuint8SerialApp_TxLen;
voidSampleApp_MessageMSGCB(afIncomingMSGPacket_t*pkt)
{
unsignedcharbuffer[42];
osal_memcpy(buffer,pkt->cmd.Data,42);
HalUARTWrite(0,buffer,42);
HalUARTWrite(0,changeline,2);
/*
unsignedcharbuffer[100];
osal_memcpy(buffer,pkt->cmd.Data,100);
SerialApp_TxLen=HalUARTRead(0,buffer,100);
HalUARTWrite(0,buffer,SerialApp_TxLen);
HalUARTWrite(0,changeline,2);
*/
}
voidSampleApp_SendData(uint8buff[],uint8length)
{
afAddrType_tSendDataAddr;
SendDataAddr.addrMode=(afAddrMode_t)AddrBroadcast;
SendDataAddr.endPoint=SAMPLEAPP_ENDPOINT;
SendDataAddr.addr.shortAddr=0xffff;
AF_DataRequest(&SendDataAddr,&SampleApp_epDesc,
2,
length,
buff,
&SampleApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);
}
staticvoidrxCB(uint8port,uint8event){
uint8length=Hal_UART_RxBufLen(0);
uint8*buff;
buff=osal_mem_alloc(length);
HalUARTRead(0,buff,length);
if(length!
=0)
{
SampleApp_SendData(buff,length);
Printn8(5,5,length,1,3);
}
osal_mem_free(buff);
}
Router.c
#include"OSAL.h"
#include"ZGlobals.h"
#include"AF.h"
#include"aps_groups.h"
#include"ZDApp.h"
#include"SampleApp.h"
#include"SampleAppHw.h"
#include"OnBoard.h"
#include"ioCC2530.h"
/*HAL*/
#include"hal_lcd.h"
#include"hal_led.h"
#include"hal_key.h"
#include"string.h"
#include"MT_UART.h"
//#include"Lcd128X64.h"
#include"UtOled.h"
#include"sensor.h"
#include"HAL_ADC.h"
#include"exsensor.h"
#include"lcd128_64.h"
constcId_tSampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS]=
{
SAMPLEAPP_PERIODIC_CLUSTERID,
SAMPLEAPP_FLASH_CLUSTERID
};
constSimpleDescriptionFormat_tSampleApp_SimpleDesc=
{
SAMPLEAPP_ENDPOINT,//intEndpoint;SAMPLEAPP_PROFID,//uint16AppProfId[2];SAMPLEAPP_DEVICEID,//uint16AppDeviceId[2];SAMPLEAPP_DEVICE_VERSION,//intAppDevVer:
4;SAMPLEAPP_FLAGS,//intAppFlags:
4;SAMPLEAPP_MAX_CLUSTERS,//uint8AppNumInClusters;(cId_t*)SampleApp_ClusterList,//uint8*pAppInClusterList;SAMPLEAPP_MAX_CLUSTERS,//uint8AppNumInClusters;(cId_t*)SampleApp_ClusterList//uint8*pAppInClusterList;
};
endPointDesc_tSampleApp_epDesc;
uint8SampleApp_TaskID;
devStates_tSampleApp_NwkState;
uint8SampleApp_TransID;
uint8myMac[16];//物理地址
uint8myShort[4];//本节点网络地址
uint8temp;
uint16NWK;
uint8readbuf[50];
uint8sendbuf[50];
uint8changeline[2]={0x0a,0x0d};
uint8LEDSTATE=1;
uint8LEDSTATE5=1;
uint8LEDSTATE6=1;
uint8PEOPLE=0;
uint8PEOPLE1=0;
uint8PEOPLE2=0;
uint8LIGHT=115;
inttimexx1=0;
inttimexx2=0;
unionh{
uint8RxBuf[70];
Union_DATARXDATA;
}UartRxBuf;//串口接收缓冲区
unione{
uint8TxBuf[70];
Union_DATATXDATA;
}UartTxBuf;//串口发送缓冲区
unionf{
uint8RxBuf[70];
Union_DATARXDATA;
}RfRx;//无线接收缓冲区
uniong{
uint8TxBuf[70];
Union_DATATXDATA;
}RfTx;//无线接收缓冲区
typedefstructRFTXBUF
{
uint8myNWK[4];
uint8myMAC[16];
uint8pNWK[4];
uint8pMAC[16];
}RFTX;
voidSampleApp_HandleKey(uint8shift,uint8keys);
voidSampleApp_SendData(void);
voidDataPack(void);
voidSampleApp_MessageMSGCB(afIncomingMSGPacket_t*pkt);voidToString(uint8*dest,char*src,uint8length);
voidShowScreen(void);
voidHalInit(void);
voidDoLight(void);
voidSampleApp_Init(uint8task_id)
{
halUARTCfg_tuartConfig;
SampleApp_TaskID=task_id;
SampleApp_NwkState=DEV_INIT;
SampleApp_TransID=0;
SampleApp_epDesc.endPoint=SAMPLEAPP_ENDPOINT;
SampleApp_epDesc.task_id=&SampleApp_TaskID;
SampleApp_epDesc.simpleDesc
=(SimpleDescriptionFormat_t*)&SampleApp_SimpleDesc;
SampleApp_epDesc.latencyReq=noLatencyReqs;
afRegister(&SampleApp_epDesc);
RegisterForKeys(SampleApp_TaskID);
MT_UartRegisterTaskID(SampleApp_TaskID);
HalInit();
uartConfig.configured=TRUE;
uartConfig.baudRate=HAL_UART_BR_115200;
uartConfig.flowControl=FALSE;
uartConfig.callBackFunc=NULL;
HalUARTOpen(0,&uartConfig);
}
uint16SampleApp_ProcessEvent(uint8task_id,uint16events)
{
afIncomingMSGPacket_t*MSGpkt;
(void)task_id;
if(events&SYS_EVENT_MSG)
{
MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(SampleApp_TaskID);
while(MSGpkt)
{
switch(MSGpkt->hdr.event)
{
caseZDO_STATE_CHANGE:
SampleApp_NwkState=(devStates_t)(MSGpkt->hdr.status);
if(SampleApp_NwkState==DEV_ROUTER)
{
HalLedBlink(0x02,0,50,500);
//DataPack();
//
osal_set_event(SampleApp_TaskID,SEND_DATA_EVENT);
//
osal_set_event(SampleApp_TaskID,SHOW_DATA_EVENT);
//
osal_set_event(SampleApp_TaskID,LIGHT_EVENT);
//ClearScreen();
uint8*buff;
buff[0]='A';
buff[1]='B';
buff[2]='C';
HalUARTWrite(0,buff,3);
HalUARTWrite(0,changeline,2);
}
break;
caseAF_INCOMING_MSG_CMD:
HalLedBlink(0x01,0,50,500);
SampleApp_MessageMSGCB(MSGpkt);
break;
default:
break;
}
osal_msg_deallocate((uint8*)MSGpkt);
MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(SampleApp_TaskID);
}
return(events^SYS_EVENT_MSG);
}
if(events&SEND_DATA_EVENT)
{
SampleApp_SendData();
osal_start_timerEx(SampleApp_TaskID,SEND_DATA_EVENT,1000);
return(events^SEND_DATA_EVENT);
}
if(events&SHOW_DATA_EVENT)
{
ShowScreen();
DataPack();
DoLight();
osal_start_timerEx(SampleApp_TaskID,SHOW_DATA_EVENT,100);
return(events^SHOW_DATA_EVENT);
}
return0;
}
voidDataPack(void)
{
sendbuf[0]='&';sendbuf[1]='S';//数据包头包括节点类型
sendbuf[2]='C';sendbuf[3]='U';
sendbuf[4]='R';sendbuf[5]='O';
sendbuf[6]='3';
ToString(myMac,NLME_GetExtAddr(),8);//物理地址for(inti=7;i<=22;i++)sendbuf[i]=myMac[i-7];
NWK=NLME_GetShortAddr();//网络地址
ToString(myShort,(uint8*)&NWK,2);
for(inti=23;i<=26;i++)sendbuf[i]=myShort[i-23];
temp=ReadTemp();//温度信息
sendbuf[27]=temp/10+'0';
sendbuf[28]=temp%10+'0';
unsignedcharadc=HalAdcRead(0x00,1);//光感传感器值
sendbuf[29]=adc/100+'0';
sendbuf[30]=(adc/10)%10+'0';
sendbuf[31]=adc%10+'0';
sendbuf[32]=LEDSTATE+'0';//总灯光控制
sendbuf[33]=LEDSTATE5+'0';//两路灯光控制sendbuf[34]=LEDSTATE6+'0';
PEOPLE=P0_4;//来人检测
sendbuf[35]=PEOPLE+'0';
sendbuf[36]=LIGHT;//光感阙值用于判断灯的亮否
for(inti=37;i<=40;i++)sendbuf[i]='z';//待用数据包
sendbuf[41]='&';
sendbuf[42]='\0';
}
staticuint8SerialApp_TxLen;
voidSampleApp_SendData()
{
afAddrType_tSendDataAddr;
SendDataAddr.addrMode=(afAddrMode_t)Addr16Bit;
SendDataAddr.endPoint=SAMPLEAPP_ENDPOINT;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Zigbee 组网 程序
![提示](https://static.bdocx.com/images/bang_tan.gif)