当前位置:首页 > 单片机 > 单片机
[导读]K1-K4 控制数码管移位显示/* 名称:K1-K4 控制数码管移位显示说明:按下 K1 时加 1 计数并增加显示位,按下 K2 时减 1 计数并减少显示位,按下 K3 时清零。*/#include<reg51.h>#define uchar unsigned char#define

K1-K4 控制数码管移位显示

/*  名称:K1-K4  控制数码管移位显示

说明:按下 K1 时加 计数并增加显示位,

按下 K2 时减 计数并减少显示位,

按下 K3 时清零。

*/

#include<reg51.h>

#define uchar unsigned char

#define uint unsigned int

//段码

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

//位码

uchar code DSY_Index[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

//待显示到各数码管的数字缓冲(开始仅在 位显示 0,其他黑屏)

uchar Display_Buffer[]={0,10,10,10,10,10,10,10};

//延时

void DelayMS(uint x)

{

uchar i;

while(x--) for(i=0;i<120;i++);

}

void Show_Count_ON_DSY()

{

uchar i;

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

{

P0=0xff;

P0=DSY_CODE[Display_Buffer[i]];

P2=DSY_Index[i];

DelayMS(2); 

}

}

//主程序

void main()

{

uchar i,Key_NO,Key_Counts=0;

 

P0=0xff;

P1=0xff;

P2=0x00;

while(1)

{

                      Show_Count_ON_DSY();

                      P1=0xff;

                      Key_NO=P1;

                      //P1 口按键状态分别为 K1-0xfeK2-0xfd,K3-0xfb 

          switch(Key_NO)

             { 

     case 0xfe: Key_Counts++;

if(Key_Counts>8) Key_Counts=8;

Display_Buffer[Key_Counts-1]=Key_Counts;

break; 

case 0xfd: if(Key_Counts>0)Display_Buffer[--Key_Counts]=10;

break; 

    case 0xfb: Display_Buffer[0]=0;

for(i=1;i<8;i++) Display_Buffer[i]=10;

Key_Counts=0; 

//若键未释放则仅刷新显示,不进行键扫描

while(P1!=0xff) Show_Count_ON_DSY();

}

}

[!--empirenews.page--]

K1-K4 控制数码管加减演示

/*  名称:K1-K4  控制数码管加减演示

说明:按下 K1 后加 计数,按下 K2

后减 计数,按下 K3 后清零。

*/

#include<reg51.h>

#include<intrins.h>

#define uchar unsigned char

#define uint unsigned int

//段码

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

//待显示的 位缓冲

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

//按键代码,按键计数

uchar Key_Code,Key_Counts=0;

//延时

void DelayMS(uint x)

{

 

 

uchar i;

while(x--) for(i=0;i<120;i++);

}

//显示函数

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);

P0=0xff;

P0=DSY_CODE[Num_Buffer[i]];

P2=j;

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;

//有键按下时,数码管刷新显示 30 次,该行代码同时起到延时作用

if(Key_Code!=0xff)

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

switch(Key_Code)

case 0xfe: if(Key_Counts<255) Key_Counts++;

break; 

case 0xfd: if(Key_Counts>0) Key_Counts--;

break; 

case 0xfb: Key_Counts=0;

Key_Code=0xff;

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭