51单片机怎么用矩阵键盘让数码管加一减一,就是每两个键盘控制一个数
51单片机怎么用矩阵键盘让数码管加一减一,就是每两个键盘控制一个数码管,一个让它加一一个让它减一...
51单片机怎么用矩阵键盘让数码管加一减一,就是每两个键盘控制一个数码管,一个让它加一一个让它减一
展开
1个回答
展开全部
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P1^0;
sbit key2=P1^1;
uchar num=0;
//共阴数码管七段码
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77};
void Delay1ms(uint i) //1ms延时程序
{
uint j;
for(;i>0;i--)
{
for(j=0;j<164;j++)
{;}
}
}
void main(void )//主程序
{
while(1)
{
if(key1==0)//判断按键1是否按下
{
Delay1ms(10);//延时消抖
if(key2==0)
{
while(key1==0);
num++;
if(num==10)num=0;//等待按键抬起
}
}
if(key2==0)//判断按键2是否按下
{
Delay1ms(10);//延时消抖
if(key2==0)
{
while(key2==0);
num--;
if(num==00)num=9;//等待按键抬起
}
}
P0=table[num];
}
}
#define uchar unsigned char
#define uint unsigned int
sbit key1=P1^0;
sbit key2=P1^1;
uchar num=0;
//共阴数码管七段码
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77};
void Delay1ms(uint i) //1ms延时程序
{
uint j;
for(;i>0;i--)
{
for(j=0;j<164;j++)
{;}
}
}
void main(void )//主程序
{
while(1)
{
if(key1==0)//判断按键1是否按下
{
Delay1ms(10);//延时消抖
if(key2==0)
{
while(key1==0);
num++;
if(num==10)num=0;//等待按键抬起
}
}
if(key2==0)//判断按键2是否按下
{
Delay1ms(10);//延时消抖
if(key2==0)
{
while(key2==0);
num--;
if(num==00)num=9;//等待按键抬起
}
}
P0=table[num];
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询