最近在做利用STM32f103rct6的SPI3接收NRF24L01的问题,SPI的大体配置是遵照SPI1和SP2的,但是SPI1和SP2在写NRf24L01的地址时都是正常的,但是SPI3写地址就不行了,配置地址为5位失效。单个读写寄存器时有效的,通道设置都是可以的。但是唯独地址的修改不行。以下是配置代码。请搞过SPI3通信的帮忙看一下,找找问题。
static void Initial_SPI(SPI_TypeDef* SPIx) //??????IOB????
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
if(SPIx==SPI1)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
else if(SPIx==SPI2)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
else if(SPIx==SPI3)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
//GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPIx, &SPI_InitStruct);
SPI_Cmd(SPIx, ENABLE);
}
static void SPI_Send_byte(SPI_TypeDef* SPIx,u8 data)
{
while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPIx,data);
while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE)==RESET);
SPI_I2S_ReceiveData(SPIx);
}
static u8 SPI_Receive_byte(SPI_TypeDef* SPIx,u8 data)
{
while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPIx,data);
while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE)==RESET);
return SPI_I2S_ReceiveData(SPIx);
}
static void delay1us(u8 t)
{
while(--t);
}
/****************????bytes×?????????*******************/
u8 SPI_Write_Buf_2(u8 reg,u8 *pBuf,u8 bytes)
{
u8 status,byte_ctr;
CSN(0);
status=SPI_Receive_byte(SPI3,reg);
delay1us(1);
for(byte_ctr=0;byte_ctr
{
SPI_Send_byte(SPI3,*pBuf++);
}
CSN(1);
return(status);
}
void RX_Mode_2(void)
{
CE(0);
SPI_Write_Buf_2(WRITE_REG_NRF24L01 + RX_ADDR_P0, TX_ADDRESS,TX_PLOAD_WIDTH);
//SPI_Write_Buf_2_1(WRITE_REG_NRF24L01 + RX_ADDR_P0, TX_ADDRESS,1) ;
SPI_RW_Reg_2(WRITE_REG_NRF24L01 + RX_PW_P0, TX_PLOAD_WIDTH);
// SPI_RW_Reg_2(WRITE_REG_NRF24L01 + RX_ADDR_P0, 0x20);
SPI_RW_Reg_2(WRITE_REG_NRF24L01 + EN_AA, 0x00); // ?????????¨??0×???????
SPI_RW_Reg_2(WRITE_REG_NRF24L01 + EN_RXADDR, 0x3f); // ?????????¨??0
SPI_RW_Reg_2(WRITE_REG_NRF24L01 + RF_CH, 40); // ?????????¨??0x40
SPI_RW_Reg_2(WRITE_REG_NRF24L01 + RF_SETUP, 0x0f);
SPI_RW_Reg_2(WRITE_REG_NRF24L01 + CONFIG, 0x0f); // CRC??????16??CRC???é????????????????
CE(1);
}
0