📄 clock_clarm.c
字号:
#include<AT89X52.h>
#include"DS1302.h"
#include"LCD12864(ST7920).h"
#include"DS18B20.h"
#define uchar unsigned char
#define uint unsigned int
#define inset P3_2
#define outset p3_3
#define up P3_4
#define clarmclose P3_5
#define bell P1_0
uchar code line1[]= "液晶编程测试程序";
uchar code line2[]= " 2008年10月11日";
uchar code line3[]= "上午12点00分00秒";
uchar code line4[]= "闹钟关 温度 度";
uchar temperature[]="28";
uchar clarm[]= "00点00分";
uchar clarm_hour[]="00";
uchar clarm_minute[]="00";
uchar clarm_time[2]={0,0};
uchar code blank[]= " ";
uchar code add[ ]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F };
uchar tt[3];
uchar mm[3];
uchar ss[3];
uchar year[3];
uchar manth[3];
uchar day[3];
/*--------------------温度转换显示子程序--------------*/
void DS18B20_display()
{ uint temp;
temp=ReadTemperature();
temperature[0]=temp/10+'0';
temperature[1]=temp%10+'0';
LCD12864_WC(add[28]);
wstring("温度 度");
LCD12864_WC(add[30]);
wstring(temperature);
}
/*-------------------LCD12864显示主程序----------*/
uchar count=0; //时钟/日期设定计数器
uchar clarm_count=0; //闹钟时/分设定计数器
static bit clarm_on_flag=0; //闹钟开标志位
static bit clarm_onoff=0; //时钟设定开/关标志位
static bit clarm_set=0; //闹钟设置开标志位
static bit clarm_on=0; //闹钟设定时间到标志位
static bit time_set_flag=0; //时钟设定开/关标志位
uchar clarm_temperature_count=0; //闹钟时间和温度交替显示时间计数器
SYSTEMTIME CurrentTime;
void LCD12864_displaytime(void);
void main()
{
EA=1;
EX0=1;
EX1=1;
PX1=1;
IT0=1;
IT1=1;
DS18B20_initial();
DS1302_Initial();
LCD12864_initial();
LCD12864_WC(0x80);
wstring(line1);
delayus(100);
LCD12864_WC(0x90);
wstring(line2);
delayus(100);
LCD12864_WC(0x88);
wstring(line3);
delayus(100);
LCD12864_WC(0x98);
wstring(line4);
delayus(100);
DS18B20_display();
LCD12864_WC(add[8]);
wstring(" ");
LCD12864_WD(0x03);
while(1)
{
DS1302_GetTime(&CurrentTime) ;
LCD12864_displaytime();
//DS18B20_display();
if(CurrentTime.Hour<13)
{LCD12864_WC(add[16]);
w2s("上午");
}
else
{LCD12864_WC(add[16]);
w2s("下午");
}
/*------------闹钟设定部分程序 ------------------- */
while(clarm_set)
{ LCD12864_WC(add[28]);
wstring(clarm);
LCD12864_WC(add[30]);
w2s(clarm_minute);
LCD12864_WC(add[28]);
w2s(clarm_hour);
clarm_on_flag=1;
//if(inset==0)
// {clarm_count++;
// if(clarm_count>1)
// {clarm_count=0;
// flag=1;
// clarm_on=0;
// }
// }
switch(clarm_count)
{case 0: if(up==0)
{
if((clarm_time[1]+=1)<60);
else
clarm_time[1]=0;
}
LCD12864_WC(add[30]);
w2s(clarm_minute);
delayus(20000);
LCD12864_WC(add[30]);
w2s(blank);
delayus(20000);
LCD12864_WC(add[30]);
w2s(clarm_minute);
break;
case 1: if(up==0)
{
if((clarm_time[0]+=1)<24);
else
clarm_time[0]=0;
}
LCD12864_WC(add[28]);
w2s(clarm_hour);
delayus(20000);
LCD12864_WC(add[28]);
w2s(blank);
delayus(20000);
LCD12864_WC(add[28]);
w2s(clarm_hour);
break;
}
if(inset==0)
{clarm_count++;
if(clarm_count>1)
{clarm_count=0;
// flag=1;
// clarm_set=0;
}
}
clarm_hour[0]=clarm_time[0]/10+ '0';
clarm_hour[1]=clarm_time[0]%10+ '0';
clarm_minute[0]=clarm_time[1]/10+ '0';
clarm_minute[1]=clarm_time[1]%10+ '0';
DS1302_GetTime(&CurrentTime) ;
LCD12864_displaytime();
}
/*--------------------闹钟定时时间比较---------------*/
if((clarm_time[0]==CurrentTime.Hour)&(clarm_time[1]==CurrentTime.Minute)&clarm_on_flag)
//{bell=~bell;
clarm_on=1;//}
if(clarm_on)
bell=~bell;
/*------------温度和闹钟时间交替显示 ----------*/
if(clarm_on_flag)
{clarm_temperature_count++;
if(clarm_temperature_count>40)
clarm_temperature_count=0;
if(clarm_temperature_count<20)
DS18B20_display();
else
{LCD12864_WC(add[28]);
wstring(clarm);
LCD12864_WC(add[30]);
w2s(clarm_minute);
LCD12864_WC(add[28]);
w2s(clarm_hour);}
}
else
DS18B20_display();
/*-------------------闹钟关闭程序-------------------
if(clarmclose==0)
{bell=1;
clarm_on_flag=0;
clarm_set=0;
LCD12864_WC(add[26]);
wstring("关");
LCD12864_WC(0x98);
wstring(line4);
} */
/*-------------时钟调整时间部分程序---------------*/
if(inset==0);
delayus(5000);
if(inset==0)
time_set_flag=1;
while(time_set_flag)
{ if(inset==0)
{count++;
if(count>6)
count=0;
}
switch(count)
{
case 0: if(up==0)
{
if((CurrentTime.Second+=1)<60);
else
CurrentTime.Second=0;
DS1302_SetTime(DS1302_SECOND,CurrentTime.Second);
}
LCD12864_WC(add[22]);
w2s(ss);
delayus(20000);
LCD12864_WC(add[22]);
w2s(blank);
delayus(20000);
LCD12864_WC(add[22]);
w2s(ss);
break;
case 1: if(up==0)
{
if((CurrentTime.Minute+=1)<60);
else
CurrentTime.Minute=0;
DS1302_SetTime(DS1302_MINUTE,CurrentTime.Minute);
}
LCD12864_WC(add[20]);
w2s(mm);
delayus(20000);
LCD12864_WC(add[20]);
w2s(blank);
delayus(20000);
LCD12864_WC(add[20]);
w2s(mm);
break;
case 2: if(up==0)
{
if((CurrentTime.Hour+=1)<24);
else
CurrentTime.Hour=0;
DS1302_SetTime(DS1302_HOUR,CurrentTime.Hour);
}
LCD12864_WC(add[18]);
w2s(tt);
delayus(20000);
LCD12864_WC(add[18]);
w2s(blank);
delayus(20000);
LCD12864_WC(add[18]);
w2s(tt);
break;
case 3: if(up==0)
{CurrentTime.Day+=1;
if(CurrentTime.Day<32);
else
CurrentTime.Day=0;
DS1302_SetTime(DS1302_DAY,CurrentTime.Day);
}
LCD12864_WC(add[14]);
w2s(day);
delayus(20000);
LCD12864_WC(add[14]);
w2s(blank);
delayus(20000);
LCD12864_WC(add[14]);
w2s(day);
break ;
case 4:if(up==0)
{
if((CurrentTime.Month+=1)<13);
else
CurrentTime.Month=0;
DS1302_SetTime(DS1302_MONTH,CurrentTime.Month);
}
LCD12864_WC(add[12]);
w2s(manth);
delayus(20000);
LCD12864_WC(add[12]);
w2s(blank);
delayus(20000);
LCD12864_WC(add[12]);
w2s(manth);
break ;
case 5:if(up==0)
{
if((CurrentTime.Year+=1)<101);
else
CurrentTime.Year=0;
DS1302_SetTime(DS1302_YEAR,CurrentTime.Year);
}
LCD12864_WC(add[10]);
w2s(year);
delayus(20000);
LCD12864_WC(add[10]);
w2s(blank);
delayus(20000);
LCD12864_WC(add[10]);
w2s(year);
break ;
case 6: if(up==0)
clarm_onoff=~clarm_onoff;
if(clarm_onoff)
{clarm_set=1;
LCD12864_WC(add[26]);
wstring("开");
delayus(20000);
LCD12864_WC(add[26]);
wstring(" ");
delayus(20000);
LCD12864_WC(add[26]);
wstring("开");
//flag=0;
}
else
{clarm_set=0;
LCD12864_WC(add[26]);
wstring("关");
delayus(20000);
LCD12864_WC(add[26]);
wstring(" ");
delayus(20000);
LCD12864_WC(add[26]);
wstring("关");
}
break;
}
//if(clarm_set)
// flag=0;
DS1302_GetTime(&CurrentTime) ;
LCD12864_displaytime();
}
}
}
/*-------------------LCD12864显示时间刷新----------------*/
void LCD12864_displaytime(void)
{
tt[0]=CurrentTime.Hour/10+ '0';
tt[1]=CurrentTime.Hour%10+ '0';
tt[2]='\0';
mm[0]=CurrentTime.Minute/10+ '0';
mm[1]=CurrentTime.Minute%10+ '0';
mm[2]='\0';
ss[0]=CurrentTime.Second/10+ '0';
ss[1]=CurrentTime.Second%10+ '0';
ss[2]='\0';
year[0]=CurrentTime.Year/10+ '0';
year[1]=CurrentTime.Year%10+ '0';
year[2]='\0';
manth[0]=CurrentTime.Month/10+ '0';
manth[1]=CurrentTime.Month%10+ '0';
manth[2]='\0';
day[0]=CurrentTime.Day/10+ '0';
day[1]=CurrentTime.Day%10+ '0';
day[2]='\0';
LCD12864_WC(add[10]);
w2s(year);
LCD12864_WC(add[12]);
w2s(manth);
LCD12864_WC(add[14]);
w2s(day);
LCD12864_WC(add[18]);
w2s(tt);
LCD12864_WC(add[20]);
w2s(mm);
LCD12864_WC(add[22]);
w2s(ss);
}
/*-------------------------
void int0() interrupt 0 using 1
{ time_set_flag=1;
count++;
if(count>6)
count=0;
delayus(20000);
}
-------------------------------*/
void int1() interrupt 2 using 2
{ time_set_flag=0;
if(clarm_on_flag)
clarm_set=0;
if(clarm_on)
{clarm_on_flag=0;
bell=1;
clarm_set=0;
clarm_on=0;
LCD12864_WC(add[26]);
wstring("关");
LCD12864_WC(0x98);
wstring(line4);}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -