Blinking Light

This project uses a simple timer to flash LED1 on and off. LED1 is connected to pin RD0 of the PIC32 and internally to Port D. We can use the digital I/O operations of the peripherials library (see Digital Output) to control the LED.

mPORTDSetPinsDigitalOut(): to configure RD0 as an output pin
mPORTDToggleBits(): to toggle an LED on and off

Project files: timer2.zip

timer2.c


01: /* timer2.c
02: */
03: 
04: #include <plib.h>
05: 
06: void mysleep(int n);
07: 
08: 
09: int main()
10: {
11:         int i;
12:         mPORTDSetPinsDigitalOut(BIT_0);   /* Make RD0 (LED1) as output */
13:         mPORTDClearBits(BIT_0);      /* Turn off LED1 on startup */
14:         for (i=0; i<20; i++) {
15:                 mPORTDToggleBits(BIT_0);     /* turn ON LED1 */
16:                 mysleep(500);
17:         }
18:         //DBPUTS("Program terminated. Click HALT and then RESET to stop the microcontroller. \n");
19:         return 0;
20: }
21: 
22: void mysleep(int n)
23: {
24:         int i,k,count;
25:         const int DLY = 600;
26:         for (i=0; i<n; i++) {
27:                 count = 0;
28:                 for (k=0; k<DLY; k++) count++;
29:         }
30: }


Results

LED1 should blink on and off for about ten seconds.

Exercise

Modify the program to flash LED1 and LED2, with LED1 on when LED2 is off and vice versa.


Maintained by John Loomis, updated Fri Aug 01 17:28:11 2008