void SPI1_Flash_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE); //使能spi1时钟
GPIO_InitStructure.GPIO_Pin = SPISO|SPICK|SPISI;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPICS;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;// 开漏有上拉电阻 GPIO_Mode_Out_PP; //I/O口的方向 推完输?
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_Init(GPIOA,&GPIO_InitStructure);
NotSelect_Flash();
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//spi 设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置为主模式
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // 时钟悬空为低
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //数据捕获第一个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件控制NSS信号
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //比特率预分频为4
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据输入从高位开始
SPI_InitStructure.SPI_CRCPolynomial =7; //定义邋錍RC多项式
SPI_Init(SPI1,&SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE); //使能SPI1
}
u8 SPI_Send1Byte(u8 data)
{
//****如果发送寄存器数据没有发送完,循环等待
// while (SPI_I2S_GetFlagStatus(SPI1, SPI_I1S_FLAG_TXE) == RESET);
//*****往发送寄存器写入要发送的数据
// SPI_I1S_SendData(SPI1, data);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_IT_TXE) == RESET);
SPI_I2S_SendData(SPI1,data);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_IT_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
死机在红色字体处,请问怎么解决? 高手指点 |