您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 实验12—无线传感网络演示实验
1实验题目:实验12—无线传感网络演示实验实验时间:2015.12.31一、实验目的:掌握ZigBee(cc2530)与传感器节点的串口通信协议。掌握传感器数据的采集与传输过程。学习无线传感网络的搭建。二、实验原理及程序分析:传感器说明串口设置波特率115200,数据位8,停止位1,无校验位传感器底层协议2协调器程序#includeOSAL.h#includeAF.h3#includeZDApp.h#includeZDObject.h#includeZDProfile.h#includestring.h#includeCoordinator.h#includeDebugTrace.h#if!defined(WIN32)#includeOnBoard.h#endif#includehal_led.h#includehal_lcd.h#includehal_key.h#includehal_uart.hconstcId_tGenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS]={GENERICAPP_CLUSTERID\};constSimpleDescriptionFormat_tGenericApp_SimpleDesc={GENERICAPP_ENDPOINT,GENERICAPP_PROFID,GENERICAPP_DEVICEID,GENERICAPP_DEVICE_VERSION,GENERICAPP_FLAGS,GENERICAPP_MAX_CLUSTERS,(cId_t*)GenericApp_ClusterList,0,(cId_t*)NULL};endPointDesc_tGenericApp_epDesc;byteGenericApp_TaskID;byteGenericApp_TransID;unsignedcharuartbuf[128];devStates_tGenericApp_NwkState;voidGenericApp_MessageMSGCB(afIncomingMSGPacket_t*pckt);voidGenericApp_SendTheMessage(void);voidGenericApp_Init(bytetask_id){halUARTCfg_tuartConfig;GenericApp_TaskID=task_id;GenericApp_TransID=0;GenericApp_epDesc.endPoint=GENERICAPP_ENDPOINT;GenericApp_epDesc.task_id=&GenericApp_TaskID;GenericApp_epDesc.simpleDesc=(SimpleDescriptionFormat_t*)&GenericApp_SimpleDesc;GenericApp_epDesc.latencyReq=noLatencyReqs;afRegister(&GenericApp_epDesc);uartConfig.configured=TRUE;uartConfig.baudRate=HAL_UART_BR_115200;4uartConfig.flowControl=FALSE;uartConfig.callBackFunc=NULL;HalUARTOpen(0,&uartConfig);}UINT16GenericApp_ProcessEvent(bytetadk_id,UINT16events){afIncomingMSGPacket_t*MSGpkt;if(events&SYS_EVENT_MSG){MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);while(MSGpkt){switch(MSGpkt-hdr.event){caseZDO_STATE_CHANGE:GenericApp_NwkState=(devStates_t)(MSGpkt-hdr.status);if(GenericApp_NwkState==DEV_ZB_COORD)HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);caseAF_INCOMING_MSG_CMD:GenericApp_MessageMSGCB(MSGpkt);break;default:break;}osal_msg_deallocate((uint8*)MSGpkt);MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);}return(events^SYS_EVENT_MSG);}return0;}voidGenericApp_MessageMSGCB(afIncomingMSGPacket_t*pkt){unsignedcharbuffer[14];inti=0;switch(pkt-clusterId){caseGENERICAPP_CLUSTERID:osal_memcpy(buffer,pkt-cmd.Data,14);uartbuf[0]=0xee;uartbuf[1]=0xcc;uartbuf[2]=0x00;uartbuf[3]=0x00;uartbuf[4]=0x00;uartbuf[5]=HI_UINT16(pkt-srcAddr.addr.shortAddr);uartbuf[6]=LO_UINT16(pkt-srcAddr.addr.shortAddr);uartbuf[7]=0x00;5uartbuf[8]=0x00;uartbuf[9]=HI_UINT16(NLME_GetCoordShortAddr());uartbuf[10]=LO_UINT16(NLME_GetCoordShortAddr());uartbuf[11]=0x01;//stateuartbuf[12]=0x0B;//chaneluartbuf[13]=pkt-endPoint;for(i=14;i=26;i++){uartbuf[i]=buffer[i-12];}HalUARTWrite(0,uartbuf,26);HalLedBlink(HAL_LED_2,0,50,500);break;}}端点端程序#includeOSAL.h#includeAF.h#includeZDApp.h#includeZDObject.h#includeZDProfile.h#includestring.h#includeCoordinator.h#includeDebugTrace.h#if!defined(WIN32)#includeOnBoard.h#endif#includehal_led.h#includehal_lcd.h#includehal_key.h#includehal_uart.h#defineSEND_DATA_EVENT0x01constcId_tGenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS]={GENERICAPP_CLUSTERID\};constSimpleDescriptionFormat_tGenericApp_SimpleDesc={GENERICAPP_ENDPOINT,GENERICAPP_PROFID,GENERICAPP_DEVICEID,GENERICAPP_DEVICE_VERSION,GENERICAPP_FLAGS,0,(cId_t*)NULL,GENERICAPP_MAX_CLUSTERS,6(cId_t*)GenericApp_ClusterList,};endPointDesc_tGenericApp_epDesc;byteGenericApp_TaskID;byteGenericApp_TransID;devStates_tGenericApp_NwkState;unsignedcharuartbuf[14];voidGenericApp_MessageMSGCB(afIncomingMSGPacket_t*pckt);voidGenericApp_SendTheMessage(void);staticvoidrxCB(uint8port,uint8event);staticvoidrxCB(uint8port,uint8event){HalUARTRead(0,uartbuf,14);osal_set_event(GenericApp_TaskID,SEND_DATA_EVENT);}voidGenericApp_Init(bytetask_id){GenericApp_TaskID=task_id;GenericApp_NwkState=DEV_INIT;GenericApp_TransID=0;GenericApp_epDesc.endPoint=GENERICAPP_ENDPOINT;GenericApp_epDesc.task_id=&GenericApp_TaskID;GenericApp_epDesc.simpleDesc=(SimpleDescriptionFormat_t*)&GenericApp_SimpleDesc;GenericApp_epDesc.latencyReq=noLatencyReqs;afRegister(&GenericApp_epDesc);halUARTCfg_tuartConfig;uartConfig.configured=TRUE;uartConfig.baudRate=HAL_UART_BR_115200;uartConfig.flowControl=FALSE;uartConfig.callBackFunc=rxCB;HalUARTOpen(0,&uartConfig);}UINT16GenericApp_ProcessEvent(bytetadk_id,UINT16events){afIncomingMSGPacket_t*MSGpkt;if(events&SYS_EVENT_MSG){MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);while(MSGpkt){switch(MSGpkt-hdr.event){caseZDO_STATE_CHANGE:GenericApp_NwkState=(devStates_t)(MSGpkt-hdr.status);if((GenericApp_NwkState==DEV_END_DEVICE)||(GenericApp_NwkState==DEV_ROUTER)){7HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);osal_set_event(GenericApp_TaskID,SEND_DATA_EVENT);}default:break;}osal_msg_deallocate((uint8*)MSGpkt);MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(GenericApp_TaskID);}return(events^SYS_EVENT_MSG);}if(events&SEND_DATA_EVENT){GenericApp_SendTheMessage();osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EVENT,1000);return(events^SEND_DATA_EVENT);}return0;}voidGenericApp_SendTheMessage(void){afAddrType_tmy_Dst
本文标题:实验12—无线传感网络演示实验
链接地址:https://www.777doc.com/doc-2500024 .html