修改时间的按键没有作用 哪位大佬帮忙看一下?
单片机源程序如下:
- #include <reg51.h>
- #include "lcd1602.h"
- #include "ds1302.h"
- #include "ds18b20.h"
- #include "delay.h"
- #define uint unsigned int
- #define uchar unsigned char
- void play1();
- void play2();
- sbit key1=P3^3;
- sbit key2=P3^4;
- sbit key3=P3^5;
- sbit key4=P3^6;
- sbit key5=P2^4;
- sbit LED=P1^4;
- sbit LED1=P1^3;
- sbit Beef=P2^0;
- uchar sec;
- unsigned int Temp;
- unsigned int TMP;
- unsigned char flag=0;
- unsigned char flag1=0;
- unsigned char num,count,key4num,key5num;
- unsigned char shi=21,fen=55,miao=0;
- uchar code tone1[]={
- 115,115,102,115,86,91,115,115,102,115,77,86,
- 115,115,57,68,86,91,102,61,61,68,86,77,86,0};
- uchar code beat1[]={
- 3,1,4,4,4,8,3,1,4,4,4,8,
- 3,1,4,4,4,4,4,3,1,4,4,4,8,0
- };
- uchar code tone2[]={
- 77,91,77,57,68,57,77,77,115,102,91,102,115,102,
- 77,91,11,57,61,68,57,77,77,102,91,86,121,115,0};
- uchar code beat2[]={
- 2,1,1,4,2,2,4,2,1,1,2,1,1,8,
- 2,1,1,2,2,2,2,4,2,1,1,2,2,8,0
- };
- void play1()
- {
- uint i=0,j,k;
- while(beat1[i]!=0||tone1[i]!=0)
- {
- for(j=0;j<beat1[i]*50;j++)
- {
- Beef=~Beef;
- for(k=0;k<tone1[i];k++);
- }
- Delay_1ms();
- i++;
- }
- }
-
- void play2()
- {
- uint i=0,j,k;
- while(beat2[i]!=0||tone2[i]!=0)
- {
- for(j=0;j<beat2[i]*50;j++)
- {
- Beef=~Beef;
- for(k=0;k<tone2[i];k++);
- }
- Delay_1ms();
- i++;
- }
- }
- void display_temp();
- void delay(uint n)
- {
- uchar a,b,c,d;
- for(d=n;d>0;d--)
- for(c=1;c>0;c--)
- for(b=222;b>0;b--)
- for(a=12;a>0;a--);
- }
- void display_time()
- {
- LCD1602_num(2,7,BCD_hex(TIME[0]) );//秒
- LCD1602_char(2,6,':');
- LCD1602_num(2,4,BCD_hex(TIME[1])); //分
- LCD1602_char(2,3,':');
- LCD1602_num(2,1,BCD_hex(TIME[2])); //时
- LCD1602_num(1,7,BCD_hex(TIME[3]) );//日
- LCD1602_char(1,6,'/');
- LCD1602_num(1,4,BCD_hex(TIME[4])); //月
- LCD1602_char(1,3,'/');
- LCD1602_num(1,1,BCD_hex(TIME[6])); //年
- LCD1602_num(1,12,BCD_hex(TIME[5]-1));//周
- }
- void display_temp(unsigned int dat)
- {
- LCD1602_string(1,10,"W:");
- LCD1602_char(1,15,'0'+sec/10%10);
- LCD1602_char(1,16,'0'+sec%10);
- LCD1602_char(2,10,dat/100%10+'0');
- LCD1602_char(2,11,dat/10%10+'0');
- LCD1602_char(2,12,'.');
- LCD1602_char(2,13,dat%10+'0');
- LCD1602_char(2,14,'C');
- }
- void Time0_init()
- {
- TMOD=0x01; //定时器0工作在模式1,
- TH0=(65536-50000)/256; //高8位=60
- TL0=(65536-50000)%256; //低8位
- TR0=1; //开定时器0
- ET0=1; //使能定时器0中断
- EA=1; //开总中断
- }
- void display_nz()
- {
- LCD1602_num(2,7,miao);//秒
- LCD1602_char(2,6,':');
- LCD1602_num(2,4,fen); //分
- LCD1602_char(2,3,':');
- LCD1602_num(2,1,shi); //时
- }
-
- void main(void)
- {
- LCD1602_init(); //初始化
- // Ds1302InitTime();
- Init_DS18B20();//初始化ds1820
- Ds1302ReadTime();
- Readtemp();
- sec=60;
-
- while(1)
- {
- if(BCD_hex(TIME[1])==fen&&BCD_hex(TIME[2])==shi)
- {
- LED1=0;
- Beef=0;
- }
- else
- {
- LED1=1;
- Beef=1;
- }
- if(Temp>260&&Temp<300)
- {
- LED=0;
- }
- else
- {
- LED=1;
- }
- if(key5==0)
- {
- Delay_1ms();
- if(key5==0)
- {
- key5num++;
- if(key5num>=4)
- {
- key5num=0;
- }
- }
- }
- if(key5num==1)
- {
- delay(100) ;
- sec--;
- }
- if(key5num==2)
- {
- delay(30);
- play1();
- delay(3000);
- }
- if(key5num==3)
- {
- delay(30);
- play2();
- delay(3000);
- }
-
-
- if(flag==0) //不修改时间就读取时间显示时间
- {
- Ds1302ReadTime(); ; //读取时间
- display_time(); //显示时间
- }
- else //修改时间
- {
- if(key2==0)
- {
- Delay_1ms();
- if(key2==0 )
- {
- fen++;
- if(fen==60)
- fen=0;
-
- }
- }
- if(key3==0)
- {
- Delay_1ms();
- if(key3==0)
- {
- fen--;
- if(fen==-1)
- fen=59;
-
- }
- }
- // display_time();
- display_nz(); //显示修改当前时间
- }
- Temp=Readtemp();//读取温度
- display_temp(Temp); //显示温度
-
- if(key4==0)
- {
- Delay_1ms();
- if(key4==0)
- {
- key4num++;
- if(key4num>=6)
- {
- key4num=0;
- }
- }
- }
- if(key4num==1)
- {
- Delay_1ms();
- if(key2==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[1]);
- TMP++;
- hex_BCD(TIME[1]);
- }
- if(key3==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[1]);
- TMP--;
- hex_BCD(TIME[1]);
- }
- }
-
-
- if(key4num==2)
- {
- Delay_1ms();
- if(key2==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[2]);
- TMP++;
- hex_BCD(TIME[2]);
- }
- if(key3==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[2]);
- TMP--;
- hex_BCD(TIME[2]);
- }
- }
-
-
- if(key4num==3)
- {
- Delay_1ms();
- if(key2==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[3]);
- TMP++;
- hex_BCD(TIME[3]);
- }
- if(key3==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[3]);
- TMP--;
- hex_BCD(TIME[3]);
- }
- }
-
-
-
- if(key4num==4)
- {
- Delay_1ms();
- if(key2==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[4]);
- TMP++;
- hex_BCD(TIME[4]);
- }
- if(key3==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[4]);
- TMP--;
- hex_BCD(TIME[4]);
- }
- }
-
- if(key4num==5)
- {
- Delay_1ms();
- if(key2==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[6]);
- TMP++;
- hex_BCD(TIME[6]);
- }
- if(key3==0)
- {
- Delay_1ms();
- TMP=BCD_hex(TIME[6]);
- TMP--;
- hex_BCD(TIME[6]);
- }
- }
- }
- if(key1==0) //++1s
- {
- Delay_1ms();
- if(key1==0)
- {
- flag=!flag;
- Ds1302InitTime();
- }
- }
- /*888888888888888888888888888888888888888888*/
- if(key4==0) //++1s
- {
- Delay_1ms();
- if(key4==0)
- {
- flag1=!flag1;
- }
- }
- }
-
-
- /*888888888888888888888888888888888888888888*/
-
复制代码
所有资料51hei提供下载:
电子时钟.zip
(151.06 KB, 下载次数: 8)
|