cc2530 火焰传感器代码
时间: 2023-07-23 10:43:14 浏览: 180
下面是基于CC2530的火焰传感器代码,供您参考:
```c
#include "ZComDef.h"
#include "AF.h"
#include "OSAL.h"
#include "OSAL_Tasks.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "DebugTrace.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include "hal_adc.h"
#define APP_ENDPOINT 10
#define APP_PROFID 0x0F08
#define APP_DEVICEID 0x0001
#define APP_DEVICETYPE 0x01
#define APP_MAX_CLUSTERS 1
#define APP_CLUSTERID 0x0006
#define SAMPLE_PERIOD 2000 //采样周期2秒
byte appTaskID;
byte appEndpoint;
void appInit(void);
void appTask(byte taskID);
void appSendData(void);
void halKeyCback(byte key, byte keyStatus);
void main(void) {
osal_sys_init();
HalLedSet(HAL_LED_1, HAL_LED_MODE_FLASH);
appInit();
osal_start_system();
}
void appInit(void) {
ZDO_Init();
appEndpoint = AF_RegisterEndpoint(APP_ENDPOINT, &appTaskID);
ZDO_RegisterForZDOMsg(appTaskID, Match_Desc_rsp);
RegisterForKeys(appTaskID);
HalUARTInit();
HalUARTWrite(HAL_UART_PORT_0, "CC2530 Flame Sensor Demo\n", 25);
HalAdcInit();
osal_set_event(appTaskID, SAMPLE_PERIOD);
}
void appTask(byte taskID) {
uint16 events;
afIncomingMSGPacket_t *MSGpkt;
(void)taskID;
events = osal_clear_event(appTaskID, 0);
if (events & SAMPLE_PERIOD) {
appSendData();
osal_start_timerEx(appTaskID, SAMPLE_PERIOD, SAMPLE_PERIOD);
}
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(appTaskID);
while (MSGpkt) {
switch (MSGpkt->hdr.event) {
case ZDO_STATE_CHANGE:
ZDO_STATE_CHANGE_CB(MSGpkt->hdr.status);
break;
case KEY_CHANGE:
halKeyCback(*(byte*)MSGpkt->hdr.data, ((keyChange_t*)MSGpkt->hdr.data)->state);
break;
case AF_DATA_CONFIRM_CMD:
break;
default:
break;
}
osal_msg_deallocate((uint8 *)MSGpkt);
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(appTaskID);
}
}
void appSendData(void) {
uint16 adcValue;
int mV;
uint8 data[2];
HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);
HalAdcRead(HAL_ADC_CHANNEL_0, HAL_ADC_RESOLUTION_8, &adcValue);
mV = HalAdcToMillivolt(adcValue);
if (mV > 2000) {
HalUARTWrite(HAL_UART_PORT_0, "Flame detected!\n", 16);
data[0] = 0x01;
data[1] = 0x00;
AF_DataRequest(appEndpoint, &zAddrCoord, APP_CLUSTERID, 2, data, &appTaskID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS);
} else {
HalUARTWrite(HAL_UART_PORT_0, "No flame detected.\n", 19);
}
HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF);
}
void halKeyCback(byte key, byte keyStatus) {
if (key == HAL_KEY_SW_2 && keyStatus == HAL_KEY_EVENT) {
HalUARTWrite(HAL_UART_PORT_0, "Button pressed.\n", 16);
appSendData();
}
}
```
该代码使用了CC2530芯片内置的ADC模块来采集火焰传感器输出的模拟信号,并通过串口输出检测结果。如果检测到火焰,将会向协调器发送一个数据包。注意,该代码只注册了一个端点(Endpoint),并且只支持一个簇(Cluster),如需扩展,请自行修改。
阅读全文