小弟刚接触STM32,现用stm32的spi1与vs1053通讯
初始化如下:
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//和spi相关的I/O口
/* Configure PA4 pin: CS pin MP3芯片选择引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PA5 6 7 pin: SCK MISO MOSI pin */
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);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
| RCC_APB2Periph_AFIO
| RCC_APB2Periph_SPI1
,ENABLE);
/* SPI 初始化 */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双工模式
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //SPI主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8bit数据
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //CLK空闲时为低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //上升沿采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //片选用软件控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //SPI频率
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7; //crc7,stm32spi带硬件ecc
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
收发一个字节的函数如下:
i = data;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
if(++j>100)
{
return(55);
}
}
SPI_I2S_SendData(SPI1, i);
j = 0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
if(++j>100)
{
return(55);
}
}
return SPI_I2S_ReceiveData(SPI1);
对spi1写一个字节接收到的竟然也是这个字节,比如对spi写55,收到的也是55,这算不算正确啊?还有,这要是正确的话,我怎么对vs1053模块写命令却没有反应啊????????
望牛人指点一二,小弟万分感激! |