stc89c52 51单片机 计数器

stc89c52 51单片机 计数器

记录实验板 上k4建按下的次数,并用数码管显示出来。


```c
#include<reg51.h>
#define uchar unsigned char
#define dula P0		
#define wela P2		
sbit k4 = P3^6;
int j,k,num0,num1,num2,num3,count=0;
unsigned char code weitable[]={0x8f,0x4f,0x2f,0x1f};

unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(uchar i)
{
  for(j=i;j>0;j--)
    for(k=125;k>0;k--);
}

void display1(uchar wei,uchar shu)
{
   wei=wei-1;
   wela|=0xf0;
   P0=table[shu];
   wela=wela&weitable[wei];
   delay(5);
}
void display(uchar a,uchar b,uchar c,uchar d)
{						  
   display1(4,a);
   display1(3,b);
   display1(2,c);
   display1(1,d);
}
void key()
{
	if(k4 == 0)
	{
		delay(10);
		if(k4 == 0)
		{
			while(k4 == 0);
			{
					count++;					 
			}
		}
	}
}

void main()
{
	while(1)
	{
		key();
		num0=count%10;		 
		num1=count/10%10;
		num2=count/100%10;		  
		num3=count/1000;
		display(num3,num2,num1,num0);	   
	}
} 

posted @ 2021-01-07 22:02  啧啧啧|(* ̄ ̄*)  阅读(45)  评论(0)    收藏  举报  来源