ledflash.c - Flashes an LED, uses I/O macros.

A simple program that flashes the Power LED, uses I/O macros. RB15 is configured as an open drain output, and drives an LED at the junction between the LED and a pullup resistor to VDD. When RB15 is driven low, the LED is off. When RB15 is driven high, the RB15 output floats because of the open drain, and so the LED is turned on by the pullup resistor to VDD. This allows the power LED to function as a ‘blinky’ LED in addition to serving as a power indicator.

 
#include "pic24_all.h"


void config_led1() {

Setup the RB15 pin to drive the LED Our normal procedure is to wire up the RB15 LED as opendrain. however, some chips do NOT support OD on RB15, so we need to first determine if the chip supports OD on RB15. We will do that by seeing if the device header file has define the OD enable bit for RB15. Unfortunately, Microchip has used two different names for this bit over the years, so we must check for both. If that SFR bit is not defined, then setup a normal CMOS digital output on RB15

#if (defined(_ODCB15) || defined(_ODB15))
  CONFIG_RB15_AS_DIG_OUTPUT();
  ENABLE_RB15_OPENDRAIN();
#else
  CONFIG_RB15_AS_DIG_OUTPUT();
#endif
}
 

_LATB15 is the port register for RB15.

#define LED1 (_LATB15)

int main(void) {
  configClock();
  config_led1();
  LED1 = 0;
  while (1) {

Delay long enough to see LED blink.

    DELAY_MS(250);

Toggle the LED.

    LED1 = !LED1;
  } // end while (1)
}