c51单片机怎么用两个按键控制两个数码管的加减,加到99就好,求程序
展开全部
#include <reg51.h>
void delay(unsigned int i); //这个函数自己写出来
int key = 0;
sbit key1 = P?? ;//哪个管教不知道
sbit key2 = P/?;
sbit ge = P??;
sbit shi = P??;
//此表为 LED 的字模, 共阴数码管 0-9 -
unsigned char code tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
void keykey()
{
if(key1 == 1)//默认高电平是按键按下
{
delay(5);
if(key1 == 1)
{
key++;
}
}
else if(key2 == 1)//默认高电平是按键按下
{
delay(5);
if(key2 == 1)
{
key--;
}
}
if(key >99) //防止越界 也可以用与的方式写 key &= 0x63;这句话就能顶了下面俩判断
key = 0;
else if(key < 0)
key = 99;
}
void display(int x)
{
P0 = tap[x%10];
ge = 1;
delay(10);
ge = 0;
delay(10);
P0 =tap[x/10];
shi = 1;
delay(10);
shi = 0;
delay(10);
}
void main(void)
{
while(1)
{
display(key);
keykey();
}
}
void delay(unsigned int i); //这个函数自己写出来
int key = 0;
sbit key1 = P?? ;//哪个管教不知道
sbit key2 = P/?;
sbit ge = P??;
sbit shi = P??;
//此表为 LED 的字模, 共阴数码管 0-9 -
unsigned char code tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
void keykey()
{
if(key1 == 1)//默认高电平是按键按下
{
delay(5);
if(key1 == 1)
{
key++;
}
}
else if(key2 == 1)//默认高电平是按键按下
{
delay(5);
if(key2 == 1)
{
key--;
}
}
if(key >99) //防止越界 也可以用与的方式写 key &= 0x63;这句话就能顶了下面俩判断
key = 0;
else if(key < 0)
key = 99;
}
void display(int x)
{
P0 = tap[x%10];
ge = 1;
delay(10);
ge = 0;
delay(10);
P0 =tap[x/10];
shi = 1;
delay(10);
shi = 0;
delay(10);
}
void main(void)
{
while(1)
{
display(key);
keykey();
}
}
追问
这个程序编译出来很多错误啊
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询