CC2530,超声波传感器通过协议栈z-stack2.5.1发送距离信息出现错误
#ifndef COORDINATOR_H
#define COORDINATOR_H
#include "ZComDef.h"
#define GENERICAPP_ENDPOINT 10
#define GENERICAPP_PROFID 0x0F04
#define GENERICAPP_DEVICEID 0x0001
#define GENERICAPP_DEVICE_VERSION 0
#define GENERICAPP_FLAGS 0
#define GENERICAPP_MAX_CLUSTERS 1
#define GENERICAPP_CLUSTERID 1
typedef union h
{
unsigned char databuf[18];
struct RFRXBUF
{
unsigned char head[2];
unsigned char type[3];
unsigned char myNWK[4];
unsigned char pNWK[4];
unsigned char value[4];
unsigned char tail;
}BUF;
}RFTX;
extern void GenericApp_Init(byte task_id);
extern UINT16 GenericApp_ProcessEvent(byte tadk_id,UINT16 events);
#endif
////////////////////////////Coordinator.c/////////////
#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_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include "OSAL_Nv.h"
#define SEND_TO_ALL_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,
GENERICAPP_MAX_CLUSTERS,
(cId_t *)GenericApp_ClusterList,
0,
(cId_t *)NULL
};
endPointDesc_t GenericApp_epDesc;
devStates_t GenericApp_NwkState;
byte GenericApp_TaskID;
byte GenericApp_TransID;
RFTX nodeinfo[3];
uint8 nodenum=0;
//unsigned char uartbuf[128];
void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pckt);
void GenericApp_SendTheMessage(void);
/*
static void rxCB(uint8 port, uint8 event);
static void rxCB(uint8 port, uint8 event)
{
unsigned char changeline[2]={0x0A,0x0D};
uint8 buf[8];
uint8 i=0;
HalUARTRead(0,buf,8);
if(osal_memcmp(buf,"topology",8))
{
for(i=0;i<3;i++)
{
HalUARTWrite(0,nodeinfo[i].type,3);
HalUARTWrite(0," NWK: ",8);
HalUARTWrite(0,nodeinfo[i].myNWK,4);
HalUARTWrite(0," pNWK: ",9);
HalUARTWrite(0,nodeinfo[i].pNWK,4);
HalUARTWrite(0,changeline,2);
}
}
}*/
void GenericApp_Init(byte task_id)
{
halUARTCfg_t uartConfig;
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;
uartConfig.flowControl =FALSE;
uartConfig.callBackFunc =NULL;
HalUARTOpen(0,&uartConfig);
}
UINT16 GenericApp_ProcessEvent(byte tadk_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 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)
{
RFTX rftx;
unsigned char changeline[2]={0x0A,0x0D};
switch(pkt->clusterId)
{
case GENERICAPP_CLUSTERID:
osal_memcpy(&rftx,pkt->cmd.Data,sizeof(rftx));
HalUARTWrite(0,rftx.databuf,sizeof(rftx));
HalUARTWrite(0,changeline,2);
break;
}
}
void GenericApp_SendTheMessage(void)
{
/*unsigned char *theMessageData="Coordinator send!";
afAddrType_t my_DstAddr;
my_DstAddr.addrMode=(afAddrMode_t)AddrGroup;
my_DstAddr.endPoint=GENERICAPP_ENDPOINT;
my_DstAddr.addr.shortAddr=GenericApp_Group.ID;
AF_DataRequest(&my_DstAddr,
&GenericApp_epDesc,
GENERICAPP_CLUSTERID,
osal_strlen(theMessageData)+1,
theMessageData,
&GenericApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);*/
}
////////////////////////////Enddevice.c//////////////////////
#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_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include "sensor.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 SendInfo(void);
void To_string(uint8 *dest,char *src,uint8 length);
//void sendVdd(void);
void sendTemp(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);
}
UINT16 GenericApp_ProcessEvent(byte tadk_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_ROUTER)||(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)
{
sendTemp();
// sendVdd();
osal_start_timerEx(GenericApp_TaskID,SEND_DATA_EVENT,5000);
return (events^SEND_DATA_EVENT);
}
return 0;
}
void sendTemp(void)
{
RFTX rftx;
uint16 nwk;
UltrasoundRanging(LoadRegBuf);
Delay_1s(1);
data=256*H2+L2-L1-256*H1;
distance=(float)data*340/10000; //cm
Delay_1us(1);
uint32 tempvalue;
tempvalue=distance;
rftx.BUF.value[0]='D';
rftx.BUF.value[1]=tempvalue/100+'0';//bai
rftx.BUF.value[2]=tempvalue%100/10+'0';//shi
rftx.BUF.value[3]=tempvalue%100%10+'0';//ge
osal_memcpy(rftx.BUF.head,"&&",2);
if(GenericApp_NwkState==DEV_END_DEVICE)
{
osal_memcpy(rftx.BUF.type,"END",3);
}
if(GenericApp_NwkState==DEV_ROUTER)
{
osal_memcpy(rftx.BUF.type,"ROU",3);
}
nwk=NLME_GetShortAddr();
To_string(rftx.BUF.myNWK,(uint8 *)&nwk,2);
nwk=NLME_GetCoordShortAddr();
To_string(rftx.BUF.pNWK,(uint8 *)&nwk,2);
rftx.BUF.tail='&';
afAddrType_t my_DstAddr;
my_DstAddr.addrMode=(afAddrMode_t)Addr16Bit;
my_DstAddr.endPoint=GENERICAPP_ENDPOINT;
my_DstAddr.addr.shortAddr=0x0000;
AF_DataRequest(&my_DstAddr,
&GenericApp_epDesc,
GENERICAPP_CLUSTERID,
18,
(uint8 *)&rftx,
&GenericApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS);
HalLedSet(HAL_LED_2,HAL_LED_MODE_TOGGLE);
}
void To_string(uint8 * dest, char * src, uint8 length)
{
uint8 *xad;
uint8 i=0;
uint8 ch;
xad=src+length-1;
for(i=0;i<length;i++,xad--)
{
ch=(*xad>>4)&0x0F;
dest[i<<1]=ch+((ch<10)?'0':'7');
ch=*xad&0x0F;
dest[(i<<1)+1]=ch+((ch<10)?'0':'7');
}
}
////////////////sensor.h////////////////////
#ifndef ULTRASOUND_H
#define ULTRASOUND_H
#define uchar unsigned char
#define uint unsigned int
#define TRIG P1_2
#define ECHO P0_1
extern uchar H1;
extern uchar L1;
extern uchar H2;
extern uchar L2;
extern uint data;
extern float distance;
extern uchar LoadRegBuf[4];
//void Delay(uint n);
void Delay_1us(uint microSecs);
void Delay_10us(uint n);
void Delay_1s(uint n);
void SysClkSet32M();
void Init_UltrasoundRanging();
void UltrasoundRanging(uchar *ulLoadBufPtr);
__interrupt void P0_ISR(void);
#endif
//////////////////sensor.c/////////////////////////////////////////
#include <ioCC2530.h>
#include "sensor.h"
uchar H1;
uchar L1;
uchar H2;
uchar L2;
uint data;
float distance;
uchar LoadRegBuf[4];//全局数据,用以存储定时计数器的值。
/////////////////////////延时子函数///////////////////////////////
void Delay_10us(uint n) /* 320NOPs == 10usecs 因为延时还有计算的缘故,用了310个nop*/
{
uint tt,yy;
for(tt = 0;tt<n;tt++);
for(yy = 310;yy>0;yy--);
{asm("NOP");}
}
void Delay_1s(uint n)
{ uint ulloop=1000;
uint tt;
for(tt =n ;tt>0;tt--);
for( ulloop=1000;ulloop>0;ulloop--)
{
Delay_10us(100);
}
}
void SysClkSet32M()
{
CLKCONCMD &= 0x40; //设置系统时钟源为32MHZ晶振
while(CLKCONSTA & 0x40); //等待晶振稳定
CLKCONCMD &= 0x47; //设置系统主时钟频率为32MHZ
//此时的CLKCONSTA为0x88。即普通时钟和定时器时钟都是32M。
}
void Init_UltrasoundRanging()
{
P1DIR = 0X04; //0为输入(默认),1为输出 00000100 TRIG P1_2
TRIG=0;
P0INP &= 0X02; //有上拉、下拉 有初始化的左右
P0IEN |= 0X02; //P0_1 中断使能
PICTL |= 0X01; //设置P0_1引脚,下降沿触发中断
IEN1 |= 0X20; // P0IE = 1;
}
void UltrasoundRanging(uchar *ulLoadBufPtr)
{
SysClkSet32M();
Init_UltrasoundRanging();
EA = 0;
TRIG =1;
Delay_1us(10); //需要延时10us以上的高电平
TRIG =0;
T1CNTL=0;
T1CNTH=0;
while(!ECHO);
T1CTL = 0x09; //通道0,中断有效,32分频;自动重装模式(0x0000->0xffff);
L1=T1CNTL;
H1=T1CNTH;
*ulLoadBufPtr++=T1CNTL;
*ulLoadBufPtr++=T1CNTH;
EA = 1;
Delay_10us(60000);
Delay_10us(60000);
}
#pragma vector = P0INT_VECTOR
__interrupt void P0_ISR(void)
{
EA=0;
T1CTL = 0x00;
LoadRegBuf[2]=T1CNTL;
LoadRegBuf[3]=T1CNTH;
L2=T1CNTL;
H2=T1CNTH;
if(P0IFG&0x02) //按键中断
{
P0IFG = 0;
}
T1CTL = 0x09;
P0IF = 0; //清中断标志
EA=1;
}
void main()
{
while(1)
{UltrasoundRanging(LoadRegBuf);
Delay_1s(1);
data=256*H2+L2-L1-256*H1;
distance=(float)data*340/10000; //cm
Delay_1us(1);
};
}
纠正://////////////////sensor.c/////////////////////////////////////////
#include <ioCC2530.h>
#include "sensor.h"
uchar H1;
uchar L1;
uchar H2;
uchar L2;
uint data;
float distance;
uchar LoadRegBuf[4];//全局数据,用以存储定时计数器的值。
/////////////////////////延时子函数///////////////////////////////
void Delay_10us(uint n) /* 320NOPs == 10usecs 因为延时还有计算的缘故,用了310个nop*/
{
uint tt,yy;
for(tt = 0;tt<n;tt++);
for(yy = 310;yy>0;yy--);
{asm("NOP");}
}
void Delay_1s(uint n)
{ uint ulloop=1000;
uint tt;
for(tt =n ;tt>0;tt--);
for( ulloop=1000;ulloop>0;ulloop--)
{
Delay_10us(100);
}
}
void SysClkSet32M()
{
CLKCONCMD &= 0x40; //设置系统时钟源为32MHZ晶振
while(CLKCONSTA & 0x40); //等待晶振稳定
CLKCONCMD &= 0x47; //设置系统主时钟频率为32MHZ
//此时的CLKCONSTA为0x88。即普通时钟和定时器时钟都是32M。
}
void Init_UltrasoundRanging()
{
P1DIR = 0X04; //0为输入(默认),1为输出 00000100 TRIG P1_2
TRIG=0;
P0INP &= 0X02; //有上拉、下拉 有初始化的左右
P0IEN |= 0X02; //P0_1 中断使能
PICTL |= 0X01; //设置P0_1引脚,下降沿触发中断
IEN1 |= 0X20; // P0IE = 1;
}
void UltrasoundRanging(uchar *ulLoadBufPtr)
{
SysClkSet32M();
Init_UltrasoundRanging();
EA = 0;
TRIG =1;
Delay_1us(10); //需要延时10us以上的高电平
TRIG =0;
T1CNTL=0;
T1CNTH=0;
while(!ECHO);
T1CTL = 0x09; //通道0,中断有效,32分频;自动重装模式(0x0000->0xffff);
L1=T1CNTL;
H1=T1CNTH;
*ulLoadBufPtr++=T1CNTL;
*ulLoadBufPtr++=T1CNTH;
EA = 1;
Delay_10us(60000);
Delay_10us(60000);
}
#pragma vector = P0INT_VECTOR
__interrupt void P0_ISR(void)
{
EA=0;
T1CTL = 0x00;
LoadRegBuf[2]=T1CNTL;
LoadRegBuf[3]=T1CNTH;
L2=T1CNTL;
H2=T1CNTH;
if(P0IFG&0x02) //按键中断
{
P0IFG = 0;
}
T1CTL = 0x09;
P0IF = 0; //清中断标志
EA=1;
}