设计一个1s的间隔时钟,能用串口修改实时修改数据。时钟具有掉电保存的功能(上电后读取保存数据)。
#include <reg52.h>
#define uchar unsigned char
#define unit unsigned int
uchar bai,shi,ge,aa,flag;
int shu,qq;
sbit scl=P2^1;
sbit sda=P2^0;
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};
void delay()
{ ;; }
void delay1(uchar z)
{
uchar q,w;
for(q=z;q>0;q--)
for(w=110;w>0;w--);
}
void star()
{
scl=1;
delay();
sda=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;//这里也有个悲剧,模拟I2C总线的原理,scl是时钟线,sda是数据线,把这里的sda和下面的scl一换就又不
delay();//能正常工作了~~附图看上面
scl=1://看图上的时序,只要scl是高,sda的上升沿前后保持一段时间就好了啊,可就是不对,求解释~~
delay();
sda=1;
delay();
}
void yingda()
{
// uchar i;
scl=1;
delay();
//while((sda==1)&&(i<250))i++;
//delay();
scl=0;
delay1(1);
}
void write_byte(uchar date)
{
uchar a,i;
a=date;
for(i=0;i<8;i++)
{
a=a<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
/* scl=0;
delay();
sda=1;
delay();*/
for(i=0;i<8;i++)
{
scl=0;
delay();
k=(k<<1)|sda;
scl=1;
delay();
}
return k;
}
void write_add(uchar addrss,uchar com)
{
star();
write_byte(0xa0);
yingda();
write_byte(addrss);
yingda();
write_byte(com);
yingda();
stop();
}
uchar read_add(uchar ak)
{
uchar date;
star();
write_byte(0xa0);
yingda();
write_byte(ak);
yingda();
star();
write_byte(0xa1);
yingda();
date=read_byte();
stop();
return date;
}
void init()
{
scl=1;
sda=1;
qq=0;
shu=0;
TMOD=0x21;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
//shu=read_add(2);
TH1=0xfd;
TL1=0xfd;
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
}
void display(uchar bai,uchar shi,uchar ge)
{
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay1(1);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay1(1);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay1(1);
}
void main()
{
init();
shu=read_add(2); → →//这里有个问题就是为什么这个读取保存数据的语句放在初始化init()里就是
while(1) //不行呢,一上电就是255再上电又是255似乎始终里面存的都是255
{ //可是放到初始化外面,一切都又正常了~~~新人求解释~~~!!!!!!!
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
display(bai,shi,ge);
}
}
void timer0() interrupt 1
{
uchar aa;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==20)
{
aa=0;
shu++;
write_add(2,shu);
if(shu==1000)
shu=0;
}
}
void ser() interrupt 4
{
RI=0;
qq=SBUF;
shu=qq;
}
|