分享

基于basicRF的进阶实验

 小酒馆II 2012-07-29
基于basicRF的进阶实验
该实验基于basicRF修改,在学习该实验之前请先掌握basicRF的代码。
节点分别为Transmit节点和Calculater节点。Transmit节点通过串口接收PC机发送的数据并将该字符串发送到Calculater节点,Calculater节点接收到数据后将数据翻转后发送给Transmit节点,Transmit将Calculater返回的数据通过串口传送到PC机。

程序流程图:



源代码:
#include <math.h>
#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"
#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<=APP_PAYLOAD_LENGTH;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<=APP_PAYLOAD_LENGTH;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;      
 }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多