修改已下代码
发送:
#include "hal_defs.h"
#include "hal_cc8051.h"
#include "hal_mcu.h"
#include "hal_int.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_rf.h"
#include "basic_rf.h"
#include <stdio.h>
#include <string.h>
#include "hal_uart.h"
#include <stdarg.h>
#define RF_CHANNEL 18
#define My_ADDR 0x400A
#define Pan_ID 0x0840
#define SEND_ADDR 0x400B
unsigned char uart_recv_cmd = 0;
unsigned int adc_value = 0;
float adc_volt = 0;
unsigned char rf_send_dat = 0;
unsigned char str[64];
static basicRfCfg_t BasicRf_Cfg;
void BasicRf_Init(void)
{
BasicRf_Cfg.myAddr = My_ADDR;
BasicRf_Cfg.panId = Pan_ID;
BasicRf_Cfg.channel = RF_CHANNEL;
BasicRf_Cfg.ackRequest = TRUE;
while(basicRfInit(&BasicRf_Cfg)==FAILED);
basicRfReceiveOn();
}
void ADC_Init()
{
APCFG |= 0X01;
P0SEL |= ~0X01;
P0DIR &= ~0X01;
}
void ADC_Data()
{
ADCCON3=0XB0;
while(!(ADCCON1 & 0X80));
adc_value =ADCH<<8;
adc_value =adc_value|ADCL;
adc_value = adc_value>>3;
adc_volt =(adc_value*3.3)/4096;
rf_send_dat=((int)(adc_volt*10));
basicRfSendPacket(SEND_ADDR,&rf_send_dat,1);
}
void Uart_W1_hhy()
{
uint16 Flag=0;
Flag = halUartRxLen();
if(Flag>0)
{
halUartRead(&uart_recv_cmd,1);
if(uart_recv_cmd==0x40)
{
sprintf((char*)str,"xxx同学,你的传感器的光照电压为 %.2f V.\r\n",adc_volt);
halUartWrite(str,sizeof(str));
}
}
}
void main()
{
halBoardInit();
BasicRf_Init();
while(1)
{
ADC_Data();
Uart_W1_hhy();
}
}
接收:
#include "hal_defs.h"
#include "hal_cc8051.h"
#include "hal_mcu.h"
#include "hal_int.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_rf.h"
#include "basic_rf.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "hal_uart.h"
#define RF_CHANNEL 18
#define My_ADDR 0x400B
#define Pan_ID 0x0840
#define SEND_ADDR 0x400A
unsigned char BasicRf_recv_cmd = 0;
static basicRfCfg_t BasicRf_Cfg;
void BasicRf_Init(void)
{
BasicRf_Cfg.myAddr = My_ADDR;
BasicRf_Cfg.panId = Pan_ID;
BasicRf_Cfg.channel = RF_CHANNEL;
BasicRf_Cfg.ackRequest = TRUE;
while(basicRfInit(&BasicRf_Cfg)==FAILED);
basicRfReceiveOn();
}
void Rece_Mes()
{
if(basicRfPacketIsReady()==TRUE)
{
basicRfReceive(&BasicRf_recv_cmd,1,NULL);
if(BasicRf_recv_cmd<10)
{
halLedSet(1);
halLedSet(2);
}
else if(BasicRf_recv_cmd>10&&BasicRf_recv_cmd<20)
{
halLedSet(1);
halLedClear(2);
}
else
{
halLedClear(1);
halLedClear(2);
}
}
}
void main()
{
halBoardInit();
BasicRf_Init();
while(1)
{
Rece_Mes();
}
}
使之能够实现
应用BasicRf软件库实现如下功能:1、采用ZigBee模块(节点1)、带串口的ZigBee模块(节点2)和PC机组成一套智能光照采集系统。2、节点1采集光照数据信息,并将其循环发送至节点2;3、节点1通过串口连接到上位机,波特率为9600,当上位机收到请求指令0x+学号后两位时,向上位机上报光照电压:“XXX同学,你的传感器光照电压为:xxxV”。4、节点2收到节点1发过来的数据后进行判定,如果数据大于20则Led1,Led2关闭,如果数据大于10小于20则Led1开,Led2关,如果数据小于10,则Led1,Led2开启。5、节点2通过串口连接上位机,波特率为9600,每隔5秒显示一次警报信息:当前灯光适中/当前光照较强/当前光照较