您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > zigbee无线开发实验材料
《ZigBee无线开发》试讲试做课堂内容计算机与通信工程学院邢海霞实验一:ZigBee任务创建与添加一实验目的:1、掌握OSAL操作系统运行机制2、掌握任务创建步骤及添加方式二实验内容:创建一任务实现串口输出“hello”字符串到PC机,并将该任务添加到OSAL操作系统。任务实施过程1、创建任务:新建一hello.c文件#includehello.huint8HelloID;voidHello_Init(uint8task_id){HelloID=task_id;//获取任务ID号osal_start_timerEx(task_id,SYS_EVENT_MSG,5000);//设置闹钟}uint16Hello_ProcessEvent(uint8task_id,uint16events){if(events&SYS_EVENT_MSG){HalUARTWrite(HAL_UART_PORT_0,Hello\r\n,7);//串口发送osal_start_timerEx(task_id,SYS_EVENT_MSG,5000);//设置闹钟}return(events^SYS_EVENT_MSG);}2、将任务添加到OSAL操作系统(1)添加任务头文件:#include”hello.h”(2)添加任务的事件处理函数到tasksArr[]数组:Hello_ProcessEvent(3)添加任务初始化函数到osalInitTasks();Hello_Init(task_id)3、单击编译4、进行硬件连接:5、单击download下载6、打开串口调试助手查看运行结果:实验二:ZigBee任务间通信一实验目的:1、掌握在zStack协议栈中创建多任务的方法2、掌握闹钟设置方法3、掌握任务间通信方法二实验内容:在协议栈中创建任务1和任务2。任务1给任务2发送字符串“ZigBee”消息,任务2收到消息后将收到的内容串口输出。实施过程:1、创建任务:Study.c#includestudy.huint8StudyTaskID=0;voidStudy_Init(uint8task_id){StudyTaskID=task_id;osal_set_event(StudyTaskID,SYS_EVENT_MSG);//设置系统事件}uint16Study_ProcessEvent(uint8task_id,uint16events){uint8*message;if(events&SYS_EVENT_MSG){message=osal_msg_allocate(10);//开辟空间osal_memcpy(message,ZigBee\n,7);osal_msg_send(ZigBeeTaskID,message);//发送消息}osal_start_timerEx(task_id,SYS_EVENT_MSG,5000);设置闹钟return(events^SYS_EVENT_MSG);}ZigBee.c:#includeZigBee.huint8ZigBeeTaskID=0;voidZigBee_Init(uint8task_id){ZigBeeTaskID=task_id;osal_set_event(ZigBeeTaskID,SYS_EVENT_MSG);//设置事件}uint16ZigBee_ProcessEvent(uint8task_id,uint16events){uint8*message;if(events&SYS_EVENT_MSG){message=osal_msg_receive(ZigBeeTaskID);//接收消息HalUARTWrite(HAL_UART_PORT_0,message,7);//串口发送该消息}osal_msg_deallocate(message);//释放空间return(events^SYS_EVENT_MSG);}2、将任务添加到OSAL操作系统(1)添加任务头文件:#include”study.h”#include”ZigBee.h”(2)添加任务的事件处理函数到tasksArr[]数组:Study_ProcessEvent,ZigBee_ProcessEvent(3)添加任务初始化函数到osalInitTasks();Study_Init(taskID);ZigBee_Init(taskID);3、单击编译4、进行硬件连接:5、单击download下载6、打开串口调试助手查看运行结果:实验三:基于CC2530的光照采集系统一实验目的:1、掌握zStack协议栈传感器驱动开发2、掌握星状网络拓扑结构的组建3、掌握ZigBee无线通信原理及实现方法二实验内容:实现一终端节点采集光照信息并将信息发送给协调器,协调器将采集数据通过串口发给PC机。1、将SampleApp.c和SampleApp.h文件添加到workspace,修改SampleApp.c文件代码如下:uint16SampleApp_ProcessEvent(uint8task_id,uint16events){afIncomingMSGPacket_t*MSGpkt;(void)task_id;//Intentionallyunreferencedparameterif(events&SYS_EVENT_MSG){MSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(SampleApp_TaskID);while(MSGpkt){switch(MSGpkt-hdr.event){#ifdefined(ZDO_COORDINATOR)caseCMD_SERIAL_MSG:SampleApp_UartMessage((uint8*)MSGpkt);HalLedBlink(HAL_LED_1,2,50,90);break;#endif#ifdefined(ZDO_COORDINATOR)||defined(CONTROL)//Receivedwhenamessagesisreceived(OTA)forthisendpointcaseAF_INCOMING_MSG_CMD:SampleApp_MessageMSGCB(MSGpkt);break;#endif//ReceivedwheneverthedevicechangesstateinthenetworkcaseZDO_STATE_CHANGE:SampleApp_NwkState=(devStates_t)(MSGpkt-hdr.status);if((SampleApp_NwkState==DEV_ZB_COORD)||(SampleApp_NwkState==DEV_ROUTER)||(SampleApp_NwkState==DEV_END_DEVICE)){#ifdefined(ZDO_COORDINATOR)ZigBeeNode.NodeMessage.head[0]=SYNC;ZigBeeNode.NodeMessage.head[1]=SYNC;//填写数据包同步头ZigBeeNode.NodeMessage.SAddr=NLME_GetShortAddr();//获取网络地址ZigBeeNode.NodeMessage.PAddr=NLME_GetCoordShortAddr();//获取网络地址memcpy(ZigBeeNode.NodeMessage.IEEE,NLME_GetExtAddr(),8);//获取物理地址ZigBeeNode.NodeMessage.SendCycle=1;ZigBeeNode.NodeMessage.NodeDesc=COORDINATOR;HalUARTWrite(HAL_UART_PORT_0,ZigBeeNode.TxBuf,17);HalLedBlink(HAL_LED_2,4,50,250);//协调器建立网络成功,LED灯闪烁4次。#elifdefined(RTR_NWK)&&(!defined(ZDO_COORDINATOR))ZigBeeNode.NodeMessage.head[0]=SYNC;ZigBeeNode.NodeMessage.head[1]=SYNC;//填写数据包同步头ZigBeeNode.NodeMessage.SAddr=NLME_GetShortAddr();//获取网络地址ZigBeeNode.NodeMessage.PAddr=NLME_GetCoordShortAddr();//获取网络地址memcpy(ZigBeeNode.NodeMessage.IEEE,NLME_GetExtAddr(),8);//获取物理地址ZigBeeNode.NodeMessage.SendCycle=1;ZigBeeNode.NodeMessage.NodeDesc=ROUTER;SendData(ZigBeeNode.TxBuf,0x0000,18);//节点向协调器发送采集数据HalLedBlink(HAL_LED_2,4,50,250);#elseZigBeeNode.NodeMessage.head[0]=SYNC;ZigBeeNode.NodeMessage.head[1]=SYNC;//填写数据包同步头ZigBeeNode.NodeMessage.SAddr=NLME_GetShortAddr();//获取网络地址ZigBeeNode.NodeMessage.PAddr=NLME_GetCoordShortAddr();//获取网络地址memcpy(ZigBeeNode.NodeMessage.IEEE,NLME_GetExtAddr(),8);//获取物理地址ZigBeeNode.NodeMessage.SendCycle=1;HalLedBlink(HAL_LED_2,4,50,250);osal_start_timerEx(SampleApp_TaskID,SAMPLEAPP_SEND_PERIODIC_MSG_EVT,SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);#endif}else{//Deviceisnolongerinthenetwork}break;default:break;}//Releasethememoryosal_msg_deallocate((uint8*)MSGpkt);//Next-ifoneisavailableMSGpkt=(afIncomingMSGPacket_t*)osal_msg_receive(SampleApp_TaskID);}//returnunprocessedeventsreturn(events^SYS_EVENT_MSG);}//Sendamessageout-Thiseventisgeneratedbyatimer//(setupinSampleApp_Init()).#if!defined(ZDO_COORDINATOR)if(events&SAMPLEAPP_SEND_PERIODIC_MSG_EVT){//SendtheperiodicmessageSampleApp_SendPeriodicMessage();//SendData(xing,0x0000,4);//xhx//Setuptosendmessageagaininnormalperiod(+alittlejitter)osal_start_timerEx(SampleApp_TaskID,SAMPLEAPP_SEND_PERIODIC_MSG_EVT,SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT);//returnunprocessedeventsreturn(events^SAMPLEAPP_SEND_PERIODIC_MSG_EVT);}#endif
本文标题:zigbee无线开发实验材料
链接地址:https://www.777doc.com/doc-2829830 .html