以下为我的设置部分代码
//SPI的发送接受函数
uint8_t SPI_RW_Byte(uint8_t TxData)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) ;
SPI_I2S_SendData(SPI1, TxData);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) ;
return SPI_I2S_ReceiveData(SPI1);
}
//引脚及spi设置函数
static void SPI_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Peripheral Clock Enable -------------------------------------------------*/
/* Enable the SPI clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* Enable GPIO clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* SPI GPIO Configuration --------------------------------------------------*/
/* GPIO Deinitialisation */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
/* SPI SCK pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource3;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI MISO pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource4;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI MOSI pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource5;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect SPI pins to AF5 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
/* SPI NSS pin configuration */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource0;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI CE pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI configuration -------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
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_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI1, &SPI_InitStructure);
}
//NRF24L01模块初始化
void NRF24L01_Init()
{
/* SPI configuration */
/* SPI master */
SPI_Config();
SPI_Cmd(SPI1, ENABLE);
NRF_CE=0;
NRF_NSS=1;
}
//write nrf24l01 reg
uint8_t NRF24L01_Write_Reg(uint8_t reg,uint8_t value)
{
unsigned char status;
NRF_NSS = 0; // CSN low, init SPI transaction
status = SPI_RW_Byte(reg); // select register
SPI_RW_Byte(value); // ..and write value into it..
NRF_NSS = 1; // CSN high again
return status;// return nRF24L01 status byte
}
//read reg
uint8_t NRF24L01_Read_Reg(uint8_t reg)
{
unsigned char reg_val;
NRF_NSS = 0; // CSN low, initialize SPI communication...
SPI_RW_Byte(reg); // Select register to read from..
reg_val = SPI_RW_Byte(NRF_NOP); // ..then read registervalue
NRF_NSS = 1; // CSN high, terminate SPI communication
return reg_val; // return register value
}
void RX_Mode()
{
NRF_CE=0;
// SPI_WR_Reg(NRF_WRITE_REG + SETUP_AW, 0x03);
NRF24L01_Write_Buf(NRF_WRITE_REG + RX_ADDR_P0, myTX_ADDRESS0, NRF_ADR_WIDTH);
NRF24L01_Write_Reg(NRF_WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
NRF24L01_Write_Reg(NRF_WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
NRF24L01_Write_Reg(NRF_WRITE_REG + RF_CH, 40); // Select RF channel 40
NRF24L01_Write_Reg(NRF_WRITE_REG + RX_PW_P0, NRF_RX_PLOAD_WIDTH);
NRF24L01_Write_Reg(NRF_WRITE_REG + RF_SETUP, 0x07);
NRF24L01_Write_Reg(NRF_WRITE_REG + CONFIG, 0x0f);
NRF_CE=1;
}
uint8_t NRF24L01_Check(void)
{
uint8_t buf[5]={0xa5,0xa5,0xa5,0xa5,0xa5};
uint8_t i;
NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);
NRF24L01_Read_Buf(TX_ADDR,buf,5);
for(i=0;i<5;i++)
{
if(buf[i]!=0xa5)
break;
}
if(i!=5)
return 1;
return 0;
}
读取数据的代码
i=NRF24L01_Read_Reg(EN_AA);
将NRF设置为接受模式后,读取EN_AA寄存器,正确返回值为1,但实际返回值要么总是变化,要么无论读哪个寄存器返回值都是8。感觉自己没有设置上的错误,但是一直调试不对,求帮助!