ZigBee 点对点通信和串行通信

ZigBee 点对点通信和串行通信_第1张图片

 

代码A模块:

#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 
#include 
#include 
/*****点对点通讯地址设置******/
#define RF_CHANNEL                25         // 频道 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

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();
}

uint8 count=0;
uint8 buf[3];

void keyScan(){
  if(SW1==0){
    halMcuWaitMs(10);
    if(SW1==0){
      count++;
      buf[1] = count;
      basicRfSendPacket(SEND_ADDR,buf,3);
      LED1 = 1;
      LED2 = 0;     
      while(SW1==0);  //等待松手
      LED1 = 1;
      LED2 = 1; 
    } 
  }
}


/********************MAIN************************/
void main(void)
{
    halBoardInit();//选手不得在此函数内添加代码
    ConfigRf_Init();//选手不得在此函数内添加代码
    
    P1DIR |= 0X03;
    
    buf[0] = 0xFA;
    buf[2] = 0xFB;
    
    while(1)
    {
    /* user code start */
      
      keyScan();  
      
    /* user code end */
    }
}

代码B模块:

#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 
#include 
#include 
/*****点对点通讯地址设置******/
#define RF_CHANNEL                25         // 频道 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

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();
}

uint8 buf[3];
void receiveData(){
  if(basicRfPacketIsReady()){
    basicRfReceive(buf,3, NULL);
    halUartWrite(buf, 3);
    halLedToggle(1);
  }
}


/********************MAIN************************/
void main(void)
{
    halBoardInit();//选手不得在此函数内添加代码
    ConfigRf_Init();//选手不得在此函数内添加代码
    
    P1DIR |= 0X03;
    halUartInit(115200);
    
    while(1)
    {
    /* user code start */
      receiveData();
      

    /* user code end */
    }
}

你可能感兴趣的:(物联网)