Introduction to PIC
PIC stands for "Peripheral Interface Controller"
Microchip Technologies, under Steve Sanghi's leadership, found this
microcontroller and coined the term PIC to identify their single chip
microcontrollers.
It initially started with the low end architectures which executed 12 bit
wide instructions with simple I/O functions. The upgrading of these low
end architectures to include more peripherals, registers and data and
program memory gave rise to the mid range (16C6X,16C7X,16F87X) PIC
microcontrollers.
The PIC microcontroller architecture comprises of CPU, I/O ports, memory
organization, A/D converter, timers/counters, interrupts, serial
communication, oscillator and CCP module which are discussed in detail
in the upcoming slides
PIC16F877A
MICROCONTROLLER
REGISTER FILE MAP
The General Purpose register file
can be accessed either directly or
indirectly, through the File Select
Register (FSR).
The Special Function Registers are
registers used by the CPU and
peripheral modules for controlling
the desired operation of the
device.
The Special Function Registers can be
classified into two sets:
core (CPU) and peripheral.
Pin Direction Select Registers:
(Input/Output)
TRISA
TRISB
TRISC
TRISD
TRISE
Pin Status Select Registers:
(High/Low)
PORTA
PORTB
PORTC
PORTD
PORTE
Example:
TRISC = 0x7F; //0111 1111
RC7 is set as Output
RC0 – RC6 set as Input
PORTB = 0x05; //0000 0101
RB0 and RB2 set as Low
All other pins are set as High
STATUS REGISTER
• The Status register contains the arithmetic status of the ALU, the Reset status
and the bank select bits for data memory.
IRP: Register Bank Select bit (used for indirect addressing)
0 = Bank 0, 1 (00h - FFh)
1 = Bank 2, 3 (100h - 1FFh)
The IRP bit is not used by the PIC16F8X. IRP should be maintained clear
RP1:RP0: Register Bank Select bits (used for direct addressing)
00 = Bank 0 (00h - 7Fh)
01 = Bank 1 (80h - FFh)
10 = Bank 2 (100h - 17Fh)
11 = Bank 3 (180h - 1FFh)
Each bank is 128 bytes. Only bit RP0 is used by the PIC16F8X. RP1 should be maintained
clear.
TO : Time-out Bit
PD : Power Down Bit(Active Low)
Z : Zero Bit
DC : Digit Carry/ Borrow
C : Carry/Borrow
RBPU: PORTB Pull-up Enable
This bit is Readable and Writable and after a reset it will get the value 1. The RB
ports have an internal programmable pull-up resistor to minimize the use of
external pull-up resistors when needed. This bit will enable or disable
those resistors. The values it can get are:
•0: The RB pull-up resistors are enabled
•1: The RB pull-up resistors are disabled
Bit 6 - INTEDG: RB0/INT pin Interrupt Edge Select
Bit 5 - T0CS: Timer 0 Clock Source Select
Bit 4 - TOSE: Timer 0 Source Edge Select
Bit 3 - PSA: Pre scalar Assignment
Bit <2:0> - PS<2:0>: Pre scalar rate selection
OPTION_REG REGISTER
Features of Pic Microcontroller
• It has a smaller 35 instructions set.
• It can operate up to 20MHz frequency.
• 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.
• It does not have an internal oscillator like other PIC18F46K22, PIC18F4550.
• The maximum current each PORT can sink or source is around 100mA.
Therefore, the current limit for each GPIO pin of PIC16F877A is 10 mili
ampere.
• It is available in four IC packaging such as 40-pin PDIP 44-pin PLCC, 44-
pin TQFP, 44-pin QFN
•Analog to digital converter module : It has 8 bit ADC module which consists of 8
channels. 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. These timers are used to generate
delays, pulse width modulation, counting external events and timer interrupts.
TIMER0 is a 8 bit timer and it can operate with internal or external clock frequency.
When we use Timer0 in timer mode, we usually operate it with internal frequency
and in counter mode, we trigger it with external clock source. Similarly, TIMER1 is a
16-bit timer and it can also operate in both modes. TIMER2 is also of 8-bit. It is used
with PWM as a time base for CCP module.
•EEPROM : It also has built-in Electrically erasable read only memory 256 x 8 bytes
which can used to store data permanently even if the microcontroller is switched off,
data will remain there. It is usually used with electronics lock related projects.
•PWM modules : It also provide 2 CCP modules. CCP stands for capture compare
PWM modules. We can easily generate two PWM signals with this microcontroller.
The maximum resolution it supports is 10 bits. you can read PWM using PIC16F877A
PIC16F877A microcontroller tutorial for more information and programming.
•Serial or UART communication pins : It support one UART channel. UART pins are
used for serial communication between digital devices. RC7 pin is a transmitter or
RX pin which is pin number 26. RC6 is a receiver or Tx pin which is pin number 25.
For additional details, check this complete guide on serial communication using
pic16f877a microcontroller.
•I2C Communication : PIC16F877A also support I2C communication and its has
one module for I2C communication. Pin#18/RC3 and 23/RC4
are SCL and SDA pins respectively. SCL is a serial clock line and SDA is serial data
line. I2C communication tutorial will help you understand further.
•Interrupts : Interrupts have wonderful applications in embedded systems field. If
you don’t know about interrupts, I suggest you to get complete understanding
about them, you will not get command on embedded programming them.
PIC16F877A microcontroller provides 8 types of interrupts namley; External
interrupts, timer interrupts, PORT state change interrupts, UART interrupt, I2C,
PWM interrupts. you can read this guide on pic microcontroller interrupts for
additional information.
•Comparator module : It has a comparator module which composed of two
comparators. They are used for comparison of analog signal similar to
comparators in electronics circuits. Input pins for these comparators are RA0,
RA1, RA2 and RA3 and output can measured through RA4 and RA5.
•Watchdog timer : WDT is a on chip separate oscillator which runs freely. It is a separate
oscillator from OSC1/CLKI. WDT will also work even if the device is in sleep mode. It is used to
wake up device from sleep mode and also used to generate watchdog timer reset.
•Sleep mode : PIC16F877A also provide sleep mode operation. In this mode, device operates at
very low power. All peripherals draws minimum amount of current. Wake up from sleep mode
from interrupts resources like timer1 interrupt, uart interrupt, EEPROM write completion
operation and many others.
•Brown out detection : It also has a brown out detection circuit which detects the significant
drop in power supply voltage. If supply voltage drop from a certain limit, it will generate a
interrupt signals. This configuration bit (BODEN) is used to disable or enable this circuitry.
•Brown out reset : This option reset the device upon detection of brown out interrupt signal from
BODEN signal. if supply voltage goes below threshold for more than 100 micro seconds,
•Programmable code protection, Brown out reset will occur and device will remain reset until the
the voltage raise to its nominal value. Device checks for voltage after every 72ms
WRITING AN ALP FOR LED BLINKING
To write an ALP to turn LED’s ON and OFF connected to PORTC in PIC16f877a:
MOV P2,#00H //Setting PORTC as an output PORT
AGAIN: CLR P2.6 //Initially the pins 1 and 6 are low
CLR P2.1
LCALL DELAY //Calling a delay function
SETB P2.6 //Setting pin 6 high
LCALL DELAY //Calling a delay function
SETB P2.1 //Setting pin 1 high
CLR P2.6 //Setting pin 6 as low
LCALL DELAY //Calling a delay function
SETB P2.6 //Setting both the pins 1 and 6 as high
LCALL DELAY //Calling a delay function
SJMP AGAIN //Creating a loop to repeat process indefinitely
EQUIVALENT C CODE FOR LED BLINKING
void main(){
TRISC=0x00; //TRIS-> Command used to set the port as input or output
//All the pins are set at output state
while(1){
PORTC=0x00; //Initially all the ports does not give output i.e Rc1 and Rc6 are OFF
__delay_ms(1000); //A delay of 1 sec is given between the two states
PORTC=0x40; //State when Rc1 is OFF and Rc6 is ON
__delay_ms(1000); //Delay of 1 sec
PORTC=0x02; //State when Rc1 is On and Rc6 is OFF
__delay_ms(1000); //Delay of 1 sec
PORTC=0x42; //State when both the LED's at Rc1 and Rc6 are ON
__delay_ms(1000); //Delay of 1 sec
}
WRITING AN ALP TO DETECT A KEY PRESS
AND BLINK LED ACCORDINGLY
CONDITION OUTPUT
RC0->LOW RD2->HIGH
RC1->LOW RD4->HIGH
RC2->LOW RD2,RD4->LOW
RC3->LOW RD2,RD4->HIGH
To check the input from PORTC(consider pull up resistors are connected to the pins Rc0-Rc3) and blink LED
accordingly with respect to the input:
MOV P2, #0FH //Setting pins 0-3 as input and 4-7 as output pins
MOV P3,#00H //Setting all the pins of PORTD as output pins
AGAIN: MOV A,P2 //Getting input from PORTC as storing in accumulator register
AND A,01H //Checking if Rc0 is pressed
JZ CASE1 //If Rc0 is pressed jump to case1
AND A,#02H //Checking if Rc1 is pressed
JZ CASE2 //If Rc1 is pressed jump to case2
AND A,#04H //Checking if Rc2 is pressed
JZ CASE3 //If Rc2 is pressed jump to case3
AND A,#08H //Checking if Rc3 is pressed
JZ CASE4 //If Rc3 is pressed jump to case4
CASE1: MOV P3,#04H //Making Rd2 high
SJMP AGAIN //Jumping back to again function to check indefinitely
CASE2: MOV P3,#10H //Making Rd4 high
SJMP AGAIN //Jumping back to again function to check indefinitely
CASE3: MOV P3,#00H //Making all the pins as low
SJMP AGAIN //Jumping back to again function to check indefinitely
CASE4: MOV P3,#14H //Making both pins Rd2 and Rd4 high
SJMP AGAIN //Jumping back to again function to check indefinitely
EQUIVALENT C CODE FOR CHECKING KEY
PRESS:
void main(){
TRISC=0x0F; //TRISC=00001111, Rc7 to Rc4 are output ports and Rc3 to Rc1 are input ports
TRISD=0x00; //TRISD=00000000, Rd7 to Rd0 are set as output ports
PORTC=0x00; //Default initialization of all the ports of PORTC to low
PORTD=0x00; //Default initialization of all the ports of PORTD to low
while(1){
if (PORTC==0x0E){ //Checking if PORTC=00001110 i.e if switch at Rc0 is closed
PORTD=0x04; //Producing output as PORTD=00000100 i.e Rd2->LED->ON and Rd4->LED->OFF }
else if(PORTC==0x0D){ //Checking if PORTC=00001101 i.e if switch at Rc1 is closed
PORTD=0x10; //Producing output as PORTD=00010000 i.e Rd2->LED->OFF and Rd4->LED->ON }
else if(PORTC==0x0B){ //Checking if PORTC=00001011 i.e if switch at Rc2 is closed
PORTD=0x00; //Producing output as PORTD=00000000 i.e Rd2->LED->OFF and Rd4->LED->OFF }
else if(PORTC==0x07){ //Checking if PORTC=00000111 i.e if switch at Rc3 is closed
PORTD=0x14; //Producing output as PORTD=00010100 i.e Rd2->LED->ON and Rd4->LED->ON }
else
PORTD=0x00; //Default case PORTD=00000000, all the ports are low }
}