Studentcompanion - Co.za-Connecting Light Emitting Diodes LED To A PIC Microcontroller XC8 PDF
Studentcompanion - Co.za-Connecting Light Emitting Diodes LED To A PIC Microcontroller XC8 PDF
Studentcompanion - Co.za-Connecting Light Emitting Diodes LED To A PIC Microcontroller XC8 PDF
za/connecting-light-emitting-diodes-led-to-a-pic-microcontroller-xc8/
A Light Emitting Diode (LED) is a semiconductor light source, when forward biased, it emits light.
LEDs are used mainly to indicate the status of electronic circuits, for example to indicate that power is on or off but
nowadays they are used in many applications including lighting and beam detection.
Early LEDs emitted low-intensity red light, but today high brightness and many colour LEDs are available (white,
blue, red, green, yellow or even infrared).
LEDs have many advantages over the traditional lights (the incandescent and neon light bulbs) such as: low voltage
of operation, very low energy consumption, smaller size, longer lifetime, available in many colours etc. In many
energy efficient applications, the LEDs are tending to replace the traditional light source.
In this article we will learn how to connect and switch on and off various LEDs to a microcontroller using the
Microchip XC8 Compiler.
This is the simplest project a beginner in embedded programming can start with before attempting any complex
projects as we have learned from the Introduction to XC8 Compiler article.
Connecting LEDs
An LED is similar to a diode, it has two legs: the longer leg is the anode (+) and the shorter leg the cathode (-). The
cathode is also identified by a flat side on the body.
The intensity of the light emitted by an LED depends on the amount of forward current passed through the device but
we must take attention not to exceed the maximum allowable forward current or draw more current than the PIC
output pin can handle. A PIC can source or sink 25mA of current per Input/Output pin.
When designing an LED circuit, we have to know the typical voltage drop, table 1 below lists some few
characteristics of some LEDs.
Most LEDs have a typical forward voltage drop of about 2V, with a typical operating current of around 10 mA (it is
always good not to operate a device at its high end current), its important to read the datasheet to get the correct
values.
An LED can be connected to a microcontroller in two different ways: in current sourcing mode (figure 2) or current
sinking mode (figure 1).
The port output voltage can be assumed to be +5V when the port is
at logic HIGH. Assuming that the LED is to be operated with 10mA
forward current, and that it has a forward voltage drop of 2V, we can
easily calculate the value of the current limiting resistor as:
As the PIC can supply up to 25mA,
the current can be increased for more brightness. We are going to
choose a resistance of 220 (forward current of about 13.6mA) in
our example but a resistance of 330 could be also do the job
very well.
XC8 Code
2. Output a LOW (to switch off) or a HIGH (switch on) to the PORT with the LAT Register.
Note that you can use the PORT Register as well it is going to work but it is not a reliable way to write to a PORT, the
best way is to use the LAT. Always use the LAT if the microcontroller has it (note that the PIC 16F series dont have
the LAT register) to write to a PORT (Output a value) and the PORT to read from the PORT.
Example:
Delay Functions
A delay function is used to create a delay in the program, if let say you need a 1 second delay between the ON and
OFF of the LED, its easier to use a delay function to generate a 1 second.
Table 2 gives a list of the available XC8 delay functions. In MPLAB X environment, press F1 to open the help file
document, search for delay to lean more. The header file delays.h must be included at the beginning of the program
when any of these functions are used. The arguments of the functions must be an 8-bit unsigned character, i.e., the
maximum allowed number in an argument is 255.
The delays are specified in terms of instruction cycle times which depend on the frequency of the clock used.
For example, when using a 4-MHz clock, the instruction cycle time is 1 s.
In PIC microcontrollers, an instruction cycle takes four clock periods, the microcontroller actually operates at a clock
rate, which is a quarter of the actual oscillator frequency. For example, in a PIC microcontroller operating at 4-MHz
clock, the instruction cycle time is only 1 s (frequency of 1 MHz).
The PIC18F series of microcontrollers can operate with clock frequencies up to 40 MHz.
Functions Descriptions
As it is often more convenient request a delay in time-based terms rather than in cycle counts, the
macros __delay_ms(x) is used to generate a delay in millisecond and __delay_us(x) is used to generate a delay in
microsecond.
These macros simply convert the time based request into instruction cycles based on the system frequency.
In order to achieve this, these macros require the prior definition of preprocessor symbol _XTAL_FREQ for the
oscillator frequency (in Hertz). An error will result if these macros are used without defining oscillator frequency
symbol or if the delay period requested is too large. For very large delays, call this function multiple times.
Example: Using an oscillator frequency of 8MHz, generate a 1 second delay:
Figure 3 above shows 8 LEDs connected to PORTB, in this code the LEDs switch ON one after the other and make
an impression of a light chaser:
1 void main(void)
2 {
3 OSCCON = 0x76; //Configure OSCON Register to use Internal Oscillator. Please check the
4 datasheet
5 TRISB=0x00; // Set PORT B as Output
6
7 unsigned char Chasing_LED =1; //Declare a char variable
8 while (1) //Endless Loop
9 {
10 LATB=Chasing_LED; // Send Chasing_LED to PORT B
11 for (int x=0; x<=20; x++) __delay_ms(50); //Generate 1 second delay
12 Chasing_LED = Chasing_LED << 1; // Shift left Chasing_LED
13 if (Chasing_LED == 0) Chasing_LED =1;
14 }
}
You can download the full project files (MPLAB XC8 source code and Proteus Schematic design) below here.
Note: All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip
files).
Related Posts:
Microcontroller Interfacing Basics
Flowcode Variables