The development board in the PIC32 Starter Kit has three LEDs and three switches connected to pins in Port D as shown:
LED | pin | SW | pin | |
---|---|---|---|---|
LED1 | RD0 | SW1 | RD6 | |
LED2 | RD1 | SW2 | RD7 | |
LED3 | RD2 | SW3 | RD13 |
This project uses the three switches to control the three lights.
The following documents the basic macros and functions for using digital output and input pins:
The project also demonstrates that the switches use inverse logic. Pressing the switch forces the input pin low.
Project files: lights.zip
lights.c
01: /* lights.c 02: * 03: * Connect the switches to the LEDs 04: */ 05: 06: #include <plib.h> 07: 08: const int LED1 = BIT_0; 09: const int LED2 = BIT_1; 10: const int LED3 = BIT_2; 11: const int SW1 = BIT_6; 12: const int SW2 = BIT_7; 13: const int SW3 = BIT_13; 14: 15: int main() 16: { 17: int n; 18: mPORTDSetPinsDigitalIn(SW1 | SW2 | SW3 ); 19: mPORTDSetPinsDigitalOut(LED1 | LED2 | LED3); 20: while (1) { 21: // SW1 -> LED1 22: n = mPORTDReadBits(SW1); 23: if (n) mPORTDClearBits(LED1); 24: else mPORTDSetBits(LED1); 25: // SW2 -> LED2 26: n = mPORTDReadBits(SW2); 27: if (n) mPORTDClearBits(LED2); 28: else mPORTDSetBits(LED2); 29: // SW3 -> LED3 30: n = mPORTDReadBits(SW3); 31: if (n) mPORTDClearBits(LED3); 32: else mPORTDSetBits(LED3); 33: } 34: return 0; 35: }
On my PIC32 Starter Kit, SW1 and SW3 worked as expected. However, LED2 (controlled by SW2) would turn on or off as my finger approached the switch. There is probably some electrical problem with this particuliar board.
Maintained by John Loomis, updated Fri Aug 01 17:56:44 2008