板子上用了SPI,分别连接的是传感器和FLASH,但两个spi都是在while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);上了,我的配置如下,
void Spi1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB , ENABLE);
/*!< SD_SPI Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/*!< Configure SD_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
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;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
/*!< Configure SD_SPI_CS_PIN pin: SD Card CS pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect PXx to SD_SPI_SCK */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_5);
/* Connect PXx to SD_SPI_MISO */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_5);
/* Connect PXx to SD_SPI_MOSI */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_5);
/*!< SD_SPI Config */
SPI_I2S_DeInit(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_128;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); /*!< SD_SPI enable */
}
uint8_t SD_ReadByte(tPruint8 TxData)
{
int i=0;
uint8_t Data = 0;
SPI_CS_Enable();
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//等待发送区空
SPI_SendData8(SPI1, TxData); //通过外设 SPIx 发送一个 byte 数据
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
Data = SPI_ReceiveData8(SPI1);
return Data;
}
目前的现象是,用示波器可以抓到发出的信号和时钟信号,都正确,片选信号正常,但发出后就是在了等待SPI_I2S_FLAG_RXNE信号,spi1 sr寄存器为02,说明没有接收到接收完成信号。如果我把while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);信号去掉,可以接收到0xff或0x00,这和MISO电平相同,但为非正常电平。我怀疑MISO引脚可以接收到电平信号,传送到SPI的接收寄存器后接收寄存器没有置位。原子哥,帮忙看一下我的故障出在哪
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
这样设计CPU比较奇怪,应当默认“只要收一个字节就置标志”。
---------------------------------
是啊,可配置为满4分之一或者二分之一置位,但如果不配置接受标志位不会出现
一周热门 更多>