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