硬件相同,甚至板子都没掉电,只是换用不同的优化重新仿真,使用IAR高度优化时SPI1的SCK无波形:高度优化时:
中度优化时:
程序:
/****************************************************************************
* 名 称:u8 SPI_SendByte(u8 dat)
* 功 能:通过SPI发送数据并获取返回数据
* 入口参数:dat
需发送的字节数据
* 出口参数:返回
接收到的数据
* 说 明:不采用DMA,仅用于小量数据时
* 调用方法:rdat = SPI_SendByte(0x55);
****************************************************************************/
u8 SPI_SendByte(u8 dat)
{
u8 tmp;
if (SPI1->SR & SPI_I2S_FLAG_OVR)
// 发生溢出错误
tmp = SPI1->DR;
// 读一次DR,清除OVR错误
while(!(SPI1->SR & SPI_I2S_FLAG_TXE));
SPI1->DR = dat;
while(!(SPI1->SR & SPI_I2S_FLAG_RXNE));
return (SPI1->DR);
}
初始化部分:
// 允许SPI1,GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA,ENABLE);
// 允许DMA1时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Configure SPI1 pins: SCK, MISO and MOSI */
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);
/* Deselect the FLASH: Chip Select high */
FLASH_Deselect();
FLASH_WPEnable();
LCD_Deselect();
/* SPI1 configuration */
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_16;
// SSD1305 SCK <= 4MHz 73.728/16=4.608MHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
// 允许收发DMA
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE);
// 允许DMA完成中断
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;
// SPI1 RX
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;
// SPI1 TX
NVIC_Init(&NVIC_InitStructure);
// 允许SPI
SPI_Cmd(SPI1, ENABLE); |