用的的C8051F310,程序如下,很简单的一个檫除问题。
#include<c8051f310.h>
#include <stdio.h>
unsigned char a;
bit c;
char EA_SAVE;
void FLASH_PageErase(unsigned int addr);
void FLASH_ByteWrite(unsigned int addr,char byte);
unsigned char FLASH_ByteRead(unsigned int addr);
void main()
{
char b=5;
PCA0MD &= ~0x40;
FLASH_PageErase(0);
while(1)
{
a = FLASH_ByteRead(0);
if (a == 0)
{
FLASH_ByteWrite(0,b);
}
c=RSTSRC^6;
P2 = FLASH_ByteRead(0);
}
}
//擦除
void FLASH_PageErase(unsigned int addr)
{
bit EA_SAVE=EA;
char xdata*data pwrite;
RSTSRC=0x00;
VDM0CN=0x80;
RSTSRC=0x02;
EA=0;
pwrite=(char xdata*)addr;
FLKEY=0xA5;
FLKEY=0xF1;
PSCTL|=0x03;
*pwrite=0;
RSTSRC=0x00;
VDM0CN=0x80;
RSTSRC=0x02;
PSCTL&=~0x03;
EA=EA_SAVE;
}
//写入
void FLASH_ByteWrite(unsigned int addr,char byt)
{
bit EA_SAVE=EA;
char xdata*data pwrite;
RSTSRC=0x00;
VDM0CN=0x80;
RSTSRC=0x02;
EA=0;
pwrite=(char xdata*)addr;
FLKEY=0xA5;
FLKEY=0xF1;
PSCTL|=0x01;
*pwrite=byt;
RSTSRC=0x00;
VDM0CN=0x80;
RSTSRC=0x02;
PSCTL&=~0x01;
EA=EA_SAVE;
}
//读取
unsigned char FLASH_ByteRead(unsigned int addr)
{
bit EA_SAVE=EA;
char code*data pread;
unsigned char byte;
RSTSRC=0x00;
VDM0CN=0x80;
RSTSRC=0x02;
EA=0;
pread=(char code*)addr;
byte =*pread;
RSTSRC=0x00;
VDM0CN=0x80;
RSTSRC=0x02;
EA=EA_SAVE;
return byte;
} |