基于cc2530获取温湿度传感器的值的组网代码在iar中怎么写
时间: 2023-11-30 17:03:39 浏览: 148
以下是基于CC2530获取温湿度传感器值的组网代码示例,使用IAR Embedded Workbench进行编写:
```
#include "ZComDef.h"
#include "OSAL.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "AF.h"
#include "ZDNwkMgr.h"
#include "OnBoard.h"
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_uart.h"
#include "hal_key.h"
#include "hal_adc.h"
#include "hal_drivers.h"
#include "string.h"
#include "stdio.h"
#define SAMPLEAPP_ENDPOINT 10
#define SAMPLEAPP_PROFID 0x0F08
#define SAMPLEAPP_DEVICEID 0x0001
#define SAMPLEAPP_DEVICE_VERSION 0
#define SAMPLEAPP_FLAGS 0
#define SAMPLEAPP_MAX_CLUSTERS 1
#define SAMPLEAPP_PERIODIC_CLUSTERID 1
#define HAL_LCD_LINE_1 0x80
#define HAL_LCD_LINE_2 0xC0
#define HAL_ADC_RESOLUTION 14
#define HAL_ADC_REF_125V 0x00
#define HAL_ADC_REF_25V 0x01
#define HAL_ADC_REF_AVDD 0x02
#define HAL_ADC_REF_1V25 0x03
#define HAL_ADC_CHN_TEMP ADC_COMPB_IN_AUXIO2
#define HAL_ADC_CHN_HUM ADC_COMPB_IN_AUXIO1
#define ADC_SAMPLES 8
byte SampleApp_TaskID;
byte SampleApp_TransID = 0;
afAddrType_t SampleApp_Periodic_DstAddr;
// Function to initialize the application.
void SampleApp_Init(byte task_id)
{
SampleApp_TaskID = task_id;
// Register the application with the ZigBee Device Object (ZDO).
ZDO_RegisterForZDOMsg(task_id, End_Device_Bind_rsp);
// Register the application's endpoint and profile ID with the AF.
afRegisterEndpoint(SAMPLEAPP_ENDPOINT, SAMPLEAPP_PROFID, SAMPLEAPP_DEVICEID, SAMPLEAPP_DEVICE_VERSION, SAMPLEAPP_MAX_CLUSTERS);
// Set up the endpoint's supported clusters.
afClusterTab_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =
{
{SAMPLEAPP_PERIODIC_CLUSTERID, AF_SIG_CLUSTER}
};
afRegisterClusters(SAMPLEAPP_ENDPOINT, SampleApp_ClusterList, SAMPLEAPP_MAX_CLUSTERS);
// Set up the periodic report destination address.
SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Periodic_DstAddr.addr.shortAddr = 0x0000;
// Start the periodic report timer.
osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_PERIODIC_CLUSTERID, 10000);
}
// Function to handle incoming messages.
void SampleApp_MessageMSGCB(afIncomingMSGPacket_t *pkt)
{
if (pkt->clusterId == SAMPLEAPP_PERIODIC_CLUSTERID)
{
// Received a periodic report message.
halLcdClearLine(HAL_LCD_LINE_2);
halLcdWriteString("Temp: ", HAL_LCD_LINE_1);
halLcdWriteStringValue(pkt->cmd.Data[0], "", HAL_LCD_LINE_1 + 6, HAL_LCD_ALIGN_RIGHT);
halLcdWriteString("Hum: ", HAL_LCD_LINE_2);
halLcdWriteStringValue(pkt->cmd.Data[1], "", HAL_LCD_LINE_2 + 6, HAL_LCD_ALIGN_RIGHT);
}
}
// Function to handle periodic report events.
void SampleApp_PeriodicReport(void)
{
uint16 temp, hum;
// Read the temperature and humidity values from the sensor.
halAdcSetReference(HAL_ADC_REF_125V);
halAdcSetChannel(HAL_ADC_CHN_TEMP);
osal_delay(1);
temp = halAdcRead(HAL_ADC_RESOLUTION);
halAdcSetReference(HAL_ADC_REF_25V);
halAdcSetChannel(HAL_ADC_CHN_HUM);
osal_delay(1);
hum = halAdcRead(HAL_ADC_RESOLUTION);
// Convert the ADC readings to temperature and humidity values.
temp = (temp * 1250) / 2048;
hum = (hum * 1000) / 4096;
// Send the periodic report message to the coordinator.
uint8 report[2] = {temp, hum};
afStatus_t status = AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_TransID, SAMPLEAPP_PERIODIC_CLUSTERID, 2, report, SAMPLEAPP_FLAGS, AF_DEFAULT_RADIUS);
if (status != afStatus_SUCCESS)
{
// Failed to send the periodic report message.
halLcdWriteString("Report Failed", HAL_LCD_LINE_1);
}
// Restart the periodic report timer.
osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_PERIODIC_CLUSTERID, 10000);
}
// Function to handle incoming ZDO messages.
void SampleApp_ProcessZDOMsgs(zdoIncomingMsg_t *inMsg)
{
switch (inMsg->clusterId)
{
case End_Device_Bind_rsp:
// Received an End Device Bind Response.
break;
}
}
// Function to process events for this application.
uint16 SampleApp_ProcessEvent(byte task_id, uint16 events)
{
if (events & SYS_EVENT_MSG)
{
// Received an incoming message.
afIncomingMSGPacket_t *pkt;
while ((pkt = (afIncomingMSGPacket_t *)osal_msg_receive(SampleApp_TaskID)) != NULL)
{
SampleApp_MessageMSGCB(pkt);
osal_msg_deallocate((uint8 *)pkt);
}
return (events ^ SYS_EVENT_MSG);
}
if (events & SAMPLEAPP_PERIODIC_CLUSTERID)
{
// Received a periodic report timer event.
SampleApp_PeriodicReport();
return (events ^ SAMPLEAPP_PERIODIC_CLUSTERID);
}
return 0;
}
```
该示例代码使用CC2530读取温湿度传感器的值,并将其作为周期性报告消息发送到协调器。你可以根据自己的需求修改该代码。
阅读全文