BasicRF点对点环境中的串口通信


实验耗材

开发板x2,天线x2,串口转USB线x1,仿真器x1,排线x1,USB线x1,电源线x2

代码部分rf_set.c

#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 
#include 
#include 



/*****点对点通讯地址设置******/


#define RF_CHANNEL                20         // 频道 11~26
#define PAN_ID                    0x1A2C     //网络id 

#define A 1

#ifdef A
    #define MY_ADDR                   0x105C     //本机模块地址
    #define SEND_ADDR                 0x2F1B     //发送地址
#else
    #define MY_ADDR                   0x2F1B     //本机模块地址
    #define SEND_ADDR                 0x105C     //发送地址
#endif


#define D5 P1_3
#define D4 P1_1
#define D3 P1_0
#define D6 P1_4


/**************************************************/
static basicRfCfg_t basicRfConfig;

unsigned char key_recv;
unsigned char key_send;

unsigned char RecvDFG;

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


void Init_Port()
{
    P1DIR |= 0x1B;
    D5 = 0;
    D4 = 0;
    D3 = 0;
    D6 = 0;
}

void Send_Byte(unsigned char content)
{
    U0DBUF = content;
    while(UTX0IF == 0);
    UTX0IF = 0;
}


void Send_String(unsigned char *content)
{
    while(*content != '\0')
    {
        Send_Byte(*content++);
    }
}


#pragma vector = URX0_VECTOR
__interrupt void URX0_ISR(void)
{
    key_send = U0DBUF;
    RecvDFG = 1;
}



/********************MAIN************************/
void main(void)
{
    halBoardInit();//选手不得在此函数内添加代码
    ConfigRf_Init();//选手不得在此函数内添加代码
    Init_Port();
    
#ifdef A
    halUartInit(9600);
    
    Send_String("Hello, World!\n");
    //Send_Byte(0xFF);
#endif
    
    while(1)
    {
    /* user code start */
        
#ifdef A
        if(RecvDFG == 1)
        {
            RecvDFG = 0;
            basicRfSendPacket(SEND_ADDR, &key_send, 1);
        }
#endif
        
        
        if(basicRfPacketIsReady())
        {
            basicRfReceive(&key_recv, 1, NULL);
            
            if(key_recv == 0xD5) D5 = ~D5;
            else if(key_recv == 0xD3) D3 = ~D3;
            else if(key_recv == 0xD4) D4 = ~D4;
            else if(key_recv == 0xD6) D6 = ~D6;
        }
      
    /* user code end */
    }
}

项目目录

使用方式

1.在A开发板插上排线,下载到开发板并调试运行,注意,此时第22行代码应该为如下

#define A 1

2.在B开发板插上排线,下载到开发板并调试运行,注意,此时第22行代码应该为如下

//#define A 1

3.打开串口调试工具

一定要注意以上几个红框里的内容,COM号,波特率一定要正确选择
4.输入D3或D4或D5或D6并发送B板LED灯会产生亮灭,暂时没做多个输入,只能一个个控制LED(还不熟悉C,不知道分割api,hhc)

视频演示

点击查看视频