#include<intrins.h>
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define RdCommand 0x01//定义ISP的操作命令
#define PrgCommand 0x02
#define EraseCommand 0x03
#define Error 1
#define Ok 0
#define WaitTime 0x01
sfr ISP_DATA=0xe2;
sfr ISP_ADDRH=0xe3;
sfr ISP_ADDRL=0xe4;
sfr ISP_CMD=0xe5;
sfr ISP_TRIG=0xe6;
sfr ISP_CONTR=0xe7;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar num;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--) //i=xms即延时约xms毫秒
for(j=110;j>0;j--);
}
void display(uchar shi,uchar ge)
{
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(5);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(5);
}
/****************打开ISP,IAP************************/
void ISP_IAP_enable(void)
{
EA=0;//关总中断
ISP_CONTR=ISP_CONTR&0x18;/*0001 1000*/
ISP_CONTR=ISP_CONTR|WaitTime;/*写入硬件延时*/
ISP_CONTR=ISP_CONTR|0x80;/*ISPEN=1*/
}
/************关闭ISP,IAP**************************/
void ISP_IAP_disable(void)
{
ISP_CONTR=ISP_CONTR&0x7f;
ISP_TRIG=0x00;
EA=1;
}
/*****************公用的出发代码*****************/
void ISPgoon(void)
{
ISP_IAP_enable();
ISP_TRIG=0x46;
ISP_TRIG=0xb9;
_nop_();
}
/******************字节读********************/
unsigned char byte_read(unsigned int byte_addr)
{
ISP_ADDRH=(unsigned char)(byte_addr>>8);
ISP_ADDRL=(unsigned char)(byte_addr&0x00ff);
ISP_CMD=ISP_CMD&0xf8;
ISP_CMD=ISP_CMD|RdCommand;
ISPgoon();
ISP_IAP_disable();
return(ISP_DATA);
}
/**************扇区擦写************************/
void SectorErase(unsigned int sector_addr)
{
unsigned int iSectorAddr;
iSectorAddr=(sector_addr&0xfe00);
ISP_ADDRH=(unsigned char)(iSectorAddr>>8);
ISP_ADDRL=0x00;
ISP_CMD=ISP_CMD&0xf8;
ISP_CMD=ISP_CMD|EraseCommand;
ISPgoon();
ISP_IAP_disable();
}
/*************字节写*************************/
void byte_write(unsigned int byte_addr,unsigned char original_data)
{
ISP_ADDRH=(unsigned char)(byte_addr>>8);
ISP_ADDRL=(unsigned char)(byte_addr&0x00ff);
ISP_CMD=ISP_CMD&0xf8;
ISP_CMD=ISP_CMD|PrgCommand;
ISP_DATA=original_data;
ISPgoon();
ISP_IAP_disable();
}
void main()
{
uchar a,b,num1;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
num1=byte_read(0x2000);
if(num1>=60)
num1=0;
a=0;
b=0;
while(1)
{
if(num>=20)
{
num=0;
num1++;
SectorErase(0x2000);
byte_write(0x2000,num1);
if(num1==60)
{
num1=0;
}
a=num1/10;
b=num1%10;
}
display(a,b);
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
} |