51单片机--某系统有K1和K2,一个数码管,当按下K1数码管加1,按下K2数码管减1

题目:某系统有K1和K2,一个数码管,当按下K1数码管加1,按下K2数码管减1

程序如下:

#include 
#define uint unsigned int
#define uchar unsigned char
#define LED_seg P0
#define LED_com P2
sbit key1=P3^1;
sbit key2=P3^0;

static int i=0;
static int j=0;

uchar code smgduan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
					0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//显示0~F的值
void delay(uint i)
{
	while(i--);
}

void main()
{
	LED_com=0x1c;//使用第一个数码管

	while(1)
	{	
		delay(1000);
	 	if(key1==0)
		{
			delay(1000);//按键消抖
			if(key1==0)
			{
				i++;
				if(i==16) //显示16进制 即0-15;
					i=0;
				LED_seg=smgduan[i];
				delay(50000);	
			}
			while(key1==0);//防止一直按着key1
		}
	
		delay(1000);
	 	if(key2==0)
		{
			delay(1000);//按键消抖
			if(key2==0)
			{
				i--;
				if(i<0) //小于0则退回15/F;
					i=15;
				LED_seg=smgduan[i];
				delay(50000);	
			}
			while(key1==0);//防止一直按着key2
		}
	}
	return 0;
}

 

你可能感兴趣的:(51单片机--某系统有K1和K2,一个数码管,当按下K1数码管加1,按下K2数码管减1)