我用VET6的SPI1操作一个加速度芯片,读字节总是0x00,但用同样的代码操作SPI2,FLASH IC确可以。不知道是什么原因。请问大家是怎么用SPI1的啊?
SPI的设置我反复修改,结果还是不行。但是在优易特的STM32开发板上操作FLASH确可以。
具体代码如下:
void SPI_LIS33_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA, ENABLE);
SPI_I2S_DeInit(SPI1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_12);
SPI_LIS33_CS_HIGH();
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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void SPI_LIS33_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
SPI_LIS33_CS_LOW();
SPI_LIS33_SendByte((WriteAddr & 0x7f) | 0x40);
while(NumByteToWrite--)
{
SPI_LIS33_SendByte(*pBuffer);
pBuffer++;
}
SPI_LIS33_CS_HIGH();
}
void SPI_LIS33_BufferRead(u8* pBuffer, u8 ReadAddr, u8 NumByteToRead)
{
SPI_LIS33_CS_LOW();
SPI_LIS33_SendByte(ReadAddr | 0xC0); //Read & Address increase
while (NumByteToRead--) /* while there is data to be read */
{
*pBuffer = SPI_LIS33_SendByte(Dummy_Byte);
pBuffer++;
}
SPI_LIS33_CS_HIGH();
}
void SPI_LIS33_StartReadSequence(u8 ReadAddr)
{
SPI_LIS33_SendByte(ReadAddr | 0xc0); //Read & Address increase
}
u8 SPI_LIS33_ReadByte(void)
{
return (SPI_LIS33_SendByte(Dummy_Byte));
}
u8 SPI_LIS33_SendByte(u8 byte)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, byte);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
} |