想用烟雾传感器控制zigbee1的灯,从而控制zigbee2的led灯
时间:10-02
整理:3721RD
点击:
布置哪位大神可以帮下忙,我这边用的CC2530,现在能做到按下zigbee1的开关,点亮zigbee2的led1灯,和能做到用烟雾传感器控制zigbee1的led1灯,我现在想要达到传感器检测到烟雾之后zigbee1的led1灯亮,zigbee1的led1灯亮之后能够使zigbee2的led1灯亮,我是新学的,弄不出来,还请大神指教,这里附上代码
以下为烟雾传感器
#include <ioCC2530.h>
#define uint unsigned int
#define uchar unsigned char
//定义控制LED灯的端口
#define LED1 P1_0 //LED1为P1.0口控制
#define AIR P2_0 //光敏为P2.0口控制
//函数声明
void Delayms(uint); //延时函数
void InitLed(void); //初始化LED1
void AirInit(); //光敏初始化
uchar AirScan(); //烟雾扫描程序
/****************************
延时函数
*****************************/
void Delayms(uint xms) //i=xms 即延时i毫秒
{
uint i,j;
for(i=xms;i>0;i--)
for(j=587;j>0;j--);
}
/****************************
LED初始化函数
*****************************/
void InitLed(void)
{
P1DIR |= 0x01; //P1_0定义为输出
LED1 = 1; //LED1灯熄灭
}
/****************************
按键初始化函数
*****************************/
void AirInit()
{
P2SEL &= ~0X01; //设置P20为普通IO口
P2DIR &= ~0X01; // 在P20口,设置为输入模式
P2INP &= ~0x01; //打开P20上拉电阻,不影响
}
/****************************
烟雾检测函数
*****************************/
uchar AirScan(void)
{
if(AIR==0)
{Delayms(10);
if(AIR==0)
{
return 1; // 无烟雾
}
}
return 0; //有烟雾
}
/***************************
主函数
***************************/
void main(void)
{
InitLed(); //调用初始化函数
AirInit();
while(1)
{
if(AirScan()) //按键改变LED状态
LED1=1; //无烟雾,LED1灭掉
else
LED1=0; //有烟雾,LED1点亮
}
}
以下为无线点灯
#include <hal_lcd.h>
#include <hal_led.h>
#include <hal_joystick.h>
#include <hal_assert.h>
#include <hal_board.h>
#include <hal_int.h>
#include "hal_mcu.h"
#include "hal_button.h"
#include "hal_rf.h"
#include "util_lcd.h"
#include "basic_rf.h"
// Application parameters
#define RF_CHANNEL 25 // 2.4 GHz RF channel
// BasicRF address definitions
#define PAN_ID 0x2007
#define SWITCH_ADDR 0x2520
#define LIGHT_ADDR 0xBEEF
#define APP_PAYLOAD_LENGTH 1
#define LIGHT_TOGGLE_CMD 0
// Application states
#define IDLE 0
#define SEND_CMD 1
// Application role
#define NONE 0
#define SWITCH 1
#define LIGHT 2
#define APP_MODES 2
/***********************************************************************************
* LOCAL VARIABLES
*/
static uint8 pTxData[APP_PAYLOAD_LENGTH];
static uint8 pRxData[APP_PAYLOAD_LENGTH];
static basicRfCfg_t basicRfConfig;
// Mode menu
static menuItem_t pMenuItems[] =
{
#ifdef ASSY_EXP4618_CC2420
// Using Softbaugh 7-seg display
" L S ", SWITCH,
" LIGHT ", LIGHT
#else
// SRF04EB and SRF05EB
"Switch", SWITCH,
"Light", LIGHT
#endif
};
static menu_t pMenu =
{
pMenuItems,
N_ITEMS(pMenuItems)
};
#ifdef SECURITY_CCM
// Security key
static uint8 key[]= {
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
};
#endif
/***********************************************************************************
* LOCAL FUNCTIONS
*/
static void appLight();
static void appSwitch();
static uint8 appSelectMode(void);
static void appLight()
{
//halLcdWriteLine(HAL_LCD_LINE_1, "Light");
//halLcdWriteLine(HAL_LCD_LINE_2, "Ready");
#ifdef ASSY_EXP4618_CC2420
halLcdClearLine(1);
halLcdWriteSymbol(HAL_LCD_SYMBOL_RX, 1);
#endif
// Initialize BasicRF
basicRfConfig.myAddr = LIGHT_ADDR;
if(basicRfInit(&basicRfConfig)==FAILED) {
HAL_ASSERT(FALSE);
}
basicRfReceiveOn();
// Main loop
while (TRUE) {
while(!basicRfPacketIsReady());
if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) {
if(pRxData[0] == LIGHT_TOGGLE_CMD)
{
halLedToggle(1);
}
}
}
}
static void appSwitch()
{
//halLcdWriteLine(HAL_LCD_LINE_1, "Switch");
//halLcdWriteLine(HAL_LCD_LINE_2, "Joystick Push");
//halLcdWriteLine(HAL_LCD_LINE_3, "Send Command");
#ifdef ASSY_EXP4618_CC2420
halLcdClearLine(1);
halLcdWriteSymbol(HAL_LCD_SYMBOL_TX, 1);
#endif
// Initialize BasicRF
basicRfConfig.myAddr = SWITCH_ADDR;
if(basicRfInit(&basicRfConfig)==FAILED) {
HAL_ASSERT(FALSE);
}
pTxData[0] = LIGHT_TOGGLE_CMD;
// Keep Receiver off when not needed to save power
basicRfReceiveOff();
// Main loop
while (TRUE)
{
if( halButtonPushed()==HAL_BUTTON_1 )
{
basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH);
// Put MCU to sleep. It will wake up on joystick interrupt
halIntOff();
halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global
// interrupt enable
halIntOn();
}
}
}
void main(void)
{
uint8 appMode = NONE;
// Config basicRF
basicRfConfig.panId = PAN_ID;
basicRfConfig.channel = RF_CHANNEL;
basicRfConfig.ackRequest = TRUE;
#ifdef SECURITY_CCM
basicRfConfig.securityKey = key;
#endif
// Initalise board peripherals
halBoardInit();
halJoystickInit();
// Initalise hal_rf
if(halRfInit()==FAILED)
{
HAL_ASSERT(FALSE);
}
// Indicate that device is powered
halLedClear(2);
halLedClear(1);
//appSwitch();
appLight();
HAL_ASSERT(FALSE);
}
以下为烟雾传感器
#include <ioCC2530.h>
#define uint unsigned int
#define uchar unsigned char
//定义控制LED灯的端口
#define LED1 P1_0 //LED1为P1.0口控制
#define AIR P2_0 //光敏为P2.0口控制
//函数声明
void Delayms(uint); //延时函数
void InitLed(void); //初始化LED1
void AirInit(); //光敏初始化
uchar AirScan(); //烟雾扫描程序
/****************************
延时函数
*****************************/
void Delayms(uint xms) //i=xms 即延时i毫秒
{
uint i,j;
for(i=xms;i>0;i--)
for(j=587;j>0;j--);
}
/****************************
LED初始化函数
*****************************/
void InitLed(void)
{
P1DIR |= 0x01; //P1_0定义为输出
LED1 = 1; //LED1灯熄灭
}
/****************************
按键初始化函数
*****************************/
void AirInit()
{
P2SEL &= ~0X01; //设置P20为普通IO口
P2DIR &= ~0X01; // 在P20口,设置为输入模式
P2INP &= ~0x01; //打开P20上拉电阻,不影响
}
/****************************
烟雾检测函数
*****************************/
uchar AirScan(void)
{
if(AIR==0)
{Delayms(10);
if(AIR==0)
{
return 1; // 无烟雾
}
}
return 0; //有烟雾
}
/***************************
主函数
***************************/
void main(void)
{
InitLed(); //调用初始化函数
AirInit();
while(1)
{
if(AirScan()) //按键改变LED状态
LED1=1; //无烟雾,LED1灭掉
else
LED1=0; //有烟雾,LED1点亮
}
}
以下为无线点灯
#include <hal_lcd.h>
#include <hal_led.h>
#include <hal_joystick.h>
#include <hal_assert.h>
#include <hal_board.h>
#include <hal_int.h>
#include "hal_mcu.h"
#include "hal_button.h"
#include "hal_rf.h"
#include "util_lcd.h"
#include "basic_rf.h"
// Application parameters
#define RF_CHANNEL 25 // 2.4 GHz RF channel
// BasicRF address definitions
#define PAN_ID 0x2007
#define SWITCH_ADDR 0x2520
#define LIGHT_ADDR 0xBEEF
#define APP_PAYLOAD_LENGTH 1
#define LIGHT_TOGGLE_CMD 0
// Application states
#define IDLE 0
#define SEND_CMD 1
// Application role
#define NONE 0
#define SWITCH 1
#define LIGHT 2
#define APP_MODES 2
/***********************************************************************************
* LOCAL VARIABLES
*/
static uint8 pTxData[APP_PAYLOAD_LENGTH];
static uint8 pRxData[APP_PAYLOAD_LENGTH];
static basicRfCfg_t basicRfConfig;
// Mode menu
static menuItem_t pMenuItems[] =
{
#ifdef ASSY_EXP4618_CC2420
// Using Softbaugh 7-seg display
" L S ", SWITCH,
" LIGHT ", LIGHT
#else
// SRF04EB and SRF05EB
"Switch", SWITCH,
"Light", LIGHT
#endif
};
static menu_t pMenu =
{
pMenuItems,
N_ITEMS(pMenuItems)
};
#ifdef SECURITY_CCM
// Security key
static uint8 key[]= {
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
};
#endif
/***********************************************************************************
* LOCAL FUNCTIONS
*/
static void appLight();
static void appSwitch();
static uint8 appSelectMode(void);
static void appLight()
{
//halLcdWriteLine(HAL_LCD_LINE_1, "Light");
//halLcdWriteLine(HAL_LCD_LINE_2, "Ready");
#ifdef ASSY_EXP4618_CC2420
halLcdClearLine(1);
halLcdWriteSymbol(HAL_LCD_SYMBOL_RX, 1);
#endif
// Initialize BasicRF
basicRfConfig.myAddr = LIGHT_ADDR;
if(basicRfInit(&basicRfConfig)==FAILED) {
HAL_ASSERT(FALSE);
}
basicRfReceiveOn();
// Main loop
while (TRUE) {
while(!basicRfPacketIsReady());
if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) {
if(pRxData[0] == LIGHT_TOGGLE_CMD)
{
halLedToggle(1);
}
}
}
}
static void appSwitch()
{
//halLcdWriteLine(HAL_LCD_LINE_1, "Switch");
//halLcdWriteLine(HAL_LCD_LINE_2, "Joystick Push");
//halLcdWriteLine(HAL_LCD_LINE_3, "Send Command");
#ifdef ASSY_EXP4618_CC2420
halLcdClearLine(1);
halLcdWriteSymbol(HAL_LCD_SYMBOL_TX, 1);
#endif
// Initialize BasicRF
basicRfConfig.myAddr = SWITCH_ADDR;
if(basicRfInit(&basicRfConfig)==FAILED) {
HAL_ASSERT(FALSE);
}
pTxData[0] = LIGHT_TOGGLE_CMD;
// Keep Receiver off when not needed to save power
basicRfReceiveOff();
// Main loop
while (TRUE)
{
if( halButtonPushed()==HAL_BUTTON_1 )
{
basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH);
// Put MCU to sleep. It will wake up on joystick interrupt
halIntOff();
halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global
// interrupt enable
halIntOn();
}
}
}
void main(void)
{
uint8 appMode = NONE;
// Config basicRF
basicRfConfig.panId = PAN_ID;
basicRfConfig.channel = RF_CHANNEL;
basicRfConfig.ackRequest = TRUE;
#ifdef SECURITY_CCM
basicRfConfig.securityKey = key;
#endif
// Initalise board peripherals
halBoardInit();
halJoystickInit();
// Initalise hal_rf
if(halRfInit()==FAILED)
{
HAL_ASSERT(FALSE);
}
// Indicate that device is powered
halLedClear(2);
halLedClear(1);
//appSwitch();
appLight();
HAL_ASSERT(FALSE);
}
通信那边没问题?