依据PIC12F617单片机的datasheet,现已写清楚关于FLASH的操作时序,咱们只需求进行一系列的装备和读写时序操作即可。
void flash_write(unsigned short addr,unsigned int value) //写FLASH函数
{
unsigned char i;
for(i=0;i<4;i++)
{
PMADRL=(((addr)&0xfc)|i); //低地址时序操作,需求接连四次的写入缓冲操作,当履行到第四次时,
//硬件会主动擦除相应FLASH然后写入
PMADRH=((addr)>>8);
PMDATH=((value)>>8);
PMDATL=((value)&0xff);
WREN=1;
GIE = 0; //关中止
PMCON2 = 0x55;
PMCON2 = 0xaa; //固定操作
WR=1;
NOP();
NOP();
WREN=0;
GIE = 1; //开中止
}
}
unsigned int flash_read(unsigned short addr) //读FALSH函数
{
unsigned int val;
PMADRL=(addr)&0xff;
PMADRH=(addr)>>8;
//WREN=0;
GIE = 0; //关中止
PMCON1 |= 0x80;
RD=1;
NOP();
NOP();
val = ((PMDATH << 8) | PMDATL);
GIE = 1; //开中止
return val;
}