总共有7个程序,完整的在附件里面。
/****************************************************************************
程序名称:2石油钻井平台监控系统_传感器_节点3_20090527_Router
程序编写:唐爱明
编写日期:20090527
使用模块:Router
程序备注: 超声波液位传感器,0~20mA电流输出,使用AD3
由Coordinator发送命令,Router执行命令并返回数据
输入"ywon",返回AD3值,开DIO13
输入"ywoff",关闭AD3值,关DIO13
输入其他则不动作
扫描时间计算:Duration (ms) = ((960 * (2^ENERGY_SCAN_DURATION + 1)) / 62.5)
主动扫描和能量扫描时间均为138.24ms
****************************************************************************/
/****************************************************************************/
/*** 头文件 ***/
/****************************************************************************/
#include <jendefs.h>//数据类型定义
#include <AppHardwareApi.h>//硬件外设
#include <AppQueueApi.h> //队列
#include <mac_sap.h>//变量定义
#include <AppApi.h>//PIB句柄
#include <mac_pib.h>//PAN Information Base设置
#include <string.h>//绑定
#include "config.h"//网络参数设置
#include "serialq.h"//串口队列
#include "uart.h"//通用异步收发机设置
#include "serial.h"//串口相关
#include "LedControl.h"//灯控
/****************************************************************************/
/*** 类型定义 ***/
/****************************************************************************/
/* 设备数据系统状态4个 */
typedef enum
{
E_STATE_OFF,//停止
E_STATE_SCANNING,//扫描
E_STATE_ASSOCIATING,//联网
E_STATE_RUNNING,//运行
} teState;
/* 设备数据*/
typedef struct
{
struct
{
teState eState;//状态
uint8 u8Channel;//通道
uint16 u16ShortAddr;//短地址
} sSystem;
} tsDeviceData;
/* 读电池电压4状态*/
typedef enum
{
E_STATE_READ_BATT_VOLT_IDLE,//空闲状态
E_STATE_READ_BATT_VOLTS_ADC_CONVERTING,//转换状态
E_STATE_READ_BATT_VOLTS_COMPLETE,//完成状态
E_STATE_READ_BATT_VOLTS_READY,//准备好了状态
}teAD;
/* 电池电压测量数据*/
typedef struct
{
uint16 u16BAT;//BAT值
uint16 u16YW3;//YW3值
teAD eState;//电池状态
}tsADSensor;
/****************************************************************************/
/*** 全局变量 ***/
/****************************************************************************/
PRIVATE tsDeviceData sDeviceData;//设备数据变量转换
PRIVATE tsADSensor sADSensor;//电池电压数据变量转换
PRIVATE MAC_Pib_s *psPib;//PIB句柄
PRIVATE void *pvMac;//PIB句柄
uint8 u8TxFrameHandle = 0;//发送数据帧数
uint8 u8RxFrameHandle = 0;//接收数据帧数
char charSensorData[13];//定义数组以存储传感器数据各位
uint32 u32YW3DataNO = 0;//记录AD3数据帧数
/****************************************************************************/
/*** 函数声明 ***/
/****************************************************************************/
PRIVATE void vInit(void);//初始化
PRIVATE void vStartActiveScan(void);//开始主动扫描
PRIVATE void vStartAssociate(void);//开始联网
PRIVATE void vHandleActiveScanResponse(MAC_MlmeDcfmInd_s *psMlmeInd);//主动扫描响应
PRIVATE void vHandleAssociateResponse(MAC_MlmeDcfmInd_s *psMlmeInd);//联网响应
PRIVATE void vReadADC(void);//读取电池电压
PRIVATE void vFuZhiYW3(void);//AD3赋值,将数据转换成字符串
PRIVATE void vToCoord_TxData(void);//往Coordinator发送数据
PRIVATE void vProcessEventQueues(void);//处理事件队列
PRIVATE void vProcessIncomingMlme(MAC_MlmeDcfmInd_s *psMlmeInd);// 处理管理实体
PRIVATE void vProcessIncomingData(MAC_McpsDcfmInd_s *psMcpsInd);// 处理数据实体
PRIVATE void vProcessIncomingHwEvent(AppQApiHwInd_s *psAHI_Ind);// 处理硬件事件
/****************************************************************************
* NAME: 冷启动
* DESCRIPTION:
****************************************************************************/
PUBLIC void AppColdStart(void)
{
vInit();//初始化
vStartActiveScan();//开始主动扫描
while(1)
{
vProcessEventQueues();//处理事件队列
}
}
/****************************************************************************
* NAME: 热启动
* DESCRIPTION:
****************************************************************************/
PUBLIC void AppWarmStart(void)
{
AppColdStart();// 调用冷启动
}
/****************************************************************************
* NAME: 初始化
* DESCRIPTION:
****************************************************************************/
PRIVATE void vInit(void)
{
sDeviceData.sSystem.eState = E_STATE_OFF;//设备数据系统关闭状态
(void)u32AppQApiInit(NULL, NULL, NULL); //初始化事件队列应用接口
(void)u32AHI_Init();//初始化协议栈
vLedInitRfd();//初始化EndDevice上的2个LED
vLedControl(0,0);//关LED1
vLedControl(1,0);//关LED2
/* Set LED to output */
vAHI_DioSetDirection(0, E_AHI_DIO13_INT);
/* LED端口上拉 */
vAHI_DioSetPullup(E_AHI_DIO13_INT, 0);
pvMac = pvAppApiGetMacHandle();//MAC句柄
psPib = MAC_psPibGetHandle(pvMac);//PIB句柄
MAC_vPibSetPanId(pvMac, PAN_ID);//设置PAN_ID
MAC_vPibSetRxOnWhenIdle(pvMac, 1, FALSE);//很重要的一个函数,将影响到后面Enable receiver to be on when idle
vSerial_Init();//串口初始化
/*电池电压初始化设置*/
sADSensor.eState = E_STATE_READ_BATT_VOLT_IDLE;//电池状态初始化,置为空闲模式
vAHI_ApConfigure(E_AHI_AP_REGULATOR_ENABLE,//使能模拟外设
E_AHI_AP_INT_DISABLE,//关闭中断
E_AHI_AP_SAMPLE_2,//时钟分频周期
E_AHI_AP_CLOCKDIV_2MHZ,//时钟分频
E_AHI_AP_INTREF);//内部基准电压
while(!bAHI_APRegulatorEnabled());//掉电
vAHI_AdcEnable(E_AHI_ADC_CONVERT_DISABLE,//不使能ADC转换
E_AHI_AP_INPUT_RANGE_2,//输入电压范围设置
E_AHI_ADC_SRC_VOLT&&//转换电池电压
E_AHI_ADC_SRC_ADC_3 );//转换通道ADC3
}
/****************************************************************************
* NAME: 处理进程队列
* DESCRIPTION:
****************************************************************************/
PRIVATE void vProcessEventQueues(void)
{
MAC_MlmeDcfmInd_s *psMlmeInd;//管理实体变量指针
MAC_McpsDcfmInd_s *psMcpsInd;//数据实体变量指针
AppQApiHwInd_s *psAHI_Ind;//硬件时间变量指针
do
{
psMlmeInd = psAppQApiReadMlmeInd();//读取管理实体
if (psMlmeInd != NULL)//如果有管理实体发生
{
vProcessIncomingMlme(psMlmeInd);//处理输入管理实体
vAppQApiReturnMlmeIndBuffer(psMlmeInd); //返回管理实体缓存
}
} while (psMlmeInd != NULL);//当有管理实体发生
do
{
psMcpsInd = psAppQApiReadMcpsInd();// 读取数据实体
if (psMcpsInd != NULL)//如果有数据实体发生
{
vProcessIncomingData(psMcpsInd);//处理输入数据实体
vAppQApiReturnMcpsIndBuffer(psMcpsInd);//返回数据实体缓存
}
} while (psMcpsInd != NULL);// 当有数据实体发生
do
{
psAHI_Ind = psAppQApiReadHwInd();//读取硬件事件
if (psAHI_Ind != NULL) //如果有硬件事件
{
vProcessIncomingHwEvent(psAHI_Ind);//处理输入硬件事件
vAppQApiReturnHwIndBuffer(psAHI_Ind);//返回硬件事件缓存
}
} while (psAHI_Ind != NULL);//当有硬件事件发生
}
/****************************************************************************
* NAME: 处理输入管理实体
* DESCRIPTION:先找出协调器的通道,再联网
****************************************************************************/
PRIVATE void vProcessIncomingMlme(MAC_MlmeDcfmInd_s *psMlmeInd)
{
switch (psMlmeInd->u8Type)//判断管理实体类型
{
case MAC_MLME_DCFM_SCAN://扫描管理实体
if (sDeviceData.sSystem.eState == E_STATE_SCANNING)//如果设备系统状态正在扫描
{
vHandleActiveScanResponse(psMlmeInd);//主动扫描响应
}
break;
case MAC_MLME_DCFM_ASSOCIATE://联网管理实体
if (sDeviceData.sSystem.eState == E_STATE_ASSOCIATING)//如果设备系统状态正在联网
{
vHandleAssociateResponse(psMlmeInd);//联网响应
}
break;
default:
break;
}
}
/****************************************************************************
* NAME: 处理输入数据实体
* DESCRIPTION:处理来自协调器的命令
如果命令为“ywon”,则返回数据,开DIO13
如果命令为“ywoff”,则关DIO13
****************************************************************************/
PRIVATE void vProcessIncomingData(MAC_McpsDcfmInd_s *psMcpsInd)
{
MAC_RxFrameData_s *psFrame;//接收数据指针
MAC_Addr_s *psAddr;//接收数据帧地址指针
uint16 u16NodeAddr;//节点地址
char charOrderData[20];//接收数据存储数组
uint8 u8j;//计数变量
psFrame = &psMcpsInd->uParam.sIndData.sFrame;//数据帧
psAddr = &psFrame->sSrcAddr;//地址
if (psMcpsInd->u8Type == MAC_MCPS_IND_DATA)//判断接收数据类型
{
u16NodeAddr = psAddr->uAddr.u16Short;//节点地址取值
if (u16NodeAddr == COORD_ADDR)//判断接收数据是否来自Coordinator
{
if (psFrame->au8Sdu[0] == u8RxFrameHandle)//如果有新帧来,且没丢帧
{
u8RxFrameHandle++;//记录接收数据帧数
for (u8j = 1; u8j < psFrame->u8SduLength; u8j++)//存储命令数据
{
charOrderData[u8j-1] = psFrame->au8Sdu[u8j];
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
if (strncmp("ywon",charOrderData,4) == 0)//输入"ywon"则返回YW3,开DIO13
{
vAHI_DioSetOutput(E_AHI_DIO13_INT,0);//开DIO13
u32YW3DataNO++;//数据帧数自增
while (sADSensor.eState != E_STATE_READ_BATT_VOLTS_READY)//没准备好
{
vReadADC();//读取电池电压
}
vFuZhiYW3();//YW3赋值
vToCoord_TxData();//发送数据
sADSensor.eState = E_STATE_READ_BATT_VOLT_IDLE;// 传感器状态重置空闲状态
}
else if (strncmp("ywoff",charOrderData,5) == 0)//输入"ywoff"则无返回数据,关DIO13
{
vAHI_DioSetOutput(0,E_AHI_DIO13_INT);//关DIO13
}
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
else if (psFrame->au8Sdu[0] > u8RxFrameHandle)//如果丢了帧
{
u8RxFrameHandle = psFrame->au8Sdu[0] + 1;// 记录帧数修正
}
else if (psFrame->au8Sdu[0] < u8RxFrameHandle)//如果旧帧
{
}
}
}
}
/****************************************************************************
* NAME: 处理输入硬件事件
* DESCRIPTION:
****************************************************************************/
PRIVATE void vProcessIncomingHwEvent(AppQApiHwInd_s *psAHI_Ind)
{
}
/****************************************************************************
* NAME: 开始主动扫描
* DESCRIPTION:主动扫描
****************************************************************************/
PRIVATE void vStartActiveScan(void)
{
MAC_MlmeReqRsp_s sMlmeReqRsp;
MAC_MlmeSyncCfm_s sMlmeSyncCfm;
sDeviceData.sSystem.eState = E_STATE_SCANNING;//设备数据系统状态为开始扫描
/* 扫描请求 */
sMlmeReqRsp.u8Type = MAC_MLME_REQ_SCAN;//扫描类型
sMlmeReqRsp.u8ParamLength = sizeof(MAC_MlmeReqScan_s);//变量长度
sMlmeReqRsp.uParam.sReqScan.u8ScanType = MAC_MLME_SCAN_TYPE_ACTIVE;//扫描类型
sMlmeReqRsp.uParam.sReqScan.u32ScanChannels = SCAN_CHANNELS;//扫描通道
sMlmeReqRsp.uParam.sReqScan.u8ScanDuration = ACTIVE_SCAN_DURATION;//主动扫描时间
vAppApiMlmeRequest(&sMlmeReqRsp, &sMlmeSyncCfm);//主动扫描请求
}
/****************************************************************************
* NAME: 主动扫描响应
* DESCRIPTION:找出协调器的通道
****************************************************************************/
PRIVATE void vHandleActiveScanResponse(MAC_MlmeDcfmInd_s *psMlmeInd)
{
MAC_PanDescr_s *psPanDesc;
int inti;
if ((psMlmeInd->uParam.sDcfmScan.u8Status == MAC_ENUM_SUCCESS)//如果网络建好
&& (psMlmeInd->uParam.sDcfmScan.u8ScanType == MAC_MLME_SCAN_TYPE_ACTIVE))//且扫描类型活动
{
inti = 0;
while (inti < psMlmeInd->uParam.sDcfmScan.u8ResultListSize)//当在规定通道内
{
psPanDesc = &psMlmeInd->uParam.sDcfmScan.uList.asPanDescr[inti];//读取节点描述
if ((psPanDesc->sCoord.u16PanId == PAN_ID)//判断协调器是否匹配
&& (psPanDesc->sCoord.u8AddrMode == 2)//短地址模式
&& (psPanDesc->sCoord.uAddr.u16Short == COORD_ADDR))//协调器地址
{
sDeviceData.sSystem.u8Channel = psPanDesc->u8LogicalChan;//读取系统通道
vStartAssociate();//开始联网
return;
}
inti++;//通道自增
}
}
vStartActiveScan();//寻找协调器失败后再次扫描
}
/****************************************************************************
* NAME: 开始联网
* DESCRIPTION:联网
****************************************************************************/
PRIVATE void vStartAssociate(void)
{
MAC_MlmeReqRsp_s sMlmeReqRsp;
MAC_MlmeSyncCfm_s sMlmeSyncCfm;
sDeviceData.sSystem.eState = E_STATE_ASSOCIATING;//置设备状态为联网
/* 创建联网请求 */
sMlmeReqRsp.u8Type = MAC_MLME_REQ_ASSOCIATE;//联网请求
sMlmeReqRsp.u8ParamLength = sizeof(MAC_MlmeReqAssociate_s);//联网请求长度
sMlmeReqRsp.uParam.sReqAssociate.u8LogicalChan = sDeviceData.sSystem.u8Channel;//系统通道
sMlmeReqRsp.uParam.sReqAssociate.u8Capability = 0x80; /* We want short address, other features off */
sMlmeReqRsp.uParam.sReqAssociate.u8SecurityEnable = FALSE;//没有安全设置
sMlmeReqRsp.uParam.sReqAssociate.sCoord.u8AddrMode = 2;//短地址
sMlmeReqRsp.uParam.sReqAssociate.sCoord.u16PanId = PAN_ID;//PAN_ID
sMlmeReqRsp.uParam.sReqAssociate.sCoord.uAddr.u16Short = COORD_ADDR;//协调器地址
vAppApiMlmeRequest(&sMlmeReqRsp, &sMlmeSyncCfm);//联网请求
}
/****************************************************************************
* NAME: 联网响应
* DESCRIPTION:读取由协调器分配的短地址,指示联网成功
****************************************************************************/
PRIVATE void vHandleAssociateResponse(MAC_MlmeDcfmInd_s *psMlmeInd)
{
if (psMlmeInd->uParam.sDcfmAssociate.u8Status == MAC_ENUM_SUCCESS)//联网成功
{
sDeviceData.sSystem.u16ShortAddr = psMlmeInd->uParam.sDcfmAssociate.u16AssocShortAddr;//保存短地址
sDeviceData.sSystem.eState = E_STATE_RUNNING;//置设备系统状态为开始运行
vLedControl(1,1);//开LED2以指示联网成功
}
else
{
vStartActiveScan();//再次尝试
}
}
/****************************************************************************
* NAME: 读电池电压和YW3
* DESCRIPTION:读电池电压和YW3并处理数据
****************************************************************************/
PRIVATE void vReadADC(void)
{
uint16 u16BATV;//BATV
uint16 u16ADC3;//ADC3
switch(sADSensor.eState)//判断传感器状态
{
case E_STATE_READ_BATT_VOLT_IDLE://空闲状态
vAHI_AdcStartSample();//开始采样
sADSensor.eState = E_STATE_READ_BATT_VOLTS_ADC_CONVERTING;//转为转换状态
break;
case E_STATE_READ_BATT_VOLTS_ADC_CONVERTING://转换状态
if (!bAHI_AdcPoll()) // 如果转换池里面的值为0
{
sADSensor.eState = E_STATE_READ_BATT_VOLTS_COMPLETE;//转为完成状态
}
break;
case E_STATE_READ_BATT_VOLTS_COMPLETE://完成状态
vAHI_AdcEnable(E_AHI_ADC_CONVERT_DISABLE,// 关闭转换
E_AHI_AP_INPUT_RANGE_2,//选取转换范围
E_AHI_ADC_SRC_VOLT);//转换电池电压
vAHI_AdcStartSample();// 采样
while(bAHI_AdcPoll() != 0x00); //当转换池里面的值不为0时,即有转换结果值
u16BATV = u16AHI_AdcRead(); // 读取最近转换结果
vAHI_AdcEnable(E_AHI_ADC_CONVERT_DISABLE,
E_AHI_AP_INPUT_RANGE_2,
E_AHI_ADC_SRC_ADC_3);//转换通道ADC3
vAHI_AdcStartSample();
while(bAHI_AdcPoll() != 0x00);
u16ADC3 = u16AHI_AdcRead();
/* 数据处理*/
sADSensor.u16BAT = ((uint32)((uint32)(u16BATV * 586) +
((uint32)(u16BATV * 586) >> 1))) /
1000;
sADSensor.u16YW3 = ((uint32)((uint32)(u16ADC3 * 586) +
((uint32)(u16ADC3 * 586) >> 1))) /
1000;
sADSensor.eState = E_STATE_READ_BATT_VOLTS_READY;//转为准备好了状态
break;
case E_STATE_READ_BATT_VOLTS_READY://准备好了状态
break;
default:
break;
}
}
/****************************************************************************
* NAME: YW3赋值
* DESCRIPTION:转换数字为字符串
3No.*** Bt3=*.***V Yw3=*.***V***
*** *.*** *.***
****************************************************************************/
PRIVATE void vFuZhiYW3(void)
{
uint8 u8a = 1;//BAT数据帧数百位
uint8 u8b = 2;//BAT数据帧数十位
uint8 u8c = 3;//BAT数据帧数个位
uint8 u8d = 4;//BAT数据千位
uint8 u8e = 6;//BAT数据百位
uint8 u8f = 7;//BAT数据十位
uint8 u8g = 8;//BAT数据个位
uint8 u8h = 9;//YW3数据千位
uint8 u8i = 11;//YW3数据百位
uint8 u8j = 12;//YW3数据十位
uint8 u8k = 13;//YW3数据个位
charSensorData[0] = '\0';//首位
// charSensorData[1] = '3';
// charSensorData[2] = 'N';
// charSensorData[3] = 'o';
// charSensorData[4] = '.';
// charSensorData[8] = '/';//间隔
// charSensorData[9] = 'B';//电池
// charSensorData[10] = 't';//电池
// charSensorData[11] = '3';//3
// charSensorData[12] = '=';//等号
charSensorData[5] = '.';//小数点
// charSensorData[18] = 'V';//单位
// charSensorData[19] = '/';//间隔
// charSensorData[20] = 'Y';//液
// charSensorData[21] = 'w';//位
// charSensorData[22] = '3';//3
// charSensorData[23] = '=';//等号
charSensorData[10] = '.';//小数点
// charSensorData[29] = 'V';//单位
// charSensorData[30] = '*';
// charSensorData[31] = '*';
// charSensorData[32] = '*';//末位
switch(u32YW3DataNO/100)//检测YW3上传数据帧数百位
{
case 0: charSensorData[u8a] = '0'; break;
case 1: charSensorData[u8a] = '1'; break;
case 2: charSensorData[u8a] = '2'; break;
case 3: charSensorData[u8a] = '3'; break;
case 4: charSensorData[u8a] = '4'; break;
case 5: charSensorData[u8a] = '5'; break;
case 6: charSensorData[u8a] = '6'; break;
case 7: charSensorData[u8a] = '7'; break;
case 8: charSensorData[u8a] = '8'; break;
case 9: charSensorData[u8a] = '9'; break;
default: break;
}
switch(u32YW3DataNO%100/10)//检测YW3上传数据帧数十位
{
case 0: charSensorData[u8b] = '0'; break;
case 1: charSensorData[u8b] = '1'; break;
case 2: charSensorData[u8b] = '2'; break;
case 3: charSensorData[u8b] = '3'; break;
case 4: charSensorData[u8b] = '4'; break;
case 5: charSensorData[u8b] = '5'; break;
case 6: charSensorData[u8b] = '6'; break;
case 7: charSensorData[u8b] = '7'; break;
case 8: charSensorData[u8b] = '8'; break;
case 9: charSensorData[u8b] = '9'; break;
default: break;
}
switch(u32YW3DataNO%100%10)//检测YW3上传数据帧数个位
{
case 0: charSensorData[u8c] = '0'; break;
case 1: charSensorData[u8c] = '1'; break;
case 2: charSensorData[u8c] = '2'; break;
case 3: charSensorData[u8c] = '3'; break;
case 4: charSensorData[u8c] = '4'; break;
case 5: charSensorData[u8c] = '5'; break;
case 6: charSensorData[u8c] = '6'; break;
case 7: charSensorData[u8c] = '7'; break;
case 8: charSensorData[u8c] = '8'; break;
case 9: charSensorData[u8c] = '9'; break;
default: break;
}
switch(sADSensor.u16BAT/1000)//检测YW3电池电压千位
{
case 0: charSensorData[u8d] = '0'; break;
case 1: charSensorData[u8d] = '1'; break;
case 2: charSensorData[u8d] = '2'; break;
case 3: charSensorData[u8d] = '3'; break;
case 4: charSensorData[u8d] = '4'; break;
case 5: charSensorData[u8d] = '5'; break;
case 6: charSensorData[u8d] = '6'; break;
case 7: charSensorData[u8d] = '7'; break;
case 8: charSensorData[u8d] = '8'; break;
case 9: charSensorData[u8d] = '9'; break;
default: break;
}
switch(sADSensor.u16BAT%1000/100)//检测YW3电池电压百位
{
case 0: charSensorData[u8e] = '0'; break;
case 1: charSensorData[u8e] = '1'; break;
case 2: charSensorData[u8e] = '2'; break;
case 3: charSensorData[u8e] = '3'; break;
case 4: charSensorData[u8e] = '4'; break;
case 5: charSensorData[u8e] = '5'; break;
case 6: charSensorData[u8e] = '6'; break;
case 7: charSensorData[u8e] = '7'; break;
case 8: charSensorData[u8e] = '8'; break;
case 9: charSensorData[u8e] = '9'; break;
default: break;
}
switch(sADSensor.u16BAT%1000%100/10)//检测YW3电池电压十位
{
case 0: charSensorData[u8f] = '0'; break;
case 1: charSensorData[u8f] = '1'; break;
case 2: charSensorData[u8f] = '2'; break;
case 3: charSensorData[u8f] = '3'; break;
case 4: charSensorData[u8f] = '4'; break;
case 5: charSensorData[u8f] = '5'; break;
case 6: charSensorData[u8f] = '6'; break;
case 7: charSensorData[u8f] = '7'; break;
case 8: charSensorData[u8f] = '8'; break;
case 9: charSensorData[u8f] = '9'; break;
default: break;
}
switch(sADSensor.u16BAT%1000%100%10)//检测YW3电池电压个位
{
case 0: charSensorData[u8g] = '0'; break;
case 1: charSensorData[u8g] = '1'; break;
case 2: charSensorData[u8g] = '2'; break;
case 3: charSensorData[u8g] = '3'; break;
case 4: charSensorData[u8g] = '4'; break;
case 5: charSensorData[u8g] = '5'; break;
case 6: charSensorData[u8g] = '6'; break;
case 7: charSensorData[u8g] = '7'; break;
case 8: charSensorData[u8g] = '8'; break;
case 9: charSensorData[u8g] = '9'; break;
default: break;
}
switch(sADSensor.u16YW3/1000)//检测YW3数据千位
{
case 0: charSensorData[u8h] = '0'; break;
case 1: charSensorData[u8h] = '1'; break;
case 2: charSensorData[u8h] = '2'; break;
case 3: charSensorData[u8h] = '3'; break;
case 4: charSensorData[u8h] = '4'; break;
case 5: charSensorData[u8h] = '5'; break;
case 6: charSensorData[u8h] = '6'; break;
case 7: charSensorData[u8h] = '7'; break;
case 8: charSensorData[u8h] = '8'; break;
case 9: charSensorData[u8h] = '9'; break;
default: break;
}
switch(sADSensor.u16YW3%1000/100)//检测YW3数据百位
{
case 0: charSensorData[u8i] = '0'; break;
case 1: charSensorData[u8i] = '1'; break;
case 2: charSensorData[u8i] = '2'; break;
case 3: charSensorData[u8i] = '3'; break;
case 4: charSensorData[u8i] = '4'; break;
case 5: charSensorData[u8i] = '5'; break;
case 6: charSensorData[u8i] = '6'; break;
case 7: charSensorData[u8i] = '7'; break;
case 8: charSensorData[u8i] = '8'; break;
case 9: charSensorData[u8i] = '9'; break;
default: break;
}
switch(sADSensor.u16YW3%1000%100/10)//检测YW3数据十位
{
case 0: charSensorData[u8j] = '0'; break;
case 1: charSensorData[u8j] = '1'; break;
case 2: charSensorData[u8j] = '2'; break;
case 3: charSensorData[u8j] = '3'; break;
case 4: charSensorData[u8j] = '4'; break;
case 5: charSensorData[u8j] = '5'; break;
case 6: charSensorData[u8j] = '6'; break;
case 7: charSensorData[u8j] = '7'; break;
case 8: charSensorData[u8j] = '8'; break;
case 9: charSensorData[u8j] = '9'; break;
default: break;
}
switch(sADSensor.u16YW3%1000%100%10)//检测YW3数据个位
{
case 0: charSensorData[u8k] = '0'; break;
case 1: charSensorData[u8k] = '1'; break;
case 2: charSensorData[u8k] = '2'; break;
case 3: charSensorData[u8k] = '3'; break;
case 4: charSensorData[u8k] = '4'; break;
case 5: charSensorData[u8k] = '5'; break;
case 6: charSensorData[u8k] = '6'; break;
case 7: charSensorData[u8k] = '7'; break;
case 8: charSensorData[u8k] = '8'; break;
case 9: charSensorData[u8k] = '9'; break;
default: break;
}
}
/****************************************************************************
* NAME: 往Coordinator发送数据
* DESCRIPTION:一次发送32位字符,
每发送一帧LED1闪烁一次
****************************************************************************/
PRIVATE void vToCoord_TxData(void)
{
MAC_McpsReqRsp_s sMcpsReqRsp;
MAC_McpsSyncCfm_s sMcpsSyncCfm;
static uint8 u8ToggleCount;//点灯标志
uint8 *pu8Payload;//被发送数据 数据路径指针
char charRxChar;//被发送数据存储变量
uint8 u8i = 0;//数据位数
uint8 u8k = 0;
/* LED1闪烁以指示存活,串口调试助手上显示频率不一致 */
if ((u8ToggleCount++) %2 == 1)
{
vLedControl(0,1);//判断是否为奇数,是则开LED1
}
else
{
vLedControl(0,0);//为偶数则关LED1
}
if (sDeviceData.sSystem.eState == E_STATE_RUNNING)//判断是否联网,在联网成功处设置
{
charRxChar = charSensorData[u8k++];//读取数据据第0位
if (charRxChar >= 0)//判断是否有被发送数据
{
sMcpsReqRsp.u8Type = MAC_MCPS_REQ_DATA;//数据请求
sMcpsReqRsp.u8ParamLength = sizeof(MAC_McpsReqData_s);//帧传送请求长度
sMcpsReqRsp.uParam.sReqData.u8Handle = 1;//设置握手信号
sMcpsReqRsp.uParam.sReqData.sFrame.sSrcAddr.u8AddrMode = 2;//短地址模式
sMcpsReqRsp.uParam.sReqData.sFrame.sSrcAddr.u16PanId = PAN_ID;//PAN_ID
sMcpsReqRsp.uParam.sReqData.sFrame.sSrcAddr.uAddr.u16Short = sDeviceData.sSystem.u16ShortAddr;//短地址
sMcpsReqRsp.uParam.sReqData.sFrame.sDstAddr.u8AddrMode = 2;//短地址模式
sMcpsReqRsp.uParam.sReqData.sFrame.sDstAddr.u16PanId = PAN_ID;//PAN_ID
sMcpsReqRsp.uParam.sReqData.sFrame.sDstAddr.uAddr.u16Short = COORD_ADDR;//协调器短地址
sMcpsReqRsp.uParam.sReqData.sFrame.u8TxOptions = MAC_TX_OPTION_ACK;//操作方式,有应答,没保护、时隙等
pu8Payload = sMcpsReqRsp.uParam.sReqData.sFrame.au8Sdu;//帧数据指针指向发送数据数据路径
pu8Payload[u8i++] = u8TxFrameHandle++;//0位存储0
pu8Payload[u8i++] = charSensorData[u8k++];//设置数据第1位
while ((charRxChar >= 0) && (u8i < MAX_DATA_PER_FRAME))//当有被发送数据且数据长度在64内时
{
charRxChar = charSensorData[u8k++];//被发送数据存储变量赋值
if (charRxChar >= 0)//设置数据从第2位起
{
pu8Payload[u8i++] = (uint8)charRxChar;
}
}
sMcpsReqRsp.uParam.sReqData.sFrame.u8SduLength = u8i;//设置帧长
vAppApiMcpsRequest(&sMcpsReqRsp, &sMcpsSyncCfm);//数据传输请求
}
}
}
/****************************************************************************/
/*** 文件结束 ***/
/****************************************************************************/
用户290990 2010-8-13 17:23
13510179723_912348475 2010-7-10 18:10