中断+1602
时间:2010-05-14 来源:caocwy
中断+1602 计数到255重新计数
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit SDA=P3^6;
sbit SCL=P3^7;
sbit lcd_rs=P1^0;
sbit lcd_rw=P1^1;
sbit lcd_e=P1^2;
unsigned char code table[] = {"0123456789"};
uint x,d,write=0;
uchar sec,tcnt,bai,shi,ge;
void delay()
{
;;
}
void delaylms(uint x)
{
uint w,y;
for(w=x;w>0;w--)
for(y=110;y>0;y--);
}
/************LCD1602*****************/
void write_com(uchar com)
{
lcd_rs=0;
P2=com;
lcd_rw=0;
delaylms(5);
lcd_e=1;
delaylms(5);
lcd_e=0;
}
void write_date(uchar date)
{
lcd_rs=1;
P2=date;
lcd_rw=0;
delaylms(5);
lcd_e=1;
delaylms(5);
lcd_e=0;
}
void lcdinit()
{
lcd_e=0;
write_com(0x38);
write_com(0x0C);
write_com(0x06);
write_com(0x01);
}
//AT24C02初始化
void init()
{
SDA=1;
delay();
SCL=1;
delay();
}
//AT24C02启动
void start()
{
SDA=1;
delay();
SCL=1;
delay();
SDA=0;
delay();
}
//AT24C02停止
void stop()
{
SDA=0;
delay();
SCL=1;
delay();
SDA=1;
delay();
}
//AT24C02应答
void promise()
{
uchar i;
SCL=1;
delay();
while((SDA==1)&&(i<255)) //SDA=1,在一段时间内自动应答;
i++;
SCL=0;
delay();
}
//写一个字节
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
SCL=0;
delay();
SDA=CY; //进位标志为把数据写进去
delay();
SCL=1;
delay();
}
SCL=0; //重新设置Data Validity
delay();
SDA=1;
delay();
}
//读一个字节
uchar readbyte()
{
uchar i,k;
SCL=0;
delay();
SDA=1;
delay();
for(i=0;i<8;i++)
{
SCL=1;
delay();
k=(k<<1)|SDA;
SCL=0;
delay();
}
delay();
return k;
}
//任意地址写数据
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0); //AT24C02器件地址吗
promise();
write_byte(address);
promise();
write_byte(date);
promise();
stop();
}
//任意地址读数据
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
promise();
write_byte(address);
promise();
start();
write_byte(0xa1);
promise();
date=readbyte();
stop();
return date;
}
/*
void display(uchar shi,uchar ge)
{
SMG_1;
P2=table[shi];
delaylms(5);
SMG_2;
P2=table[ge];
delaylms(5);
}
*/
void main()
{
init();
sec=0;
d=0; //片内地址池初始为0,最大到256 (0-255)
lcdinit();
sec=read_add(1); //存储单元地址
TMOD=0x01;
EA=1;
ET0=1;
TH0=(65536-50000);
TL0=(65536-50000);
TR0=1;
while(1)
{
// display(sec/10,sec%10);
write_com(0x80);
write_date(table[bai]);
write_com(0x80+0x01);
write_date(table[shi]);
write_com(0x80+0x02);
write_date(table[ge]);
if(write==1)
{
write=0;
write_add(1,sec); //存储单元地址,数据
}
}
}
void time() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tcnt++;
if(tcnt==20)
{
tcnt=0;
sec++;
bai=sec/100;
shi=sec%100/10;
ge=sec%10;
write=1;
if(sec>255)
sec=0;
}
}