大佬们来看一下,w25q64数据读取全是ff,应该是没写入不进去,程序在下面各位看下
2024-4-10 00:24 来自 dengxs 发布 @ 综合讨论
#include "w25q128.h"
/************************************************************************/
void SPI_init(void)
{
SPI_CE_High();
CLK_FLASH = 0; // set clock to low initial state
DI_FLASH = 1;
}
/************************************************************************/
void SPI_WriteByte(u8 out)
{
u8 i;
i = 8;
do{
out <<= 1;
DI_FLASH = CY;
CLK_FLASH = 1;
CLK_FLASH = 0;
}while(--i);
DI_FLASH = 1;
}
/************************************************************************/
u8 SPI_ReadByte(void)
{
u8 i, in;
i = 8;
do{
in <<= 1;
if (DO_FLASH) in++;
CLK_FLASH = 1;
CLK_FLASH = 0;
}while(--i);
return in;
}
/************************************************
检测Flash是否准备就绪
入口参数: 无
出口参数:
0 : 没有检测到正确的Flash
1 : Flash准备就绪
************************************************/
void FlashCheckID(void)
{
u16 ID,ID1,ID2;
SPI_CE_Low();
SPI_WriteByte(SFC_RDID); //发送读取ID命令
SPI_WriteByte(0x00); //空读3个字节
SPI_WriteByte(0x00);
SPI_WriteByte(0x00);
ID = SPI_ReadByte(); //读取制造商ID1
ID1 = SPI_ReadByte(); //读取设备ID
ID2 = SPI_ReadByte(); //读取制造商ID2
SPI_CE_High();
printf("ID:%x,ID1:%x,ID2:%x\r\n",ID,ID1,ID2);
}
/************************************************
检测Flash的忙状态
入口参数: 无
出口参数:
0 : Flash处于空闲状态
1 : Flash处于忙状态
************************************************/
u8 CheckFlashBusy(void)
{
u8 dat;
SPI_CE_Low();
SPI_WriteByte(SFC_RDSR); //发送读取状态命令
dat = SPI_ReadByte(); //读取状态
SPI_CE_High();
return (dat); //状态值的Bit0即为忙标志
}
/************************************************
使能Flash写命令
入口参数: 无
出口参数: 无
************************************************/
void FlashWriteEnable(void)
{
while(CheckFlashBusy() > 0); //Flash忙检测
SPI_CE_Low();
SPI_WriteByte(SFC_WREN); //发送写使能命令
SPI_CE_High();
}
/****************
/************************************************************************/
void SPI_init(void)
{
SPI_CE_High();
CLK_FLASH = 0; // set clock to low initial state
DI_FLASH = 1;
}
/************************************************************************/
void SPI_WriteByte(u8 out)
{
u8 i;
i = 8;
do{
out <<= 1;
DI_FLASH = CY;
CLK_FLASH = 1;
CLK_FLASH = 0;
}while(--i);
DI_FLASH = 1;
}
/************************************************************************/
u8 SPI_ReadByte(void)
{
u8 i, in;
i = 8;
do{
in <<= 1;
if (DO_FLASH) in++;
CLK_FLASH = 1;
CLK_FLASH = 0;
}while(--i);
return in;
}
/************************************************
检测Flash是否准备就绪
入口参数: 无
出口参数:
0 : 没有检测到正确的Flash
1 : Flash准备就绪
************************************************/
void FlashCheckID(void)
{
u16 ID,ID1,ID2;
SPI_CE_Low();
SPI_WriteByte(SFC_RDID); //发送读取ID命令
SPI_WriteByte(0x00); //空读3个字节
SPI_WriteByte(0x00);
SPI_WriteByte(0x00);
ID = SPI_ReadByte(); //读取制造商ID1
ID1 = SPI_ReadByte(); //读取设备ID
ID2 = SPI_ReadByte(); //读取制造商ID2
SPI_CE_High();
printf("ID:%x,ID1:%x,ID2:%x\r\n",ID,ID1,ID2);
}
/************************************************
检测Flash的忙状态
入口参数: 无
出口参数:
0 : Flash处于空闲状态
1 : Flash处于忙状态
************************************************/
u8 CheckFlashBusy(void)
{
u8 dat;
SPI_CE_Low();
SPI_WriteByte(SFC_RDSR); //发送读取状态命令
dat = SPI_ReadByte(); //读取状态
SPI_CE_High();
return (dat); //状态值的Bit0即为忙标志
}
/************************************************
使能Flash写命令
入口参数: 无
出口参数: 无
************************************************/
void FlashWriteEnable(void)
{
while(CheckFlashBusy() > 0); //Flash忙检测
SPI_CE_Low();
SPI_WriteByte(SFC_WREN); //发送写使能命令
SPI_CE_High();
}
/****************
免责声明:
本平台旨在开源共享精神,请勿发布敏感信息,任何违法信息我们将移交公安机关;
上一篇: 开天斧程序运行问题