我使用STM32F103进行一项SPI通信实验,用示波器发现片选信号在通信中途会被拉高,而且总是剩余8byte数据时被拉高,如附件图所示(其中绿色是片选信号,粉色是时钟信号):
附件图中我发送3个字节,只有两个字节的片选信号正常,第三字节发送前片选信号就被拉高。我尝试发送过多个字节,也总是在最后一个字节前(8byte)片选信号被拉高。希望知道解决方法的大神帮助一下。
下面是我的代码:
int main(void)
{
SPI_DAC_Init();
DAC_SetVValue(2.8);
while(1);
}
void SPI_DAC_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
SPI_DAC_APBxclock_FUN ( SPI_DAC_CLK, ENABLE );
/* 使能SPI引脚相关的时钟 */
SPI_DAC_CS_APBxClock_FUN ( SPI_DAC_CS_CLK|SPI_DAC_SCK_CLK|SPI_DAC_MOSI_PIN, ENABLE );
/* 配置SPI的 CS引脚,普通IO */
GPIO_InitStructure.GPIO_Pin = SPI_DAC_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SPI_DAC_CS_PORT, &GPIO_InitStructure);
/* 配置SPI的 SCK引脚*/
GPIO_InitStructure.GPIO_Pin = SPI_DAC_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI_DAC_SCK_PORT, &GPIO_InitStructure);
/* 配置SPI的 MOSI引脚*/
GPIO_InitStructure.GPIO_Pin = SPI_DAC_MOSI_PIN;
GPIO_Init(SPI_DAC_MOSI_PORT, &GPIO_InitStructure);
/* 停止信号 : CS引脚高电平*/
SPI_DAC_CS_HIGH();
/* SPI 模式配置 */
// FLASH芯片 支持SPI模式0及模式3,据此设置CPOL CPHA
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
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_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(SPI_DAC , &SPI_InitStructure);
/* 使能 SPI */
SPI_Cmd(SPI_DAC , ENABLE);
}
void SPI_DAC_Write(uint8_t *Data)
{
uint8_t i=0;
SPI_DAC_CS_LOW();
/* 发送当前要写入的字节数据 */
for(;i<3;i++)
{
SPI_DAC_SendByte(*Data);
Data++;
}
SPI_DAC_CS_HIGH();
}
void SPI_DAC_SendByte(u8 byte)
{
while(SPI_I2S_GetFlagStatus(SPI_DAC ,SPI_I2S_FLAG_BSY) == SET){}
while (SPI_I2S_GetFlagStatus(SPI_DAC , SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI_DAC , byte);
}
void DAC_SetVValue(float VValue)
{
uint8_t Value[]={0,0,0};
VValueSet16Bits = VValue/5.0*65536;
Value[0] = DAC_NORMAL_OPERATION;
Value[1] = VValueSet16Bits / 0x100;
Value[2] = VValueSet16Bits % 0x100;
SPI_DAC_Write(Value);
}
|