本帖最后由 浮忞 于 2020-5-15 09:35 编辑
想试下EEPROM功能,显示一个数掉电后上电继续加数,但每次上电都是从0开始,有些延时,初始化,显示程序我就没贴出来
void Write_Data(unsigned int addr1,unsigned char dat1)
{
CY = EA;
EA = 0;
FLASHCON = 0x01; //SSP编程需先关闭中断
IB_DATA = dat1; //编程用数据寄存器
IB_OFFSET = addr1; //被擦除/编程的块单元地址
XPAGE = 0x00; //擦除/编程用扇区选择和地址选择寄存器
if(gBFlashLock!=0x5A) //判断写操作标志位
{
goto Write_Err;
}
IB_CON1 = 0x6E; //SSP操作模式选择寄存器:存储单元编程
IB_CON2 = 0x05; //SSP流程控制寄存器1
IB_CON3 = 0x0A;
IB_CON4 = 0x09;
IB_CON5 = 0x06;
_nop_();
_nop_();
_nop_();
_nop_();
Write_Err:
gBFlashLock=0x00;
XPAGE=0x00;
FLASHCON = 0x00; //当不需要对类EEPROM操作时,必须将FAC位清0。
EA = CY;
}
/*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
SSP操作类EEPROM-读数据:
入口:addr = 扇区单元地址
出口:dat = 读出的数据
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
unsigned char Read_Data(unsigned int addr2)
{
unsigned char dat2;
CY = EA;
EA = 0;
FLASHCON = 0x01;
XPAGE = 0x00;
IB_OFFSET = addr2;
if(gBFlashLock!=0x5A) // 烧写/擦除的超级抗干扰措施
{
goto Read_Err;
}
IB_CON1 = 0x6E; //SSP操作模式选择寄存器:存储单元编程
IB_CON2 = 0x05; //SSP流程控制寄存器1
IB_CON3 = 0x0A;
IB_CON4 = 0x09;
IB_CON5 = 0x06;
dat2 = IB_DATA; //取出数据
_nop_();
_nop_();
_nop_();
_nop_();
Read_Err:
gBFlashLock=0x00;
XPAGE=0x00;
FLASHCON = 0x00; //当不需要对类EEPROM操作时,必须将FAC位清0。
EA = CY;
return dat2;
}
//擦除
void cc(unsigned int addr)
{
CY = EA;
EA = 0;
FLASHCON = 0x01;
XPAGE = addr;
if(gBFlashLock != 0xA5) //判断写操作标志位
{
goto Erase_Err;
}
IB_CON1 = 0xE6; //SSP操作模式选择寄存器:扇形擦除
IB_CON2 = 0x05;
IB_CON3 = 0x0A;
IB_CON4 = 0x09;
IB_CON5 = 0x06;
_nop_();
_nop_();
_nop_();
_nop_();
Erase_Err:
gBFlashLock=0x00;
XPAGE=0x00;
FLASHCON = 0x00;
EA = CY;
}
void work()
{
T++;
if(T == 1000)
{
T = 0;
num ++;
{
if(num > 60)
{
num = 0;
}
}
}
cc(0x01);
Write_Data(0x01,num);
}
void view()
{
Display_DIA(0,num);
}
void main()
{
Oscillator_Init();
CLK32KTO24M();
init();
Delayms(1);
ClearDisplay();
gBFlashLock = 0x5A; //烧写/擦除的超级抗干扰措施 标志位
num = Read_Data(0x01);
while(1)
{
work();
view();
Delayms(1);
}
} |