完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
我想编一个程序实现:Zigbee模块中光敏传感器采集到的值通过无线通信传给另外一个模块,并在串口打印出来。
协调器程序: #include "OSAL.h" #include "AF.h" #include "ZDApp.h" #include "ZDObject.h" #include "ZDProfile.h" #include #include "Coordinator.h" #include "DebugTrace.h" #if !defined( WIN32 ) #include "OnBoard.h" #endif #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "hal_uart.h" const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] = { GENERICAPP_CLUSTERID }; const SimpleDescriptionFormat_t GenericApp_SimpleDesc = //简单设备描述符 { GENERICAPP_ENDPOINT, // int Endpoint; GENERICAPP_PROFID, // uint16 AppProfId[2]; GENERICAPP_DEVICEID, // uint16 AppDeviceId[2]; GENERICAPP_DEVICE_VERSION, // int AppDevVer:4; GENERICAPP_FLAGS, // int AppFlags:4; GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters; (cId_t *)GenericApp_ClusterList, // byte *pAppInClusterList; 0 , // byte AppNumInClusters; (cId_t *)NULL // byte *pAppInClusterList; }; /////定义了三个变量 endPointDesc_t GenericApp_epDesc; //节点描述符 byte GenericApp_TaskID; //任务优先级 byte GenericApp_TransID; //数据发送序列号 void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );//声明消息处理函数 void GenericApp_SendTheMessage( void );//声明数据发送函数 void GenericApp_Init(byte task_id) //任务初始化函数 { halUARTCfg_t uartConfig; GenericApp_TaskID = task_id; //初始化了任务优先级 GenericApp_TransID = 0; //发送数据包的序号初始化为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; uartConfig.flowControl =FALSE; uartConfig.callBackFunc =NULL; HalUARTOpen(0,&uartConfig); } UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events )//消息处理函数 { afIncomingMSGPacket_t *MSGpkt; //定义了一个指向接收消息结构体的指针MSGpkt if ( events & SYS_EVENT_MSG ) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { case AF_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); } return 0; } void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { int AdcValue; unsigned char buffer[2] ={0x0A,0x0D}; switch ( pkt->clusterId ) { case GENERICAPP_CLUSTERID: osal_memcpy(&AdcValue,pkt->cmd.Data,2); HalUARTWrite(0,(uint8 *)&AdcValue,2); HalUARTWrite(0,buffer,2); break; } } 终端节点程序: #include "OSAL.h" #include "AF.h" #include "ZDApp.h" #include "ZDProfile.h" #include "ZDObject.h" #include #include "Coordinator.h" #include "DebugTrace.h" #include #if !defined(WIN32) #include "OnBoard.h" #endif #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "hal_uart.h" #define SEND_DATA_EVENT 0x01 const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] = { GENERICAPP_CLUSTERID }; const SimpleDescriptionFormat_t GenericApp_SimpleDesc = { GENERICAPP_ENDPOINT, GENERICAPP_PROFID, GENERICAPP_DEVICEID, GENERICAPP_DEVICE_VERSION, GENERICAPP_FLAGS, 0, (cId_t *)NULL, GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList }; endPointDesc_t GenericApp_epDesc; byte GenericApp_TaskID; byte GenericApp_TransID; devStates_t GenericApp_NwkState; //保存节点的状态 void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );//声明消息处理函数 void GenericApp_SendTheMessage( void );//声明数据发送函数 int getADC(void) ; void GenericApp_Init(byte task_id) { halUARTCfg_t uartConfig; 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); /*uartConfig.configured =TRUE; uartConfig.baudRate =HAL_UART_BR_115200; uartConfig.flowControl =FALSE; uartConfig.callBackFunc =NULL; HalUARTOpen(0,&uartConfig);*/ } UINT16 GenericApp_ProcessEvent(byte task_id,UINT16 events) { afIncomingMSGPacket_t *MSGpkt; if(events & SYS_EVENT_MSG) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(GenericApp_TaskID); while(MSGpkt) { switch(MSGpkt->hdr.event) { case ZDO_STATE_CHANGE: GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status); if(GenericApp_NwkState == DEV_END_DEVICE)//加入网络 { osal_set_event(GenericApp_TaskID,SEND_DATA_EVENT); } break; 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); } return 0; } void GenericApp_SendTheMessage(void) { // char StrAdc[10]; int AdcValue; AdcValue=getADC(); //sprintf(StrAdc,"%drn",AdcValue); afAddrType_t my_DstAddr; //定义了afAddrType_t的变量 my_DstAddr.addrMode=(afAddrMode_t)Addr16Bit; //发送数据的方式为单播 my_DstAddr.endPoint=GENERICAPP_ENDPOINT; //初始化端口号 my_DstAddr.addr.shortAddr=0x0000; //协调器的网络地址是固定的0x0000; AF_DataRequest(&my_DstAddr,&GenericApp_epDesc, //调用数据发送函数进行无线数据的发送 GENERICAPP_CLUSTERID, //描述的是命令号 2, //标识了发送数据的长度 (uint8 *)&AdcValue, &GenericApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ); } int getADC(void) { unsigned int value; P0SEL |= 0x02; ADCCON3 = (0xB1); //选择AVDD5为参考电压;12分辨率;P0_1 ADC ADCCON1 |= 0x30; //选择ADC的启动模式为手动 ADCCON1 |= 0x40; //启动AD转化 while(!(ADCCON1 & 0x80)); //等待ADC转化结束 value = ADCL >> 2; value |= (ADCH << 6); //取得最终转化结果,存入value中 return ((value) >> 2); } |
|
相关推荐 |
|
只有小组成员才能发言,加入小组>>
物联网工程师必备:怎么选择不同的无线连接技术,本指南帮你忙!
3293 浏览 1 评论
【DFRobot TinkerNode NB-IoT 物联网开发板试用连载】WIFI功能测试
3945 浏览 0 评论
【DFRobot TinkerNode NB-IoT 物联网开发板试用连载】Arduino的替代SublimeText3+STino
3449 浏览 0 评论
使用端口扩展器轻松高效地向IIoT端点添加具有成本效益的子节点
4015 浏览 1 评论
20691 浏览 11 评论
模组有时候复位重启后输出日志为“REBOOT_CAUSE_SECURITY_PMU_POWER_ON_RESET”的原因?
812浏览 2评论
1041浏览 2评论
1041浏览 1评论
1154浏览 1评论
387浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-28 00:00 , Processed in 0.847068 second(s), Total 79, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号