NRF24L01STM32的驱动程序
时间: 2023-09-15 20:17:25 浏览: 146
STM32_NRF24L01-drive-(RF-module).rar_STM32 NRF24L01_nrf24l01 驱动_
NRF24L01是一款2.4GHz无线收发模块,可用于Arduino、STM32等单片机上,实现无线通信功能。下面是一个基于STM32的NRF24L01驱动程序,供参考:
```c
#include "stm32f10x.h"
#include "nrf24l01.h"
void delay_us(uint32_t nus)
{
uint32_t i,j;
for(i=0;i<nus;i++)
for(j=0;j<8;j++);
}
void delay_ms(uint32_t nms)
{
uint32_t i,j;
for(i=0;i<nms;i++)
for(j=0;j<8000;j++);
}
void NRF24L01_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(NRF24L01_CE_GPIO_CLK | NRF24L01_CSN_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = NRF24L01_CE_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(NRF24L01_CE_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = NRF24L01_CSN_PIN;
GPIO_Init(NRF24L01_CSN_GPIO_PORT, &GPIO_InitStructure);
}
void NRF24L01_SPI_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(NRF24L01_SPI_CLK, ENABLE);
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_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(NRF24L01_SPI, &SPI_InitStructure);
SPI_Cmd(NRF24L01_SPI, ENABLE);
}
uint8_t NRF24L01_SPI_RW(uint8_t byte)
{
while (SPI_I2S_GetFlagStatus(NRF24L01_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(NRF24L01_SPI, byte);
while (SPI_I2S_GetFlagStatus(NRF24L01_SPI, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(NRF24L01_SPI);
}
void NRF24L01_CE_Low(void)
{
GPIO_ResetBits(NRF24L01_CE_GPIO_PORT, NRF24L01_CE_PIN);
}
void NRF24L01_CE_High(void)
{
GPIO_SetBits(NRF24L01_CE_GPIO_PORT, NRF24L01_CE_PIN);
}
void NRF24L01_CSN_Low(void)
{
GPIO_ResetBits(NRF24L01_CSN_GPIO_PORT, NRF24L01_CSN_PIN);
}
void NRF24L01_CSN_High(void)
{
GPIO_SetBits(NRF24L01_CSN_GPIO_PORT, NRF24L01_CSN_PIN);
}
void NRF24L01_Init(void)
{
NRF24L01_GPIO_Init();
NRF24L01_SPI_Init();
NRF24L01_CE_Low();
NRF24L01_CSN_High();
delay_ms(5);
NRF24L01_Write_Reg(NRF24L01_REG_CONFIG, 0x08); //16位CRC,上电
NRF24L01_Write_Reg(NRF24L01_REG_EN_AA, 0x00); //禁用自动应答
NRF24L01_Write_Reg(NRF24L01_REG_EN_RXADDR, 0x01); //允许接收通道0
NRF24L01_Write_Reg(NRF24L01_REG_SETUP_AW, 0x03); //地址宽度为5字节
NRF24L01_Write_Reg(NRF24L01_REG_SETUP_RETR, 0x00);//自动重传延时250us,不重发
NRF24L01_Write_Reg(NRF24L01_REG_RF_CH, 0x01); //射频通道为2.401GHz
NRF24L01_Write_Reg(NRF24L01_REG_RF_SETUP, 0x06); //250kbps,0dBm
NRF24L01_Write_Reg(NRF24L01_REG_RX_PW_P0, 0x01); //接收通道0的有效数据宽度为1字节
NRF24L01_Write_Reg(NRF24L01_REG_DYNPD, 0x00); //禁用动态数据长度
NRF24L01_Write_Reg(NRF24L01_REG_FEATURE, 0x00); //禁用特性
NRF24L01_Flush_TX_FIFO();
NRF24L01_Flush_RX_FIFO();
NRF24L01_CE_High();
delay_ms(5);
}
void NRF24L01_Write_Reg(uint8_t reg, uint8_t value)
{
NRF24L01_CSN_Low();
NRF24L01_SPI_RW(NRF24L01_CMD_W_REGISTER | reg);
NRF24L01_SPI_RW(value);
NRF24L01_CSN_High();
}
uint8_t NRF24L01_Read_Reg(uint8_t reg)
{
uint8_t value;
NRF24L01_CSN_Low();
NRF24L01_SPI_RW(NRF24L01_CMD_R_REGISTER | reg);
value = NRF24L01_SPI_RW(NRF24L01_CMD_NOP);
NRF24L01_CSN_High();
return value;
}
void NRF24L01_Write_Buf(uint8_t reg, uint8_t *pBuf, uint8_t len)
{
uint8_t i;
NRF24L01_CSN_Low();
NRF24L01_SPI_RW(NRF24L01_CMD_W_REGISTER | reg);
for (i = 0; i < len; i++) {
NRF24L01_SPI_RW(pBuf[i]);
}
NRF24L01_CSN_High();
}
void NRF24L01_Read_Buf(uint8_t reg, uint8_t *pBuf, uint8_t len)
{
uint8_t i;
NRF24L01_CSN_Low();
NRF24L01_SPI_RW(NRF24L01_CMD_R_REGISTER | reg);
for (i = 0; i < len; i++) {
pBuf[i] = NRF24L01_SPI_RW(NRF24L01_CMD_NOP);
}
NRF24L01_CSN_High();
}
void NRF24L01_Flush_TX_FIFO(void)
{
NRF24L01_CSN_Low();
NRF24L01_SPI_RW(NRF24L01_CMD_FLUSH_TX);
NRF24L01_CSN_High();
}
void NRF24L01_Flush_RX_FIFO(void)
{
NRF24L01_CSN_Low();
NRF24L01_SPI_RW(NRF24L01_CMD_FLUSH_RX);
NRF24L01_CSN_High();
}
void NRF24L01_TX_Mode(void)
{
uint8_t value;
NRF24L01_CE_Low();
value = NRF24L01_Read_Reg(NRF24L01_REG_CONFIG);
value &= ~NRF24L01_MASK_PRIM_RX;
value |= NRF24L01_BIT_PWR_UP;
NRF24L01_Write_Reg(NRF24L01_REG_CONFIG, value);
NRF24L01_CE_High();
delay_us(130);
}
void NRF24L01_RX_Mode(void)
{
uint8_t value;
NRF24L01_CE_Low();
value = NRF24L01_Read_Reg(NRF24L01_REG_CONFIG);
value |= NRF24L01_MASK_PRIM_RX;
value |= NRF24L01_BIT_PWR_UP;
NRF24L01_Write_Reg(NRF24L01_REG_CONFIG, value);
NRF24L01_CE_High();
delay_us(130);
}
void NRF24L01_Write_TX_Payload(uint8_t *pBuf, uint8_t len)
{
NRF24L01_CSN_Low();
NRF24L01_SPI_RW(NRF24L01_CMD_W_TX_PAYLOAD);
while (len--) {
NRF24L01_SPI_RW(*pBuf++);
}
NRF24L01_CSN_High();
}
void NRF24L01_Read_RX_Payload(uint8_t *pBuf, uint8_t len)
{
NRF24L01_CSN_Low();
NRF24L01_SPI_RW(NRF24L01_CMD_R_RX_PAYLOAD);
while (len--) {
*pBuf++ = NRF24L01_SPI_RW(NRF24L01_CMD_NOP);
}
NRF24L01_CSN_High();
}
uint8_t NRF24L01_Get_Status(void)
{
uint8_t status;
NRF24L01_CSN_Low();
status = NRF24L01_SPI_RW(NRF24L01_CMD_NOP);
NRF24L01_CSN_High();
return status;
}
uint8_t NRF24L01_Get_FIFO_Status(void)
{
uint8_t value;
value = NRF24L01_Read_Reg(NRF24L01_REG_FIFO_STATUS);
value &= NRF24L01_MASK_FIFO_STATUS;
return value;
}
```
这是一个基本的驱动程序,包括GPIO初始化、SPI初始化、各种读写操作函数以及模式转换函数等。需要根据具体的硬件环境进行修改和适配。
阅读全文