#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint time[]={0,0,0,0,0,0};//用来储存时间
uint date[]={0,0,0,0,0,0};//用来储存日期
/*定义按键-------------------------------------------------------------------*/
sbit K1 = P1^1; //K1-设置
sbit K2 = P1^2; //K2-确认、返回
sbit K3 = P1^3; //K3-加
sbit K4 = P1^4; //K4-减
sbit K5 = P1^5; //闹钟设置
sbit K6 = P1^6; //闹钟开关
sbit K7 =P1^7;//手动模式
sbit LED=P3^7;
sbit LED1=P3^6;
sbit beep=P3^2;
uchar flag1=0,hour1;
uchar flag,set,num=20,nao=1,tt,nz_flag;
uchar nz[7]={20,13,5,5,16,0,0};
/**************************************************/
void delay1ms(uint ms)//延时1毫秒(不够精确的)
{ uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
void delaynms(uint ms)//延时1毫秒(不够精确的)
{
uint i;
for(i=0;i<ms;i++)
delay1ms(1);
}
#include "DS1302.h"
#include "1602.h"
void aclock();
uchar button_time(n,x,y) //时钟调整子程序
uchar n,x,y;
{
if(K3==0)
{
delaynms(20);
if(K3==0)
{
n++;
if(n==x)
n=0;
while(K3==0);
}
}
if(K4==0)
{
delaynms(20);
if(K4==0)
{
if(n==0)
n=y;
else
n--;
while(K4==0);
}
}
return n;
}
uchar button_date(n,x,y) //日期调整子程序
uchar n,x,y;
{
if(K3==0)
{
delaynms(20);
if(K3==0)
{
n++;
if(n==x)
n=1;
while(K3==0);
}
}
if(K4==0)
{
delaynms(20);
if(K4==0)
{
if(n==1)
n=y;
else
n--;
while(K4==0);
}
}
return n;
}
/********************主程序***********************************/
void main()
{
LcdInt();//液晶初始化
Init_DS1302();//DS1302初始化
beep=0;
for(;;)
{
if(flag==0)
{
red_1302();
dis_shijie(num/10+0x30,num%10+0x30);
dis_riqi(date[0],date[1],date[2],date[3],date[4],date[5]);
dis_week(week%10+0x30);
dis_shijian(time[0],time[1],time[2],time[3],time[4],time[5]);
}
else
{
red_1302();
switch(set)
{
case 1: num=button_date(num,100,99);
dis_shijie(num/10+0x30,num%10+0x30);
dis_shijian('-','-','-','-','-','-');
dis_riqi('-','-','-','-','-','-');
dis_week('-');
break;
case 2:year=button_date(year,100,99); //调整nian
WriteSet1302(0x8E,0);
DS1302_SetTime(0x8c,year);
WriteSet1302(0x8E,0x80);
dis_shijie('-','-');
dis_riqi(date[0],date[1],'-','-','-','-');
dis_shijian('-','-','-','-','-','-');
dis_week('-');
break;
case 3:month=button_date(month,13,12); //调整月
WriteSet1302(0x8E,0);
DS1302_SetTime(0x88,month);
WriteSet1302(0x8E,0x80);
dis_shijie('-','-');
dis_riqi('-','-',date[2],date[3],'-','-');
dis_shijian('-','-','-','-','-','-');
dis_week('-');
break;
case 4:day=button_date(day,32,31); //调整日
WriteSet1302(0x8E,0);
DS1302_SetTime(0x86,day);
WriteSet1302(0x8E,0x80);
dis_shijie('-','-');
dis_riqi('-','-','-','-',date[4],date[5]);
dis_shijian('-','-','-','-','-','-');
dis_week('-');
break;
case 5:week=button_date(week,8,7); //调整周
WriteSet1302(0x8E,0);
DS1302_SetTime(0x8a,week);
WriteSet1302(0x8E,0x80);
dis_shijie('-','-');
dis_riqi('-','-','-','-','-','-');
dis_shijian('-','-','-','-','-','-');
dis_week(week%10+0x30);
break;
case 6:hour=button_time(hour,24,23); //调整时
WriteSet1302(0x8E,0);
DS1302_SetTime(0x84,hour);
WriteSet1302(0x8E,0x80);
dis_shijie('-','-');
dis_week('-');
dis_riqi('-','-','-','-','-','-');
dis_shijian(time[0],time[1],'-','-','-','-');
break;
case 7:min=button_time(min,60,59); //调整分
WriteSet1302(0x8E,0);
DS1302_SetTime(0x82,min);
WriteSet1302(0x8E,0x80);
dis_shijie('-','-');
dis_week('-');
dis_riqi('-','-','-','-','-','-');
dis_shijian('-','-',time[2],time[3],'-','-');
break;
case 8:sec=button_time(sec,60,59); //调整分
WriteSet1302(0x8E,0);
DS1302_SetTime(0x80,sec);
WriteSet1302(0x8E,0x80);
dis_shijie('-','-');
dis_week('-');
dis_riqi('-','-','-','-','-','-');
dis_shijian('-','-','-','-',time[4],time[5]);
break;
}
}
/*设置时间------------------------------------------------------------------*/
if (K1 == 0)
{
delaynms(20); //按键消抖
if(K1 == 0) //当是调时状态 本键用于调整下一项
{
flag=1;
set++;
if(set==9)
{
set=0;
flag=0;
}
}
while(K1 == 0); //等待键松开
}
/*--------------------------------------------------------------------------*/
if (K2 == 0) // 当在调时状态时就退出调时
{
delaynms(20);
if(K2 == 0 && flag==1)
{
flag=0;
set=0;
}
while(K2 == 0);
}
/**********是否设置闹钟**********************/
aclock();//闹钟设置
if(nz_flag)//flag=1;手动模式下的
{
WriteAddress(0x00+15);//设定屏幕上的显示位置
WriteData(0xef);
if(((hour-nz[4])*60+(min-nz[5])<=1))//1分钟
{
beep=1;
}
else
{
beep=0;
}
}
else
{
WriteAddress(0x00+15);//设定屏幕上的显示位置
WriteData(' ');
beep=0;
}
/**************闹钟开关********************************/
if (K6 == 0)//自动模式下面的
{
delaynms(20); //按键消抖
if(K6 == 0)
{
nz_flag=~nz_flag;
if(nz_flag)
{
WriteAddress(0x00+15);//设定屏幕上的显示位置
WriteData(0xef);
LED=0;
}
else
{
WriteAddress(0x00+15);//设定屏幕上的显示位置
WriteData(' ');
LED=1;
}
}
while(K6 == 0); //等待键松开
}
if(flag1==1)//手动手动按键
{
if(((hour-nz[4])*60+(min-nz[5])<=1))//1分钟
{
beep=1;
}
else
{
beep=0;
}
}
if(K7==0)
{
delaynms(20);
if(K7==0)
{
nz[4]=hour;
nz[5]=min;
flag1=!flag1;
}
while(!K7);
}
}
}
void aclock()
{
/************闹钟设置********************************************/
if (K5 == 0)
{
delaynms(20);
if(K5 == 0)
{
nao=1;
Write_com(0x01); //清屏幕指令,将以前的显示内容清除
delaynms(5);
WriteAddress(0x00+11);
WriteData('C');
WriteData('L');
WriteData('O');
WriteData('C');
WriteData('K');
WriteAddress(0xC0+12);
WriteData('S');
WriteData('E');
WriteData('T');
while(nao==1)
{
switch(tt)
{
case 0: dis_shijie(nz[0]/10+0x30,nz[0]%10+0x30);
dis_riqi(nz[1]/10+0x30,nz[1]%
10+0x30,nz[2]/10+0x30,nz[2]%
10+0x30,nz[3]/10+0x30,nz[3]%
10+0x30);
dis_shijian(nz[4]/10+0x30,nz[4]%
10+0x30,nz[5]/10+0x30,nz[5]%
10+0x30,nz[6]/10+0x30,nz[6]%
10+0x30);
break;
case 1:nz[0]=button_date(nz[0],100,99); //调整nian
dis_shijie(nz[0]/10+0x30,nz[0]%10+0x30);
dis_riqi('-','-','-','-','-','-');
dis_shijian('-','-','-','-','-','-');
break;
case 2:nz[1]=button_date(nz[1],100,99);
dis_shijie('-','-');
dis_riqi(nz[1]/10+0x30,nz[1]%10+0x30,'-','-','-','-');
dis_shijian('-','-','-','-','-','-');
break;
case 3:nz[2]=button_date(nz[2],13,12);
dis_shijie('-','-');
dis_riqi('-','-',nz[2]/10+0x30,nz[2]%10+0x30,'-','-');
dis_shijian('-','-','-','-','-','-');
break;
case 4:nz[3]=button_date(nz[3],32,31);
dis_shijie('-','-');
dis_riqi('-','-','-','-',nz[3]/10+0x30,nz[3]%10+0x30);
dis_shijian('-','-','-','-','-','-');
break;
case 5:nz[4]=button_time(nz[4],24,23);
dis_shijie('-','-');
dis_riqi('-','-','-','-','-','-');
dis_shijian(nz[4]/10+0x30,nz[4]%10+0x30,'-','-','-','-');
break;
case 6:nz[5]=button_time(nz[5],60,59);
dis_shijie('-','-');
dis_riqi('-','-','-','-','-','-');
dis_shijian('-','-',nz[5]/10+0x30,nz[5]%10+0x30,'-','-');
break;
case 7:nz[6]=button_time(nz[6],60,59);
dis_shijie('-','-');
dis_riqi('-','-','-','-','-','-');
dis_shijian('-','-','-','-',nz[6]/10+0x30,nz[6]%10+0x30);
break;
}
if (K1 == 0)
{
delaynms(20); //按键消抖
if(K1 == 0) //当是调时状态 本键用于调整下一项
{
tt++;
if(tt==8)
tt=0;
}
while(K1 == 0); //等待键松开
}
if (K2 == 0) // 当在调时状态时就退出调时
{
delaynms(20);
if(K2 == 0 )
{
tt=0;
nao=0;
Write_com(0x01); //清屏幕指令,将以前的显示内容清除
delaynms(5);
}
while(K2 == 0);
}
}
}
}
/********************************************************/
}
这是个定时开关程序,分手动模式和自动模式,但是对于手动模式的定时方法不太明白,求高手帮主 |