4.50 JD                      Add to cart

  High Brightness 5 mm LED 

   0.10 JD                     Add to cart

  830-Point Breadboard

   6.00 JD                      Add to cart

  Tactile Push Button

Tactile Button

   0.15 JD                      Add to cart



LED Blinking

Every time you learn a new programing language you meet the "Hello World!" program but in microcontrollers world the first program is "LED Blinking", PIC16F877A has 33 eneral Purpose Input output pins (GPIO),which could be used as input or output by configuiring the corespondiiing.

PIC16f877a has five  ports PORTA,PORTB,PORTC,PORTD and PORTE and five direction registers one for each TRISA,TRISB,TRISC,TRISD and TRISE. 



  In the next figure TRIS register value is 00110101      
 where 0 mean that this bit will act as output, while 1 
means that this bit will act as input.

In this lesson we will use MikroC enviroment to write 
our code you can down load it for free from here



 Maybe the main goal of this lesson is to lear how to get PIC16f877A running the code inside it. Here is the required connection for our LED Blinking.



  Like any microcontroller we need an oscillator for our PIC16f877A, you can see it connected to pins 13 and 14. the oscillator value
could be 4, 8 or 16 MHz. and for more stable oscillator signal two 30 pF capacitos are connected between ech leg for the oscillators and ground.

  Another thing to note is the reset button circuit, it's a simple circuit that drive the Master Clear  (MCLR) pin always to High voltage to keep normal operation of the microcontroller unless the bush button is pressed, at that moment the MCLR pin is grounded and the microcontroller resets the operation of the code.

 You migh also noticed that there are two VCC and two VSS pins, this is nessesary to spread the heat generated by current flow. 




void main ()
TRISD= 0b11111110; // set bit0 to output and other bits to input
PORTD=1; // LED ON , bit0 of portd --> 1
Delay_ms(100); // Some time delay so we could notice the blinking
PORTD=0x00; // LED OFF,bit0 of portd --> 0

Delay_ms(100);//100 ms delay


Code Details

void main()           
   All of your projects should have both the main function and the endless loop.The main() function in C language
projcts is the function that the processor works in and call other functions from.                                   
 TRISD= 0b11111110;

  As you noticed we used the binary format supported by mikroC, by starting with 0b and filling 8 bits 


  The forever loop, you dont need your programe to be excuted once, you need it to keep working for ever. the for ever loop 
is created by using a while with alwyas true condition.


  In our circuit we have connected the led to bit0 of PORTD so by setting bit0 to 1 the LED is on and by clearing it to zero the LED is off.
agaim here we used anoth formats that supported by mikroC the decimal format as is
and the the hexadecimal format as in:


To make it possible for us to notice the blinking we need a time delay between the On/Off states. without this you will see the LED ON all the