按键控制两位数码管加减

关于按键方法的原帖地址: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	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值