05-独立按键

#include "reg52.h"

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;


void SelectHC573(unsigned char n)
{
	switch(n)
	{
        case 4:
			P2 = (P2 & 0x1f) | 0x80;
        	break;
		
        case 5:
			P2 = (P2 & 0x1f) | 0xa0;
        	break;
		
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
        	break;
		
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;
        	break;
	}
}

void delay(unsigned char t)
{
	while(t--);
}


void ScanKey()
{
	if(S7 == 0)
	{
		delay(100);
		if(S7 == 0)
		{
			L1 = 0;
			while(S7 = 0);
			L1 = 1;
		}
	}
	
	if(S6 == 0)
	{
		delay(100);
		if(S6 == 0)
		{
			L2 = 0;
			while(S6 = 0);
			L2 = 1;
		}
	}
	
	if(S5 == 0)
	{
		delay(100);
		if(S5 == 0)
		{
			L3 = 0;
			while(S5 = 0);
			L3 = 1;
		}
	}
	
	if(S4 == 0)
	{
		delay(100);
		if(S4 == 0)
		{
			L4 = 0;
			while(S4 = 0);
			L4 = 1;
		}
	}
	

}
	



void main()
{
	SelectHC573(4);
	while(1)
	{
		ScanKey();
	}
}

#include "reg52.h"

sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^3;
sbit L6 = P0^3;


void SelectHC573(unsigned char n)
{
	switch(n)
	{
        case 4:
			P2 = (P2 & 0x1f) | 0x80;
        	break;
		
        case 5:
			P2 = (P2 & 0x1f) | 0xa0;
        	break;
		
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
        	break;
		
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;
        	break;
	}
}

void delay(unsigned char t)
{
	while(t--);
}

unsigned char state = 0;

void ScanKey()
{
	if(S7 == 0)
	{
		delay(100);
		if(S7 == 0)
		{
			if(state == 0)
			{
				L1 = 0;
				state = 1;
			}
			else if(state == 1)
			{
				L1 = 1;
				state = 0;
			}
			while(S7 ==0);
		}
	}
	
	if(S6 == 0)
	{
		delay(100);
		if(S6 == 0)
		{
			if(state == 0)
			{
				L2 = 0;
				state = 2;
			}
			else if(state == 2)
			{
				L2 = 1;
				state = 0;
			}
			while(S6 ==0);
		}
	}
	
	if(S5 == 0)
	{
		delay(100);
		if(S5 == 0)
		{
			if(state == 1)
			{
				L3 = 0;
				while(S5 ==0);
				L3 = 1;
			}
			else if(state == 2)
			{
				L5 = 0;
				while(S5 ==0);
				L5 = 1;
			}
		}
	}
	
	if(S4 == 0)
	{
		delay(100);
		if(S4 == 0)
		{
			if(state == 1)
			{
				L4 = 0;
				while(S4 ==0);
				L4 = 1;
			}
			else if(state == 2)
			{
				L6 = 0;
				while(S4 ==0);
				L6 = 1;
			}
		}
	}
	

}
	



void main()
{
	SelectHC573(4);
	while(1)
	{
		ScanKey();
	}
}

 

posted @ 2022-12-24 12:11  Unclesundada  阅读(36)  评论(0)    收藏  举报