给个参考程序:
#include <reg52.h>
#include <absacc.h>
#include <stdio.h>
sbit p10=P1^0;
sbit p11=P1^1;
sbit p12=P1^2;
#define UP 20
#define DOWN 30
#define STOP 40
code char CharTab[128] ={0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xfe,0xb0,0xed,0xf9,0xb3,0xdb,0xdf,0xf0,// 01234567
0xff,0xfb,0xff,0xff,0xff,0xff,0xff,0xff,// 89
0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0x01,0x80,0xff,// -./
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,// 01234567
0x7f,0x6f,0x11,0x1c,0x36,0x09,0x16,0x17,// 89 =
0xff,0x77,0x1f,0x4e,0x3d,0x4f,0x47,0xff,// AbCdEFG
0x37,0xff,0x38,0x07,0x0e,0xff,0xff,0x7e,// HI L NO
0x67,0xff,0xff,0x5b,0x46,0x3e,0xff,0xff,// P RSTU
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x77,0x1f,0x0d,0x3d,0x6f,0x47,0x7b,// AbcdEFg
0x17,0xff,0x38,0x4e,0x0e,0xff,0x15,0x1d,// hi lmno
0xff,0xff,0x05,0x5b,0x0f,0x1c,0xff,0xff,// rStu
0xff,0x3b,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char temp;
unsigned char COUNTER;
unsigned int key_no;//按键次数
unsigned int sju;//20m秒计数
idata char DisBuf[16];
void Ext_int0_INTT() interrupt 0
{
key_no++; //计数值加1
}
void tm0_INTT() interrupt 1
{
COUNTER++; //计数值加1
}
/********************************************************************
主函数
*********************************************************************/
main()
{
//long K;
char bb;
temp=0;
COUNTER=0;
TMOD=0x02; //定时器0在模式1下工作
TL0=0xbf; //定时器每200us产生一次溢出
TH0=0xbf; //自动重装的值
//显示值
/*K=87654321;
for(bb=2;bb<8;){DisBuf[bb]=0x20;bb++;}//清缓存
sprintf(DisBuf,"%lu",K);*/
IT0=1;
EX0=1;//
ET0=1; //使能定时器0中断
EA=1; //使能总中断
TR0=1; //开始计时
while(1)
{
if(COUNTER==100)//定时到 100-20ms,
{
COUNTER=0;
sju++;if(sju>3000){EX0=0;}//sju=0;
if(sju==0){key_no=0;}
for(bb=0;bb<8;){DisBuf[bb]=0x20;bb++;}//清缓存
sprintf(DisBuf,"%lu",(long)key_no);
temp++;if(temp>=0x08){temp=0;}
P0=temp;//位选
P2=CharTab[DisBuf[temp]];//列值
}
}
}
|