Microcontrollers and Systems Lesson7
Microcontrollers and Systems Lesson7
PIC16F877A Microcontroller
The PIC microcontroller PIC16f877a is one of the most renowned microcontrollers in the
industry. This microcontroller is very convenient to use, the coding or programming of this
controller is also easier. One of the main advantages is that it can be write-erase as many times
as possible because it uses FLASH memory technology. It has a total number of 40 pins and
there are 33 pins for input and output. PIC16F877A is used in many pic microcontroller
projects. PIC16F877A also have much application in digital electronics circuits.
PIC16f877a finds its applications in a huge number of devices. It is used in remote sensors,
security and safety devices, home automation and many industrial instruments.
An EEPROM is also featured in it which makes it possible to store some of the information
permanently like transmitter codes and receiver frequencies and some other related data. The
cost of this controller is low and its handling is also easy. It is flexible and can be used in areas
where microcontrollers have never been used before as in microprocessor applications and
timer functions etc.
• It has a smaller 35 instructions set.
• It can operate up to 20MHz frequency with 200ns instruction cycle.
• The operating voltage is between 4.2 volts to 5.5 volts. If you provide it
voltage more than 5.5 volts, it may get damaged permanently.
As it has been mentioned before, there are 40 pins of this microcontroller IC. It consists of two
8 bit and one 16 bit timer. Capture and compare modules, serial ports, parallel ports and five
input/output ports are also present in it. This picture shows the pinout diagram of
PIC16F877A.
PIN 1: MCLR:The first pin is the master clear pin of this IC. It resets the microcontroller and
is active low, meaning that it should constantly be given a voltage of 5V and if 0 V are given
then the controller is reset. Resetting the controller will bring it back to the first line of the
PIN 15: RC0/T1OSO/T1CKI: has three functions. It is a bidirectional digital input output
port / Timer1 oscillator output / Timer1external clock input
PIN 16: RC1/T1OSI/CCP2: has three functions. It is a bidirectional digital input output port /
Timer1 oscillator input / the capture 2 input - compare 2 output - PWM 2 output.
PIN 17: RC2/CCP1: has two functions. It is a bidirectional digital input output port/ It can
be the capture 1 input/ compare 1 output/ PWM 1 output.
PIN 18: RC3/SCK/SCL: has three functions. It is a bidirectional digital input-output port/ It
can be the input-output for synchronous serial clock for SPI modes /input-output for
synchronous serial clock for I2C mode.
PIN 23: RC4/SDI/SDA: has three functions. It is a bidirectional digital input-output port / It
can be the SPI data in pin/ data input-output pin in I2C mode.
PIN 24: RC5/SDO: has two functions. It is a bidirectional digital input-output port/ It can be
the SPI data out in the SPI mode.
PIN 25: RC6/TX/CK: has three functions. It is a bidirectional digital input-output port/ It can
be the asynchronous transmit (output) data pin for USART/ synchronous clock pin.
PIN 26: RC7/RX/DT: has three functions. It is a bidirectional digital input-output port/ It
can be the USART asynchronous receive (input) data pin /synchronous data pin.
PIN 19, 20, 21, 22, 27, 28, 29, and 30: All of these pins belong to PORTD which is again a
bidirectional input and output port. When the microprocessor bus is to be interfaced, it can act
as the parallel slave port data.
PIN 33-40: PORT B: All these pins belong to PORTB. Out of which RB0 can be used as the
external interrupt pin and RB6 and RB7 can be used as in-circuit debugger pins.
PIN 33: RB0/INT: has two functions. It can be a bidirectional digital input output port / can
be used as external interrupt (input) pin
PIN 34: RB1: It is a bidirectional digital input output port
PIN 35: RB2: It is a bidirectional digital input output port
PIN 39: RB6/PGC: has two functions. It can be a bidirectional digital input-output port/ It
can be used as in circuit debugger and ICSP programing clock.
PIN 40: RB7/PGD: has two functions. It can be a bidirectional digital input-output port/ It
can be used as in circuit debugger and ICSP programing data.
Note: All pins have multiple functionalities. Like PORTC pins can be used as digital input pins,
digital output pins, for UART communication, I2C communication. But, we can use one
function of each port at a time or you have to program it smartly so that you can switch between
different functionalities. Like once you need one pin to read analog signal, define it for analog
purpose and then switch to other functions like digital output etc. For beginners, we suggest
to use one pin for single functionality and you can use multi-function mode, once you
become an expert in programming.
7.3- How to program the input and output ports
• As we have studied 5 input and output ports namely PORTA, PORTB, PORTC,
PORTD and PORTE which can be digital as well as analog.
• We will configure them according to our requirements. But in case of analog
mode, the pins or the ports can only act as inputs. There is a built in A to D
converter which is used in such cases. Multiplexer circuits are also used.
• But in digital mode, there is no restriction. We can configure the ports as output
or as input. This is done through programming. For PIC the preferable
compiler is mikro C pro which can be downloaded from their website.
• There is a control register named as ‘TRIS’ which controls the direction of
ports. For different ports there are different registers such as TRISA, TRISB
TRISC, TRISD, and TRISE.
• If we set a bit of the TRIS register to 0, the corresponding port bit will act as
the digital output.
• If we set a bit of the TRIS register to 1, the corresponding port bit will act as
the digital input.
Four popular compiler which are used to program pic microcontrollers are MPLAB
XC8, Mikro C for pic, PIC CCS compiler and Hi-Tech compiler.
The official compiler is MPLAB XC8 compiler which is developed by manufactures of
PIC16F877A.
We generally recommend Mikro C for pic compiler for beginners and MPLAB XC8 compiler
for those who want to learn pic microcontrollers programming from register level bare metal
concepts.
Like all other microcontroller, PIC16F877A also provide built-in useful features as mentioned
in this list:
- Analog to digital converter module : It has 8 bit ADC module which consists of 8 channels
(AN0,…,AN7) . We can use 8 analog sensors with this microcontroller.
-TIMERS: It provides three timers: timer0, timer1 and timer2. All these timers can be used
either in timer mode or in counter mode (timer/counter). These timers are used to generate
delays, pulse width modulation (PWM), counting external events and timer interrupts.
• Power on Reset
• Multiple oscillator group
• In-Circuit Debugger
• In-Circuit Serial programming
• Low voltage ICSP programming