void setup_hardware (void) {

    lcd_start();
    trisc = 0xff;
    trisa = 0xf0 ;

    

    intcon = 0b10100000;

    
    option_reg = 0b11000100 ;
}
unsigned char newportb ;
unsigned char oldportb ;
unsigned char portbinputs ;
unsigned int door_timer=0;
unsigned char door_bit;

void refresh ( void ) {
    if ( door_timer > 0 )
    {
        
        door_bit = 0x10 ;
        door_timer = door_timer - 1 ;
    }
    else
    {
        door_bit = 0 ;
    }

    newportb = portc;
    if ( newportb == oldportb )
    {
		portbinputs = newportb ;
	}

	/* Store the old value for next time */
	oldportb = newportb;
}


/* TMR0 Overflow handler */
void tmrHandler( void ) {
    refresh () ;
}


unsigned char counter = 0;

void interrupt( void )
{
    /* if the timer has overflowed */
    /* bit 2 of INTCON is set high */

    if( intcon & 4 )
    {
		/* clear the bit to turn   */
        /* off this interrupt      */
        clear_bit( intcon, 2 );

		counter = counter + 1;
		if ( counter > 4 )
		{
			counter = 0;

			/* call the handler        */
			/* function                */
			tmrHandler();
        }
    }
}



void display_value ( unsigned int value )
{
    lcd_cursor (3,0);
    lcd_print_ch ('0'+value%10);
    value /=10;
    lcd_cursor (2,0);
    lcd_print_ch ('0'+value%10);
    value /=10;
    lcd_cursor (1,0);
    lcd_print_ch ('0'+value%10);
    value /=10;
    lcd_cursor (0,0);
    lcd_print_ch ('0'+value%10);
}

void write_eeprom
    ( unsigned char address,
    unsigned char data )
{
	/* Wait for any outstanding writes to complete */
	while (eecon1 & WR );

	/* Set the address and data */
    eeadr = address;
	eedata = data;

	/* Make sure that we're talking the the EEPROM */
	clear_bit(eecon1, EEPGD);

	/* Enable writes */
	set_bit(eecon1, WREN);

	/* Disable interrupts */
	clear_bit(intcon, GIE);

	/* Send the magic sequence to enable writes */
	eecon2 = 0x55;
	eecon2 = 0xAA;

	/* Set the write going */
	set_bit(eecon1, WR);

	/* Renable interrupts */
	set_bit(intcon, GIE);

	/* Disable writes */
	clear_bit(eecon1, WREN);
}

unsigned char read_eeprom (
    unsigned char address )
{
	unsigned char data = 0;

	/* Tell the chip where to read from */
	eeadr = address;

	/* We want to read from EEPROM, not the program */
	clear_bit(eecon1, EEPGD);

	/* Send the read command */
	set_bit(eecon1, RD);

	/* Copy across the data */
	data = eedata;

	/* Return the result                */
	return data ;
}


#define NO_DIGIT 255


unsigned char get_digit ( void )
{
    unsigned char i, mask ;

    if ( portbinputs == 0 )
    {
        /* if no key is down - return     */
        /* immediately                    */
        return NO_DIGIT ;
    }

    /* find out which bit of portb is set */

    mask = 1 ;

    for ( i=0 ; i<8 ; i=i+1 )
    {
        if ( portbinputs & mask )
        {
            /* i holds the number of  */
            /* the key to send back   */
            return i ;
        }

        /* shift on to the    */
        /* next bit           */
        mask = mask << 1 ;
    }

    /*  if we get here portb must have    */
    /*  cleared while we were looking for */
    /*  the bits - in which case we       */
    /*  return an empty value             */

    return NO_DIGIT ;
}


#define DIGITS 4

int get_value ( void )
{
    int total = 0 ;
    unsigned char i;
    unsigned char digit;

    for ( i=0 ; i < 4 ; i = i + 1 )
    {
        /* wait for a digit  */
        while (1)
        {
            digit = get_digit () ;

            if ( digit != NO_DIGIT )
            {
                /* get out if we have a  */
                /* digit to look at      */
                break ;
            }
        }

        /* update the new total        */

        total = total * 10 ;
        total = total + digit ;

        while ( get_digit () != NO_DIGIT ) ;

    }

    return total ;
}

unsigned int read_eeprom_int( unsigned char location )
{
	unsigned char big, small ;
	unsigned int result;

	small = read_eeprom ( location ) ;
	big = read_eeprom( location + 1 ) ;

	result = big * 256 + small;

	return result;
}

void write_eeprom_int( unsigned char location, unsigned int value )
{
	unsigned char big, small ;
    big = value / 256 ;
    small = value % 256 ;

	write_eeprom( location, small ) ;
	write_eeprom( location + 1, big ) ;
}

void main ( void ) {

    setup_hardware () ;
	unsigned int attempt;
    unsigned int key;
    while (1)
    {
        attempt = get_value();
        if ( attempt == key ) {
            door_timer = 400 ;
        }
		if (attempt == 1234){
		porta = attempt;
		key = get_value();
		porta = key;
		write_eeprom_int ( 1, key ) ;
    }
}
}