标签:
SPI:有四根线的串行通信协议,允许与其他设备以半、全双工、同步、串行方式通信。void SPI_FLASH_Init(void) { //SPI_CLOCK:PB5, SPI_MOSI: PB6, SPI_MISO: PB7 GPIO_Init(GPIOB, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Fast); GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_Out_PP_High_Fast); //主机模式,配置为输入 该设置很关键 GPIO_Init(GPIOB, GPIO_Pin_7, GPIO_Mode_In_PU_No_IT); /* 初始化SPI */ SPI_Init(SPI1, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_4, SPI_Mode_Master, SPI_CPOL_High, SPI_CPHA_2Edge, SPI_Direction_2Lines_FullDuplex, SPI_NSS_Soft, 0x07); SPI_Cmd(SPI1, ENABLE); /* 使能SPI */ /* 配置CS管脚 */ GPIO_Init(SPI_CS , SPI_Pin_CS, GPIO_Mode_Out_PP_High_Fast); GPIO_WriteBit(SPI_CS, SPI_Pin_CS, SET); /* 拉高不使能外部SPI设备 */ }
uint8_t SPI_FLASH_SendByte(u8 byte) { /* Loop while DR register in not emplty */ while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET); /* Send byte through the SPI1 peripheral */ SPI_SendData(SPI1, byte); /* Wait to receive a byte */ while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET); /* Return the byte read from the SPI bus */ return SPI_ReceiveData(SPI1); }
uint16_t SPI_FLASH_ReadID(void) { u16 Device_ID = 0; /* Select the FLASH: Chip Select low */ SPI_FLASH_CS_LOW(); //拉低片选管脚电平,选中外设 /* Send "RDID " instruction */ SPI_FLASH_SendByte(0x90); //读取设备ID指令 SPI_FLASH_SendByte(0X00); SPI_FLASH_SendByte(0X00); SPI_FLASH_SendByte(0X00); /* Read a byte from the FLASH */ Device_ID = (SPI_FLASH_SendByte(Dummy_Byte)<<8); Device_ID |= SPI_FLASH_SendByte(Dummy_Byte); SPI_FLASH_CS_HIGH();//拉高片选管脚电平, return Device_ID; }在W25Q16的手册中,
STM8L151 使用硬件SPI驱动W25Q16 Flash
标签:
原文地址:http://blog.csdn.net/jnu_fangzebin/article/details/51332076