最近看论坛,有些人调试这个,很多的时候都不成功。所以共享一个自己使用的在很多MCU上调试通过的NRF24L01库。这个库是从mbed上移植过来的,相当的稳定。而且对于开发者来说使用起来也很简单。
先看下这个库的目录结构:
NRF240L01_LIB
│
├─Driver
│ NRF24L01P.c
│ NRF24L01P.h
│
└─Hal
│ i2c_api.h
│
└─Target
└─GD32F105R
gd32f1x0_i2c_api.c
其中Drvier是nRF24L01P是基于HAL下的i2c_api.h实现的驱动,这个我们是不用管。我们需要做的就是实现特定平台下i2c_api.h定义的函数。例如上面代码实现了GD32F105系统MCU的i2c_api接口。那么i2c_api.h有些什么东西呢,其实也很简单,只需要实现5个函数,看代码就知道了:
- /**
- ****************************************************************************************
- *
- * [url=home.php?mod=space&uid=1455510]@file[/url] i2c_api.h
- *
- * [url=home.php?mod=space&uid=247401]@brief[/url] Header file of i2c hardware abstract layer api.
- *
- * Copyright (C) sunsjw 2012-2020
- *
- * $Rev: 5444 $
- *
- ****************************************************************************************
- */
- #ifndef _I2C_API_H
- #define _I2C_API_H
- #include <stdint.h>
- //**************************************
- //向I2C设备写入一个字节数据
- //**************************************
- void i2c_init(void);
- //**************************************
- //向I2C设备写入一个字节数据
- //**************************************
- void i2c_WriteByte(uint8_t SlaveAddr,uint8_t REG_Address,uint8_t REG_data);
- //**************************************
- //从I2C设备读取一个字节数据
- //**************************************
- uint8_t i2c_ReadByte(uint8_t SlaveAddr,uint8_t REG_Address);
- //**************************************
- //从I2C设备读取指定长度字节数据
- //**************************************
- int i2c_ReadBytes(int dev_address,int reg_address,char* buffer,int length);
- //**************************************
- //向I2C设备写入多字节数据
- //**************************************
- int i2c_WriteBytes(int dev_address,int reg_address,char* buffer,int length);
- #endif
就是I2C的初始化,读写操作。每个MCU不尽相同,大家去实现自己所用MCU的就可以了。
主程序调用就更简单了,首先声名一个全局的nRF24L01的实例 extern NRF_Class_cb_TypeDef nrf_obj;然后在main函数里调用初始化,上电,设置等一些函数,就可以了。还是发代码给大家看比较清楚:
- extern NRF_Class_cb_TypeDef nrf_obj;
- char txbuf[20];
- int main()
- {
- nrf_obj.initialize();
-
- nrf_obj.powerUp();
- //
- nrf_obj.setAirDataRate(NRF24L01P_DATARATE_2_MBPS);
- //设置传输大小
- nrf_obj.setTransferSize(sizeof(txbuf),NRF24L01P_PIPE_P0);
- //设置为发送模式 如果是接收则调用 nrf_obj.setReceiveMode
- nrf_obj.setTransmitMode();
- nrf_obj.enable();
-
- while(1)
- {
- result = nrf_obj.write(NRF24L01P_PIPE_P0,(char*)&txbuf,sizeof(txbuf));
- if( result == -1)
- {
- wait_us(100);
- }
- }
- }
OK,搞定,希望对大家有用。如有疑问可以跟贴。
|