软件SPI读写W25Q64

主函数

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "stdint.h"
#include "OLED.h"
#include "W25Q64.h"


uint8_t MID;
uint16_t DID;

uint8_t ArrayWrite[]={0x01,0x02,0x03,0x04};
uint8_t ArrayRead[4];

int main(void)
{
	OLED_Init();
	W25Q64_Init();
	
	OLED_ShowString(1,1,"MID:  DID:");
	OLED_ShowString(2,1,"W:");
	OLED_ShowString(3,1,"R:");
	
	W25Q64_ReadID(&MID,&DID);
	OLED_ShowHexNum(1,5,MID,2);
	OLED_ShowHexNum(1,12,DID,4);
	
	W25Q64_SectorErase(0x000000);//写入数据前必须擦除数据
	W25Q64_PageProgram(0x000000,ArrayWrite,4);
	W25Q64_ReadData(0x000000,ArrayWrite,4);
	
	
	OLED_ShowHexNum(2,3,ArrayWrite[0],2);
	OLED_ShowHexNum(2,6,ArrayWrite[1],2);
	OLED_ShowHexNum(2,9,ArrayWrite[2],2);
	OLED_ShowHexNum(2,12,ArrayWrite[3],2);	
	
	OLED_ShowHexNum(3,3,ArrayWrite[0],2);
	OLED_ShowHexNum(3,6,ArrayWrite[1],2);
	OLED_ShowHexNum(3,9,ArrayWrite[2],2);
	OLED_ShowHexNum(3,12,ArrayWrite[3],2);	
	
	
	
	
	
	while(1)
	{
		
	}
}

MySPI.c

#include "stm32f10x.h"                  // Device header
#include "stdint.h"

void MySPI_W_SS(uint8_t BitValue)//从机选择,也可以叫MySPI_W_CS,表示写SS或CS的引脚
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)BitValue);
}

void MySPI_W_SCK(uint8_t BitValue)//表示写SCK或CLK的引脚
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_5,(BitAction)BitValue);
}

void MySPI_W_MOSI(uint8_t BitValue)//表示写MOSI或DI的引脚
{
	GPIO_WriteBit(GPIOA,GPIO_Pin_7,(BitAction)BitValue);
}

uint8_t MySPI_R_MISO(void)//MISO输入引脚
{
	return GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6);
}

void MySPI_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	//置初始化之后引脚的默认电平
	MySPI_W_SS(1);//默认不选中从机
	MySPI_W_SCK(0);//使用SPI模式0,所以默认是低电平
	//剩下两个不用管
	
}


void MySPI_Start(void)
{
	MySPI_W_SS(0);
}

void MySPI_Stop(void)
{
	MySPI_W_SS(1);
}

//方法一
uint8_t MySPI_SwapByte(uint8_t ByteSend)//模式0
{
	uint8_t i,ByteReceive = 0x00;
	
	for(i=0;i<8;i++)
	{
		MySPI_W_MOSI(ByteSend & (0x80>>i));//写MOSI
		MySPI_W_SCK(1);//产生上升沿,上升沿时,从机会自动把MOSI的数据读走 //主机的任务就是把刚才放到MISO的数据读进来
		if(MySPI_R_MISO() == 1){ByteReceive |= (0x80>>i);}
		MySPI_W_SCK(0);//产生下降沿
	}
	
	return ByteReceive;
}

//方法二//之后ByteSend不能再使用了
//uint8_t MySPI_SwapByte(uint8_t ByteSend)
//{
//	uint8_t i;
//	
//	for(i=0;i<8;i++)
//	{
//		MySPI_W_MOSI(ByteSend & 0x80);//写MOSI
//		ByteSend<<=1;
//		MySPI_W_SCK(1);//产生上升沿,上升沿时,从机会自动把MOSI的数据读走 //主机的任务就是把刚才放到MISO的数据读进来
//		if(MySPI_R_MISO() == 1){ByteSend |= 0x01;}//把收到的数据放在ByteSend的最低位
//		MySPI_W_SCK(0);//产生下降沿
//	}
//	
//	return ByteSend;
//}

//uint8_t MySPI_SwapByte(uint8_t ByteSend)//模式1
//{
//	uint8_t i,ByteReceive = 0x00;
//	
//	for(i=0;i<8;i++)
//	{
//		MySPI_W_SCK(1);//产生上升沿,上升沿时,从机会自动把MOSI的数据读走 //主机的任务就是把刚才放到MISO的数据读进来
//		MySPI_W_MOSI(ByteSend & (0x80>>i));//写MOSI
//		MySPI_W_SCK(0);//产生下降沿
//		if(MySPI_R_MISO() == 1){ByteReceive |= (0x80>>i);}
//	}
//	
//	return ByteReceive;
//}

//模式3,在模式1的基础上翻转SCK所有极性,  //模式2,在模式0的基础上翻转SCK所有极性



MySPI.h

#ifndef __MYSPI_H
#define __MYSPI_H
#include "stdint.h"
void MySPI_W_SS(uint8_t BitValue);
void MySPI_W_SCK(uint8_t BitValue);
void MySPI_W_MOSI(uint8_t BitValue);
uint8_t MySPI_R_MISO(void);
void MySPI_Init(void);
void MySPI_Start(void);
void MySPI_Stop(void);
uint8_t MySPI_SwapByte(uint8_t ByteSend);
#endif


W25Q64_Ins.h

#ifndef __W25Q64_INS_H
#define __W25Q64_INS_H

#define W25Q64_WRITE_ENABLE							0x06
#define W25Q64_WRITE_DISABLE						0x04
#define W25Q64_READ_STATUS_REGISTER_1				0x05
#define W25Q64_READ_STATUS_REGISTER_2				0x35
#define W25Q64_WRITE_STATUS_REGISTER				0x01
#define W25Q64_PAGE_PROGRAM							0x02
#define W25Q64_QUAD_PAGE_PROGRAM					0x32
#define W25Q64_BLOCK_ERASE_64KB						0xD8
#define W25Q64_BLOCK_ERASE_32KB						0x52
#define W25Q64_SECTOR_ERASE_4KB						0x20
#define W25Q64_CHIP_ERASE							0xC7
#define W25Q64_ERASE_SUSPEND						0x75
#define W25Q64_ERASE_RESUME							0x7A
#define W25Q64_POWER_DOWN							0xB9
#define W25Q64_HIGH_PERFORMANCE_MODE				0xA3
#define W25Q64_CONTINUOUS_READ_MODE_RESET			0xFF
#define W25Q64_RELEASE_POWER_DOWN_HPM_DEVICE_ID		0xAB
#define W25Q64_MANUFACTURER_DEVICE_ID				0x90
#define W25Q64_READ_UNIQUE_ID						0x4B
#define W25Q64_JEDEC_ID								0x9F
#define W25Q64_READ_DATA							0x03
#define W25Q64_FAST_READ							0x0B
#define W25Q64_FAST_READ_DUAL_OUTPUT				0x3B
#define W25Q64_FAST_READ_DUAL_IO					0xBB
#define W25Q64_FAST_READ_QUAD_OUTPUT				0x6B
#define W25Q64_FAST_READ_QUAD_IO					0xEB
#define W25Q64_OCTAL_WORD_READ_QUAD_IO				0xE3

#define W25Q64_DUMMY_BYTE							0xFF

#endif

W25Q64.c

#include "stm32f10x.h"                  // Device header
#include "MySPI.h"
#include "stdint.h"
#include "W25Q64_Ins.h"

void W25Q64_Init(void)
{
	MySPI_Init();
}

void W25Q64_ReadID(uint8_t *MID,uint16_t *DID)//MID 制造商ID (manufacture ID)DID 16位设备ID(device ID)高八位是存储器类型,低八位是容量
{
	MySPI_Start();
	MySPI_SwapByte(W25Q64_JEDEC_ID);//0x9F代表读ID的指令,下一次交换就会把ID号返回给主机了,所以再来一次交换,并且返回的东西不用在意
	*MID = MySPI_SwapByte(W25Q64_DUMMY_BYTE);//随便给从机一个东西,因为目的是接收不是发送,返回MID 制造商ID
	*DID = MySPI_SwapByte(W25Q64_DUMMY_BYTE);//返回设备ID(device ID)高八位存储器类型
	*DID<<=8;
	*DID |= MySPI_SwapByte(W25Q64_DUMMY_BYTE);//返回设备ID低八位容量
	MySPI_Stop();
}
//W25Q64_DUMMY_BYTE  表示接收时交换过去的无用数据OxFF

void W25Q64_WriteEnable(void)
{
	MySPI_Start();
	MySPI_SwapByte(W25Q64_WRITE_ENABLE);
	MySPI_Stop();
}

void W25Q64_WaitBusy(void)//等待Busy为0,读状态寄存器1
{
	MySPI_Start();
	MySPI_SwapByte(W25Q64_READ_STATUS_REGISTER_1);
	uint32_t Timeout=1000;
	while((MySPI_SwapByte(W25Q64_DUMMY_BYTE)&0x01)==0x01)//接收数据,取出最低位,如果等于0x01,就是BUSY为1
	{
		Timeout--;
		if(Timeout==0)
		{
			break;
		}
	}
	MySPI_Stop();
}


void W25Q64_PageProgram(uint32_t Address,uint8_t *DataArray,uint16_t Count)//页编程,Count表示一次写多少个
{
	W25Q64_WriteEnable();
	uint16_t i;
	MySPI_Start();
	MySPI_SwapByte(W25Q64_PAGE_PROGRAM);
	MySPI_SwapByte(Address>>16);//Address有24位.右移16位去最高8位,如0x123456>>16 为0x12
	MySPI_SwapByte(Address>>8);//0x123456>>8 为0x1234,但是交换字节函数只能接收8位数据,故把高位舍弃。实际发送0x34
	MySPI_SwapByte(Address);
	for(i=0;i<Count;i++)
	{
		MySPI_SwapByte(DataArray[i]);
	}

	MySPI_Stop();
	
	W25Q64_WaitBusy();
}

void W25Q64_SectorErase(uint32_t Address)//扇区擦除,调用这个函数,指定地址所在扇区就会被擦除
{
	W25Q64_WriteEnable();
	MySPI_Start();
	MySPI_SwapByte(W25Q64_SECTOR_ERASE_4KB);
	MySPI_SwapByte(Address>>16);//Address有24位.右移16位去最高8位,如0x123456>>16 为0x12
	MySPI_SwapByte(Address>>8);//0x123456>>8 为0x1234,但是交换字节函数只能接收8位数据,故把高位舍弃。实际发送0x34
	MySPI_SwapByte(Address);
	MySPI_Stop();
	
	W25Q64_WaitBusy();
}

void W25Q64_ReadData(uint32_t Address,uint8_t *DataArray,uint32_t Count)
{
	uint16_t i;
	MySPI_Start();
	MySPI_SwapByte(W25Q64_READ_DATA);
	MySPI_SwapByte(Address>>16);//Address有24位.右移16位去最高8位,如0x123456>>16 为0x12
	MySPI_SwapByte(Address>>8);//0x123456>>8 为0x1234,但是交换字节函数只能接收8位数据,故把高位舍弃。实际发送0x34
	MySPI_SwapByte(Address);
	for(i=0;i<Count;i++)
	{
		DataArray[i]=MySPI_SwapByte(W25Q64_DUMMY_BYTE);
	}
	MySPI_Stop();
	
}





W25Q64.h

#ifndef __W25Q64_H
#define __W25Q64_H
#include "stdint.h"

void W25Q64_Init(void);
void W25Q64_ReadID(uint8_t *MID,uint16_t *DID);
void W25Q64_WriteEnable(void);
void W25Q64_WaitBusy(void);//等待Busy为0,读状态寄存器1
void W25Q64_PageProgram(uint32_t Address,uint8_t *DataArray,uint16_t Count);//页编程,Count表示一次写多少个
void W25Q64_SectorErase(uint32_t Address);//扇区擦除,调用这个函数,指定地址所在扇区就会被擦除
void W25Q64_ReadData(uint32_t Address,uint8_t *DataArray,uint32_t Count);
#endif

04-11 00:24