How to Create a PIC Tracing LEDs Project in C

I am creating this project in C language using MPLAB X IDE XC8 compiler. However, you may use any other IDE that works with PIC microcontrollers. In this project, eight LEDs are connected to PORTC of a PIC18F45K22 microcontroller. This microcontroller is operated from an 8 MHz crystal. When the power is supplied to the microcontroller or when the microcontroller is reset, the LEDs turn ON alternatively in an anticlockwise style where only one LED is ON at any time. A 1-s delay is used between each output so that the LEDs can be seen turning ON and OFF a tracing manner.

Connection of the Key Elements to the Microcontroller

Normally to connect an LED to a microcontroller output, we can do it in two ways: the current sinking mode and current sourcing mode. I have explained these two modes in this article, but for this particular project, I will use the current sourcing mode.

Just to demonstrate LED connected in a current sourcing mode, consider the figure below:

LED connected in current sourcing mode
Figure 1.0 LED connected in current sourcing mode

It is clear from the diagram above that, the anode leg of the LED is connected to the microcontroller output port, and the cathode leg is connected to the ground through the current limiting resistor.

In current sourcing mode, the LED is turned ON when the microcontroller output port is at logic 1, that is, +5 V. However, in practice, the output voltage is usually around 4.85 V and the value of the limiting resistor can be established as follows:

limiting resistor calculation

Where, VO is the output voltage of the microcontroller port when at logic 1 (i.e. +4.85 V), VLED is the voltage drop across the LED (2 V), ILED the current through the LED (10 mA).

Hence the value of the resistor needed is:

limiting resistor calculation

The nearest physical resistor is 290 𝝮

The circuit diagram below shows how each LED and other elements are connected in the completed project.

The LEDs are connected to PORTC in the current sourcing mode with eight 290 𝝮 resistors. An 8 MHz crystal is connected between OSC1 and OSC2 pins of the microcontroller. Additionally, an external reset push button is connected to the Master Clear (MCLR) input so as to reset the microcontroller when necessary.

Creating a Project with MPLAB X IDE

Create the project using MPLAB X IDE and select XC8 as the compiler. If you are new to MPLAB X IDE, first you have to learn how to create a new project using this particular IDE with XC8 compiler; this article can guide you on how to go about it. Since we are using PIC18F45K22 microcontroller, ensure you have configured the MPLAB for that particular device type.

After creating and naming your project, create a source file and copy and paste the following code into the file:

/*
 * File:   tracingleds.c
 * Author: zamulinj
 *
 * Created on February 17, 2024, 3:57 PM
 * 
 * In this program, 8 LEDs are connected to PORTC of PIC18F45K22 microcontroller. 
 * This microcontroller is operated from an 8 MHz crystal. The program turns ON the LEDs in an anticlockwise way with a 2s delay between each output. This results in LEDs coming ON in trailing or tracing manner. 
 */

#include <xc.h>
// Configuration settings
#pragma config MCLRE = EXTMCLR, WDTEN = OFF, FOSC = HSHP

// Define clock frequency
#define _XTAL_FREQ 8000000

void delay(unsigned char k); // function prototype/declaration

void main(void) {
    unsigned char k = 1; 
    ANSELC = 0; // configure PORTC as digital
    TRISC = 0; // configure PORTC as output
    
    //create endless loop
    for (;;) {
        PORTC = k; 
        delay(1); // delay for 1s
        k = k << 1; // shift left k
        // if you reached the last LED, move to the first one
        if (k == 0)
            k = 1; 
    }
    return;
}

/*
 * This functions creates seconds delay. The argument in the function specifies the delay time in seconds. 
 */
void delay(unsigned char s) {
    unsigned char j, k; 
    for (j = 0; j < s; j++) {
        for (k = 0; k < 100; k++)
            __delay_ms(10); // MPLAB XC8 library function-creates a delay
    }   
}

To compile the program, right click on the project then click Build from the drop down menu. The program should compile successful and a message “Loading completed” should be displayed as illustrated below:

Compiled Successfully
Figure 1.2 Compiled Successful

To load the program to the target microcontroller on a development board, instead of selecting a simulator when you are creating a new project, you should select for instance, PICKit 4 as the tool (hardware tool). The PICKit 4 programmer is connected to a development board such as PICDEM HPC Explorer Board. You then click Make and Program Device on the project from the project drop down menu (right click on the project to access it) to have the program loaded or burned to the target microcontroller on the development board. On the board, the LEDs can be connected to the microcontroller output as shown in Figure 1.1, also connect the external reset push button.

Please follow us and share:

Author: John Mulindi

John Mulindi has a background in a technical field and he writes on topics ranging from automation, computer systems, embedded systems, mechatronics to measurement and control.

Leave a Reply