nrf24l01.c与nrf24l01.h,程序示例
时间: 2024-05-06 12:16:01 浏览: 275
Wire (1)_NRF24L01_
以下是一个基本的nrf24l01.c和nrf24l01.h文件的程序示例,仅供参考:
nrf24l01.h 文件:
```c
#ifndef __NRF24L01_H
#define __NRF24L01_H
#include "stm32f10x.h"
//定义NRF24L01的寄存器地址
#define NRF24L01_CONFIG 0x00
#define NRF24L01_EN_AA 0x01
#define NRF24L01_EN_RXADDR 0x02
#define NRF24L01_SETUP_AW 0x03
#define NRF24L01_SETUP_RETR 0x04
#define NRF24L01_RF_CH 0x05
#define NRF24L01_RF_SETUP 0x06
#define NRF24L01_STATUS 0x07
#define NRF24L01_OBSERVE_TX 0x08
#define NRF24L01_CD 0x09
#define NRF24L01_RX_ADDR_P0 0x0A
#define NRF24L01_RX_ADDR_P1 0x0B
#define NRF24L01_RX_ADDR_P2 0x0C
#define NRF24L01_RX_ADDR_P3 0x0D
#define NRF24L01_RX_ADDR_P4 0x0E
#define NRF24L01_RX_ADDR_P5 0x0F
#define NRF24L01_TX_ADDR 0x10
#define NRF24L01_RX_PW_P0 0x11
#define NRF24L01_RX_PW_P1 0x12
#define NRF24L01_RX_PW_P2 0x13
#define NRF24L01_RX_PW_P3 0x14
#define NRF24L01_RX_PW_P4 0x15
#define NRF24L01_RX_PW_P5 0x16
#define NRF24L01_FIFO_STATUS 0x17
#define NRF24L01_DYNPD 0x1C
#define NRF24L01_FEATURE 0x1D
//定义NRF24L01指令
#define NRF24L01_R_REGISTER 0x00
#define NRF24L01_W_REGISTER 0x20
#define NRF24L01_R_RX_PAYLOAD 0x61
#define NRF24L01_W_TX_PAYLOAD 0xA0
#define NRF24L01_FLUSH_TX 0xE1
#define NRF24L01_FLUSH_RX 0xE2
#define NRF24L01_REUSE_TX_PL 0xE3
#define NRF24L01_NOP 0xFF
//定义NRF24L01寄存器位
#define NRF24L01_CONFIG_MASK_RX_DR 0x40
#define NRF24L01_CONFIG_MASK_TX_DS 0x20
#define NRF24L01_CONFIG_MASK_MAX_RT 0x10
#define NRF24L01_CONFIG_EN_CRC 0x08
#define NRF24L01_CONFIG_CRCO 0x04
#define NRF24L01_CONFIG_PWR_UP 0x02
#define NRF24L01_CONFIG_PRIM_RX 0x01
#define NRF24L01_STATUS_RX_DR 0x40
#define NRF24L01_STATUS_TX_DS 0x20
#define NRF24L01_STATUS_MAX_RT 0x10
#define NRF24L01_STATUS_TX_FULL 0x01
#define NRF24L01_RF_SETUP_CONT_WAVE 0x80
#define NRF24L01_RF_SETUP_RF_DR_LOW 0x20
#define NRF24L01_RF_SETUP_PLL_LOCK 0x10
#define NRF24L01_LNA_HCURR 0x01
#define NRF24L01_RX_PW_PX_DEFAULT 0x00
#define NRF24L01_SPI_TIMEOUT 100
//定义NRF24L01模块的IO口
#define NRF24L01_CE_PORT GPIOB
#define NRF24L01_CE_PIN GPIO_Pin_0
#define NRF24L01_CSN_PORT GPIOB
#define NRF24L01_CSN_PIN GPIO_Pin_1
#define NRF24L01_IRQ_PORT GPIOB
#define NRF24L01_IRQ_PIN GPIO_Pin_5
void NRF24L01_GPIO_Init(void);
void NRF24L01_SPI_Init(void);
void NRF24L01_Init(void);
void NRF24L01_CE(uint8_t level);
void NRF24L01_CSN(uint8_t level);
uint8_t NRF24L01_ReadWriteByte(uint8_t txData);
uint8_t NRF24L01_ReadReg(uint8_t regAddr);
void NRF24L01_WriteReg(uint8_t regAddr, uint8_t txData);
void NRF24L01_ReadBuf(uint8_t regAddr, uint8_t *pBuf, uint8_t len);
void NRF24L01_WriteBuf(uint8_t regAddr, uint8_t *pBuf, uint8_t len);
void NRF24L01_SetupRxMode(void);
void NRF24L01_SetupTxMode(void);
void NRF24L01_TxPacket(uint8_t *txBuf, uint8_t len);
uint8_t NRF24L01_RxPacket(uint8_t *rxBuf);
#endif
```
nrf24l01.c 文件:
```c
#include "nrf24l01.h"
static uint8_t NRF24L01_SPI_SendByte(uint8_t txData)
{
uint8_t retry = NRF24L01_SPI_TIMEOUT;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
if (--retry == 0)
{
return 0;
}
}
SPI_I2S_SendData(SPI1, txData);
retry = NRF24L01_SPI_TIMEOUT;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
if (--retry == 0)
{
return 0;
}
}
return SPI_I2S_ReceiveData(SPI1);
}
void NRF24L01_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = NRF24L01_CE_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(NRF24L01_CE_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = NRF24L01_CSN_PIN;
GPIO_Init(NRF24L01_CSN_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = NRF24L01_IRQ_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(NRF24L01_IRQ_PORT, &GPIO_InitStructure);
}
void NRF24L01_SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void NRF24L01_Init(void)
{
NRF24L01_GPIO_Init();
NRF24L01_SPI_Init();
NRF24L01_CE(0);
NRF24L01_CSN(1);
}
void NRF24L01_CE(uint8_t level)
{
if (level)
{
GPIO_SetBits(NRF24L01_CE_PORT, NRF24L01_CE_PIN);
}
else
{
GPIO_ResetBits(NRF24L01_CE_PORT, NRF24L01_CE_PIN);
}
}
void NRF24L01_CSN(uint8_t level)
{
if (level)
{
GPIO_SetBits(NRF24L01_CSN_PORT, NRF24L01_CSN_PIN);
}
else
{
GPIO_ResetBits(NRF24L01_CSN_PORT, NRF24L01_CSN_PIN);
}
}
uint8_t NRF24L01_ReadWriteByte(uint8_t txData)
{
NRF24L01_CSN(0);
uint8_t rxData = NRF24L01_SPI_SendByte(txData);
NRF24L01_CSN(1);
return rxData;
}
uint8_t NRF24L01_ReadReg(uint8_t regAddr)
{
NRF24L01_CSN(0);
NRF24L01_SPI_SendByte(NRF24L01_R_REGISTER | regAddr);
uint8_t regValue = NRF24L01_SPI_SendByte(NRF24L01_NOP);
NRF24L01_CSN(1);
return regValue;
}
void NRF24L01_WriteReg(uint8_t regAddr, uint8_t txData)
{
NRF24L01_CSN(0);
NRF24L01_SPI_SendByte(NRF24L01_W_REGISTER | regAddr);
NRF24L01_SPI_SendByte(txData);
NRF24L01_CSN(1);
}
void NRF24L01_ReadBuf(uint8_t regAddr, uint8_t *pBuf, uint8_t len)
{
NRF24L01_CSN(0);
NRF24L01_SPI_SendByte(NRF24L01_R_REGISTER | regAddr);
for (uint8_t i = 0; i < len; i++)
{
pBuf[i] = NRF24L01_SPI_SendByte(NRF24L01_NOP);
}
NRF24L01_CSN(1);
}
void NRF24L01_WriteBuf(uint8_t regAddr, uint8_t *pBuf, uint8_t len)
{
NRF24L01_CSN(0);
NRF24L01_SPI_SendByte(NRF24L01_W_REGISTER | regAddr);
for (uint8_t i = 0; i < len; i++)
{
NRF24L01_SPI_SendByte(pBuf[i]);
}
NRF24L01_CSN(1);
}
void NRF24L01_SetupRxMode(void)
{
NRF24L01_CE(0);
NRF24L01_WriteReg(NRF24L01_CONFIG, NRF24L01_CONFIG_EN_CRC | NRF24L01_CONFIG_CRCO | NRF24L01_CONFIG_PWR_UP | NRF24L01_CONFIG_PRIM_RX);
NRF24L01_WriteReg(NRF24L01_EN_AA, 0x01);
NRF24L01_WriteReg(NRF24L01_EN_RXADDR, 0x01);
NRF24L01_WriteReg(NRF24L01_SETUP_RETR, 0x5F);
NRF24L01_WriteReg(NRF24L01_RF_CH, 0x02);
NRF24L01_WriteReg(NRF24L01_RF_SETUP, NRF24L01_RF_SETUP_RF_DR_LOW | NRF24L01_RF_SETUP_CONT_WAVE | NRF24L01_RF_SETUP_PLL_LOCK);
NRF24L01_WriteReg(NRF24L01_RX_PW_P0, 0x20);
NRF24L01_WriteReg(NRF24L01_FEATURE, 0x06);
NRF24L01_CE(1);
}
void NRF24L01_SetupTxMode(void)
{
NRF24L01_CE(0);
NRF24L01_WriteReg(NRF24L01_CONFIG, NRF24L01_CONFIG_EN_CRC | NRF24L01_CONFIG_CRCO | NRF24L01_CONFIG_PWR_UP | !NRF24L01_CONFIG_PRIM_RX);
NRF24L01_WriteReg(NRF24L01_EN_AA, 0x01);
NRF24L01_WriteReg(NRF24L01_EN_RXADDR, 0x01);
NRF24L01_WriteReg(NRF24L01_SETUP_RETR, 0x5F);
NRF24L01_WriteReg(NRF24L01_RF_CH, 0x02);
NRF24L01_WriteReg(NRF24L01_RF_SETUP, NRF24L01_RF_SETUP_RF_DR_LOW | NRF24L01_RF_SETUP_CONT_WAVE | NRF24L01_RF_SETUP_PLL_LOCK);
NRF24L01_WriteReg(NRF24L01_RX_PW_P0, 0x20);
NRF24L01_WriteReg(NRF24L01_FEATURE, 0x06);
NRF24L01_CE(1);
}
void NRF24L01_TxPacket(uint8_t *txBuf, uint8_t len)
{
NRF24L01_CE(0);
NRF24L01_WriteBuf(NRF24L01_W_TX_PAYLOAD, txBuf, len);
NRF24L01_CE(1);
GPIO_ResetBits(NRF24L01_CE_PORT, NRF24L01_CE_PIN);
delay_us(10);
NRF24L01_CE(0);
}
uint8_t NRF24L01_RxPacket(uint8_t *rxBuf)
{
uint8_t status = NRF24L01_ReadReg(NRF24L01_STATUS);
if (status & NRF24L01_STATUS_RX_DR)
{
NRF24L01_ReadBuf(NRF24L01_R_RX_PAYLOAD, rxBuf, 32);
NRF24L01_WriteReg(NRF24L01_STATUS, status | NRF24L01_STATUS_RX_DR);
return 1;
}
return 0;
}
```
这个程序示例是基于STM32F10x的,如果您使用其他单片机,您可能需要根据您的单片机进行适当的修改。
阅读全文