clock程序

#include<reg52.h>
#include "./delay/delay.h"
#define KEYPORT P1
#define LCD_WRITE_DATA 1
#define LCD_WRITE_COM 0
#define LCDPORT P0
unsigned char s1 = 8;
unsigned char s3 = 0;
unsigned char s2 = 0;
unsigned char shi = 0;
unsigned char fen = 0;
unsigned char miao = 0;
unsigned char ch[9]={0};
unsigned char flag1 = 0;
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
sbit beep = P3^7;






unsigned char key_scan(void)
{
unsigned char keyvalue;
KEYPORT = 0xf0;
if(0xf0 != KEYPORT)
{
delay_ms(10);
if(0xf0 != KEYPORT)
{
KEYPORT = 0xfe;
if(0xfe != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0;
keyvalue += 0x0e;
while(0xfe != KEYPORT);
delay_ms(10);
while(0xfe != KEYPORT);
return keyvalue;
}
 KEYPORT = 0xfd;
if(0xfd != KEYPORT)
{
keyvalue = KEYPORT;
while(0xfd != KEYPORT);
delay_ms(10);
while(0xfd != KEYPORT);
return keyvalue;
}
KEYPORT = 0xfb;
if(0xfb != KEYPORT)
{
keyvalue = KEYPORT;
while(0xfb != KEYPORT);
delay_ms(10);
while(0xfb != KEYPORT);
return keyvalue;
}
KEYPORT = 0xf7;
if(0xf7 != KEYPORT)
{
keyvalue = KEYPORT;
while(0xf7 != KEYPORT);
delay_ms(10);
while(0xf7 != KEYPORT);
return keyvalue;
}
}
}
return 0xff;
}
unsigned char key_pro()
{
unsigned char keynum;
keynum = key_scan();
switch(keynum)
{
case 0x7e:return 1;break;
case 0xbe:return 2;break;
case 0xde:return 3;break;
case 0xee:return 10;break;
case 0x7d:return 4;break;
case 0xbd:return 5;break;
case 0xdd:return 6;break;
case 0xed:return 11;break;
case 0x7b:return 7;break;
case 0xbb:return 8;break;
case 0xdb:return 9;break;
case 0xeb:return 12;break;
case 0x77:return 0;break;
case 0xb7:return 13;break;
case 0xd7:return 14;break;
case 0xe7:return 15;break;
default:return 0;break;
}
}




void lcd1602_write_data(unsigned char byte,unsigned char flag)
{
if(flag)
{
RS = 1;
}
else
{
RS = 0;
}
RW = 0;
E = 1;
LCDPORT = byte;
delay_us(5);
E = 0;
}


void lcd_init()
{
delay_ms(15);
lcd1602_write_data(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd1602_write_data(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd1602_write_data(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd1602_write_data(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd1602_write_data(0x08,LCD_WRITE_COM);
delay_ms(5);
lcd1602_write_data(0x01,LCD_WRITE_COM);
delay_ms(5);
lcd1602_write_data(0x06,LCD_WRITE_COM);
delay_ms(5);
lcd1602_write_data(0x0c,LCD_WRITE_COM);
}
void lcd_display()
{
lcd1602_write_data(0x80+4,LCD_WRITE_COM);
lcd1602_write_data((s3/10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data((s3%10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data(':',LCD_WRITE_DATA);
lcd1602_write_data((s2/10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data((s2%10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data(':',LCD_WRITE_DATA);
lcd1602_write_data((s1/10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data((s1%10)+0x30,LCD_WRITE_DATA);


}
void timer0_init(void)
{
 EA = 1;
 TMOD |= 0x01;
 TH0 = (65536 -20000) / 256;
 TL0 = (65536 -20000) % 256; 
 ET0 = 1;  
 TR0 = 1;  



void timer0_isr() interrupt 1
{
//unsigned char key;
 static unsigned char i = 0;
 
    TH0 = (65536 - 20000) / 256;
 TL0 = (65536 - 20000) % 256;   
if(i == 0)
{
beep = ~beep;
}
 i++;
 if(50 == i)
{
 i = 0;


        s1++;
 if(60 == s1)
{   


 s1 = 0;
 s2++;
 if(60 == s2)
{
 s2 = 0;
 s3++;
 if(24 == s3)
{
 s3 = 0;
}
}
}
    }

}
void alarm()
{
if((s1 == miao)&&(s2 == fen)&&(s3 == shi))
{
beep = ~beep;
delay_ms(20);
beep = 0;
}
}




void main()
{
unsigned char key1;
timer0_init();
lcd_init();
while(1)
{



key1 = key_pro();


if(key1 == 10)
{
while(10 != key1);
delay_ms(10);
while(10 != key1);
TR0 = ~TR0;  
}
if(key1 == 1)
{
while(1 != key1);
delay_ms(10);
while(1 != key1);
s3++;

if(s3 == 24)
{
 s3 = 0;
}

}
if(key1 == 2)
{
while(2 != key1);
delay_ms(10);
while(2 != key1);
s2++;
if(s2 == 60)
{
 s2 = 0;
}
 
}
if(key1 == 3)
{
while(3 != key1);
delay_ms(10);
while(3 != key1);
s1++;
if(s1 >= 60)
{
 s1 = 0;
}

}
if(key1 == 4)
{
while(4 != key1);
delay_ms(10);
while(4 != key1);

shi++;
if(shi == 24)
{
shi = 0;
}
}
if(key1 == 5)
{
while(5 != key1);
delay_ms(10);
while(5 != key1);
fen++;
if(fen == 60)
{
fen = 0;
}
}
if(key1 == 6)
{
while(6 != key1);
delay_ms(10);
while(6 != key1);
miao++;
if (miao == 60)
{
miao = 0;
}
}

lcd1602_write_data(0x80+0x40+4,LCD_WRITE_COM);
lcd1602_write_data((shi/10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data((shi%10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data(':',LCD_WRITE_DATA);
lcd1602_write_data((fen/10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data((fen%10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data(':',LCD_WRITE_DATA);
lcd1602_write_data((miao/10)+0x30,LCD_WRITE_DATA);
lcd1602_write_data((miao%10)+0x30,LCD_WRITE_DATA);
alarm();

lcd_display();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值