楼主的程序:
- #include<reg51.h>
- #include<INTRINS.H>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key1=P1^0;
- sbit key2=P1^1;
- sbit key3=P1^2;
- sbit key4=P1^3;
- sbit key5=P1^4;
- sbit key6=P1^5;
- sbit key7=P1^6;
- sbit key8=P1^7;
- sbit buzzer=P2^5; //蜂鸣器端口
- sbit lcd_RS = P2^0; //1602数据/命令选择端(H:数据寄存器L:指令寄存器)
- sbit lcd_RW = P2^1; //1602读/写选择端
- sbit lcd_E = P2^2;//1602使能信号端
- uchar time0[]="CLOCK 23:58";
- uchar mark1[]="2015-01-01";
- uchar mark2[]=" SET UP ";
- uchar code tab[]={'0','1','2','3','4','5','6','7','8','9'};//液晶显示
- uchar code tab2[]={0x08,0x14,0x0A,0x15,0x0A,0x14,0x08,0x10}; //有闹铃声响开关标示显示
- uchar alarm_on; //闹铃声响级
- uchar sound_on; //闹铃发声标示
- uchar display_alarm; //显示闹铃标示
- uchar num=0;
- uchar flag_clock,flag_alarm,flag_year; //时钟/闹铃设置模式标示
- uchar second,minute,hour; //秒,分,时
- uint nian,yue,ri; //年月日
- uchar alarm_h,alarm_m; //闹铃的时,分
- uchar clock[11]; //时间
- uchar alarm[11]; //闹铃
- uint year[10]; //年月日
- void delay1(uint t)//短延时函数
- {
- while(t--);
- }
- void delay2(uint t) //长延时函数
- {
- uint x,y;
- for(x=t;x>0;x--)
- for(y=110;y>0;y--);
- }
- void WriteCommand(uchar com) //写入指令
- {
- delay2(5);//操作前短暂延时,保证信号稳定
- lcd_E=0;
- lcd_RS=0;
- lcd_RW=0;
- P3=com;
- lcd_E=1;
- delay2(5);
- lcd_E=0;
- }
- void WriteData(uchar dat) //写数据
- {
- delay2(5); //操作前短暂延时,保证信号稳定
- lcd_E=0;
- lcd_RS=1;
- lcd_RW=0;
- P3=dat;
- lcd_E=1;
- delay2(5);
- lcd_E=0;
- }
- void handle_time() //显示时间
- {
- uchar i;
- clock[0]='C';
- clock[1]='L';
- clock[2]='O';
- clock[3]='C';
- clock[4]='K';
- clock[5]=0x20;//显示为空
- clock[6]=tab[hour/10];
- clock[7]=tab[hour%10];
- clock[8]=0x3a; //显示冒号
- clock[9]=tab[minute/10];
- clock[10]=tab[minute%10];
- WriteCommand(0x80+0x03);//光标指向第1行
- for(i=0;i<11;i++)// 显示
- {
- WriteData(clock[i]);
- delay2(10);
- }
- }
- void handle_year() //显示年月日
- {
- uchar i;
- year[0]=tab[nian/1000];
- year[1]=tab[(nian%1000)/100];
- year[2]=tab[((nian%1000)%100)/10];
- year[3]=tab[((nian%1000)%100)%10];
- year[4]='-';
- year[5]=tab[yue/10];
- year[6]=tab[yue%10];
- year[7]='-';
- year[8]=tab[ri/10];
- year[9]=tab[ri%10];
- WriteCommand(0x80+0x43); //第2行
- for(i=0;i<10;i++)
- {
- WriteData(year[i]);
- delay2(10);
- }
- }
- void InitLcd1602() //lcd1602初始化
- {
- uchar i;
- delay2(15);
- WriteCommand(0x38); //display mode
- WriteCommand(0x38); //display mode
- WriteCommand(0x38); //display mode
- WriteCommand(0x06); //显示光标移动位置
- WriteCommand(0x0c); //显示开及光标设置
- WriteCommand(0x01); //显示清屏
- WriteCommand(0x80+0x03);//光标指向第一行
- for(i=0;i<11;i++)// 显示
- {
- WriteData(time0[i]);
- delay2(10);
- }
- WriteCommand(0x80+0x43);//将光标移到第二行
- for(i=0;i<10;i++)// 显示
- {
- WriteData(mark1[i]);
- delay2(10);
- }
- }
- void handle_alarm() //显示闹钟
- {
- uchar i;
- alarm[0]='A';
- alarm[1]='L';
- alarm[2]='A';
- alarm[3]='R';
- alarm[4]='M';
- alarm[5]=0x20;//显示为空
- alarm[6]=tab[alarm_h/10];
- alarm[7]=tab[alarm_h%10];
- alarm[8]=0x3a; //显示冒号
- alarm[9]=tab[alarm_m/10];
- alarm[10]=tab[alarm_m%10];
- WriteCommand(0x80+0x03);//光标指向第1行第一个字符
- for(i=0;i<11;i++)// 显示
- {
- WriteData(alarm[i]);
- delay2(10);
- }
- }
- void clock_set_up() //时钟设置模式
- {
- while(flag_clock)
- {
- uchar i;
- WriteCommand(0x80+0x43);
- for(i=0;i<11;i++)// 显示
- {
- WriteData(mark2[i]);
- delay2(10);
- }
- if(key1==0) //时++
- {
- delay2(5);//延时消抖
- if(key1==0)
- {
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- while(!key1);//松手检测
- }
- if(key2==0)
- {
- delay2(5);//延时消抖
- if(key2==0)
- {
- if(hour!=0) //时减
- hour--;
- }
- while(!key2);//松手检测
- }
- if(key3==0) //分++
- {
- delay2(5);
- if(key3==0)
- {
- minute++;
- if(minute==60)
- {
- minute=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- while(!key3);
- }
- if(key4==0) //分--
- {
- delay2(5);
- if(key4==0)
- {
- if(minute!=0)
- minute--;
- }
- while(!key4);
- }
- handle_time(); //显示时间
- if(key7==0) //结束时钟设置
- {
- delay2(5);//延时消抖
- while(!key7);//松手检测
- flag_clock=0;
- }
- }
- }
- void alarm_set_up() //闹铃设置模式
- {
- while(flag_alarm)
- {
- uchar i;
- WriteCommand(0x80+0x43);
- for(i=0;i<11;i++)// 显示
- {
- WriteData(mark2[i]);
- delay2(10);
- }
- if(key1==0) //时++
- {
- delay2(5);//延时消抖
- if(key1==0)
- {
- alarm_h++;
- if(alarm_h==24)
- {
- alarm_h=0;
- }
- }
- while(!key1);//松手检测
- }
- if(key2==0) //时--
- {
- delay2(5);
- if(key2==0)
- {
- if(alarm_h!=0)
- alarm_h--;
- }
- while(!key2);
- }
- if(key3==0) //分++
- {
- delay2(5);//延时消抖
- if(key3==0)
- {
- alarm_m++;
- if(alarm_m==60)
- {
- alarm_m=0;
- alarm_h++;
- if(alarm_h==24)
- {
- alarm_h=0;
- }
- }
- }
- while(!key3);//松手检测
- }
- if(key4==0) //分--
- {
- delay2(5);
- if(key4==0)
- {
- if(alarm_m!=0)
- alarm_m--;
- }
- while(!key4);
- }
- handle_alarm(); //显示闹铃
- if(key7==0) //结束闹铃设置
- {
- delay2(5);//延时消抖
- while(!key7);//松手检测
- flag_alarm=0;
- }
- }
- }
- void alarm_sound() //声响
- {
- buzzer=0;
- delay2(50);
- buzzer=1;
- delay2(50);
- buzzer=0;
- delay2(50);
- buzzer=1;
- delay2(50);
- buzzer=0;
- delay2(50);
- }
- void year_set_up() //key4年月日设置
- {
- while(flag_year)
- {
- uchar i;
- WriteCommand(0x80+0x03);
- for(i=0;i<11;i++)// 显示
- {
- WriteData(mark2[i]);
- delay2(10);
- }
- if(key1==0) //年+
- {
- delay2(5);
- if(key1==0)
- {
- nian++;
- }
- while(!key1);
- }
- if(key2==0) //年--
- {
- delay2(5);
- if(key2==0)
- {
- if(key2==0)
- nian--;
- }
- while(!key2);
- }
- if(key3==0) //月+
- {
- delay2(5);
- if(key3==0)
- {
- yue++;
- if(yue==13)
- yue=1;
- }
- while(!key3);
- }
- if(key4==0) //月--
- {
- delay2(5);
- if(key4==0)
- {
- yue--;
- if(yue==0)
- yue=1;
- }
- while(!key4);
- }
- if(key5==0) //日++
- {
- delay2(5);
- if(key5==0)
- {
- if(yue==2||yue==4||yue==6||yue==9||yue==11)
- {
- ri++;
- if(yue==2)
- {
- if(ri==29)
- {
- if((nian%4==0&&nian%100!=0)||(nian%400==0));//闰年
- else //平年
- ri=1;
- }
- if(ri==30)
- ri=1;
- }
- if(ri==31)
- ri=1;
- }
- else
- {
- ri++;
- if(ri==32)
- ri=1;
- }
- }
- while(!key5);
- }
- if(key6==0) //日—-
- {
- delay2(5);
- if(key6==0)
- {
- ri--;
- if(ri==0)
- ri=1;
- }
- while(!key6);
- }
- handle_year();
- if(key7==0)
- {
- delay2(5);
- if(key7==0)
- flag_year=0;
- while(!key7);
- }
- }
- }
- void key7_set_up() //K7开关闹铃
- {
- uchar i;
- alarm_on++;
- if(alarm_on==2)
- {
- alarm_on=0;
- }
- if(alarm_on==1)
- {
- sound_on=1;
- WriteCommand(0x40);//写入自定义字符
- for(i=0;i<8;i++)
- {
- WriteData(tab2[i]);
- }
- WriteCommand(0x80+0x40);//显示自定义字符
- WriteData(0x00);
- delay2(10);
- }
- if(alarm_on==0)
- {
- sound_on=0;
- buzzer=1;
- WriteCommand(0x80+0x40);//显示闹铃标示为空
- WriteData(0x20);
- delay2(10);
- }
- }
-
- void main()
- {
- TMOD=0x10;
- IE=0x88;
- TH1=0x3c;
- TL1=0xb0;
- TR1=1;
- nian=2015;
- yue=1;
- ri=1;
- hour=23; //初始时间
- minute=58;
- second=0;
- display_alarm=0; //开机不显示闹铃
- alarm_h=0; // 初始闹铃
- alarm_m=0;
- alarm_on=0;
- sound_on=1; //开机默认无声响
- InitLcd1602();
- while(1)
- {
- uchar i;
- handle_year();
- handle_time();
- if(key1==0) //进入时钟设置模式
- {
- delay2(5);//延时消抖
- while(!key1);//松手检测
- flag_clock=1;
- clock_set_up();
- }
- if(key2==0) //设置闹铃模式
- {
- delay2(5);//延时消抖
- while(!key2);//松手检测
- flag_alarm=1;
- alarm_set_up();
- }
- if(key3==0) //显示闹铃,
- {
- delay2(5);//延时消抖
- while(!key3);//松手检测
- display_alarm=1;
- while(display_alarm)
- {
- handle_alarm();
- if(key7==0)
- {
- delay2(5);
- if(key7==0)
- display_alarm=0;
- while(!key7);
- }
- }
- }
- if(key4==0) //设置年月日模式
- {
- delay2(5);
- while(!key4);
- flag_year=1;
- year_set_up();
- }
- if(key7==0) //闹铃开关启
- {
- delay2(5);
- while(!key7);
- key7_set_up();
- }
- if((hour==alarm_h)&&(minute==alarm_m)) //闹铃时间是否到了
- {
- if(sound_on==1)
- {
- sound_on=0;
- alarm_sound();
- }
- }
- }
- }
- void ti_func() interrupt 3
- {
- TH1=0x3c;
- TL1=0xb0;
- if(num<20)
- {
- num=num+1;
- }
- if(num==20) //num为20时1S时间到
- {
- num=0;
- second=second+1;
- if(second==60) //1分钟时间到
- {
- second=0;
- minute=minute+1;
- if(minute==60) //1小时时间到
- {
- minute=0;
- hour=hour+1;
- if(hour==24) // 24小时到
- {
- hour=0;
- ri++;
- if(yue==2||yue==4||yue==6||yue==9||yue==11)
- {
- if(yue==2)
- {
- if(ri==29)
- {
- if((nian%4==0&&nian%100!=0)||(nian%400==0));//闰年
- else
- {
- ri=1;
- yue++;
- }
- }
- if(ri==30)
- {
- ri=1;
- yue++;
- }
- }
- if(ri==31)
- {
- ri=1;
- yue++;
- }
- }
- else
- {
- if(ri==32)
- {
- ri=1;
- yue++;
- if(yue==13)
- {
- nian++;
- yue=1;
- }
- }
- }
- }
- }
- }
- }
- }
复制代码 |