使用STM8L芯片、W25Q16Flash,先将要存储的扇区1数据读出放入备用扇区0,将数据修改后,再讲修改的数据和备用扇区0的数据,存回扇区1,但是存储的过程中,要么数据存不进去,要么存入的数据有问题,请各位帮忙看下是什么问题?void Write_Admin_Passord(uc *p,uc passordNum)
{
ui i,n;
uc flashBuffer[256];
W25q16_Erasure(0);
for(n = 0;n < 16;n++)
{
W25Q16_Read(4096+n*256,flashBuffer,256);
W25Q16_Write(n*256,flashBuffer,256);
}
W25q16_Erasure(1);
W25Q16_Read(4096,flashBuffer,256);
for(i = 0;i < passordNum;i++)
flashBuffer[i] = p[i];
W25Q16_Write(4096,flashBuffer,256);
for(n = 1;n < 16;n++)
{
W25Q16_Read(n*256,flashBuffer,256);
W25Q16_Write(4096+n*256,flashBuffer,256);
}
}
void W25Q16_Write(ul address,uc *data,ui j)
{
ui i;
while(W25_Read_Busy());
W25_Write_Enable();
W25_CS_ENABLE();
W25_SPI_Write(0x02);
W25_SPI_Write(address>>16);
W25_SPI_Write(address>>8);
W25_SPI_Write(address);
for(i = 0;i < j;i++)
{
W25_SPI_Write(*(data+i));
}
W25_CS_DISABLE();
}
/*===============================================================
*功能说明:W25 读数据
*传入参数:地址、数据、个数
*返回参数:无
=================================================================*/
void W25Q16_Read(ul address,uc *data,ui j)
{
ui i;
while(W25_Read_Busy());
W25_Write_Enable();
W25_CS_ENABLE();
W25_SPI_Write(0x03);
W25_SPI_Write(address>>16);
W25_SPI_Write(address>>8);
W25_SPI_Write(address);
for(i = 0;i < j;i++)
{
*(data+i) = W25_SPI_Read();
}
W25_CS_DISABLE();
}
void W25q16_Erasure(ul Dst_Addr)
{
Dst_Addr *= 4096;
W25_Write_Enable();
while(W25_Read_Busy());
W25_CS_ENABLE();
W25_SPI_Write(0x20);
W25_SPI_Write((uc)((Dst_Addr)>>16));
W25_SPI_Write((uc)((Dst_Addr)>>8));
W25_SPI_Write((uc)Dst_Addr);
W25_CS_DISABLE();
while(W25_Read_Busy());
}
ui SPI_Flash_ReadID(void)
{
ui Temp = 0;
W25_Write_Enable();
W25_CS_ENABLE();
W25_SPI_Write(0x90);//发送读取ID命令
W25_SPI_Write(0x00);
W25_SPI_Write(0x00);
W25_SPI_Write(0x00);
Temp |= W25_SPI_Read()<<8;
Temp |= W25_SPI_Read();
W25_CS_DISABLE();
return Temp;
}
|