新闻  |   论坛  |   博客  |   在线研讨会
zigbee基础实验(7)—基于basicRF的进阶实验
luker | 2016-02-25 14:25:56    阅读:404   发布文章
该实验基于basicRF修改,在学习该实验之前请先掌握basicRF的代码。

节点分别为Transmit节点和Calculater节点。Transmit节点通过串口接收PC机发送的数据并将该字符串发送到Calculater节点,Calculater节点接收到数据后将数据翻转后发送给Transmit节点,Transmit将Calculater返回的数据通过串口传送到PC机。

程序流程图:


源代码:
#include
#include
#include
#include
#include
#include
#include
#include "hal_mcu.h"
#include "hal_button.h"
#include "hal_rf.h"
#include "util_lcd.h"
#include "basic_rf.h"
#include "USART.h"

#define RF_CHANNEL                25      // 2.4 GHz RF channel

// BasicRF address definitions
#define PAN_ID                0x2007  //网络ID
#define TRANSMIT_ADDR           0x2520 //发送器地址
#define CALCULATER_ADDR            0xBEEF //运算器地址
#define APP_PAYLOAD_LENGTH        30      //数据长度

// Application states
#define IDLE                      0
#define SEND_CMD                  1

// Application role
#define NONE                      0
#define TRANSMIT                  1
#define CALCULATER                2
#define APP_MODES                 2


static uint8 pTxData[APP_PAYLOAD_LENGTH];
static uint8 pRxData[APP_PAYLOAD_LENGTH];
static basicRfCfg_t basicRfConfig;

// Mode menu
static menuItem_t pMenuItems[] =
{
  "Transmit",   TRANSMIT,
  "Calculater",    CALCULATER
};

static menu_t pMenu =
{
  pMenuItems,
  N_ITEMS(pMenuItems)
};

static void appTransmit();
static void appCalculater();
static uint8 appSelectMode(void);
uint8 temp;
int datanum;
int readyToSend;

/*
Calculater节点程序
*/
static void appCalculater()
{
    halLcdWriteLine(HAL_LCD_LINE_1, "Calculater");
    halLcdWriteLine(HAL_LCD_LINE_2, "Ready");
    // Initialize BasicRF
    basicRfConfig.myAddr = CALCULATER_ADDR;
    if(basicRfInit(&basicRfConfig)==FAILED) {
      HAL_ASSERT(FALSE);
    }
    basicRfReceiveOn();
    // Main loop
    while (TRUE) {
        while(!basicRfPacketIsReady());     
        if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)0) {    
                halLedToggle(1);
                int current=0,end;
                for(int i=APP_PAYLOAD_LENGTH;i=0;i--){  //找出数据末尾
                  if(pRxData[i] == ' '&&pRxData[i-1] == ' ')
                  {end=i;
                  }            
                }
                for(int m=end-2;m=0;m--){                           //执行数据调转
                 pTxData[current++]=pRxData[m];
                }
                current=0;
               halLcdClear();
               halLcdWriteLine(HAL_LCD_LINE_1,"From Ttransmiter:");
               halLcdWriteLine(HAL_LCD_LINE_2,(const char XDATA*)&pRxData);
               halLcdWriteLine(HAL_LCD_LINE_3,"To Ttransmiter");
               halLcdWriteLine(HAL_LCD_LINE_4,(const char XDATA*)&pTxData);
               basicRfSendPacket(TRANSMIT_ADDR,pTxData, APP_PAYLOAD_LENGTH);  //发送数据
               for(int i=0;i                  pTxData[i]=' ';
                 pRxData[i]=' ';
               }           
        }  
    }
}

/*
Transmit节点程序
*/
static void appTransmit()
{
    halLcdWriteLine(HAL_LCD_LINE_1, "Transmit");
    halLcdWriteLine(HAL_LCD_LINE_2, "Joystick Push");
    halLcdWriteLine(HAL_LCD_LINE_3, "Send Command");
    // Initialize BasicRF
    basicRfConfig.myAddr = TRANSMIT_ADDR;
    if(basicRfInit(&basicRfConfig)==FAILED) {
      HAL_ASSERT(FALSE);
    }
     basicRfReceiveOn();
    while(1) {     
     while(readyToSend){
        halLedToggle(1);           //LED闪烁
            basicRfSendPacket(CALCULATER_ADDR,pTxData, APP_PAYLOAD_LENGTH);
            datanum=0;
            for(int i=0;i               pTxData[i]=' ';
            readyToSend=0;
            while(!basicRfPacketIsReady()); 
            basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL);
            halLedToggle(1);
            pRxData[APP_PAYLOAD_LENGTH-1]='\n';
            UartTX_Send_String("From EndDevice:\r\n",sizeof("From EndDevice:\r"),1);
            UartTX_Send_String(pRxData,APP_PAYLOAD_LENGTH,1);          
     }
    }
}


void main(void)
{
   initUART0(); //串口初始化
   uint8 appMode = NONE;
    basicRfConfig.panId = PAN_ID;
    basicRfConfig.channel = RF_CHANNEL;
    basicRfConfig.ackRequest = TRUE;
    halBoardInit();
    halJoystickInit();
    if(halRfInit()==FAILED) {
      HAL_ASSERT(FALSE);
    }
    halLedSet(1);
    utilPrintLogo("Welcome!");
    while (halButtonPushed()!=HAL_BUTTON_1);
    halMcuWaitMs(350);
    halLcdClear();
    appMode = appSelectMode();
    halLcdClear();
    if(appMode == TRANSMIT) {
        appTransmit();
    }
    else if(appMode == CALCULATER) {
        appCalculater();
    }
    HAL_ASSERT(FALSE);
}


static uint8 appSelectMode(void)
{
    halLcdWriteLine(1, "Device Mode: ");
    return utilMenuSelect(&pMenu);
}

#pragma vector = URX0_VECTOR         //中断服务子程序,处理接收到字符产生的中断
 __interrupt void UART0_ISR(void)
 {
        URX0IF = 0;//清中断标志   
        RLED = 1;//发送状态指示  
        if(U0DBUF!='#')
        pTxData[datanum++]=U0DBUF; 
        else
        readyToSend=1;       
 }

实验结果:

zigbee基础实验(7)—基于basicRF的进阶实验 - j_evil - J_evil
calculator节点的效果图(接收到的数据为12 34 56 78,返回的数据将为87 65 43 21)

zigbee基础实验(7)—基于basicRF的进阶实验 - j_evil - J_evil
 PC机效果图




参与讨论
登录后参与讨论
推荐文章
最近访客