|
#include <reg52.h> /* special function register declarations */
#include <absacc.h> /* for the intended 8051 derivative */
#include <stdio.h> /* prototype declarations for I/O functions */
unsigned int i,j;
unsigned char timer1=0;
unsigned char timer2=0;
unsigned char xdata buf[256];
void read_flash(unsigned int Sector,unsigned char xdata *p);
void write_flash(unsigned int Sector,unsigned char xdata *p);
void delay_1s(void);
void main (void)
{ /* execution starts here after stack init */
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x21; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xfd; /* TH1: reload value for 19200 baud */
TR1 = 1; /* TR1: timer 1 run */
TH0 = 0xff;
TR0 = 1;
TI = 1; /* TI: set TI to send first char of UART */
IE = 0x82;
read_flash(0,buf);
i=buf[0];
i=i*256;
i+=buf[1];
printf("Counter is %d when last Power off.\n\r",i);
while (1) {
delay_1s();
i++;
buf[0]=i/256;
buf[1]=i%256;
write_flash(0,buf);
printf("Now Counter is %d.\n\r",i);
}
}
void delay_1s(void)
{
j=0;
while(j<10){
timer2=0;
while(timer2<100);
j++;
}
}
dir() interrupt 1
{
static unsigned char i;
TL0=0xcd;
TH0=0xf8;
timer1++;
timer2++;
}
void read_flash(unsigned int Sector,unsigned char xdata *p)
{
unsigned int i;
unsigned int j;
XBYTE[0xffff]=Sector/8;
i=(Sector%8)*256+0x8000;
for( j=0;j<256;j++ )
{
p[j]=XBYTE[i+j];
}
}
void write_flash(unsigned int Sector,unsigned char xdata *p)
{
unsigned int i;
unsigned int j;
i=(Sector%8)*256+0x8000;
EA=0;
XBYTE[0x9000]=0x0a;
XBYTE[0x8555]=0xaa;
XBYTE[0x9000]=0x05;
XBYTE[0x82aa]=0x55;
XBYTE[0x9000]=0x0a;
XBYTE[0x8555]=0xa0;
XBYTE[0x9000]=Sector/8;
for( j=0;j<256;j++ )
{
XBYTE[i+j]=p[j];
}
EA=1;
timer1=0;
while( timer1<20 ){}
}
|