微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 SPI W25X16驱动

STM32 SPI W25X16驱动

时间:11-25 来源:互联网 点击:

printf("LED初始化...");
User_LedConfig();
printf("LED测试......");
User_LedSpark(Led0,2);
printf("完成");

printf("SPI初始化...");
User_SPI_Config();
User_LedSpark(Led0,2);
printf("完成");

User_SPI_Test();

while(1);
}

//==================================================
PUTCHAR_PROTOTYPE
{


USART_SendData(USART1, (uint8_t) ch);


while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}

return ch;
}
//==================================================

3、user_spi_w25x16.c

//program function:SPI1 Init && FLASF Chip W25X16 driver

#include"stm32f10x.h"
#include"user_spi_w25x16.h"
#include

#define WriteEnable0x06
#define WriteDisable0x04
#define ReadStatusRegister0x05
#define WriteStatusRegister0x01
#define ReadData0x03
#define FastRead0x0b
#define FsatReadDualOutput0x3b
#define PageProgram0x02
#define BlukErase0xd8
#define SectorErase0x20
#define ChipErase0xc7
#define PowerDown0xb9
#define WakeUp0xab
#define DeviceID0xab
#define ManufatureID0x90
#define JedecID0x9f

#define JudgeCode0x01 //用于判断通讯是否结束用
#define NoneCode0xa5 //无意义的指令,用于:接收数据时,发送这个质量来产生接收时候的时钟

void User_SPI_Config(void)
{


SPI_InitTypeDef SPI_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;



RCC_APB2PeriphClockCmd(SPI_24G_CS_Clock,ENABLE);
GPIO_InitStructure.GPIO_Pin =SPI_24G_CS_Pin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(SPI_24G_CS_Port,&GPIO_InitStructure);
GPIO_SetBits(SPI_24G_CS_Port,SPI_24G_CS_Pin);


RCC_APB2PeriphClockCmd(SPI_VS1003B_CS_Clock,ENABLE);
GPIO_InitStructure.GPIO_Pin =SPI_VS1003B_CS_Pin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(SPI_VS1003B_CS_Port,&GPIO_InitStructure);
GPIO_SetBits(SPI_VS1003B_CS_Port,SPI_VS1003B_CS_Pin);



RCC_APB2PeriphClockCmd(SPI_W25X16_Clock,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);



RCC_APB2PeriphClockCmd(SPI_W25X16_CS_Clock,ENABLE);
GPIO_InitStructure.GPIO_Pin =SPI_W25X16_CS_Pin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(SPI_W25X16_CS_Port,&GPIO_InitStructure);
SPI_W25X16_CS_DisSelect;


RCC_APB2PeriphClockCmd(SPI_W25X16_SCK_Clock,ENABLE);
GPIO_InitStructure.GPIO_Pin =SPI_W25X16_SCK_Pin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(SPI_W25X16_SCK_Port,&GPIO_InitStructure);


RCC_APB2PeriphClockCmd(SPI_W25X16_MISO_Clock,ENABLE);
GPIO_InitStructure.GPIO_Pin =SPI_W25X16_MISO_Pin;
GPIO_Init(SPI_W25X16_MISO_Port,&GPIO_InitStructure);


RCC_APB2PeriphClockCmd(SPI_W25X16_MOSI_Clock,ENABLE);
GPIO_InitStructure.GPIO_Pin =SPI_W25X16_MOSI_Pin;
GPIO_Init(SPI_W25X16_MOSI_Port,&GPIO_InitStructure);



SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex; //通讯模式:双向全双工模式
SPI_InitStructure.SPI_Mode =SPI_Mode_Master;//主从:主模式
SPI_InitStructure.SPI_DataSize =SPI_DataSize_8b;//数据帧长度:8bits
SPI_InitStructure.SPI_CPOL =SPI_CPOL_High;//空闲时置高
SPI_InitStructure.SPI_CPHA =SPI_CPHA_2Edge;//第二个时钟采样
SPI_InitStructure.SPI_NSS =SPI_NSS_Soft;//NSS控制选择:软件控制
SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_2;//波特率分配系数:2分频
SPI_InitStructure.SPI_FirstBit =SPI_FirstBit_MSB;//数据帧格式:MSB在前
SPI_InitStructure.SPI_CRCPolynomial =7;//CRC效验多项式

SPI_Init(SPI1,&SPI_InitStructure);

SPI_Cmd(SPI1,ENABLE);
}

u8 User_SPI_W25X16_SendByte(u8 SendByteData)
{
u8 ReceiveData;


while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)!=SET);

SPI_I2S_SendData(SPI1,SendByteData);


while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)!=SET);

ReceiveData=SPI_I2S_ReceiveData(SPI1);

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top