Creating and using your own push buttons

For many applications it comes in handy to connect push buttons to the microcontroller. The Dwengo board already has 5 push buttons connected to port B of the microcontroller. The Dwengo library provides several useful macros to use these buttons. In this page we explain how you can create extra push buttons and how to read them out.

When push buttons are added by using the extension connector one always has to switch between 5 V and the ground. For this we make use of a pull-up resistor that pulls the input of the microcontroller high. This is illustrated in the next figure (right):

schema drukknoppen

.
Port B forms an exception since each of these pins already has an internal pull-up resistor to pull the pin high. The internal pull-up resistor can be activated by setting the NOT_RBPU register to 0 (or deactivating it by setting the register to 1).

  1. INTCON2bits.NOT_RBPU = 0; // enable port B pull-up (buttons)

By using the TRI-state registers one can configure the correct pins as an input, by setting the corresponding TRI-state bit to 1, or as an output, by setting the corresponding TRI-state bit to 0:

  1. TRISB = 0xFF; // all port B as inputs (buttons)
  2. TRISC = 0x01; // configure pin 0 of port C as input, all other pins as output

Reading out the digital input is always done by setting the PORT bits:

  1. unsigned char dummy = PORTB; // write port B into variable dummy
  2. PORTCbits.RC0; // read pin 0 of port C