关于按键方法的原帖地址:http://www.amobbs.com/thread-4308630-1-1.html
//本文按键未经消抖处理,有了好方法再来上传。
#include "reg51.h"
sbit we = P2^7;
sbit du = P2^6;
typedef unsigned char uchar8;
typedef unsigned int uint16;
uchar8 code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77};//段码
uchar8 i = 0;
char buf[2] = {0};
bit SystemTime2ms = 0;//系统2MS时标
void Smg_Display(char* Smg_Cnt)
{
buf[0] = *Smg_Cnt/10;
buf[1] = *Smg_Cnt%10;
if(SystemTime2ms == 1)//2MS定时时间到
{
SystemTime2ms = 0;//2MS标志清零
P0 = ~(0x01 << i);
we = 1;//打开位选
we = 0;//关闭位选
P0 = table[buf[i]];
du = 1;//打开段选
du = 0;//关闭段选
if(++i > 1)
{
i = 0;
}
}
}
void T0_Init(void)//定时器初始化函数
{
TMOD = 0X01;
TH0 = (65536 - 2000)/256;//设置定时器初值
TL0 = (65536 - 2000)%256;
TR0 = 1;//打开定时器0
ET0 = 1;//打开定时器0中断
EA = 1;//开总中断
}
void Key_Read(uchar8 *Trg,uchar8 *Cont)
{
uchar8 ReadData = ((P3 ^ 0xff)&0x0f);
*Trg = ReadData & (ReadData ^ (*Cont));
*Cont = ReadData;
}
void Key_Process(void)
{
static uchar8 Trg = 0,Cont = 0;
static char Smg_Cnt = 0;
Key_Read(&Trg,&Cont);
switch(Trg)
{
case 0x01:
{
if(++Smg_Cnt > 99)
{
Smg_Cnt = 0;
}
}
break;
case 0x02:
{
if(--Smg_Cnt < 0)
{
Smg_Cnt = 99;
}
}
break;
}
Smg_Display(&Smg_Cnt);
}
void main()
{
T0_Init(void);
while(1)
{
Key_Process();
}
}
void Time0(void)interrupt 1
{
TH0 = (65536 - 2000)/256;//重装定时器初值
TL0 = (65536 - 2000)%256;
SystemTime2ms = 1;//系统2MS时标置1
}