#include<reg51.h>
#include <INTRINS.H>
bit write=0;
sbit SDA=P2^0;
sbit SCL=P2^1;
sbit wela=P2^7;
sbit dula=P2^6;
sbit beep=P2^3;
//sbit RST=P2^5; //时钟 不需要,可以关掉
/**** 注意:以下这句不可关掉,否则断电后数据不会保存****/
sbit CS_DA =P2^4 ; //HJ-3G DAC HL-1不需要,可以关掉
unsigned char num;
unsigned char a,b,sec,count;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void Delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=110;k>0;k--);
}
/*
void Diaplay(unsigned char a,unsigned char b)
{
wela=1;
P0=0x7d;
wela=0;
Delay(5);
dula=1;
P0=table[a]; //显示百位
dula=0;
wela=1;
P0=0x7e;
wela=0;
Delay(5);
dula=1;
P0=table[b]; //显示十位
dula=0;
}
*/
/****与上面的比较有什么不同,上面的会点亮别的数码管****/
void Diaplay(unsigned char a,unsigned char b)
{
dula=0;
P0=table[a]; //显示百位
dula=1;
dula=0;
wela=0;
P0=0x7e;
wela=1;
wela=0;
Delay(5);
dula=0;
P0=table[b]; //显示十位
dula=1;
dula=0;
wela=0;
P0=0x7d;
wela=1;
wela=0;
Delay(5);
}
void nop()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void init()
{
// RST=0; //总线时钟低电评
CS_DA =0 ; //若无此语句,则不会读出保存值
SCL=1;
nop();
SDA=1;
nop();
}
//开始信号
void Start_i2c()
{
SDA=1;
nop();
SCL=1;
nop();
SDA=0;
nop();
//SCL=0;
//nop();
}
//停止信号
void Stop_i2c()
{
SDA=0;
nop();
SCL=1;
nop();
SDA=1;
nop();
}
void Respons()
{
unsigned char i;
SCL=1;
nop();
while((SDA==1)&&(i<250)) i++;
SCL=0;
nop();
}
//发送一个字节
void Write_Byte(unsigned char j)
{
unsigned char i,temp;
temp=j;
for(i=0;i<8;i++)
{
temp=temp<<1;
SCL=0;
nop();
SDA=CY;
nop();
SCL=1; //待sda线上的数据稳定后,将scl拉高
nop();
}
SCL=0;
nop();
SDA=1;
nop();
}
unsigned char Read_Byte() //为什么不是void的形式
{
unsigned char i,k;
k=0;
SCL=0;
nop();
SDA=1; //??置数据线为输入方式
nop();
for(i=0;i<8;i++)
{
SCL=1;
nop();
k=(k<<1)|SDA;
SCL=0;
nop();
}
nop();
return(k);
}
void Write_add(unsigned char address,unsigned char dat)
{
Start_i2c();
Write_Byte(0xa0);
Respons();
Write_Byte(address);
Respons();
Write_Byte(dat);
Respons();
Stop_i2c();
}
unsigned char Read_add(unsigned char address)
{
unsigned char dat;
Start_i2c();
Write_Byte(0xa0);
Respons();
Write_Byte(address);
Respons();
Start_i2c();
Write_Byte(0xa1);
Respons();
dat=Read_Byte();
Stop_i2c();
return dat;
}
void Time0_init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void main()
{
init();
sec=Read_add(3);
Time0_init();
Delay(100);
while(1)
{
a=sec/10;
b=sec%10;
Diaplay(a,b);
if(write==1)
{
write=0;
Write_add(3,sec);
beep=0;
Delay(50);
beep=1;
//Delay(200);
}
}
}
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{
count=0;
sec++;
write=1;
if(sec==100)
sec=0;
}
}
#include <INTRINS.H>
bit write=0;
sbit SDA=P2^0;
sbit SCL=P2^1;
sbit wela=P2^7;
sbit dula=P2^6;
sbit beep=P2^3;
//sbit RST=P2^5; //时钟 不需要,可以关掉
/**** 注意:以下这句不可关掉,否则断电后数据不会保存****/
sbit CS_DA =P2^4 ; //HJ-3G DAC HL-1不需要,可以关掉
unsigned char num;
unsigned char a,b,sec,count;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void Delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=110;k>0;k--);
}
/*
void Diaplay(unsigned char a,unsigned char b)
{
wela=1;
P0=0x7d;
wela=0;
Delay(5);
dula=1;
P0=table[a]; //显示百位
dula=0;
wela=1;
P0=0x7e;
wela=0;
Delay(5);
dula=1;
P0=table[b]; //显示十位
dula=0;
}
*/
/****与上面的比较有什么不同,上面的会点亮别的数码管****/
void Diaplay(unsigned char a,unsigned char b)
{
dula=0;
P0=table[a]; //显示百位
dula=1;
dula=0;
wela=0;
P0=0x7e;
wela=1;
wela=0;
Delay(5);
dula=0;
P0=table[b]; //显示十位
dula=1;
dula=0;
wela=0;
P0=0x7d;
wela=1;
wela=0;
Delay(5);
}
void nop()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void init()
{
// RST=0; //总线时钟低电评
CS_DA =0 ; //若无此语句,则不会读出保存值
SCL=1;
nop();
SDA=1;
nop();
}
//开始信号
void Start_i2c()
{
SDA=1;
nop();
SCL=1;
nop();
SDA=0;
nop();
//SCL=0;
//nop();
}
//停止信号
void Stop_i2c()
{
SDA=0;
nop();
SCL=1;
nop();
SDA=1;
nop();
}
void Respons()
{
unsigned char i;
SCL=1;
nop();
while((SDA==1)&&(i<250)) i++;
SCL=0;
nop();
}
//发送一个字节
void Write_Byte(unsigned char j)
{
unsigned char i,temp;
temp=j;
for(i=0;i<8;i++)
{
temp=temp<<1;
SCL=0;
nop();
SDA=CY;
nop();
SCL=1; //待sda线上的数据稳定后,将scl拉高
nop();
}
SCL=0;
nop();
SDA=1;
nop();
}
unsigned char Read_Byte() //为什么不是void的形式
{
unsigned char i,k;
k=0;
SCL=0;
nop();
SDA=1; //??置数据线为输入方式
nop();
for(i=0;i<8;i++)
{
SCL=1;
nop();
k=(k<<1)|SDA;
SCL=0;
nop();
}
nop();
return(k);
}
void Write_add(unsigned char address,unsigned char dat)
{
Start_i2c();
Write_Byte(0xa0);
Respons();
Write_Byte(address);
Respons();
Write_Byte(dat);
Respons();
Stop_i2c();
}
unsigned char Read_add(unsigned char address)
{
unsigned char dat;
Start_i2c();
Write_Byte(0xa0);
Respons();
Write_Byte(address);
Respons();
Start_i2c();
Write_Byte(0xa1);
Respons();
dat=Read_Byte();
Stop_i2c();
return dat;
}
void Time0_init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void main()
{
init();
sec=Read_add(3);
Time0_init();
Delay(100);
while(1)
{
a=sec/10;
b=sec%10;
Diaplay(a,b);
if(write==1)
{
write=0;
Write_add(3,sec);
beep=0;
Delay(50);
beep=1;
//Delay(200);
}
}
}
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{
count=0;
sec++;
write=1;
if(sec==100)
sec=0;
}
}