For my first program I wanted to use the push-buttons and LEDs on the Explorer 16 development board as this would make the program easy to test. The goal is to make the lights blink when a button is pressed. Peripherals like buttons and LEDs are trivial to use on the Explorer 16 board, each are wired directly to a primary I/O pin on the PIC. The LEDs are activated by writing a logical 1 to their output pin. The buttons are active low, so a button press can be detected by seeing a logical 0 on their input pin. The code first disables the JTAG port. This is needed because one of the LEDs is wired to a pin also used for JTAG. It then configures the LED pins as outputs and the button pins as inputs. The rest of the program is an infinite loop that detects if any of the buttons are pressed, toggling the LED state if a button is pressed, and clearing the LED state if one is not pressed. The loop also contains a busy loop to cause a delay before the buttons are checked again. This delay is needed to hold the LEDs in their current state for a while so that they are clearly blinking to the human eye. Without it the LEDs would blink so fast they would just look very dimly lit.
Code:
Tip:
The Microchip peripheral library uses a lot of macro functions. The lowercase 'm' in the function name indicates that it is a really a C pre-processor macro and not a true C function. Macro calls can not have line breaks in them so the line break must be escaped with a '\' (back-slash). For example:
mPORTAClearBits(BIT_7 | BIT_6 | BIT_5 | BIT_5 | BIT_4 | \ BIT_3 | BIT_2 | BIT_1 | BIT_0 );