//头文件
1 #include <iocc2530.h> 2 3 #include "hal_mcu.h" 4 #include "hal_assert.h" 5 #include "hal_board.h" 6 #include "hal_rf.h" 7 8 #include <stdio.h> 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过来的数据! "}; uint8 ret; printf("send node start up... "); 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... "); 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过来的数据!"