求助啊 帮忙看一下 谢谢啦
时间:10-02
整理:3721RD
点击:
我想编一个程序实现:Zigbee模块中光敏传感器采集到的值通过无线通信传给另外一个模块,并在串口打印出来。
协调器程序:
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include<string.h>
#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 <string.h>
#include "Coordinator.h"
#include "DebugTrace.h"
#include<stdio.h>
#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,"%d\r\n",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);
}
协调器程序:
#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include<string.h>
#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 <string.h>
#include "Coordinator.h"
#include "DebugTrace.h"
#include<stdio.h>
#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,"%d\r\n",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);
}