欢迎光临散文网 会员登陆 & 注册

51单片机100例实例之15例

2021-01-21 14:35 作者:技术流阿宅  | 我要投稿

//项目名称:按键控制数码管加减演示

//项目再创作者:科技小宅神

//完成时间:2021/01/21

proteus仿真图

#include <reg51.h>//c51头文件

#include <intrins.h>//c51内部移位函数

#define uchar unsigned char

#define uint  unsigned int

//段码

uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};

//待显示的3位缓冲

uchar Num_Buffer[]={0,0,0};

//按键代码,按键计数

uchar Key_Code,Key_Counts=0;


//1毫秒延时子程序

void DelayMS(uint x)

{

  uchar t;

while(x--)

{

for(t=0;t<120;t++);

}

}

//刷新显示函数

void Show_Counts_ON_DSY()

{

  uchar i,j=0x01;

Num_Buffer[2]=Key_Counts/100; //百位

    Num_Buffer[1]=Key_Counts/10%10; //十位

Num_Buffer[0]=Key_Counts%10; //个位

for(i=0;i<3;i++)

{

j = _cror_(j,1);//循环右移,每次移一位

P2 = j;//P2循环右移,每次移一位

P0 = DSY_CODE[Num_Buffer[i]];

DelayMS(1);

}

}

//主程序

void main()

{

  uchar i;

P0 = 0xff;

P1 = 0xff;

P2 = 0x00;

Key_Code=0xff;

while(1)

{

Show_Counts_ON_DSY();

P1 = 0xff;

Key_Code=P1;

if(Key_Code != 0xff)

{

for(i=0;i<30;i++)

{

Show_Counts_ON_DSY();

}

}

switch(Key_Code)//按键代码多分支选择结构

{

case 0xfe: //当P1=0xfe,即P1.0=0,即k1按下时

       if(Key_Counts<255) Key_Counts++;break;//如果按键计数小于

case 0xfd: //当P1=0xfd,即P1.1=0,即k2按下时

       if(Key_Counts>0) Key_Counts--;break;

case 0xfb: //当P1=0xfb,即P1.2=0,即k3按下时

       Key_Counts=0;

}

Key_Code = 0xff;

}

}


51单片机100例实例之15例的评论 (共 条)

分享到微博请遵守国家法律