#include<reg51.h>
#include<intrins.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define anod P2
#define seg7 P1
sbit CLK=P1^7;
sbit IO=P1^6;
sbit RST=P1^5;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit tiaoshi=P3^7;
sbit inck=P3^6;
sbit DQ=P3^2;
uint count;
uchar temp,readdata;
bit inck_flag=0x20;
bit ReadTempFlag;//读时间的标志
typedef struct
{
uchar Second;
uchar Minute;
uchar Hour;
uchar timestr[6];
}
SYSTEMTIME;
SYSTEMTIME currenttime;
#define second 0x80
#define minute 0x82
#define hour 0x84
void mdelay(uint count)
{
uchar i,j;
for(i=0;i<count;i++)
{for(j=0;j<106;j++)
;
}
}
void ds1302inputbyte(uchar d)
{
uchar i;
ACC=d;
for(i=8;i>0;i--)
{IO=ACC0;
CLK=1;
CLK=0;
ACC=ACC>>1;
}
}
uchar ds1302utputbyte(void)
{
uchar i;
ACC=0;
for(i=8;i>0;i--)
{ACC=ACC>>1;
ACC7=IO;
CLK=1;
CLK=0;
}
return(ACC);
}
void write1302(uchar ucaddr,uchar ucda)
{
RST=0;
CLK=0;
RST=1;
ds1302inputbyte(ucaddr);
ds1302inputbyte(ucda);
CLK=1;
RST=0;
}
uchar read1302(uchar ucaddr)
{
uchar ucdata;
RST=0;
CLK=0;
RST=1;
ds1302inputbyte(ucaddr|0x01);
ucdata=ds1302utputbyte();
CLK=1;
RST=0;
return(ucdata);
}
void ds1302_gettime(SYSTEMTIME*time)
{
uchar readvalue;
readvalue=read1302(second);
time->Second=((readvalue&0x70)>>4)*10+(readvalue&0x0f);
readvalue=read1302(minute);
time->Minute=((readvalue&0x70)>>4)*10+(readvalue&0x0f);
readvalue=read1302(hour);
time->Hour=((readvalue&0x70)>>4)*10+(readvalue&0x0f);
}
void timetostr(SYSTEMTIME*time)
{
time->timestr[0]=time->Hour/10;
time->timestr[1]=time->Hour%10;
time->timestr[2]=time->Minute/10;
time->timestr[3]=time->Minute%10;
time->timestr[4]=time->Second/10;
time->timestr[5]=time->Second%10;
}
void dis(SYSTEMTIME*time)
{
uchar disbit,shitfb;
ds1302_gettime(time);
timetostr(time);
disbit=1;
switch(count)
{
case 0:
for(shitfb=0;shitfb<6;shitfb++)
{
_nop_(); _nop_();
seg7=time->timestr[shitfb];
anod=_crol_(disbit,shitfb);
mdelay(6);
anod=0;
}
break;
case 1:
for(shitfb=0;shitfb<2;shitfb++)
{
_nop_(); _nop_();
seg7=time->timestr[shitfb];
anod=_crol_(disbit,shitfb);
mdelay(6);
anod=0;
}
break;
case 2:
for(shitfb=0;shitfb<4;shitfb++)
{
_nop_(); _nop_();
seg7=time->timestr[shitfb];
anod=_crol_(disbit,shitfb);
mdelay(6);
anod=0;
}
break;
case 3:
for(shitfb=0;shitfb<6;shitfb++)
{
_nop_(); _nop_();
seg7=time->timestr[shitfb];
anod=_crol_(disbit,shitfb);
mdelay(6);
anod=0;
}
break;
default:break;
}
}
void setkey()
{
if(tiaoshi==0)
{
mdelay(8);
if(tiaoshi==0)
{
count=count+1;
if(count==4)
{
count=0;
readdata=(read1302(0x80))&0x7f;
write1302(0x80,readdata);
write1302(0x8e,0x80);
}
while(tiaoshi==0);
}
}
}
void inckey()
{
if(inck==0)
{
mdelay(8);
switch(count)
{
case 1:
temp=read1302(hour);
temp=((temp&0x70)>>4)*10+(temp&0x0f);
temp=temp+1;
if(temp>=24)
temp=0;
inck_flag=1;
break;
case 2:
temp=read1302(minute);
temp=((temp&0x70)>>4)*10+(temp&0x0f);
temp=temp+1;
inck_flag=1;
if(temp>=60)
temp=0;
break;
case 3:
temp=read1302(second);
temp=((temp&0x70)>>4)*10+(temp&0x0f);
temp=temp+1;
inck_flag=1;
if(temp>=60)
temp=0;
break;
default: inck_flag=0;break;
}
while(inck==0);
}
}
void keydone()
{
write1302(0x8e,0x00);
readdata=read1302(0x80);
write1302(0x80,readdata|0x80);
switch(count)
{
case 1:do
{
inckey();
if(inck_flag==1)
{
temp=(temp/10)<<4|temp%10;
write1302(0x84,temp);
inck_flag=0;
}
dis(¤ttime);
}
while(count==2);
break;
case 2:do
{
inckey();
if(inck_flag==1)
{
temp=(temp/10)<<4|temp%10;
write1302(0x82,temp);
inck_flag=0;
}
dis(¤ttime);
}
while(count==3);
break;
case 3:do
{
inckey();
if(inck_flag==1)
{
temp=(temp/10)<<4|temp%10;
write1302(0x80,temp);
inck_flag=0;
}
dis(¤ttime);
}
while(count==4);
break;
default:break;
}
}
void serial_init()
{
SCON =0X50;
TMOD |=0X20;
TH1=0XFD;
TR1=1;
TI=1;
}
void Init_Timer0(void)
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
//TH0=0x00; //给定初值
//TL0=0x00;
EA=1; //总中断打开
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开
}
/*------------------------------------------------
定时器中断子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1
{
static unsigned int num;
TH0=(65536-2000)/256; //重新赋值 2ms
TL0=(65536-2000)%256;
num++;
if(num==300) //
{
num=0;
ReadTempFlag=1; //读标志位置1
}
}
void main()
{
int temp;
float temperature;
Init_Timer0();
serial_init();
inck_flag=0;
mdelay(100);
readdata=read1302(0x80);
write1302(0x80,readdata&0x7f);
while(1)
{
ds1302_gettime(¤ttime);
timetostr(¤ttime);
dis(¤ttime);
setkey();
if(count!=0)
keydone();
if(ReadTempFlag==1)
{
ReadTempFlag=0;
temp=ReadTemperature();
temperature=(float)temp*0.0625;
printf("%4.4fC\r",temperature);//打印温度值
}
}
}
评论0