清翔电子51单片机8课独立键盘作业2

/*******************************/
//让后4位数码管以0.01秒速度从0到9999显示	
//按下S2开始跑数   按下S3停止跑数
//按下S4数码管数字加1  按下	S5 数码管数字减1      
//需要在停止跑数时候才可以进行S4 和 S5 操作;

#include "reg52.h"
#include "intrins.h"

sbit WE=P2^7;
sbit DU=P2^6;
sbit S2=P3^0;
sbit S3=P3^1;
sbit S4=P3^2;
sbit S5=P3^3;

#define UCHAR unsigned char
#define UINT unsigned int

UCHAR time_counter0,time_counter1;
UCHAR dis_num;
UINT count = 0;

UCHAR code leddata[]={ 
 
                0x3F,  //"0"
                0x06,  //"1"
                0x5B,  //"2"
                0x4F,  //"3"
                0x66,  //"4"
                0x6D,  //"5"
                0x7D,  //"6"
                0x07,  //"7"
                0x7F,  //"8"
                0x6F,  //"9"
                0x77,  //"A"
                0x7C,  //"B"
                0x39,  //"C"
                0x5E,  //"D"
                0x79,  //"E"
                0x71,  //"F"  15
                0x76,  //"H"
                0x38,  //"L"
                0x37,  //"n"
                0x3E,  //"u"
                0x73,  //"P"
                0x5C,  //"o"
                0x40,  //"-"
                0x00,  //熄灭
                0x00  //自定义

};

void delay(UCHAR y)
{
	UCHAR i,j;	
	for(i=y;i>0;i--)	
		for(j=114;j>0;j--)
			;
}
void display(UINT count)
{
	P0 = 0xff;
	WE = 1;
	P0 = 0xef; //点亮第五位数码管千位
	WE = 0;

	DU = 1;
	P0 = leddata[count / 1000];
	DU = 0;
	delay(5);

	P0 = 0xff;
	WE = 1;
	P0 = 0xdf; //点亮第liu数码管千
	WE = 0;

	DU = 1;
	P0 = leddata[count % 1000 / 100];
	DU = 0;
	delay(5);

	P0 = 0xff;
	WE = 1;
	P0 = 0xbf; //点亮第7码管
	WE = 0;

	DU = 1;
	P0 = leddata[count % 100 / 10];
	DU = 0;
	delay(5);

	P0 = 0xff;
	WE = 1;
	P0 = 0x7f; //点亮第8
	WE = 0;

	DU = 1;
	P0 = leddata[count % 10];
	DU = 0;
	delay(5);

}				  
void time_init()
{
	TMOD = 0x01; //time0 16
	TH0 = 0xdb;
	TL0 = 0xff;  // 定时10ms

}


//S2=P3^0;start count
//S3=P3^1;stop count
//S4=P3^2;increase count
//S5=P3^3;decline count
int main()
{
	time_init();
	while(1)
	{
	   if(TF0 == 1)   //10ms coming  and push S2
	   {
	   		TF0 = 0;
			TH0 = 0xdb;
			TL0 = 0xff; 
			count++;
			if(count > 9999)
				count = 0;
	   		display(count);	
	   }
	   	if(S2 == 0)   
		{
			delay(5);
			if(S2 == 0)
			{
				TR0 = 1;	
				while(!S2) ;
			}
		
		}
		if(S3 == 0)   
		{
			delay(5);
			if(S3 == 0)
			{
				TR0 = 0;
				while(!S3);
			}
		
		}
		if(S4 == 0)
		{
			delay(5);
			if(S4 == 0 && TR0 == 0)
			{
				count++;
				while(!S4);
			}
		
		}
		if(S5 == 0)   
		{
			delay(5);
			if(S5 == 0 && TR0 == 0)
			{
				TR0 = 0;
				while(!S5);
			}
		
		}
	

	   
	   	   
	}

	return 1;
}

上一篇:凸包板子


下一篇:单片机(一):基础知识