Pic 16F84
Pic 16F84
1
Outline
• Microcontroller PIC 16F84
– Architecture, Instructions, Applications
2
h e
T e r
1 : l l
r t r o
te n
a p c o 8 4
h
C ic r o 6 F
1
m IC
P
3
Plan of presentation
• Definition of microcontroller
• Architecture
• Registers
• Timers
• Interrupts
• Addressing modes
• subroutines
4
Microcontrollers versus
Microprocessors
• Microcontroller differs from a microprocessor in
many ways. First and the most important is its
functionality. In order for a microprocessor to be
used, other components such as memory, or
components for receiving and sending data must be
added to it. In short that means that microprocessor
is the very heart of the computer.
10
Reset
• Reset is used for putting the microcontroller into a 'known' condition. That
practically means that microcontroller can behave rather inaccurately
under certain undesirable conditions. In order to continue its proper
functioning it has to be reset, meaning all registers would be placed in a
starting position. Reset is not only used when microcontroller doesn't
behave the way we want it to, but can also be used when trying out a
device as an interrupt in program execution, or to get a microcontroller
ready when loading a program.
• In order to prevent from bringing a logical zero to MCLR pin accidentally
(line above it means that reset is activated by a logical zero), MCLR has to
be connected via resistor to the positive supply pole. Resistor should be
between 5 and 10K. This kind of resistor whose function is to keep a
certain line on a logical one as a preventive, is called a pull up.
11
• Central processing unit (CPU) is the brain of a microcontroller. That part is
responsible for finding and fetching the right instruction which needs to be
executed, for decoding that instruction, and finally for its execution.
• Arithmetic logic unit is responsible for performing operations of adding,
subtracting, moving (left or right within a register) and logic operations.
Moving data inside a register is also known as 'shifting'. PIC16F84 contains
an 8-bit arithmetic logic unit and 8-bit work registers.
• Depending on which instruction is being executed, ALU can affect values of
Carry (C), Digit Carry (DC), and Zero (Z) bits in STATUS register.
12
13
PORTB and TRISB
• PORTB has adjoined 8 pins. The appropriate register for data
direction is TRISB. Setting a bit in TRISB register defines the
corresponding port pin as input, and resetting a bit in TRISB
register defines the corresponding port pin as output.
14
PORTA and TRISA
• PORTA has 5 adjoining pins. The corresponding register for
data direction is TRISA at address 85h. Like with port B, setting
a bit in TRISA register defines also the corresponding port pin
as input, and clearing a bit in TRISA register defines the
corresponding port pin as output.
15
16
Some instructions
• BSF : bit set in file register
– BSF PORTA,3
– BSF TRISA,2
• BCF : bit clear in file register
– BCF TRISB,6; RB6 is output
• MOVLW: move lateral (immediate number) to register w
– MOVLW 0x6D
– ADDLW 0x56; w+56 w
– ANDLW B’00011000’
– ADDLW D’56
17
Memory organization
PIC16F84 has two separate memory blocks, one for data and the
other for program. EEPROM memory with GPR and SFR
registers in RAM memory make up the data block, while FLASH
memory makes up the program block.
• Program memory
• SFR registers
Registers which take up first 12 locations in banks 0 and 1 are registers of
specialized function assigned with certain blocks of the microcontroller.
These are called Special Function Registers.
19
Some instructions
• Movwf, f: move W to a file register
– MOVWF PORTA
– MOVLW 0x45
– MOVWF TRISA
• ADDWF f,d ;add W+fd (destination)
– ADDWF PORTA,w; porta+ww
– ADDWF PORTA,f; porta+wporta
• INCF PORTA,f; porta+1porta
• DECF PORTB,w; portb-1w
20
Bit test, indirect mode
• BTFSS PORTA,3
• GOTO label
• BTFSC INTCON,5
21
Program counter and stack
• Program Counter
Program counter (PC) is a 13-bit register that contains the address of the
instruction being executed. It is physically carried out as a combination of a
5-bit register PCLATH for the five higher bits of the address, and the 8-bit
register PCL for the lower 8 bits of the address. By its incrementing or
change (i.e. in case of jumps) microcontroller executes program instructions
step-by-step.
• Stack
PIC16F84 has a 13-bit stack with 8 levels, or in other words, a group of 8
memory locations, 13 bits wide, with special purpose. Its basic role is to
keep the value of program counter after a jump from the main program to an
address of a subprogram . In order for a program to know how to go back to
the point where it started from, it has to return the value of a program
counter from a stack.
22
Addressing modes
• Direct Addressing
25
Interrupts
• PIC16F84 has four interrupt sources:
26
27
Interrupts
• PIC16F84 has four interrupt sources:
28
Interrupts
• Procedure of recording
important registers before
going to an interrupt routine
is called PUSH, while the
procedure which brings
recorded values back, is
called POP. PUSH and POP
are instructions with some
other microcontrollers (Intel),
but are so widely accepted
that a whole operation is
named after them.
32
33
Program
34
Watchdog
35
EEPROM
• PIC16F84 has 64 bytes of EEPROM memory locations on addresses from
00h to 63h that can be written to or read from. The most important
characteristic of this memory is that it does not lose its contents with the
loss of power supply. Data can be retained in EEPROM without power
supply for up to 40 years (as manufacturer of PIC16F84 microcontroller
states), and up to 1 million cycles of writing can be executed.
36
37
Machine cycle
• Instruction cycle consists of cycles Q1, Q2, Q3 and Q4. Cycles of calling
and executing instructions are connected in such a way that in order to
make a call, one instruction cycle is needed, and one more is needed for
decoding and execution. However, due to pipelining, each instruction is
effectively executed in one cycle. If instruction causes a change on program
counter, and PC doesn't point to the following but to some other address
(which can be the case with jumps or with calling subprograms), two cycles
are needed for executing an instruction. This is so because instruction must
be processed again, but this time from the right address. Cycle of calling
begins with Q1 clock, by writing into instruction register (IR). Decoding and
executing begins with Q2, Q3 and Q4 clocks.
38
Interrupt location
LIST p=16F877
#include "P16F877.INC"
cblock 0x20
count, lc1, lc2;
endc
org 4
goto inthlr
…..
…
39
Assembly language programming
• "Assembly language" and "assembler" are two different notions. The first
represents a set of rules used in writing a program for a microcontroller,
and the other is a program on the personal computer which translates
assembly language into a language of zeros and ones. A program that is
translated into "zeros" and "ones" is also called "machine language".
40
Assembly language programming
_CONFIG _CP_OFF&_WDT_OFF&PWRTE_ON&XT_OSC
41
42
INCFSZ Increment f, skip if=0
DECFSZ
43
Other instructions
• Clrw
• Clrf
• Movf
• …
44
Macro
bank0 macro;
bcf STATUS, RP0
endm; End of macro
bank1macro;
bsf STATUS, RP0;
endm;
Enableint macro;
bsf INTCON, 7; Set the bit
endm; End of macro
49
Program
50
DEBOUNCE
• Button function is simple. When we push a button, two contacts are joined
together and connection is made. Still, it isn't all that simple. The problem
lies in the nature of voltage as an electrical dimension, and in the
imperfection of mechanical contacts. That is to say, before contact is made
or cut off, there is a short time period when vibration (oscillation) can occur
as a result of unevenness of mechanical contacts, or as a result of the
different speed in pushing a button (this depends on person who pushes the
button). The term given to this phenomena is called SWITCH (CONTACT)
DEBOUNCE.
51
Optocouplers
• The way it works is simple: when a signal arrives, the LED
within the optocoupler is turned on, and it illuminates the
base of a photo-transistor within the same case. When the
transistor is activated, the voltage between collector and
emitter falls to 0.7V or less and the microcontroller sees
this as a logic zero on its RA4 pin.
52
53
Optocoupler - output
• An Optocoupler can be also used to separate the
output signals. If optocoupler LED is connected to
microcontroller pin, logical zero on pin will activate
optocoupler LED, thus activating the transistor. This
will consequently switch on LED in the part of device
working on 12V. Layout of this connection is shown
below.
54
Program
55
Sounds
• Generating sound
In microcontroller systems, beeper is used for indicating certain
occurrences, such as push of a button or an error. To have the
beeper started, it needs to be delivered a string in binary code -
in this way, you can create sounds according to your needs.
Connecting the beeper is fairly simple: one pin is connected to
the mass, and the other to the microcontroller pin through a
capacitor, as shown on the following image.
56
Program
57
7 segment-display
• To produce a 4, 5 or 6 digit display, all the 7-segment displays are connected in parallel. The common line
(the common-cathode line) is taken out separately and this line is taken low for a short period of time to turn
on the display. Each display is turned on at a rate above 100 times per second, and it will appear that all the
displays are turned on at the same time. As each display is turned on, the appropriate information must be
delivered to it so that it will give the correct reading.
58
Program
59