#include 源碼下載
#include
#define uchar unsigned char
#define uint unsigned int
uchar secs = 0;
uchar minutes = 1;
uchar LED,num1,num2,num = 4;
uchar flag1 = 1,flag2 = 0;
uchar m_secs = 24;
uchar key_value = 0;
uchar table[13] = {1,2,3,4,5,6,7,8,9,0,1,2,3};
uchar code LedNum[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar scan_kay(void);
void delay(uchar z);
void INT0_Initial(void)
{
EX0 = 1;
IT0 = 1;
EA = 1;
P1 = 0xF0;
}
本文引用地址:http://m.butianyuan.cn/article/201611/319673.htmvoid INT0_Interrupt(void) interrupt 0
{
uchar temp;
temp = scan_kay();
if(temp!=0x7f)
{
key_value = temp;
}
P1 = 0xF0;
}
void Time0_Initial(void)
{
TMOD = 0x01;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
ET0 = 1;
TR0 = 1;
EA = 1;
PT0 = 1;
PX0 = 0;
}
volatile uchar cnt = 0;
void Time0_Interrupt(void) interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
cnt ++;
minutes=11;
if(cnt==15)
{
cnt = 0;
secs --;
m_secs --;
if(flag2==1)
{
LED = !LED;
}
if(flag2==0)
{
LED = 0;
}
}
if(secs>60)
{
secs = 59;
minutes --;
}
if(m_secs>24)
{
m_secs = 23;
if(flag1==1)
{
flag2 = 1;
}
if(flag1==0)
{
flag2 = 0;
}
}
if(minutes>60)
{
minutes = 11;
m_secs = 24;
num ++;
}
if(num==4)
{
num = 1;
}
}
void HEXTOBCD(void)
{
table[0] = LedNum[num];
table[1] = LedNum[m_secs/10];
table[2] = LedNum[m_secs%10];
table[3] = LedNum[minutes/10];
table[4] = LedNum[minutes%10];
table[5] = LedNum[secs/10];
table[6] = LedNum[secs%10];
table[7] = LedNum[num1/100];
table[8] = LedNum[num1%100/10];
table[9] = LedNum[num1%10];
table[10] = LedNum[num2/100];
table[11] = LedNum[num2%100/10];
table[12] = LedNum[num2%10];
}
void Display(uchar *str)
{
uchar i,temp = 0;
for(i=0;i<13;i++)
{
P0 = 0x00;
_nop_();
P2 = temp+i;
P0 = str[i];
delay(2);
}
}
uchar i,j;
void delay(uchar z)
{
for(i=1;i for(j=1;j<110;j++);
}
void main()
{
INT0_Initial();
Time0_Initial();
delay(10);
LED = 0;
while(1)
{
switch(key_value)
{
case 12:
{
num1 ++;key_value = 0;
flag1 = 0;
if(num1>255)
{
num1 = 0;
}
break;
}
case 13:
{
num1 --;key_value = 0;
flag1 = 0;
if(num1>255)
{
num1 = 255;
}
break;
}
case 14:
{
num2 ++;key_value = 0;
flag1 = 0;
if(num2>255)
{
num2 = 0;
}
break;
}
case 15:
{
num2 --;key_value = 0;
flag1 = 0;
if(num2>255)
{
num2 = 255;
}
break;
}
case 1:
{
TR0 = 1;
key_value = 0;
break;
}
case 2:
{
num1 = 0;num2 = 0;
num = 4;
secs = 0;minutes = 12;
m_secs = 24;
key_value = 0;
TR0 = 1;
break;
}
case 3:
{
TR0 = 0;
key_value = 0;
break;
}
default:
{
break;
}
}
HEXTOBCD();
delay(5);
Display(table);
delay(10);
}
}
uchar scan_key(void)
{
uchar i,key_value;
P1 = 0xFF;
for(i=0;i<4;i++)
{
P1 = ~(1< key_value = P1&0xF0;
if(key_value!=0xF0)
{
delay(20);
key_value = P1&0xF0;
if(key_value!=0xF0)
{
key_value &= 0xF0;
switch(key_value)
{
case 0x70: key_value = 15-(3-i)*4;break;
case 0xb0: key_value = 14-(3-i)*4;break;
case 0xd0: key_value = 13-(3-i)*4;break;
case 0xe0: key_value = 12-(3-i)*4;break;
default: key_value = 0x7f;
}
return key_value;
}
}
P1 = 0xFF;
}
return 0x7f;
}
uchar scan_kay(void)
{
uchar key_va1,key_va2;
key_va1 = scan_key();
if(key_va1==0x7f)
{
return 0x7f;
}
do
{
key_va2 = scan_key();
}while(key_va1==key_va2);
return key_va1;
}
評論