    /*  Debounced Light Switch    */
    /*  Rob Miles 2000            */

    unsigned char key ( void )
    {
      unsigned char count=0;
      unsigned char oldv, newv ;
      oldv = input_pin_port_b(0);
      while ( count<20 )
      {
        newv = input_pin_port_b(0);
        if ( oldv==newv )
        {
           count++ ;
        }
        else
        {
          count=0 ;
          oldv=newv ;
        }
      }
      return oldv ;
    }

    void main ( void )
    {
      /* Select Bank 1           */
      set_bit(STATUS,RP0);

      /* set all PORTB for input */
      TRISB=0xff;

      /*  PORTA bit 0 as output  */
      TRISA=0x1e;

      /* now use Register bank 0 */
      clear_bit(STATUS,RP0);

      /* repeat forever */
        while (1)
      {
        /* wait for the button to */
        /* go down                */
        while (key()==0);
        /* if light on turn off */
        if (input_pin_port_a(0))
        {
          output_low_port_a(0);
        }
        else
        {
        /* if light off turn on */
          output_high_port_a(0);
        }
        /* wait for the button  */
        /* to go up again       */
        while (key()==1);
      }
    }