Выбрать главу

An LED can be connected to a microcontroller output port in two different modes: current sinking and current sourcing.

Current Sinking Mode

As shown in Figure 6.1, in current sinking mode the anode leg of the LED is connected to the +5V supply, and the cathode leg is connected to the microcontroller output port through a current limiting resistor.

Figure 6.1: LED connected in current sinking mode

The voltage drop across an LED varies between 1.4V and 2.5V, with a typical value of 2V. The brightness of the LED depends on the current through the LED, and this current can vary between 8 and 16mA, with a typical value of 10mA.

The LED is turned ON when the output of the microcontroller is at logic 0 so the current flows through the LED. Assuming the microcontroller output voltage is about 0.4V when the output is low, we can calculate the value of the required resistor as follows:

     (6.1)

where

 VS is the supply voltage (5V)

 VLED is the voltage drop across the LED (2V)

 VL is the maximum output voltage when the output port is low (0.4V)

 ILED is the current through the LED (10mA)

Substituting the values into Equation (6.1) we get,

 

The nearest physical resistor is 270 ohms.

Current Sourcing Mode

As shown in Figure 6.2, in current sourcing mode the anode leg of the LED is connected to the microcontroller output port and the cathode leg is connected to the ground through a current limiting resistor.

Figure 6.2: LED connected in current sourcing mode

In this mode the LED is turned ON when the microcontroller output port is at logic 1 (i.e., +5V). In practice, the output voltage is about 4.85V and the value of the resistor can be determined as: 

     (6.2)

where

 VO is the output voltage of the microcontroller port when at logic 1 (+4.85V).

Thus, the value of the required resistor is:

 

The nearest physical resistor is 290 ohm.

Project Hardware

The circuit diagram of the project is shown in Figure 6.3. LEDs are connected to PORTC in current sourcing mode with eight 290-ohm resistors. A 4MHz resonator is connected between the OSC1 and OSC2 pins. Also, an external reset push button is connected to the MCLR input to reset the microcontroller when required.

Figure 6.3: Circuit diagram of the project

Project PDL

The PDL of this project is very simple and is given in Figure 6.4.

START

 Configure PORTC pins as output

 Initialize J = 1

 DO FOREVER

  Set PORTC = J

  Shift left J by 1 digit

  IF J = 0 THEN

   J = 1

  ENDIF

  Wait 1 second

 ENDDO

END

Figure 6.4: PDL of the project

Project Program

The program is named as LED1.C, and the program listing is given in Figure 6.5. At the beginning of the program PORTC pins are configured as outputs by setting TRISC = 0. Then an endless for loop is formed, and the LEDs are turned ON alternately in an anticlockwise manner to create a chasing effect. The program checks continuously so that when LED 7 is turned ON, the next LED to be turned ON is LED 0.

This program can be compiled using the mikroC compiler. Project settings should be configured to 4MHz clock, XT crystal mode, and WDT OFF. The HEX file (LED1.HEX) should be loaded to the PIC18F452 microcontroller using either an in-circuit debugger or a programming device.

/*****************************************************************************

                               CHASING LEDS

                               ============

In this project 8 LEDs are connected to PORTC of a PIC18F452 microcontroller

and the microcontroller is operated from a 4MHz resonator. The program turns

on the LEDs in an anti-clockwise manner with one second delay between each

output. The net result is that the LEDs seem to be chasing each other.

Author: Dogan Ibrahim

Date:   July 2007

File:   LED1.C

******************************************************************************/

void main() {

 unsigned char J = 1;

 TRISC = 0;

 for(;;)             // Endless loop

 {

  PORTC = J;         // Send J to PORTC

  Delay_ms(1000);    // Delay 1 second

  J = J << 1;        // Shift left J

  if (J == 0) J = 1; // If last LED, move to first LED

 }

}

Figure 6.5: Program listing

Further Development

The project can be modified such that the LEDs chase each other in both directions. For example, if the LEDs are moving in an anticlockwise direction, the direction can be changed so that when LED RB7 is ON the next LED to turn ON is RB6, when RB6 is ON the next is RB5, and so on.

PROJECT 6.2 — LED Dice 

Project Description

This is a simple dice project based on LEDs, a push-button switch, and a PIC18F452 microcontroller operating with a 4MHz resonator. The block diagram of the project is shown in Figure 6.6.

Figure 6.6: Block diagram of the project

As shown in Figure 6.7, the LEDs are organized such that when they turn ON, they indicate numbers as on a real dice. Operation of the project is as follows: The LEDs are all OFF to indicate that the system is ready to generate a new number. Pressing the switch generates a random number between 1 and 6 which is displayed on the LEDs for 3 seconds. After 3 seconds the LEDs turn OFF again.

Figure 6.7: LED dice

Project Hardware

The circuit diagram of the project is shown in Figure 6.8. Seven LEDs representing the faces of a dice are connected to PORTC of a PIC18F452 microcontroller in current sourcing mode using 290-ohm current limiting resistors. A push-button switch is connected to bit 0 of PORTB (RB0) using a pull-up resistor. The microcontroller is operated from a 4MHz resonator connected between pins OSC1 and OSC2. The microcontroller is powered from a +9V battery, and a 78L05-type voltage regulator IC is used to obtain the +5V supply required for the microcontroller.