用的是STM32 107VCT的最小系统,用SPI1驱动NRF24l01的时候查看返回的STATUS寄存器是0x0E,在初始状态下这个是正确的,但是用SPI2进行操作,就遇到问题了,不管怎么操作,读取的返回值永远是0x1E,以为是NRF怀了,换了一个能用的还是有问题,然后用SPI1 SPI2通信的例程测试了下SPI2能收到正确的数据,说明还是我的配置有问题,但是实在找不出哪里来了。。。。。。。
SPI2初始代码:
void nrf_spi2_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* SPI1 Init, before continuing, start the spio clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE);
/* initialize GPIO A5.A6.A7 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*
GPIO B5 --- CSN(SPI NSS chip select)
GPIO B6 --- CE (chip mode set)
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*
idle
*/
NRF_LOW(NRFB_CE_PORT, NRFB_CE_PIN);
NRF_HIGH(NRFB_CSE_PORT, NRFB_CSE_PIN);
/*
GPIO B1 for nRF2401 IRQ
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_Cmd(SPI2, DISABLE); //必须先禁用,才能改变MODE
/* configure spi1 */
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_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/* enable spi */
SPI_Cmd(SPI2, ENABLE);
}
之后调用读写函数进行读取:
unsigned char spi2_readwritebyte(unsigned char tx)
{
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, tx);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI2);
}
返回值永远是0x1E,要是断开与NRF的物理连接返回0xFF
请大侠帮看下啊,万分感谢!
另,实在不行想用IO模拟SPI,但是不知道怎么下手,有高手有例程吗?
多谢啊!!!!!! |