您的当前位置:首页正文

无线传感网——zigbee基础实验-点对点通信

2023-02-17 来源:华佗健康网
⽆线传感⽹——zigbee基础实验-点对点通信

//头⽂件

1 #include 2

3 #include \"hal_mcu.h\" 4 #include \"hal_assert.h\" 5 #include \"hal_board.h\" 6 #include \"hal_rf.h\" 7

8 #include 9 #include \"basic_rf.h\"10

11 #define NODE_TYPE 0 12 #define RF_CHANNEL 25 13

14 #define PAN_ID 0x2007 15 #define SEND_ADDR 0x2530 16 #define RECV_ADDR 0x2520 17

18 static basicRfCfg_t basicRfConfig;

先将NODE_TYPE改为1(发送),然后可找⼀个标识为Status的盒⼦编译烧写此程序(断电)再将NODE_TYPE改为0(接收),然后可找⼀个标识为Data的盒⼦编译烧写此程序

RF数据发送函数

void rfSendData(void){

uint8 pTxData[] = {\"你好,我是发送端CC2530过来的数据!\\r\\n\\r\\n\ uint8 ret;

printf(\"send node start up...\\r\\n\");

basicRfReceiveOff();

while(TRUE) {

ret = basicRfSendPacket(RECV_ADDR, pTxData, sizeof pTxData);

if (ret == SUCCESS) {

hal_led_on(1);

halMcuWaitMs(100); hal_led_off(1);

halMcuWaitMs(900); }

else {

hal_led_on(1);

halMcuWaitMs(1000); hal_led_off(1); } }}

 RF数据接收函数 

1 void rfRecvData(void) 2 {

3 uint8 pRxData[128]; 4 int rlen; 5 6

7 printf(\"recv node start up...\\r\\n\"); 8

9 basicRfReceiveOn();10

11 while(TRUE) 12 {

13 while(!basicRfPacketIsReady());

14 rlen = basicRfReceive(pRxData, sizeof pRxData, NULL);15 if(rlen > 0) 16 {

17 printf((char *)pRxData); 18 }19 }20 }

主函数

void main(){

halMcuInit(); //MCU初始化 hal_led_init(); //LED初始化 hal_uart_init(); //Uart初始化

if(FAILED == halRfInit()) //CC2530-RF 初始化 {

HAL_ASSERT(FALSE); }

basicRfConfig.panId = PAN_ID;

basicRfConfig.channel = RF_CHANNEL; basicRfConfig.ackRequest = TRUE;

#if NODE_TYPE

basicRfConfig.myAddr = SEND_ADDR; //(0x2530) #else

basicRfConfig.myAddr = RECV_ADDR; //(0x2520) #endif

if(basicRfInit(&basicRfConfig)==FAILED) {

HAL_ASSERT(FALSE); }

#if NODE_TYPE rfSendData(); #else

rfRecvData(); #endif}

再将刚才烧写好的发送盒⼦拼接到接收盒⼦上开串⼝调试器后(两根线都连接收盒)进⾏跟踪结果如下:(接收盒⼦不断有数据过来)\"你好,我是发送端CC2530过来的数据!\"

因篇幅问题不能全部显示,请点此查看更多更全内容