STM32驱动SPI接口FLASH.pdf
文本预览下载声明
SPI 总线
与 IIC 类似,SPI 也是一种通信协议。今天我们就以 WX25X16 芯片为例来介绍 SPI.首先我们
来看下硬件连接。
、
4 CS,DO,DIO,CLK. CS
从原理图可以看到该芯片需要单片机控制的管脚有 个,非别是 其中 是
片选信号,只有将该位拉低才能选中该芯片。DO,DIO 分别是输出和输入。CLK 是时钟信号。
SPI 通信的步骤如下所示:
1) 获取地址 1
2) 获取地址 2
3) 擦除扇区
4) 写入数据
好的,下面我们对每个步骤进行分析
1 在对芯片操作前先要对端口及 SPI 外设进行相应的设置:
()
/*
函数名:SPI_FLASH_Init(void)
功能 :对端口和 SPI 初始化
输入 :无
输出 :无
调用 :被主函数调用
*/
void SPI_FLASH_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD,
ENABLE);
/*! SPI_FLASH_SPI Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/ 将 PA5(CLK)配置成复用推挽输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, GPIO_InitStructure);
/ 将 PA6(DO)设置成浮空输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, GPIO_InitStructure);
/ PA7 DIO /
将 ( )设为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, GPIO_InitStructure);
/将 PA4(CS)设为推挽输出/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, GPIO_InitStructure);
/拉高 CS,失能芯片,该语句是宏定义,就是置高 PA4/
SPI_FLASH_CS_HIGH();
/* SPI 配置/
// W25X16: data input on the DIO pin is sampled on the rising edge of the CLK.
// Data on the DO and DIO pins are clocked out on the falling edge of CLK.
/ 将 SPI 设为全双工模式*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
/ 将 SPI 设为主模式*/
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
/ 将 SPI 通信的数据
显示全部