Blinking LED Using PIC 16F877A micro-controller
By Bernice Zuiya
Introduction
The blinking of LED (Light Emitting Diode) using a PIC micro-controller is one of the important step when for the first time you decide to learn micro-controllers; Basically it consist of toggling one or many pin of a micro-controller connected to LED(s) for this first and easy project only two registers are used ( TRIS and PORT ). One is used to give the direction of a pin whether it should works as Input or Output pin. If you already know how to used an LED then this project maybe easy for you but in case you never used an LED then what you have to know is an LED has two terminals p and n as compared to a diode where for a LED the long pin correspond to p terminal and the short is the n terminal, when this is connected in forward configuration in series with a resistance which limits the current flowing in the LED and a battery.
Let first understand some important aspect of this project which consist simply of switching ON and OFF an LED using an 8-Bit PIC micro-controller, there are various type of LEDs according to their light colors; But in case you want to have more colors in one LED only, this maybe possible with the RGB LED and this topic will be developed so far in our tutorial and project.
Working
So far we said that the blinking of an LED is done using two important registers:
-
TRIS register: this register is used to set the direction of a port of a micro-controller or even a single pin connected to a port of a MCU, the PIC16F877A has 5 Ports (A,B,C,D and E) where if an LED is connected to for example one pin of port A then the TRIS register will be written as TRISA or if the LED is on port B then this is TRISB so to know which pin is working as an input pin or output pin you basically need to assign a 1 or 0 to a specific pin of a port for example: OSat board version 6.10 has a test LED connected on port B and is on 2nd pin of this port ( RB1 ), if we want to toggle this pin we need to set it as an output pin, we will discuss two ways of representation here: 1. Binary representation: TRISB = 0b11111101 when this line means all pin connected on port B except the second are in input mode and the second pin is in output mode 2. Hexadecimal representation: TRISB = 0xFE you can also use the decimal but I prefer to use these two mode.
-
PORT register: is used to give the state of a pin connected to a port of a PIC micro-controller, if we want the toggle a pin of a port this register is responsible. if you desire to put a pin high you need to assign 1 and 0 for low state. If for example you want to switch ON the test LED connected to port B you can write as in binary representation: PORTB = 0b00000010 and Hexadecimal representation: PORTB = 0x02
But in the blinking of LED it better to introduce some delay between the two state, when the LED is ON and when it is OFF, mikroC compiler offers the delay function; the step of creating a blinking project using mikroC compiler and MPLAB IDE with XC8 compiler is shown in the video below and an additional simulation using Proteus software.
LEDs
The circuit diagram shown below has the following elements but in case you are working with our Board you don't need to add components.
-
a resistor of value 4k7 ohm is connected in series with +5 volt and the reset pin (1st pin)
-
Q1 is the crystal oscillator in this case its frequency oscillation is 20 MHz.
-
supply: the circuit is connected with a voltage maximum equal 5 volt
-
R3 is a limiting current resistor to protect the LED.
-
C1 & C2 are of 22pF.
Circuit Diagram
Code and Comment
/* The LED will basically remain ON for one second and one second OFF*/
// mikroC compiler for PIC <24/sept/2016>
unsigned int i,j;
void main(void){
TRISB.B1 = 0; // This pin is considered to be as an output pin
PORTB = 0X00; // All pin of PORTB are at low state
for(;;){ // infinite for loop
for(i =0 ; i < 10 ; i++){
PORTB.B1 = 1; // the second pin of PORTB is at high State
Delay_ms(100); // Delay of 100 ms
}
for(j =0 ; j < 10 ; j++){
PORTB.B1 = 1; // the second pin of PORTB is at low State
Delay_ms(100); // Delay of 100 ms
}
}// end of for(;;)
}// end main function
This code were written using mikroC compiler software, you can copy and compile it or modify it as well, this is used to blink an led OFF and ON (second).
Download the full project here