stm32f103c8t6用nrf24l01代码
时间: 2023-08-23 21:08:08 浏览: 167
以下是一个基于STM32F103C8T6和nRF24L01的简单代码示例,使用了SPI1控制器和PA4引脚作为CE引脚。
```c
#include "stm32f10x.h"
// 定义SPI控制器和CE引脚
#define SPIx SPI1
#define CE_PIN GPIO_Pin_4
#define CE_PORT GPIOA
void nRF24L01_init(void);
void nRF24L01_CE(int level);
void nRF24L01_write_reg(uint8_t reg, uint8_t data);
uint8_t nRF24L01_read_reg(uint8_t reg);
int main(void) {
// 初始化SPI1控制器和CE引脚
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = CE_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CE_PORT, &GPIO_InitStructure);
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPIx, &SPI_InitStructure);
SPI_Cmd(SPIx, ENABLE);
// 初始化nRF24L01
nRF24L01_init();
while (1) {
// 发送数据
uint8_t data[] = {0x01, 0x02, 0x03};
nRF24L01_CE(1);
nRF24L01_write_reg(0x07, 0b00111000); // 写TX FIFO寄存器
SPIx->DR = data[0];
while (!(SPIx->SR & SPI_I2S_FLAG_TXE));
SPIx->DR = data[1];
while (!(SPIx->SR & SPI_I2S_FLAG_TXE));
SPIx->DR = data[2];
while (!(SPIx->SR & SPI_I2S_FLAG_TXE));
nRF24L01_CE(0);
while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)); // 等待发送完成
// 延时
for (int i = 0; i < 1000000; i++);
}
}
void nRF24L01_init(void) {
nRF24L01_CE(0);
nRF24L01_write_reg(0x20, 0b00000110); // Enable 2-byte CRC and Power Up
nRF24L01_write_reg(0x21, 0b00000000); // Disable auto-acknowledge
nRF24L01_write_reg(0x22, 0b00000000); // Disable retransmission
nRF24L01_write_reg(0x23, 0b00000001); // Set channel to 2.401 GHz
nRF24L01_write_reg(0x24, 0b00000000); // Set data rate to 1 Mbps
nRF24L01_write_reg(0x25, 0b00000111); // Set output power to 0 dBm
nRF24L01_write_reg(0x26, 0b00000000); // Disable dynamic payload length and enable ACK payload
nRF24L01_write_reg(0x27, 0b00000000); // Disable dynamic payload length
nRF24L01_write_reg(0x3C, 0b00000000); // Flush TX FIFO
nRF24L01_write_reg(0x3D, 0b00000000); // Flush RX FIFO
}
void nRF24L01_CE(int level) {
if (level) {
GPIO_SetBits(CE_PORT, CE_PIN);
} else {
GPIO_ResetBits(CE_PORT, CE_PIN);
}
}
void nRF24L01_write_reg(uint8_t reg, uint8_t data) {
GPIO_ResetBits(GPIOA, GPIO_Pin_1); // 使能nRF24L01
SPIx->DR = 0x20 | reg; // 写寄存器指令
while (!(SPIx->SR & SPI_I2S_FLAG_TXE));
SPIx->DR = data;
while (!(SPIx->SR & SPI_I2S_FLAG_TXE));
GPIO_SetBits(GPIOA, GPIO_Pin_1); // 禁止nRF24L01
}
uint8_t nRF24L01_read_reg(uint8_t reg) {
uint8_t data;
GPIO_ResetBits(GPIOA, GPIO_Pin_1); // 使能nRF24L01
SPIx->DR = reg; // 读寄存器指令
while (!(SPIx->SR & SPI_I2S_FLAG_TXE));
SPIx->DR = 0xFF;
while (!(SPIx->SR & SPI_I2S_FLAG_RXNE));
data = SPIx->DR;
GPIO_SetBits(GPIOA, GPIO_Pin_1); // 禁止nRF24L01
return data;
}
```
需要注意的是,此示例代码中的nRF24L01_init()函数仅设置了基本的配置,您可能需要根据自己的需求进行更改。另外,由于nRF24L01是一个SPI设备,因此在使用它之前,需要先初始化SPI控制器,并正确连接它的MISO、MOSI、SCK和CSN引脚。
阅读全文