http://blog.sina.com.cn/s/blog_5f6e4fce0100htd4.html
实验器材:
无线龙C51RF3-PK ,(06光盘)...\C51RF-3-PK无线ZigBee网络开发平台V5.10\C51RF-3演示程序\点对点无线通信\点对点无线基本通信
实验目的:
实现个人目的的无线传输。
实验步骤:
1> 函数调用脉络:主函数 app_ex_main.c
void main(void)
{
SET_MAIN_CLOCK_SOURCE(CRYSTAL); //设置主时钟:晶振32MHz
RFPWR = 0x04; //射频功率
while(RFPWR & 0x10); //等待稳定
initUART(); //初始化串口
//IO_DIR_PORT_PIN(0,5,IO_OUT);
//IO_DIR_PORT_PIN(1,3,IO_OUT);
//IO_DIR_PORT_PIN(1,2,IO_IN);
//IO_DIR_PORT_PIN(1,2,IO_OUT);
//P0_5 = 1;
//P1_0 = 0;
rf_test_main(); //调用rf_test.c中的主函数rf_test_main();
}
2> 射频测试子程序 rf_test.c
#ifdef COMPLETE_APPLICATION
void rf_test_main(void){
#else
void main(void){
#endif //条件编译主函数名
INT_GLOBAL_ENABLE(INT_ON); //EA=1;开中中断
#ifdef RX
myAddr = ADDRESS_0;
remoteAddr = ADDRESS_1;
initRfTest(); //初始化射频
receiveMode(); //定义本模块为接收功能
}
#else
{
myAddr = ADDRESS_1;
initRfTest();
contionuousMode(); //定义接收
}
#endif
}
3> 接收部分:receiveMode()
void receiveMode(void)
{
BYTE* receiveBuffer;
BYTE length;
BYTE res;
BYTE sender;
while(1)
{
RLED = LED_ON; //开始接受,红灯闪烁
res = radioReceive(&receiveBuffer, &length, RECEIVE_TIMEOUT, &sender);
halWait(200);
RLED = LED_OFF;
if(res == TRUE)
{
GLED = LED_ON; //接收成功,绿灯亮闪烁
YLED=!YLED; //自己添加的黄色指示灯,添加方法:RF04EB.h
halWait(200);
}
else
{
GLED = LED_OFF; //接受失败,绿灯灭
halWait(200);
}
GLED = LED_OFF;
}
}
4> 发送部分 contionuousMode()
void contionuousMode(void)
{
BOOL res; //结果变量
BYTE sendBuffer[] = "Hello"; //待发送字符
while(1)
{
RLED = LED_ON;
res = radioSend(sendBuffer, sizeof(sendBuffer), remoteAddr, DO_NOT_ACK );
halWait(200);
RLED = LED_OFF;
if(res == TRUE) //发送成功,绿灯亮
{
GLED = LED_ON;
YLED=!YLED; //自己加一句用来测试
halWait(200);
}
else //发送失败,绿灯灭
{
GLED = LED_OFF;
halWait(200);
}
GLED = LED_OFF;
}
}
总结:条件编译的好处是可以把 收发双方的程序写在同一个工程目录下,无须分开写,编译器会有选择编译