标题: 求帮忙看看这个程序这样写可以吗? [打印本页]

作者: hmgqljz111    时间: 2017-9-4 21:12
标题: 求帮忙看看这个程序这样写可以吗?
P0驱动数码管,按键1按下时,数值加1,到9停止;按键2按下时,在当前数值上减一,到0停止。请帮忙看看这样写合适吗谢谢。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P2^0;
sbit key2=P2^1;
uchar num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void delay(uint);
void keyscan();
void main()
  {
   P1=0xfe;
num=5;
P0=table[num];
while(1)
   {
    keyscan();
  P0=table[num];
   }

  }
void delay(uint z)
   {
     uint x,y;
  for(x=z;x>0;x--)
    for(y=110;y>0;y--);
   }
void keyscan()
   {
     if(key1==0)
  delay(10);
  if(key1==0)
    {
      
      if(num<=8)
    num++;
   while(!key1);
    }
   if(key2==0)
   delay(10);
   if(key2==0)
     {
   
   if(num>=1)
    num--;  
   while(!key2);
  }
   }

作者: hmgqljz111    时间: 2017-9-4 21:13
通电扣数码管先显示5,然后在5的基础上加1或者减1显示
作者: 15249798685    时间: 2017-9-5 15:16
烧写进单片机看看,没看出毛病
作者: gaochang000000    时间: 2017-9-5 15:34
有几处问题哦,if语句。
作者: 中国红    时间: 2017-9-5 15:39
你可以用仿真proteus看看,应该是可以的
作者: wulin    时间: 2017-9-5 16:28
if(key1==0),if(key2==0)下面少了花括号,delay起不到消抖作用。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P2^0;
sbit key2=P2^1;
uchar num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void delay(uint);
void keyscan();
void main()
{
//        P1=0xfe;
        num=5;
        P0=table[num];
        while(1)
        {
                keyscan();
                P0=table[num];
        }
}
void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
void keyscan()
{
        if(key1==0)
        {
                delay(10);
                if(key1==0)
                {
                        if(num<9)
                                num++;
                        while(!key1);
                }
        }
        if(key2==0)
        {
                delay(10);
                if(key2==0)
                {
                        if(num>0)
                        num--;  
                        while(!key2);
                }
        }
}
作者: 鄢洪伟    时间: 2017-9-5 19:53
烧进单片机看看 能实现你的要求 但是有些语句格式不太规范 再改改
作者: hmgqljz111    时间: 2017-9-6 14:21
谢谢各位。刚开始学单片机以后还要向大家多学习
作者: kokocc    时间: 2017-9-6 14:44
在单片机调试下程序,看下结果现象
作者: kokocc    时间: 2017-9-6 14:46
hmgqljz111 发表于 2017-9-6 14:21
谢谢各位。刚开始学单片机以后还要向大家多学习

推荐一份资料【手把手教你学51单片机】很经典,对新手很有借鉴性,希望能够帮到你
作者: xiaoshuai2333    时间: 2017-9-6 14:54
可以先调试一下程序,如果有问题,可以再问一下

作者: hmgqljz111    时间: 2019-9-28 15:42
wulin 发表于 2017-9-5 16:28
if(key1==0),if(key2==0)下面少了花括号,delay起不到消抖作用。
#include
#define uchar unsigned cha ...

谢谢谢谢谢谢




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1