NRF24L01全双工调试程序 自动切换收发模式 可用于实现对讲机
更新时间:2023-11-12 20:57:01 阅读量: 教育文库 文档下载
//NRF24L01调试程序,全双工双向通信,即两个模块兼具收发功能,自动高速切换收发模式。
//使用本程序的前提条件是:硬件没问题。如果能正确运行本程序,也能说明你的硬件电路没问题。
//大家根据实际情况,更改 NRF24L01 各引脚、LED 和 KEY 的宏定义。
//LED灯的状态由RxBuf[0]来决定,RxBuf[0] == 0,LED输出0;RxBuf[0] == 1,LED输出1;你也可以自行更改规则。
//KEY可以改变TxBuf[0]的值,从而改变另一起模块RxBuf[0]的值,进而改变LED的状态。 //此程序未用到外部中断,即不响应IRQ 因此IRQ引脚可以接任意I/O口,不必接到外部中断引脚,不过建议接到外部中断引脚。
//若注释部分为乱码:设置keil:Edit>Configuration>Editor>Encoding 选择chinese2312。 //以下程序经过多次复制,粘贴到keil,没有错误,如出现错误,请下载文档后再复制。 //粘贴到word后排布可能会变得不整齐,请自行排布。
//全双工应用实例:http://v.youku.com/v_show/id_XNzM4MDYwNzY0.html
#include
#define uchar unsigned char #define uint unsigned int
sbit P00 = P0^0; sbit P01 = P0^1; sbit P02 = P0^2; sbit P03 = P0^3; sbit P04 = P0^4; sbit P05 = P0^5; sbit P06 = P0^6; sbit P07 = P0^7;
sbit P10 = P1^0; sbit P11 = P1^1; sbit P12 = P1^2; sbit P13 = P1^3; sbit P14 = P1^4; sbit P15 = P1^5; sbit P16 = P1^6; sbit P17 = P1^7;
sbit P20 = P2^0; sbit P21 = P2^1; sbit P22 = P2^2; sbit P23 = P2^3;
sbit P24 = P2^4; sbit P25 = P2^5; sbit P26 = P2^6; sbit P27 = P2^7;
sbit P30 = P3^0; sbit P31 = P3^1; sbit P32 = P3^2; sbit P33 = P3^3; sbit P34 = P3^4; sbit P35 = P3^5; sbit P36 = P3^6; sbit P37 = P3^7;
#define LED P36 #define KEY P20
//*****************************************延***************************************// void delayus(uint us) { for(;us >0;us--) { _nop_(); } } /*
void delayms(uint ms)//延时?个 ms {
uchar a,b,c; while(ms--) { for(c=1;c>0;c--)
for(b=142;b>0;b--) for(a=2;a>0;a--); } }*/
//****************************************NRF24L01 *********************************// #define CE P10 #define SCK P11 #define MISO P12 #define CSN P16
时IO端函定数
义
口#define MOSI P17 #define IRQ P32
//*****************************NRF24L01的接收和发送地址***************************************//
#define TX_ADR_WIDTH 5 // 5个字节的TX地址长度 #define RX_ADR_WIDTH 5 // 5个字节的RX地址长度 #define TX_PLOAD_WIDTH 16 // ?个字节的TX数据长度 #define RX_PLOAD_WIDTH 16 // ?个字节的RX数据长度
uchar const TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址 uchar const RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址 //***************************************NRF24L01寄存器指令**********************************//
#define READ_REG 0x00 // 读寄存器 #define WRITE_REG 0x20 // 写寄存器 #define RD_RX_PLOAD 0x61 // 读取接收数据 #define WR_TX_PLOAD 0xA0 // 写待发数据 #define FLUSH_TX 0xE1 // 冲洗发送 FIFO #define FLUSH_RX 0xE2 // 冲洗接收 FIFO #define REUSE_TX_PL 0xE3 // 定义重复装载数据 #define NOP 0xFF // 保留
//*************************************SPI(nRF24L01)寄存器地址*****************************//
#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式 #define EN_AA 0x01 // 自动应答功能设置 #define EN_RXADDR 0x02 // 可用信道设置 #define SETUP_AW 0x03 // 收发地址宽度设置 #define SETUP_RETR 0x04 // 自动重发功能设置 #define RF_CH 0x05 // 工作频率设置
#define RF_SETUP 0x06 // 发射速率、功耗功能设置 #define STATUS 0x07 // 状态寄存器 #define OBSERVE_TX 0x08 // 发送监测功能
#define CD 0x09 // 地址检测 #define RX_ADDR_P0 0x0A // 频道0接收数据地址 #define RX_ADDR_P1 0x0B // 频道1接收数据地址 #define RX_ADDR_P2 0x0C // 频道2接收数据地址 #define RX_ADDR_P3 0x0D // 频道3接收数据地址 #define RX_ADDR_P4 0x0E // 频道4接收数据地址 #define RX_ADDR_P5 0x0F // 频道5接收数据地址 #define TX_ADDR 0x10 // 发送地址寄存器
#define RX_PW_P0 0x11 // 接收频道0接收数据长度 #define RX_PW_P1 0x12 // 接收频道0接收数据长度 #define RX_PW_P2 0x13 // 接收频道0接收数据长度 #define RX_PW_P3 0x14 // 接收频道0接收数据长度 #define RX_PW_P4 0x15 // 接收频道0接收数据长度
#define RX_PW_P5 0x16 // 接收频道0接收数据长度 #define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置 void init_NRF24L01(); uchar SPI_RW(uchar num); uchar SPI_Read(uchar reg); void SetRX_Mode(); void SetTx_Mode();
uchar SPI_RW_Reg(uchar reg, uchar value);
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars); uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars); void nRF24L01_RxPacket(uchar *rx_buf); void nRF24L01_TxPacket(uchar *tx_buf);
//****************************************状态标志****************************************// uchar bdata sta; sbit RX_DR =sta^6; sbit TX_DS =sta^5; sbit MAX_RT =sta^4;
//********************************NRF24L01初始化******************************************// void init_NRF24L01() {
delayus(100);
CE=0; // 片选使能 CSN=1; // SPI使能 SCK=0; // SPI时钟拉低 SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); //写本地地址 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); //写接收端地址 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); //通道0自动应答 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); //允许接收地址频道0 SPI_RW_Reg(WRITE_REG + RF_CH, 0x32); //设置信道工作频率,收发必须一致 SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //设置发射速率为2MHZ,发射功率为最大值0dB SPI_RW_Reg(WRITE_REG + CONFIG, 0x7c); //IRQ引脚不显示中断 掉电模式 1~16CRC校验 }
/****************************************************************************************************
/*函数:uint SPI_RW(uint uchar) /*功能:NRF24L01的SPI写时序
/****************************************************************************************************/ uchar SPI_RW(uchar num) { uchar bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit { MOSI = (num & 0x80); // output 'uchar', MSB to MOSI num = (num << 1); // shift next bit into MSB.. SCK = 1; // Set SCK high.. num |= MISO; // capture current MISO bit SCK = 0; // ..then set SCK low again }
return(num); // return read uchar }
/****************************************************************************************************
函数:uchar SPI_Read(uchar reg) 功能:NRF24L01的SPI时序
****************************************************************************************************/ uchar SPI_Read(uchar reg) { uchar reg_val; CSN = 0; // CSN low, initialize SPI communication... SPI_RW(reg); // Select register to read from.. reg_val = SPI_RW(0); // ..then read registervalue CSN = 1; // CSN high, terminate SPI communication return(reg_val); // return register value }
/****************************************************************************************************/
/*功能:NRF24L01读写寄存器函数
/****************************************************************************************************/
uchar SPI_RW_Reg(uchar reg, uchar value) { uchar status;
CSN = 0; // CSN low, init SPI transaction status = SPI_RW(reg); // select register SPI_RW(value); // ..and write value to it.. CSN = 1; // CSN high again return(status); // return nRF24L01 status uchar }
/****************************************************************************************************/
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes) { uchar status,byte_ctr; CSN = 0; // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status byte for(byte_ctr=0;byte_ctr /********************************************************************************************************* /*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) /*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数 /*********************************************************************************************************/ uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) { uchar status,uchar_ctr; CSN = 0; //SPI使能 status = SPI_RW(reg); for(uchar_ctr=0; uchar_ctr //********** nrf收发程序 ********** void nrf_RxTx(uchar mod_nrf,uchar *buff) //NRF24L01收发程序 { static uchar mod_nrf_b;//static 地址不释放 //******进入发射模式****** if(mod_nrf == 't') { if(mod_nrf_b != 't') { mod_nrf_b = 't'; CE = 0; SPI_RW_Reg(WRITE_REG+STATUS,0xff); //清除中断标志 SPI_RW_Reg(FLUSH_TX,0x00); //清除TX_FIFO寄存器 SPI_RW_Reg(WRITE_REG + CONFIG,0x7e);//IRQ引脚不显示中断 上电 发射模式 1~16CRC校验 CE = 1; delayus(130);//从CE = 0 到 CE = 1;即待机模式到收发模式,需要最大130us } //******发送数据****** CE = 0; //StandBy I模式 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址 SPI_Write_Buf(WR_TX_PLOAD,buff,TX_PLOAD_WIDTH); // 装载数据 CE = 1; //置高CE激发数据发送 delayus(130);//从CE = 0 到 CE = 1;即待机模式到收发模式,需要最大130us delayus(100); //给发送数据一点时间 比发送速度较快 延时可以比接收少 sta = SPI_Read(STATUS);//读取状态寄存器的值 SPI_RW_Reg(WRITE_REG+STATUS,sta);//清除对应的中断 if(TX_DS == 1)//发送成功再清除tx fifo寄存器 { CE = 0; SPI_RW_Reg(FLUSH_TX,0x00); //清除tx fifo寄存器 //********重要********* CE = 1; } } //******进入接收模式****** else if(mod_nrf == 'r')//接收模式 { if(mod_nrf_b != 'r') { mod_nrf_b = 'r'; CE = 0; SPI_RW_Reg(WRITE_REG+STATUS,0xff); //清除中断标志 SPI_RW_Reg(FLUSH_RX,0x00); //清除RX_FIFO寄存器 SPI_RW_Reg(WRITE_REG + CONFIG, 0x7f);//IRQ引脚不显示中断 上电 接 收模式 1~16CRC校验 CE = 1; delayus(130);//从CE = 0 到 CE = 1;即待机模式到收发模式,需要最大130us } delayus(500); //不能少 值可调 给接收数据一点时间 sta = SPI_Read(STATUS); SPI_RW_Reg(WRITE_REG+STATUS,sta); if(RX_DR == 1) { CE = 0; SPI_Read_Buf(RD_RX_PLOAD,buff,RX_PLOAD_WIDTH);//读取数据 存入数组 SPI_RW_Reg(FLUSH_RX,0x00);//清除rx fifo寄存器 数据不抖动 CE = 1; } } } void main() { uchar idata RxBuf[16]; //接收缓存 存入idata区 uchar idata TxBuf[16]; //发送缓存 uint t_while = 0; uint t_tx = 100; uint t_rx = 100; init_NRF24L01(); //nrf24l01初始化 RxBuf[0] = 0; TxBuf[0] = 1; LED = 0; while(1) { t_while = t_tx; while(t_while--) //一旦发送成功就跳出循环 最多发送t_while次 { nrf_RxTx('t',TxBuf); //发送模式 if(TX_DS == 1) break; } t_while = t_rx ; while(t_while--) { nrf_RxTx('r',RxBuf); //发送成功 //跳出循环进入接收模式 //一旦接收成功就跳出循环 最多接收t_while次 //接收模式 if(RX_DR == 1) break; } if(RxBuf[0] == 0) LED = 0; else LED = 1; if(KEY == 0) TxBuf[0] = 0; else TxBuf[0] = 1; } } //接收成功 //跳出循环进入发送模式
正在阅读:
NRF24L01全双工调试程序 自动切换收发模式 可用于实现对讲机11-12
材料力学性能试题集11-15
第三讲学生111-15
2011年12月中国房地产政策跟踪报告04-29
行政助理试用期工作小结(精选多篇)09-27
秘书四级考试情景录像题解读(整理版)11-15
票据法案例题选05-08
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 双工
- 对讲机
- 收发
- 调试
- NRF24L01
- 切换
- 用于
- 模式
- 实现
- 自动
- 程序
- 中建三局周年纪念画册策划文案 - 图文
- 《七律长征》评课稿
- 成都市文化创意产业发展现状和问题
- 微生物学名词解释
- 农村初中体育学习评价方法的有效策略研究 - 图文
- 西工大画法几何与建筑制图(下)大作业B卷16年9月作业考核答案
- 部编人教版六年级上册《道德与法治》第一单元测试卷及答案 - 图文
- 迪斯尼主题公园成功经营策略
- EXCEL考核题目
- ansys复杂几何模型网格划分技术
- SQL - Server实用教程(第三版)实验4 - 数据库的查询和视图
- 第7课时 回顾与整理—三位数乘两位数 台儿庄 田仲粉
- 仿写句子练习题
- 全数字锁相环与位同步实验
- 2019-2020年高二上学期第一次月考生物试题(选修)缺答案
- 热风岗位新员工强化学习第一阶段考试题
- 如何编制2013版能源行业清洁生产项目商业计划书(符合VC风投+甲级资质)及融资方案实施指导 - 图文
- 审计学认定
- CANopen协议
- 德国宪法案例选释基本权利总论编写说明