|
- #include<reg51.h>
- #include<lcd1602.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key0=P1^0;
- sbit key1=P1^1;
- sbit key2=P1^2;
- sbit key3=P1^3;
- sbit buzzer=P2^5;
- sbit relay=P2^3;//buzzer & relay均为低电平工作
- sbit led=P2^4;
- sbit lamp=P2^5;
- uchar code date[]=" H.I.T. CHINA ";
- uchar code time[]=" ALARM 23:58 ";
- uchar code bell[]=" ALARM : ";
- uchar second=40,minute=58,hour=23,counter=0;
- uchar bellminute=0,bellhour=0;
- uchar buzzerflag,clockflag;//若标志为1,则工作
- void ledshow()//led闪烁,标示程序开始运转
- {
- uchar i;
- for(i=0;i<=100;i++)
- {
- led=~led;
- delay(5);
- }
- }
- uchar keyscan()//键盘扫描
- {
- uchar keyvalue,temp;
-
- keyvalue=0;
- P1=0xff;
- temp=P1;
- if(~(P1&temp))
- {
- switch(temp)
- {
- case 0xfe:
- keyvalue=1;
- break;
- case 0xfd:
- keyvalue=2;
- break;
- case 0xfb:
- keyvalue=3;
- break;
- case 0xf7:
- keyvalue=4;
- break;
- default:
- keyvalue=0;
- break;
- }
- }
- return keyvalue;
- }
- void clock_init()//lcd时钟写入初始化
- {
- uchar i,j;
- for(i=0;i<16;i++)
- {
- write_data(date[i]);[/i][i]
- }
- write_com(0x80+0x40);
- for(j=0;j<16;j++)
- {
- write_data(time[j]);
- }
- }
- void timer0_init()//中断初始化
- {
- EA=1;
- ET0=1;
- TMOD=0x01;
- TH0=0xd8;//每10m中断一次
- TL0=0xf0;
- TR0=1;
- }
- void clock_write()//时钟实时写入
- {
- write_sfm(0x49,hour);
- write_sfm(0x4c,minute);
- }
- void key_menu()//总键盘扫描函数
- {
- unsigned char keyvalue_menu=0,keyvalue_change=0,i,j;
- keyvalue_menu=keyscan();
- if(keyvalue_menu)
- {
- if(keyvalue_menu==1)//按键1,走时停止,开始更改时钟值
- {
- while(~key0);//防抖
- TR0=0;
- do{
- keyvalue_change=keyscan();
- if(keyvalue_change==1)//按下1键,更改小时
- {
- while(~key0);
- if(hour<23)
- {
- hour++;
- }
- else
- {
- hour=0;
- }
- }
- else if(keyvalue_change==2)//按下2键,更改分钟
- {
- while(~key1);
- if(minute<59)
- {
- minute++;
- }
- else
- {
- minute=0;
- }
- }
- else if(keyvalue_change==4)//按下4键,更改响铃方式
- {
- while(~key3);
- buzzerflag=~buzzerflag;
- if(buzzerflag)//闹铃运转,3声
- {
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- }
- else//闹铃关闭,1声
- {
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- }
- }
- write_sfm(0x49,hour);
- write_sfm(0x4c,minute);
- }while(keyvalue_change!=3);//按下3键,从新开始走时
- while(~key2==0);
- TR0=1;//调整时间后从新开始走时
- }
- else if(keyvalue_menu==2)//按键2,走时继续,显示闹钟值
- {
- while(~key1);//防抖
- do{
- for(i=0;i<16;i++)
- {
- write_data(date[i]);[/i][i]
- }
- write_com(0x80+0x40);
- for(j=0;j<16;j++)
- {
- write_data(bell[j]);
- }
- write_sfm(0x49,bellhour);
- write_sfm(0x4c,bellminute);
- keyvalue_change=keyscan();
- }while(keyvalue_change!=3);//未完成设置前始终显示当前闹钟
- while(~key2);
- }
- else if(keyvalue_menu==3)//按键3,走时继续,设置闹钟值
- {
- while(~key2);
- do{
- if(keyvalue_change==1)//按下1键,更改小时
- {
- while(~key0);
- if(bellhour<23)
- {
- bellhour++;
- }
- else
- {
- bellhour=0;
- }
- }
- else if(keyvalue_change==2)//按下2键,更改分钟
- {
- while(~key1);
- if(bellminute<59)
- {
- bellminute++;
- }
- else
- {
- bellminute=0;
- }
- }
- else if(keyvalue_change==4)//按下4键,更改响铃方式
- {
- while(~key3);
- buzzerflag=~buzzerflag;
- if(buzzerflag)//闹铃运转,3声
- {
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- }
- else//闹铃关闭,1声
- {
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- }
- }
- for(i=0;i<16;i++) //实时显示修改结果
- {
- write_data(date[i]);[/i][i]
- }
- write_com(0x80+0x40);
- for(j=0;j<16;j++)
- {
- write_data(bell[j]);
- }
- write_sfm(0x49,bellhour);
- write_sfm(0x4c,bellminute);
- keyvalue_change=keyscan();
- }while(keyvalue_change!=3);//按下3键,重新开始显示走时
- while(~key2);//防抖
- }
- else if(keyvalue_menu==4)//按键4,关闭/开启闹钟
- {
- while(~key2);
- clockflag=!clockflag;
- if(clockflag)//闹钟运转,3声
- {
- [color=#f4a460] buzzer=0;[/color]
- [color=#f4a460] delay(100);[/color]
- [color=#f4a460] buzzer=1;[/color]
- [color=#f4a460] delay(100);[/color]
- [color=#f4a460] buzzer=0;[/color]
- [color=#f4a460] delay(100);[/color]
- [color=#f4a460] buzzer=1;[/color]
- [color=#f4a460] delay(100);[/color]
- [color=#f4a460] buzzer=0;[/color]
- [color=#f4a460] delay(100);[/color]
- [color=#f4a460] buzzer=1;[/color]
- [color=#f4a460] delay(100);[/color]
- }
- else//闹钟关闭,1声
- {
- buzzer=0;
- delay(100);
- buzzer=1;
- delay(100);
- }
- }
- }
- }
- void alarm_clock()
- {
- if((minute==bellminute)&&(hour==bellhour)&&(clockflag))//闹钟打开且达到预设时间时启动闹钟
- {
- if(buzzerflag)//如果闹铃打开
- {
- relay=0;
- buzzer=0;
- }
- else
- {
- relay=0;
- buzzer=1;
- }
- }
- else
- {
- relay=1;
- buzzer=1;
- }
- }
- void main()
- {
- ledshow();//程序启动,led闪烁
- init1602();//lcd初始化
- clock_init();//时钟初始化
- timer0_init();//中断初始化
- while(1)
- {
- clock_write();
- key_menu();
- alarm_clock();
- }
- }
- void timer0() interrupt 1
- {
- if(counter<100) //判断时间变换问题
- {
- counter++;
- }
- else
- {
- counter=0;
- led=~led;
- if(second<59)
- {
- second++;
- }
- else
- {
- second=0;
- if(minute<59)
- {
- minute++;
- }
- else
- {
- minute=0;
- if(hour<23)
- {
- hour++;
- }
- else
- {
- hour=0;
- }
- }
- }
- }
- TH0=0xd8;//重新加载中断值
- TL0=0xf0;
- TR0=1;
- }
复制代码
|
|