第1个回答 2010-07-30
基于AT89S52的时钟程序
#include <REG51.H>
#include <intrins.h>
sbit rs = P2^0;
sbit rw = P2^1;
sbit e = P2^2;
#define data P0
sbit BF=P0^7;
#define com 0
#define dat 1
void ShowTime(void);
void SetKey(void);
void UpKey(void);
void OutKey(void);
sbit fmq=P3^6;
sbit DQ=P3^3;
unsigned char temp_value,temp1_value,TempBuffer[8];
bit tempflag;
bit chimeflag=0;
sbit SCLK=P1^0; //ds1302
sbit DATA=P1^1;
sbit CE =P1^2;
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
//秒,分,时到日,月,年位闪的计数
unsigned char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year;
unsigned char week_value[3];
bit am_pm=0;
sbit Set = P1^4;
sbit Up = P1^5;
sbit Down= P1^6;
sbit out = P1^7;
unsigned char done,mos=0,count1=0,count2=0,count3=0,count4=0,temp,up_flag,down_flag;
#define OP_READ 0xa1
#define OP_WRITE 0xa0
sbit SDA=P3^5;
sbit SCL=P3^4;
bit clockflag;
unsigned char clock[7];
unsigned char cvalue,cvalue1,cvaluea;
unsigned char hide_c_min,hide_c_hour;
void Delayms(unsigned int ms)
{
unsigned int i;
for(;ms>0;ms--)
{
for(i=0;i<80;i++)
{;}
}
}
void Delayus(unsigned int i) {
while(i--);
}
void Beep() //产生方波,使蜂鸣器发声
{
unsigned int a3;
for(a3=0;a3<150;a3++)
{
fmq=0;
Delayus(23);
fmq=1;
Delayus(23);
}
}
void Start()
{
SDA = 1;
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 0;
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0; }
void Stop()
{
SDA = 0;
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 1;
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0;
SCL=0;
}
unsigned char ReadData()
{
unsigned char i;
unsigned char x;
for(i = 0; i < 8; i++)
{
SCL = 1;
x<<=1;
x|=(unsigned char)SDA;
SCL = 0;
}
return(x);
}
bit WriteCurrent(unsigned char y)
{
unsigned char i;
bit ack_bit;
for(i = 0; i < 8; i++)
{
SDA = (bit)(y&0x80);
_nop_();
SCL = 1;
_nop_();
_nop_();
SCL = 0;
y <<= 1;
}
SDA = 1;
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ack_bit = SDA;
SCL = 0;
return ack_bit;
}
void WriteSet(unsigned char add, unsigned char Dat)
{
Start();
WriteCurrent(OP_WRITE);
WriteCurrent(add);
WriteCurrent(Dat);
Stop();
Delayms(4); }
unsigned char ReadCurrent()
{
unsigned char x;
Start();
WriteCurrent(OP_READ);
x=ReadData();
Stop();
return x; }
unsigned char ReadSet(unsigned char set_addr)
// 在指定地址读取
{
Start();
WriteCurrent(OP_WRITE);
WriteCurrent(set_addr);
return(ReadCurrent());
}
void InitialDSCE(void)
{
DQ = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
DQ = 0;
Delayus(90);
DQ = 1;
Delayus(4);
tempflag=DQ;
Delayus(10);
}
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char Dat = 0;
for (i=8;i>0;i--)
{
DQ = 1;
_nop_();
DQ = 0;
Dat>>=1;
DQ = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(DQ)
Dat|=0x80;
Delayus(9);
}
return(Dat);
}
void WriteOneChar(unsigned char Dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
_nop_();
DQ = Dat&0x01;
Delayus(10);
DQ = 1;
Dat>>=1;
}
}
/**************读取ds18b20当前温度************/
void ReadTemp(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned char t;
InitialDSCE();
if(tempflag==0)
{
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Delayms(200); // this message is wery important
InitialDSCE();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
Delayus(2);
a=ReadOneChar(); //读取温度值低位
b=ReadOneChar(); //读取温度值高位
t=b&0xf8;
if(t)
{
TempBuffer[0]='-';
temp_value=b<<4;
temp_value+=(a&0xf0)>>4;
temp_value=~temp_value+1;
temp1_value=(~a)&0x0f;
}
else
{
TempBuffer[0]=' ';
temp_value=b<<4;
temp_value+=(a&0xf0)>>4;
temp1_value=a&0x0f;
}
TempBuffer[1]=temp_value%100/10+'0'; //十位
if(TempBuffer[1]=='0')
{
TempBuffer[1]=' ';
}
//温度数据转换成液晶字符显示
TempBuffer[2]=temp_value%10+'0'; //个位
TempBuffer[3]='.';
TempBuffer[4]=temp1_value*625/1000%10+'0';
TempBuffer[5]=temp1_value*625/100%10+'0';
TempBuffer[6]='\0';
}
}
//DS1302时钟部分子程序模块
//***********************************
typedef struct __SYSTEMTIME__
{
unsigned char Hour1;
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[13];
unsigned char TimeString[9];
}SYSTEMTIME; //定义的时间类型
SYSTEMTIME CurrentTime;
#define AM(X) X
#define PM(X) (X+12) // 转成24小时制
#define DS1302_SECOND 0x80 //时钟芯片的寄存器位置,存放时间
#define DS1302_MINUTE 0x82
#define DS1302_HOUR 0x84
#define DS1302_WEEK 0x8A
#define DS1302_DAY 0x86
#define DS1302_MONTH 0x88
#define DS1302_YEAR 0x8C
//**********************************
//实时时钟写入一字节(内部函数)
//**********************************
void WriteByteDS(unsigned char d)
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DATA = ACC0; //相当于汇编中的 RRC
SCLK = 1;
SCLK = 0;
ACC = ACC >> 1;
}
}
//*************************************
//实时时钟读取一字节(内部函数)
//*************************************
unsigned char ReadByteDS(void)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; //相当于汇编中的 RRC
ACC7 = DATA;
SCLK = 1;
SCLK = 0;
}
return(ACC);
}
//**************************************
//ucAddr: DS1302地址, ucData: 要写的数据
//**************************************
void WriteDS(unsigned char ucAddr, unsigned char ucDa)
{
CE = 0;
SCLK = 0;
CE = 1;
WriteByteDS(ucAddr); // 地址,命令
WriteByteDS(ucDa); // 写1Byte数据
SCLK = 1;
CE = 0;
}
//**************************************
//读取DS1302某地址的数据
//**************************************
unsigned char ReadDS(unsigned char ucAddr)
{
unsigned char ucData;
CE = 0;
SCLK = 0;
CE = 1;
WriteByteDS(ucAddr|0x01); // 地址,命令
ucData = ReadByteDS(); // 读1Byte数据
SCLK = 1;
CE = 0;
return(ucData);
}
//******************************************
//获取时钟芯片的时钟数据到自定义的结构型数组
//******************************************
void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue;
ReadValue = ReadDS(DS1302_SECOND);
Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);//转换为相应的10进制数
ReadValue = ReadDS(DS1302_MINUTE);
Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = ReadDS(DS1302_HOUR);
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
Time->Hour1 = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
if(am_pm)
{
if((((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F))>12)
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)-12;
}
ReadValue = ReadDS(DS1302_DAY);
Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = ReadDS(DS1302_WEEK);
Time->Week = ((ReadValue&0x10)>>4)*10 + (ReadValue&0x0F);
ReadValue = ReadDS(DS1302_MONTH);
Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = ReadDS(DS1302_YEAR);
Time->Year = ((ReadValue&0xf0)>>4)*10 + (ReadValue&0x0F);
}
//******************************************
//将时间年,月,日,星期数据转换成液
//晶显示字符串,放到数组里DateString[]
//******************************************
void DateToStr(SYSTEMTIME *Time)
{
unsigned char tab[ ]={0XD2,0XBB,0XB6,0XFE,0XC8,0XFD,0XCB,0XC4,0XCE,0XE5,0XC1,0XF9,0XC8,0XD5};
if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示
{
Time->DateString[0] = '2';
Time->DateString[1] = '0';
Time->DateString[2] = Time->Year/10 + '0';
Time->DateString[3] = Time->Year%10 + '0';
}
else
{
Time->DateString[0] = ' ';
Time->DateString[1] = ' ';
Time->DateString[2] = ' ';
Time->DateString[3] = ' ';
}
Time->DateString[4]='-';
Time->DateString[5]='-';
if(hide_month<2)
{
Time->DateString[6] = Time->Month/10 + '0';
Time->DateString[7] = Time->Month%10 + '0';
}
else
{
Time->DateString[6] = ' ';
Time->DateString[7] = ' ';
}
Time->DateString[8]='-';
Time->DateString[9]='-';
if(hide_day<2)
{
Time->DateString[10] = Time->Day/10 + '0';
Time->DateString[11] = Time->Day%10 + '0';
}
else
{
Time->DateString[10] = ' ';
Time->DateString[11] = ' ';
}
if(hide_week<2)
{
//星期的数据另外放到 week_value[]数组里,跟年,月,日的分开存放,因为等一下要在最后显示
week_value[0] =tab[2*(Time->Week%10)-2];
week_value[1] =tab[2*(Time->Week%10)-1];
}
else
{
week_value[0] = ' ';
week_value[1]=' ';
}
week_value[2] = '\0';
Time->DateString[12] = '\0'; //字符串末尾加 '\0' ,判断结束字符
}
//******************************************
//将时,分,秒数据转换成液晶
//显示字符放到数组 TimeString[]
//*****************************************
void TimeToStr(SYSTEMTIME *Time)
{ if(hide_hour<2)
{
Time->TimeString[0] = Time->Hour/10 + '0';
Time->TimeString[1] = Time->Hour%10 + '0';
}
else
{
Time->TimeString[0] = ' ';
Time->TimeString[1] = ' ';
}
Time->TimeString[2] = ':';
if(hide_min<2)
{
Time->TimeString[3] = Time->Minute/10 + '0';
Time->TimeString[4] = Time->Minute%10 + '0';
}
else
{
Time->TimeString[3] = ' ';
Time->TimeString[4] = ' ';
}
Time->TimeString[5] = ':';
if(hide_sec<2)
{
Time->TimeString[6] = Time->Second/10 + '0';
Time->TimeString[7] = Time->Second%10 + '0';
}
else
{
Time->TimeString[6] = ' ';
Time->TimeString[7] = ' ';
}
Time->TimeString[8] = '\0';
}
//******************************
//时钟芯片初始化
//******************************
void InitialDS(void)
{
unsigned char Second=ReadDS(DS1302_SECOND);
if(Second&0x80) //判断时钟芯片是否关闭
{
WriteDS(0x8e,0x00); //写入允许
WriteDS(0x8c,0x0a); //以下写入初始化时间 日期:07/07/25.星期: 3. 时间: 23:59:59
WriteDS(0x88,0x04);
WriteDS(0x86,0x0a);
WriteDS(0x8a,0x06);
WriteDS(0x84,0x00);
WriteDS(0x82,0x00);
WriteDS(0x80,0x00);
WriteDS(0x8e,0x80); //禁止写入
SDA = 1; // SDA=1,SCL=1,使主从设备处于空闲状态
SCL = 1;
WriteSet(0x36,0x00);
WriteSet(0x37,0x00);
WriteSet(0x38,0x00);
WriteSet(0x39,0x00);
WriteSet(0x3a,0x00);
}
}
void ClockTimeToStr()
{
unsigned char ReadValue;
ReadValue = ReadSet(0x36);
cvalue = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);//转换为相应的10进制数
cvaluea= ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
if(am_pm)
{
if((((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F))>12)
cvalue = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F)-12;
}
ReadValue = ReadSet(0x37);
cvalue1 = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
if(hide_c_hour<2)
{
clock[0] =cvalue/10 + '0';
clock[1] =cvalue%10 + '0';
}
else
{
clock[0] = ' ';
clock[1] = ' ';
}
clock[2] = ':';
if(hide_c_min<2)
{
clock[3]=cvalue1/10 + '0';
clock[4]=cvalue1%10 + '0';
}
else
{
clock[3]=' ';
clock[4]=' ';
}
clock[5]=' ';
clock[6]='\0';
}
void Clocking()
{
clockflag=ReadSet(0x3a);
if(clockflag)
{
if(CurrentTime.Second==0&&CurrentTime.Minute==cvalue1&&CurrentTime.Hour1==cvaluea)
{
do
{
ShowTime();
SetKey(); //扫描各功能键
OutKey();
Beep();
Delayms(10);
Beep();
Delayms(10);
if(clockflag==0)break;
}while(CurrentTime.Minute!=cvalue1+1);
}
}
}
void Chime()
{
chimeflag=ReadSet(0x38);
if(chimeflag)
{ if(CurrentTime.Second==0&&CurrentTime.Minute==0)
{
do
{
ShowTime();
Beep();
Delayms(50);
Beep();
Delayms(100);
}while(CurrentTime.Second!=3);
}
}
}
void ShowTime(void)
{
switch(mos)
{
case 0: DS1302_GetTime(&CurrentTime); //获取时钟芯片的时间数据
TimeToStr(&CurrentTime); //时间数据转换液晶字符
DateToStr(&CurrentTime); //日期数据转换液晶字符
chimeflag=ReadSet(0x38);
if(chimeflag) Print("◎",0,0);
else Print("⊙",0,0);
Print(CurrentTime.DateString,1,0);
Print("年",3,0);
Print("月",5,0);
Print("日",7,0);
am_pm=ReadSet(0x39);
if(am_pm)
{
if(CurrentTime.Hour1<=12) Print("am ",0,1);
if(CurrentTime.Hour1>12) Print("pm ",0,1);
}
else Print(" ",0,1);
Print(CurrentTime.TimeString,2,1);
Print(" ",6,1);
Print("星期",0,2);
Print(week_value,2,2);
Print(" ",3,2);
clockflag=ReadSet(0x3a);
if(clockflag)
Print(" ((◎))((◎)) ",0,3);
else
Print(" --◎----◎-- ",0,3);
ReadTemp(); //开启温度采集程序
if(tempflag==0)
{
Print(TempBuffer,4,2);
Print("℃",7,2);
}
else
{
Print("unexist ",4,2);
}
Delayms(200); //扫描延时
break;
case 1:
DS1302_GetTime(&CurrentTime);
TimeToStr(&CurrentTime);
Print("******时间******",0,0);
Print("┏━━━━━━┓",0,1);
am_pm=ReadSet(0x39);
if(am_pm)
{
if(CurrentTime.Hour1<=12) Print("┃am",0,2);
if(CurrentTime.Hour1>12) Print("┃pm",0,2);
}
else Print("┃ ",0,2);
Print(CurrentTime.TimeString,2,2);
Print(" ┃",6,2);
Print("┗━━━━━━┛",0,3);
Delayms(200);
break;
case 2:
Print("******日期******",0,0);
Print("┏━━━━━━┓",0,1);
Print("┃ ",0,2);
DS1302_GetTime(&CurrentTime);
DateToStr(&CurrentTime);
Print(CurrentTime.DateString,1,2);
Print("┃",7,2);
Print("┗━━━━━━┛",0,3);
Delayms(200);
break;
case 3:
Print("******星期******",0,0);
Print("┏━━━━━━┓",0,1);
Print("┃ ",0,2);
DS1302_GetTime(&CurrentTime);
DateToStr(&CurrentTime);
Print("星期",2,2);
Print(week_value,4,2);
Print(" ┃",5,2);
Print("┗━━━━━━┛",0,3);
Delayms(200);
break;
case 4:
ClockTimeToStr();
Print("******闹钟******",0,0);
Print("┏━━━━━━┓",0,1);
am_pm=ReadSet(0x39);
if(am_pm)
{
if(cvaluea<=12) Print("┃am ",0,2);
if(cvaluea>12) Print("┃pm ",0,2);
}
else Print("┃ ",0,2);
Print(clock,3,2);
Print(" ┃",6,2);
Print("┗━━━━━━┛",0,3);
Delayms(200);
break;
default : break;
}
}
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
//************************************************
temp=((temp&0x70)>>4)*10 + (temp&0x0F);
temp=temp+1; //日数加1
up_flag=1;
Delayms(150);
if(temp>31)
temp=1;
temp=temp/10*16+temp%10;
break;
case 2:
temp=ReadDS(DS1302_MONTH); //读取月数
temp=((temp&0x70)>>4)*10 + (temp&0x0F);
temp=temp+1; //月数加1
up_flag=1;
Delayms(150);
if(temp>12)
temp=1;
temp=temp/10*16+temp%10;
break;
case 1:
temp=ReadDS(DS1302_YEAR); //读取年数
temp=((temp&0xf0)>>4)*10 + (temp&0x0F);
temp=temp+1; //年数加1
up_flag=1;
Delayms(150);
if(temp>99)
temp=0;
temp=temp/10*16+temp%10;
break;
default:break;
}break;
case 3:
temp=ReadDS(DS1302_WEEK); //读取星期数
temp=((temp&0x70)>>4)*10 + (temp&0x0F);
temp=temp+1; //星期数加1
up_flag=1;
Delayms(150);
if(temp>7)
temp=1;
temp=temp/10*16+temp%10;
break;
case 4:
switch(count4)
{
case 1:
temp=ReadSet(0x36);
temp=((temp&0x70)>>4)*10 + (temp&0x0F);
temp=temp+1; //小时数加1
up_flag=1;
clockflag=1;
Delayms(150);
if(temp>23) //超过23小时,清零
temp=0;
temp=temp/10*16+temp%10;
break;
case 2:
temp=ReadSet(0x37);
temp=((temp&0x70)>>4)*10 + (temp&0x0F);
temp=temp+1; //分数加1
up_flag=1;
clockflag=1;
Delayms(150);
if(temp>59) //超过59分,清零
temp=0;
temp=temp/10*16+temp%10;
break;
default:break;
}break;
case 0: chimeflag=~chimeflag;
SDA = 1; // SDA=1,SCL=1,使主从设备处于空闲状态
SCL = 1;
WriteSet(0x38,chimeflag);
break;
default:break;
}
}
}