51单片机linux环境LED、数码管、开关、循环、判断演示

2021-12-02 14:15:05 浏览数 (1)

示例程序如下,有bug,能运行但效果可提升:

代码语言:javascript复制
#include <8052.h>       	

#define LSA P1_5  			
#define LSB P1_6  			
#define LSC P1_7 

char SEG[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

void init0(void)
{
	LSA = 1;
	LSB = 0;
	LSC = 0;
} 
void init1(void)
{
	LSA = 1;
	LSB = 0;
	LSC = 0;
} 			
void init2(void)
{
	LSA = 0;
	LSB = 1;
	LSC = 0;
}
void init3(void)
{
	LSA = 1;
	LSB = 1;
	LSC = 0;
}
void init4(void)
{
	LSA = 0;
	LSB = 0;
	LSC = 1;
}
void ledsplus(unsigned char leds,unsigned int delay)
{
	P0=leds;
	while(delay--);
	delay=10000;
}
void seg1(unsigned char leds,unsigned int delay)
{
	P0=SEG[leds];
//	delay=50000;
	while(delay--);
}
int  main(void)			
{
	init1();
	unsigned char leds=0xff;
	unsigned char cnt=0x00;
	unsigned int delay=10000;
	while(1)				
	{
		if(P3_2==0)
		{
			for(leds=0xff;leds>0x00;leds--)
			{
				ledsplus(leds,delay);
				if(P3_4==0)
				{
					break;
				}
			}
		}
		if(P3_3==0)
		{
			for(leds=0x00;leds<11;leds  )
			{
				seg1(leds,10000*cnt);
			}
		}
		if(P3_4==0)
		{
			cnt  ;
			if(cnt>7)
			{
				cnt=0x00;
			}
		}
		
		P0=0xff;
		delay=10000;
	}
}

0 人点赞