Sunday, January 14, 2018

Led Blinking Using PIC16F877A in Basic and C Language

PIC16F877A Pin out & Descriptions

There are 5 ports that provide parallel I/O interfaces to outside world PORTA, PORTB, PORTC, PORTD, PORTE,
Each port provides 8 bidirectional digital I/O lines which are conneected to PIC16F877A pins provided that alternate functions are not selected on that port.
Eventhough Bidirectional at any time the I/O line can either be INPUT or OUTPUT
The Directions of each I/O lines must be configured (INPUT or OUTPUT) before they are used.

I/O Registers related to Ports




By clearing some bit of the TRISx register (bit=0), the corresponding port pin is configured as output. Similarly, by setting some bit of the TRISx register (bit=1), the corresponding port pin is configured as input. This rule is easy to remember 0=Output, 1= Input. 


Role of Pull up resistors at the port pins

Let us say you have configured a port pin as input pin.
If there is nothing connected with the pin and your program reads the state of the pin, will it be high or low??? It will be difficult to tell
This phenomena is referred to as floating state.
In order to prevent this unknown state , pull up resistor is used.
Pull ups are often used with button and switches.



A switch connected at the input pin without pull up resistor
A switch connected at the input pin with pull up resistor 

Summary Action During Output

Step1: If we want to send (out) data through a pin of a port, the corresponding bit of TRISx Register has to be set to ‘0’.
Step 2: Then if we want to send ‘1’ write ‘1’ to the corresponding bit of PORTx Register and ‘0’ if we want to send ‘0’.
Note: We can not send data (either ‘1’ or ‘0’) if we do not complete step 1. At START UP or UPON RESET TRISx register contains 0x00, that is all pins remain at output state.

Summary Action During Input

Step1: If we want to read (in) data through a pin of a port, the corresponding bit of TRISx Register has to be set to ‘1’.
Step 2: Whatever data is in that pin of the port the data will appear in the corresponding bit of TRISx register.
Step 3: Each pin has a provision of connecting a pull up resistor internally. If we want to connect that resistor, we have to write ‘1’ to the corresponding bit of PORTx  register.
Note:  At START UP or UPON RESET, TRISx register contains 0x11. That is the port remains at Input state. But if it is used as OUTPUT at one stage of program, afterwards, we can not read data from a pin if we do not complete step 1.  

A Practical example of Outputting Data through a Port

Let us assume that 8 LEDs are connected to 8-pins of PORTD of an PIC16F877A chip.
We want to glow eight LEDs alternately [if '1' is seent, the corresponding LED glows ]
And they will toggle at every 1 sec and it will be repeated continuously.

What is an LED?

Light Emitting Diode (LED) is a special diode that emits light when electric voltage is applied to it. It is a common electronic equipment used in many devices for indication purpose. There are two leads of an LED that are used to supply input voltage. The longer lead is positive and known as "Post", the smaller is negative known as "Anvil"

Connection of LEDs with a Port


Proteus Design:



PIC Simulator IDE:

PIC Simulator IDE is powerful application that supplies Microchip Microcontroller users with user-friendly graphical development environment for windows with integrated simulator (emulator), pic basic compiler, assembler, disassembler and debugger. PIC simulator IDE supports the extensive number of microcontrollers (MCUs) from the Microchip 8-bit PIC Mid-Range architecture product line (selected PIC16F,PIC12F,PIC10F).

Basic Code:


Define CONF_WORD = 0x3f72       ' Configuration of PIC16F877A 
Define CLOCK_FREQUENCY = 12    ' Clock frequency 12MHz 
AllDigital                                       ' All Ports are Digital 
Dim i As Byte                           ' i is a variables, data types is Byte 
TRISD = 0x00                     ' PORTD is output 
PORTD= 0x00                   ' PORTD is zero or off state 
loop:                                 ' loop call
  For i = 0 To 255              ' counting 0 to 255 by the for loop 
  PORTD = i           ' PORTD output will depend i variable when i=1,PORTD will be 1
  WaitMs 100        ' delay 100ms
  Next i                  ' count next number 
Goto loop            ' finishing for loop then goto again "loop "


C Code 

int i:
void main (){
                TRISD=0x00;
                PORTD= 0x00;
while(1){
for(i=0; i<255; i++){
       PORTD=i;
       delay_ms(100);
}
}
}



Video link: