HOME

LEDs - 08

In this section we are going to:

- Include a simple delay function to make the PB7 LED flash

Turn the red LED (PB7) on and off

Now that the PB7 port pin is configured for LED current and as an output, how do we turn on the PB7 red LED? 

PBOUT &=~0x80;  // Clearing bit 7 of PBOUT will turn PB7 on. (A low or 0 is needed to complete the electrical circuit.)

How do we turn off the PB7 red LED?

PBOUT |= 0x80;   // Setting bit 7 to a 1 will turn PB7 off.

If we include the two instructions in a row, they will occur too quickly for us to see the LED flash. If we include a delay after turning it on and another after turning it off, it will appear to flash.

 

Adding a Delay

The simplest way to make the LED flash is to add a delay. While the microcontroller is running the delay, it is doing nothing else. Although this is fine for our purposes, we would not expect to see this in a serious production design.

 

We'll include a function named pause() to call the delay before main(). We'll include a return to ensure it returns to the while() loop to run more than once.

We'll include a while() loop within main() to call the delay, turn the LED on, call another delay,  and then turn the LED off.

 

For the sake of page space, we've commented out the other two LED routines and moved them out of sight.

 

This concludes the section on programming LEDs to illuminate or flash.

Some additional information: leds.h and leds.c

These two files can be found in <install>\samples\XP_F6482\XP_F6482_LedBlink_C\include and ..\src folders.

The file leds.h defines the red LED for PB7 as (1 << 7) indicating bit 7 of Port B. The yellow LED for PC4 is (1 << 4) and the green LED for PC5 is (1 <<5).

There are 3 prototype functions in leds.c:

- Leds_Init(). Curiously, it defines the data direction as input (0x01) but does not specify high drive current as well.

- Leds_Off(). The direction bit is set to a 1 which interrupts the circuit.

- Leds_On(). The direction bit is cleared to a 1 which completes the circuit.

 

TOP

 

HOME