/*
Geiger pulse counter to lpt interface
(c) OH3NWQ 4.11.2006

v. 3 added about 1ms delay @ 4MHz

XTAL = whatever you like 1-8 MHz

*/

//macros
static unsigned char PORTA @ 0x05;
static unsigned char PORTB @ 0x06;

static unsigned char bank1 TRISA  @ 0x85;
static unsigned char bank1 TRISB  @ 0x86;
static unsigned char bank1 OPTION @ 0x81;



//constants & tables					
                             

//codez

void main() {
	char input,old,count1,count2,count3,count4,del,j;
	count1 = 0x00;
	count2 = 0x00;
	count3 = 0x00;
	count4 = 0x00;
	
	old    = 0x00;

	TRISB  = 0xff;		/* port B all bits input                    */
  	TRISA  = 0xff;		/* port A all bits input                    */
    	PORTA  = 0x00;		/* set enable OFF                           */
    	OPTION = 0x18;		/* weak pullup on port B                    */

        del = 37;                /* delay set   magic number 37 for 1ms delay */
				/*	       @ 4MHz                        */


	for(;;){
		input=(PORTA & 0x0f);

		if ((input == 0) && (old != input))
			{
			old = input;
			TRISB = 0xff; 	/* port B all bits input                    */
			}
		
		if ((input == 1) && (old != input))
			{
			old = input;
                                j=del;
                                while(--j != 0) continue;
			TRISB = 0xff; 	/* port B all bits input                    */
			count1 = PORTB; /* read 8 bit count lowest byte   */
			}

		if ((input == 2) && (old != input))
			{
			old = input;
                                j=del;
                                while(--j != 0) continue;
			TRISB = 0xff; 	/* port B all bits input                    */
			count2 = PORTB; /* read 8 bit count 2nd byte    */
			}

			
		if ((input == 3) && (old != input))
			{
			old = input;
                                j=del;
                                while(--j != 0) continue;
			TRISB = 0xff; 	/* port B all bits input                    */
			count3 = PORTB; /* read 8 bit count 3rd byte   */
			}

		if ((input == 4) && (old != input))
			{
			old = input;
                                j=del;
                                while(--j != 0) continue;
			TRISB = 0xff; 	/* port B all bits input                    */
			count4 = PORTB; /* read 8 bit count highest byte   */
			}

		if ((input == 5) && (old != input))
			{
			old = input;
			TRISB = 0xff; 	/* port B all bits input                    */
			}

		if ((input == 6) && (old != input))
                        {
			old = input;
			TRISB = 0x00;     /* port B all bits output 	 */
			PORTB = count1;   /* lower nibble on lower nibble */
                        }


		if ((input == 7) && (old != input))
                        {
			old = input;
			TRISB = 0x00;     	/* port B all bits output 	 */
			PORTB = (count1 >> 4);  /* lower nibble on lower nibble */
                        }

		if ((input == 8) && (old != input))
                        {
			old = input;
			TRISB = 0x00;     /* port B all bits output 	 */
			PORTB = count2;   /* lower nibble on lower nibble */
                        }


		if ((input == 9) && (old != input))
                        {
			old = input;
			TRISB = 0x00;     	/* port B all bits output 	 */
			PORTB = (count2 >> 4);  /* lower nibble on lower nibble */
                        }

		if ((input == 10) && (old != input))
                        {
			old = input;
			TRISB = 0x00;     /* port B all bits output 	 */
			PORTB = count3;   /* lower nibble on lower nibble */
                        }

		if ((input == 11) && (old != input))
                        {
			old = input;
			TRISB = 0x00;     	/* port B all bits output 	 */
			PORTB = (count3 >> 4);  /* lower nibble on lower nibble */
                        }

		if ((input == 12) && (old != input))
                        {
			old = input;
			TRISB = 0x00;     /* port B all bits output 	 */
			PORTB = count4;   /* lower nibble on lower nibble */
                        }

		if ((input == 13) && (old != input))
                        {
			old = input;
			TRISB = 0x00;     	/* port B all bits output 	 */
			PORTB = (count4 >> 4);  /* lower nibble on lower nibble */
                        }
		if ((input == 14) && (old != input))
                        {
			old = input;
			TRISB = 0xff;     /* port B all bits input 	 */
                        }

		if ((input == 15) && (old != input))
                        {
			old = input;
			TRISB = 0xff;     /* port B all bits input 	 */
                        }
                        
		}

	    }

