ZigBee学习五 无线温度检测
1)修改公用头文件GenericApp.h
typedef union h
{
uint8 TEMP[4];
struct RFRXBUF
{
unsigned char Head;
unsigned char value[2];
unsigned char Tail;
}BUF;
}TEMPERATURE;
2)协调器编程
协调器编程时,只需要修改一下消息处理函数GenericApp_MessageMSGCB()即可:
修改coordinator.c文件
byte GenericApp_TransID; // This is the unique message ID (counter)
afAddrType_t GenericApp_DstAddr;
//unsigned char uartbuf[128];
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
static void GenericApp_HandleKeys( byte shift, byte keys );
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );
//static void rxCB(uint8 port,uint8 event);
void GenericApp_Init( uint8 task_id )
{
halUARTCfg_t uartConfig;
GenericApp_TaskID = task_id;
GenericApp_NwkState = DEV_INIT;
GenericApp_TransID = 0;
... ...
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( uint8 task_id, uint16 events )
{
... ...
switch ( MSGpkt->hdr.event )
{
case ZDO_CB_MSG:
break;
case KEY_CHANGE:
break;
case AF_DATA_CONFIRM_CMD:
break;
case AF_INCOMING_MSG_CMD:
GenericApp_MessageMSGCB( MSGpkt );
break;
case ZDO_STATE_CHANGE:
break;
default:
break;
}
... ...
}
/*
static void rxCB(uint8 port,uint8 event)
{
HalUARTRead(0,uartbuf,16); //读取数据并存放到uartbuf数组中
if(osal_memcmp(uartbuf,"www.wlwmaker.com",16)) //使用osal_memcmp()函数判断接收到的数据是否是字符串"www.wlwmaker.com",如果是,执行{}
{
HalUARTWrite(0,uartbuf,16); //调用HalUARTWrite()函数将接收到的字符输出到串口
}
}
*/
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
unsigned char buffer[2] = {0x0A,0x0D};
TEMPERATURE temperature;
switch ( pkt->clusterId )
{
case GENERICAPP_CLUSTERID:
osal_memcpy(&temperature,pkt->cmd.Data,sizeof(temperature));
HalUARTWrite(0,(uint8 *)&temperature,sizeof(temperature));
HalUARTWrite(0,buffer,2);
break;
}
}
3)终端节点编程
建立驱动文件Sensor.h和Sensor.c
Sensor.h文件
#ifndef SENSOR_H
#define SENSOR_H
#include <hal_types.h>
extern int8 readTemp(void);
#endif
Sensor.c文件
#include "Sensor.h"
#include <ioCC2530.h>
#define HAL_ADC_REF_115V 0x00
#define HAL_ADC_DEC_256 0x20
#define HAL_ADC_CHN_TEMP 0x0e
int8 readTemp(void)
{
static uint16 reference_voltage;
static uint8 bCalibrate = TRUE;
uint16 value;
int8 temp;
ATEST =0x01;
TR0 |= 0x01;
ADCIF = 0;
ADCCON3 = (HAL_ADC_REF_115V|HAL_ADC_DEC_256|HAL_ADC_CHN_TEMP);
while(!ADCIF);
ADCIF = 0;
value = ADCL;
value |= ((uint16)ADCH)<<8;
value >>=4;
if(bCalibrate)
{
reference_voltage = value;
bCalibrate = FALSE;
}
temp = 22 + ((value - reference_voltage)/4);
return temp;
}
修改enddevice.c文件
只需修改消息发送函数就行了
static void GenericApp_SendTheMessage( void )
{
//char theMessageData[] = "EndDevice";
uint8 tvalue;
TEMPERATURE temperature;
temperature.BUF.Head = '&';
tvalue = readTemp();
temperature.BUF.value[0] = tvalue /10 +'0';
temperature.BUF.value[1] = tvalue %10 +'0';
temperature.BUF.Tail = 'C';
afAddrType_t my_DstAddr;
my_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;//单播发送
my_DstAddr.endPoint = GENERICAPP_ENDPOINT; //目的端口号
my_DstAddr.addr.shortAddr = 0x0000; //协调器网络地址
if ( AF_DataRequest( &my_DstAddr, &GenericApp_epDesc,
GENERICAPP_CLUSTERID,
sizeof(temperature ),
(byte *)&temperature,
&GenericApp_TransID,
AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
// Successfully requested to be sent.
HalLedBlink(HAL_LED_1,0,50,500);
}else
{
// Error occurred in request to send.
}
}