独立按键c语言程序详解

2025-11-23 09:15:49

1、独立按键0~F数码管显示,实现加一加二,减一减二数码管显示

独立按键c语言程序详解

2、#include "reg51.h"//包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义

独立按键c语言程序详解

3、#define uint unsigned int

#define uchar unsigned char

sbit k0=P3^0;//定义按键

sbit k1=P3^1;//定义按键

sbit k2=P3^2;//定义按键

sbit k3=P3^3;//定义按键

独立按键c语言程序详解

4、uchar smg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f

          ,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F

独立按键c语言程序详解

5、/******************************************************************/

/*                    延时函数声明                                */

/******************************************************************/

/*void mdelay(uint t)

{

  uchar n;

for(;t>0;t--)

 for(n=0;n<125;n++)

  {;}

}*/

void mdelay(uint t)

{

  uchar n;

while(t--)

{

for(n=0;n<123;n++)

  {;}

  }  

}

独立按键c语言程序详解

6、/******************************************************************/

/*                   主函数                                       */

/******************************************************************/

void main()

{

uchar count=0;

while(1)

P2=0x00;

P0=smg[count];//给数码管赋个初值

if(k0==0)

{

mdelay(100);//去抖动

 if(k0==0)

{

P0=smg[count];//k0按下数值加一

count++;

while(!k0);//如果按键按下,在此停下;如果按键松开,继续检测

if(count==16)

count=0;

}

}

独立按键c语言程序详解

7、  if(k1==0)

{

mdelay(100);//去抖动

 if(k1==0)

{

P0=smg[count];//k1按下数值减一

while(!k1);

if(count==0)

 count=15;

else

count--;

}

}

独立按键c语言程序详解

8、 if(k2==0)

{

mdelay(100);//去抖动

 if(k2==0)

{

P0=smg[count];//k0按下数值加二

count=count+2;

while(!k2);//如果按键按下,在此停下;如果按键松开,继续检测

if(count==16)

count=0;

if(count==17)

count=1;

}

}

独立按键c语言程序详解

9、  if(k3==0)

{

mdelay(100);//去抖动

 if(k3==0)

{

P0=smg[count];//k1按下数值减二

while(!k3);

if(count==0)

 count=14;

else if(count==1)

count=15;

else

count=count-2;

}

}

}

}

独立按键c语言程序详解

10、本经验只供参考,如有不足,还请见谅,如果有什么疑问请在下边留言,及时给你解答。。。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢