/*
 * 	(C)opyright SOOO "Gomelsky Priborostroitelny Zavod"
 *	www.gomel-pribor.com
 */

#include <pic18.h>

	
void interrupt HI_ISR(void);


unsigned char KeyPressed = 0;


//#pragma interrupt_level 1
void sound()
{ 	int i;
	for (i=1;i<200*2;i++)
	{
		RA4 = i; 
		DelayUs(196);
		DelayUs(196);
		DelayUs(196);
	}
}

unsigned char ReadKey()
{
	unsigned char ch;
	di();
	ch = KeyPressed;
	KeyPressed = 0;
	ei();
	return ch;
}


//#pragma interrupt_level 1
void interrupt HI_ISR(void)
{
	unsigned char i, j;
	
	if (RBIF)
	{
		if (((PORTB & 0xF0) != 0xF0) && (KeyPressed==0))
		{
			i = 4;
			while (i)
			{
				i--;
				if (i==0) {RB2 = 0; RC1 = 1; RA2 = 1; RA5 = 1;} 
				else if (i==1) {RB2 = 1; RC1 = 0; RA2 = 1; RA5 = 1;}
				else if (i==2) {RB2 = 1; RC1 = 1; RA2 = 0; RA5 = 1;}
				else {RB2 = 1; RC1 = 1; RA2 = 1; RA5 = 0;}
				j = ((~PORTB) & 0xf0) >> 4;
				if (j == 4) j = 3; else if (j==8) j =4;
				KeyPressed = i * 4 + j;
				if (j) 
				{
					sound();
					break;
			 	} else KeyPressed = 0;
		  	}
			RB2 = 0; RC1 = 0; RA2 = 0; RA5 = 0;
		}
		RBIF = 0;
	}
	
}

}


