#include<reg52.h>
#include<math.h>
#include<absacc.h>
#include<ds1302.h> //DS1302驱动程序
#include<LCD_1602A.h> //LCD1302驱动程序
//==========================
#define uchar unsigned char
#define uint unsigned int
//==========================
sbit k1=P1^3;
sbit k2=P1^1;
sbit k3=P1^5;
sbit beep=P3^7;
uchar key1;
uchar tab_time[8]={0,0,10,0,0,10,0,1}; //时间
uchar tab_ymdw[12]={2,0,0,2,11,0,0,11,0,2,12,0}; //年月日星期
uchar tab_num[]="0123456789:/ ";
uchar tab_week[21]="MonTeuWedThuFriSatSun";
uchar tab_ap[]="AMPM";
uchar tab_t[]="T:",tab_d[]="D:", alarm_time[]={0,15,7};
//--------------------------
//------- 函数声明 --------
void lcd_disp();
void trans();
void key();
void delay(uint);
void display_set(uchar ,uchar ,uchar );
void music();
void display_char1(uchar ,uchar );
void display_char2(uchar ,uchar );
//==========================
//------ 主函数 -----------
void main()
{
ds1302_init(); //DS1302初始化
write_command(0x01); //LCD清屏
initial_lcd();
delay(10);//LCD1602初始化
while(1)
{
ds1302();
key();
trans();
if(!key1)
lcd_disp();
if(tab_1302[0]==alarm_time[0]&&tab_1302[1]==alarm_time[1]&&tab_1302[2]==alarm_time[2])
music();
}
}
//=============显示函数============
void display_char2(uchar adr,uchar dat)
{
write_command(0x80+0x40+adr);
write_data(dat);
}
void display_char1(uchar adr,uchar dat)
{
write_command(0x80+adr);
write_data(dat);
}
//=============音乐播放===========================
void music()
{ uint ul,n,p=20;
while(p--)
{
for( ul=0;ul<150;ul++)
{
for( n=0;n<30;n++);
beep=~beep;
}
delay(200);
for(ul=0;ul<150;ul++)
{
for(n=0;n<30;n++);
beep=~beep;
}
delay(800);
}
}
//==================设定显示函数=========================
void display_set(uchar p,uchar adr,uchar dat)
{
uchar shi,ge,i,j;
shi=dat/10;
ge=dat%10;//分离date的十位和个位数
if(p==1&&adr==13)
{write_command( 0xc0+13);
for(i=0;i<3;i++)
{ //星期
j=ge; //星期
write_data(tab_week[(j-1)*3+i]);
}
}
else if(!p)
write_command(0x80+adr);
else
write_command(0xc0+adr);
write_data( 0x30+shi);
write_data( 0x30+ge);
}
//----------------- 数据转换函数 ---------------------------
void trans()
{
tab_time[0]=tab_1302[2]/10; //小时十位
tab_time[1]=tab_1302[2]%10; //小时个位
tab_time[3]=tab_1302[1]/10; //分十位
tab_time[4]=tab_1302[1]%10; //分个位
tab_time[6]=tab_1302[0]/10; //秒十位
tab_time[7]=tab_1302[0]%10; //秒个位
tab_ymdw[2]=tab_1302[6]/10; //年十位
tab_ymdw[3]=tab_1302[6]%10; //年个位
tab_ymdw[5]=tab_1302[4]/10; //月十位
tab_ymdw[6]=tab_1302[4]%10; //月个位
tab_ymdw[8]=tab_1302[3]/10; //日十位
tab_ymdw[9]=tab_1302[3]%10; //日个位
tab_ymdw[11]=tab_1302[5]; //星期
}
void lcd_disp()
{
uchar i,j ; //LCD1602写命令函数,从第一行开始显
//------------------------
for(i=0;i<2;i++)
display_char1(i,tab_t[i]);
for(i=0;i<8;i++)
{
j=tab_time[i];
display_char1(3+i,tab_num[j]); //LCD1602写数据函数,显示时间
}
//-----------------------------------------
if(tab_1302[2]<12)
for(i=0;i<2;i++)
display_char1(14+i,tab_ap[i]);
else for(i=2;i<4;i++)
display_char1(12+i,tab_ap[i]);
for(i=0;i<2;i++)
display_char2(i,tab_d[i]);
for(i=0;i<11;i++)
{ j=tab_ymdw[i];
display_char2(i+2,tab_num[j]); //LCD1602写数据函数,显示年月日星期
}
display_char2(12,tab_num[12]) ;
for(i=0;i<3;i++)
{
j=tab_ymdw[11]; //星期
display_char2(13+i,tab_week[(j-1)*3+i]);
}
write_command( 0x80); //LCD1602写命令函数
}
//=============================================
//------- 时间日期调节函数 ----------
//------- k1选定,k2加1,k3减1 ------
//=============================================
void key()
{
//-------------------------
if(k1==0)
{
delay(10);
if(k1==0)
{
while(!k1);
key1++;
if(key1==8) //一共要调7个
{key1=0;
write_command( 0x0c);}
}
//______________________光标控制__________________________
if(key1==1)
{
write_command( 0x80+10);
write_command( 0x0d); }
if(key1==2) write_command( 0x80+7);
if(key1==3) write_command( 0x80+4);
if(key1==4) write_command( 0xc0+11);
if(key1==5) write_command( 0xc0+8);
if(key1==6) write_command( 0xc0+13);
if(key1==7) write_command( 0xc0+5);
}
//--------------------------------------------------------
if(k2==0) //加一
{
delay(10);
if(k2==0)
{
while(!k2);
if(key1==1) //秒调节
{
tab_1302[key1-1]++;
if(tab_1302[key1-1]>=60)
{tab_1302[key1-1]=0;}
if(tab_1302[key1-1]<0)
{tab_1302[key1-1]=60;}
display_set(0,9,tab_1302[0]);
write_command( 0x80+10);
writeds_add1(0x80+(key1-1)*2,tab_1302[key1-1]);
}
//---------------
if(key1==2) //分调节
{
tab_1302[key1-1]++;
if(tab_1302[key1-1]>=60)
{tab_1302[key1-1]=0;}
if(tab_1302[key1-1]<0)
{tab_1302[key1-1]=60;}
display_set(0,6,tab_1302[1]);
write_command( 0x80+7);
writeds_add1(0x80+(key1-1)*2,tab_1302[key1-1]);
}
//---------
if(key1==3) //小时调节
{
tab_1302[key1-1]++;
if(tab_1302[key1-1]>=24)
{tab_1302[key1-1]=0;}
if(tab_1302[key1-1]<0)
{tab_1302[key1-1]=24;}
display_set(0,3,tab_1302[2]);
write_command( 0x80+4);
writeds_add1(0x80+(key1-1)*2,tab_1302[key1-1]);
}
//--------------
if(key1==4) //日期调节
{
tab_1302[key1-1]++;
if(tab_1302[key1-1]>=32)
{tab_1302[key1-1]=1;}
if(tab_1302[key1-1]<=0)
{tab_1302[key1-1]=31;}
display_set(1,10,tab_1302[3]);
write_command( 0xc0+11);
writeds_add1(0x80+(key1-1)*2,tab_1302[key1-1]);
}
//-------------------
if(key1==5) //月调节
{
tab_1302[key1-1]++;
if(tab_1302[key1-1]>=13)
{tab_1302[key1-1]=1;}
if(tab_1302[key1-1]<=0)
{tab_1302[key1-1]=12;}
display_set(1,7,tab_1302[4]);
write_command( 0xc0+8);
writeds_add1(0x80+(key1-1)*2,tab_1302[key1-1]);
}
//------------------
if(key1==6) //星期调节
{
tab_1302[key1-1]++;
if(tab_1302[key1-1]>=8)
{tab_1302[key1-1]=1;}
if(tab_1302[key1-1]<=0)
{tab_1302[key1-1]=7;}
display_set(1,13,tab_1302[5]);
write_command( 0xc0+13);
writeds_add1(0x80+(key1-1)*2,tab_1302[key1-1]);
}
//-----------------
if(key1==7) //年调节
{
tab_1302[key1-1]++;
if(tab_1302[key1-1]>=99)
{tab_1302[key1-1]=0;}
if(tab_1302[key1-1]<0)
{tab_1302[key1-1]=99;}
display_set(1,4,tab_1302[6]);
write_command( 0xc0+5);
writeds_add1(0x80+(key1-1)*2,tab_1302[key1-1]);
}
}
}
//======================================================
if(k3==0) //减一
{
delay(10);
if(k3==0)
{
while(!k3);
if(key1==1) //秒调节
{
tab_1302[key1-1]--;
if(tab_1302[key1-1]>=60)
{tab_1302[key1-1]=0;}
if(tab_1302[key1-1]<0)
{tab_1302[key1-1]=60;}
display_set(0,9,tab_1302[0]);
write_command( 0x80+10);
writeds_add1(0x80+(key1-1)*2,tab_1302[key1-1]);
}
//---------------
if(key1==2) //fen调节
{
tab_1302[key1-1]--;
if(tab_1302[key1-1]>=60)
{tab_1302[key1-1]=0;}
if(tab_1302[key1-1]<0)
{tab_1302[key1-1]=60;}
display
- 1
- 2
前往页