控制端代码:
#include "hal_defs.h"
#include "hal_cc8051.h"
#include "hal_int.h"
#include "hal_mcu.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_rf.h"
#include "basic_rf.h"
#include "hal_uart.h"
#include "sensor_drv/sensor.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
/*****点对点通讯地址设置******/
#define RF_CHANNEL 11 // 频道 11~26
#define PAN_ID 0x1111 //网络id
#define MY_ADDR 0xAAAA //本机模块地址
#define SEND_ADDR 0xBBBB //发送地址
/**************************************************/
#define LED1 P1_0
#define LED2 P1_1
#define SW1 P1_2
#define FAN P1_7 //风扇
#define LIGHT P2_0 //照明灯
static basicRfCfg_t basicRfConfig;
// 无线RF初始化
void ConfigRf_Init(void)
{
basicRfConfig.panId = PAN_ID;
basicRfConfig.channel = RF_CHANNEL;
basicRfConfig.myAddr = MY_ADDR;
basicRfConfig.ackRequest = TRUE;
while(basicRfInit(&basicRfConfig) == FAILED);
basicRfReceiveOn();
}
/********************MAIN************************/
int keyFlag =0;//按键标志位,当为1时,启动
void keyScan(){
if(SW1==0){
halMcuWaitMs(10);
if(SW1==0){
keyFlag = 1-keyFlag;
while(SW1==0);
}
}
}
void flash(){ //闪烁,为了能够按键按下快速跳出程序,采用循环方式
for(int i=0;i<25;i++){
LED1 = 1;
halMcuWaitMs(10);
if(SW1==0) return;
}
for(int i=0;i<25;i++){
LED1 = 0;
halMcuWaitMs(10);
if(SW1==0) return;
}
}
void main(void)
{
halBoardInit();//选手不得在此函数内添加代码
ConfigRf_Init();//选手不得在此函数内添加代码
P1DIR |= 0X83; //设置P1_0和P1_1和P1_7引脚为输出模式,
P2DIR |= 0X01; //设置P2_0
LED1 = 0;
LED2 = 0;
uint8 buf[1]; //定义一个数组,发送数据
while(1)
{
/* user code start */
keyScan();
if(keyFlag==1){
buf[0] ='k'; //发送一个字节数据 ,'k' 表示开启
basicRfSendPacket(SEND_ADDR,buf,1);
flash(); //开启闪烁
}
else if(keyFlag==0){
buf[0] ='g'; //发送一个字节数据 ,'g' 表示关闭
basicRfSendPacket(SEND_ADDR,buf,1);
LED1 = 0;
halMcuWaitMs(100);
}
/* user code end */
}
}
节点端代码:
#include "hal_defs.h"
#include "hal_cc8051.h"
#include "hal_int.h"
#include "hal_mcu.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_rf.h"
#include "basic_rf.h"
#include "hal_uart.h"
#include "sensor_drv/sensor.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
/*****点对点通讯地址设置******/
#define RF_CHANNEL 11 // 频道 11~26
#define PAN_ID 0x1111 //网络id
#define MY_ADDR 0xBBBB //本机模块地址
#define SEND_ADDR 0xAAAA //发送地址
/**************************************************/
#define LED1 P1_0
#define LED2 P1_1
#define SW1 P1_2
#define FAN P1_7 //风扇
#define LIGHT P2_0 //照明灯
static basicRfCfg_t basicRfConfig;
// 无线RF初始化
void ConfigRf_Init(void)
{
basicRfConfig.panId = PAN_ID;
basicRfConfig.channel = RF_CHANNEL;
basicRfConfig.myAddr = MY_ADDR;
basicRfConfig.ackRequest = TRUE;
while(basicRfInit(&basicRfConfig) == FAILED);
basicRfReceiveOn();
}
/********************MAIN************************/
int keyFlag =0;//按键标志位,当为1时,启动
void keyScan(){
if(SW1==0){
halMcuWaitMs(10);
if(SW1==0){
keyFlag = 1-keyFlag;
while(SW1==0);
}
}
}
int circle = 600;
void pwm(){
for(int i=0;i<circle;i++){
LED1 =1;
halMcuWaitUs(i);
LED1 =0;
halMcuWaitUs(circle-i);
if(SW1==0) return; //如果按键按下,跳出PWM
}
for(int i=0;i<circle;i++){
LED1 =0;
halMcuWaitUs(i);
LED1 =1;
halMcuWaitUs(circle-i);
if(SW1==0) return; //如果按键按下,跳出PWM
}
}
void main(void)
{
halBoardInit();//选手不得在此函数内添加代码
ConfigRf_Init();//选手不得在此函数内添加代码
P1DIR |= 0X83; //设置P1_0和P1_1和P1_7引脚为输出模式,
P2DIR |= 0X01; //设置P2_0为输出模式
LED1 = 0;
LED2 = 0;
uint8 buf[1]; //定义一个数组,发送数据
while(1)
{
/* user code start */
if(basicRfPacketIsReady()){
basicRfReceive(buf,1,NULL);
if(buf[0]=='k'){
FAN = 1;
}
else if(buf[0]=='g'){
FAN = 0;
}
}
keyScan();
if(keyFlag==1){
pwm(); //呼吸灯
LIGHT = 1;
}
else if(keyFlag==1){
LED1 =0;
LIGHT = 0;
}
/* user code end */
}
}