#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P1^4;
sbit key2=P1^5;
sbit key3=P1^6;
sbit key4=P1^7;
sbit tdis=P2^6;
sbit sdis=P2^7;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90};
void delayms(uint);
uchar numt0,num;
/**************************************************
ÏÔʾ¹¦Äܺ¯Êý
***************************************************/
void display(uchar numdis)
{
uchar tendis,sigledis;
tendis=numdis/10;
sigledis=numdis%10;
//
tdis=0;
P0=table[tendis];
delayms(5);
tdis=1;
P0=0xff;
//
sdis=0;
P0=table[sigledis];
delayms(5);
sdis=1;
P0=0xff;
}
/**************************************************
º¯Êý¹¦ÄÜ£ºÑÓʱº¯Êý£¬ÑÓʱһ¶Îʱ¼ä
***************************************************/
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
/**************************************************
³õʼ»¯º¯Êý
***************************************************/
void init()
{
TMOD=0x01;
TH0=(65536-45827)/256;
TL0=(65536-45827)%256;
IE=0x82;
}
/**************************************************
¼üÅÌɨÃ躯Êý
***************************************************/
void keyscan()
{
if(key1==0)
{
delayms(10);
if(key1==0)
{
num++;
if(num==60)
num=0;
while(!key1);
}
}
//
if(key2==0)
{
delayms(10);
if(key2==0)
{
if(num==0)
num=60;
num--;
while(!key2);
}
}
//
if(key3==0)
{
delayms(10);
if(key3==0)
{
num=0;
while(!key3);
}
}
//
if(key4==0)
{
delayms(10);
if(key4==0)
{
while(!key4);
TR0=~TR0;
}
}
}
/**************************************************
Ö÷º¯Êý
***************************************************/
void main()
{
init();
while(1)
{
keyscan();
display(num);
}
}
/**************************************************
ÖжϺ¯Êý
***************************************************/
void T0_time()interrupt 1
{
TH0=(65536-45827)/256;
TL0=(65536-45827)%256;
numt0++;
if(numt0>=20)
{
numt0=0;
num++;
if(num>=60)
num=0;
}
}
這個(gè)是根據(jù)郭老師教程的程序改的,現(xiàn)在這個(gè)程序也是正常的:K1顯示數(shù)字加,K2顯示數(shù)字減,K3顯示數(shù)字清0,K4開中斷和關(guān)中斷,開中斷時(shí)顯示00-59循環(huán)。現(xiàn)在我有一點(diǎn)不明白,響應(yīng)中斷函數(shù)應(yīng)該是60秒,在這60秒按按鍵應(yīng)該是沒有反應(yīng)的,但燒進(jìn)學(xué)習(xí)板按按鍵隨時(shí)按隨時(shí)有,是不是我分析中斷程序有錯(cuò)誤,請(qǐng)各位老師解答。
|