Professional Documents
Culture Documents
JBILOU Mohammed
Contact: mjbilou@hotmail.fr
Electrical Engineering
1
Definition of microcontroller
Architecture
Registers
Input/Output
Interrupts
Timer
Addressing modes
2
PIC16F84-04/P IC
CMOS PIC Microcontroller
Pins: 18
Configuration: Flash
Speed: 4MHz
EEPROM bits: 1K x 14
RAM bytes: 68
I/O lines: 13
3
DIP/SOIC
Pin Name Description
Number
OSC1/CLKIN 16 Oscillator crystal input/external clock source input.
Oscillator crystal output. Connects to crystal or resonator in
crystal oscillator mode. In RC mode, OSC2 pin outputs
OSC2/CLKOUT 15
CLKOUT which has 1/4 the frequency of OSC1, and denotes
the instruction cycle rate.
Master clear (reset) input/programming voltage input. This
MCLR 4
pin is an active low reset to the device.
RA0 17
RA1 18
PORTA is a bi-directional I/O port.
RA2 1
RA3 2
This Pin can also be selected to be the clock input to the
RA4/T0CKI 3
TMR0 timer/counter.
RB0/INT 6
RB1 7
RB2 8
RB3 9 PORTB is a bi-directional I/O port.
RB4 10 RB0/INT can also be selected as an external interrupt pin.
RB5 11
RB6 12
RB7 13
VSS 5 Ground reference for logic and I/O pins.
VDD 14 Positive supply for logic and I/O pins.
4
5
6
7
INDF: Data memory contents by indirect addressing
TMR0: Timer counter
PCL: Low order 8 bits of the program counter
STATUS: Flag of the calculation result
FSR: Indirect data memory address pointer
PORTA: PORTA DATA I/O
PORTB: PORTB DATA I/O
EEDATA: Data for EEPROM
EEADR: Address for EEPROM
PCLATH: Write buffer for upper 5 bits of the program counter
INTCON: Interruption control
OPTION_REG: Mode set
TRISA: Mode set for PORTA
TRISB: Mode set for PORTB
EECON1: Control Register for EEPROM
EECON2: Write protection Register for EEPROM
8
The first 12 bytes at the head of the
RAM file register are used for the SFR.
This group of registers is very
important to the operation of the PIC.
Below I will explain the operation of
these registers, which are necessary to
the management of the PIC.
9
SFR is an area of data memory dedicated to
registers that are required for configuration and
dataflow control. This dedicated memory area is
divided into a number of special function registers
which can not be used as general purpose registers
by the programmer. The special function registers
are TMR0, OPTION, PCL, STATUS, FSR, PORTA,
PORTB, TRISA, TRISB, EEDATA, EECON1, EEADR,
EECON2, PCLATH and INTCON and are situated in
the data memory locations shown in Figure below
10
Special Function Registers (SFR 1/2)
Value
Value
on
File on
Register Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit2 Bit 1 Bit 0 Power-
Address Other
On
Resets
Reset
Bank 0
Uses contents of FSR to address data memory (not a physical
0x00 INDF register) ---- ---- ---- ----
xxxx uuuu
0x01 TMR0 8-bit real-time clock/counter
xxxx uuuu
Least Significant 8 bits of the Program Counter (PC) 0000 0000
0x02 PCL
b7 b6 b5 b4 b3 b2 b1 b0 0000 0000
0001 000q
0x03 STATUS IRP RP1 RP0 TO PD Z DC C
1xxx quuu
xxxx uuuu
0x04 FSR Indirect data memory address pointer 0
xxxx uuuu
RA4/ ---x ---u
0x05 PORTA - - - RA3 RA2 RA1 RA0
T0CKI xxxx uuuu
xxxx uuuu
0x06 PORTB RB7 RB6 RB5 RB4 RB3 RB2 RB1 RB0
xxxx uuuu
0x07 Unimplemented Unimplemented location, read as '0 ---- ---- ---- ----
xxxx uuuu
0x08 EEDATA EEPROM data register
xxxx uuuu
xxxx uuuu
0x09 EEADR EEPROM address register
xxxx uuuu
Most Significant 5 Bits of the PC ---0 ---0
0x0A PCLATH - - -
b12 b11 b10 b9 b8 0000 0000
0000 0000
0x0B INTCON GIE EEIE T0IE INT E RBIE T0IF INTF RBIF
000x 000u
Special Function Registers (SFR 2/2)
Value on Value on
File Address Register Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit2 Bit 1 Bit 0 Power-On Other
Reset Resets
Bank 1
0x80 INDF Uses contents of FSR to address data memory (not a physical register) ---- ---- ---- ----
0x81 OPTION RBPU INTEDG T0CS T0SE PSA PS2 PS1 PS0 1111 1111 1111 1111
0x82 PCL Least Significant 8 bits of the Program Counter (PC) 0000 0000 0000 0000
0x83 STATUS IRP RP1 RP0 TO PD Z DC C 0001 1xxx 000q quuu
0x84 FSR Indirect data memory address pointer 0 xxxx xxxx uuuu uuuu
PORTA data direction register
0x85 TRISA - - - ---1 1111 ---1 1111
b4 b3 b2 b1 b0
PORTB data direction register
0x86 TRISB 1111 1111 1111 1111
b7 b6 b5 b4 b3 b2 b1 b0
0x87 Unimplemented Unimplemented location, read as '0 ---- ---- ---- ----
0x88 EECON1 - - - EEIF WRERR WREN WR RD ---0 x000 ---0 q000
0x89 EECON2 EEPROM control register 2 (not a physical register) ---- ---- ---- ----
Most Significant 5 Bits of the PC
0x8A PCLATH - - - ---0 0000 ---0 0000
b12 b11 b10 b9 b8
0x8b INTCON GIE EEIE T0IE INT E RBIE T0IF INTF RBIF 0000 000x 0000 000u
Legend: x = unknown, u = unchanged. - = unimplemented read as ’0’, q = value depends on condition, 0x stands for hexadecimal
12
R/W-0 R/W-0 R/W-0 R-1 R-1 R/W-x R/W-x R/W-x R = Readable bit
W= Writable bit
IRP RP1 RP0 TO PD Z DC C U = Unimplemented bit, read as ‘0’
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 -n= Value at POR reset
13
R/W-0 R/W-0 R/W-0 R-1 R-1 R/W-x R/W-x R/W-x R = Readable bit
W= Writable bit
IRP RP1 RP0 TO PD Z DC C U = Unimplemented bit, read as ‘0’
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 -n= Value at POR reset
14
15
The A port corresponds to the PORTA register and the B
port corresponds to the PORTB. Each register is composed
of 8 bits and the input/output pin corresponds every bit
As for PORTA, 5 bits from bit 0 to bit 4 are used and 3 bits
from bit 5 to bit 7 aren't used. As for PORTB, all of the 8
bits correspond to the input/output pins respectively
The mode (the input or the output) of each pin is specified
by the TRISA register (for PORTA) and the TRISB register
(for PORTB).
16
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.
17
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.
18
The A port and the B port rather differently in
the circuit and the function. Also, in the A port,
the RA4 pin can be used as the clock input of
the TMR0.
19
Interrupts are a mechanism of a microcontroller which enables it to respond
to some events at the moment they occur, regardless of what microcontroller
is doing at the time. This is a very important part, because it provides
connection between a microcontroller and environment which surrounds it.
Generally, each interrupt changes the program flow, interrupts it and after
executing an interrupt subprogram (interrupt routine) it continues from that
same point on.
20
PIC16F84 has four interrupt sources:
1. Termination of writing data to EEPROM.
2. TMR0 interrupt caused by timer overflow.
3. Interrupt during alteration on RB4, RB5, RB6
and RB7 pins of port B.
4. External interrupt from RB0/INT pin of
microcontroller.
R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-x R = Readable bit
W = Writable bit
GIE EEIE T0IE INTE RBIE T0IF INTF RBIF U = Unimplemented bit, read as ‘0’
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 -n = Value at POR reset
INTCON Register
21
22
23
External interrupt on RB0/INT pin of
microcontroller.
External interrupt on RB0/INT pin is triggered by
rising signal edge (if bit INTEDG=1 in OPTION<6>
register), or falling edge (if INTEDG=0). When
correct signal appears on INT pin, INTF bit is set in
INTCON register. INTF bit (INTCON<1>) must be
cleared in interrupt routine, so that interrupt
wouldn't occur again while going back to the main
program. This is an important part of the program
which programmer must not forget, or program
will constantly go into interrupt routine. Interrupt
can be turned off by resetting INTE control bit
(INTCON<4>). 24
This is an 8-bit counter that have 2 modes:
25
Tim er s ar e usu al ly th e
most complicated parts of
a microcontroller, so it is
necessary to set aside
m o r e t i m e f o r
understanding them
thoroughly. Through their
application it is possible to
establish relations between
a real dimension such as
"time" and a variable which
represents status of a
t i m e r w i t h i n a
microcontroller. Physically,
timer is a register whose
value is continually
increasing to 255, and then
it starts all over again: 0, 1,
2, 3, 4...255....0,1, 2,
3 . . . . . . e t c .
26
During each transition from 255 to zero, T0IF bit in INTCON register
is set. If interrupts are allowed to occur, this can be taken advantage
of in generating interrupts and in processing interrupt routine. It is
up to programmer to reset T0IF bit in interrupt routine, so that new
interrupt, or new overflow could be detected. Beside the internal
oscillator clock, timer status can also be increased by the external
clock on RA4/TOCKI pin. Choosing one of these two options is done
in OPTION register through T0CS bit. If this option of external clock
was selected, it would be possible to define the edge of a signal
(rising or falling), on which timer would increase its value.
27
OPTION_REG:
TOCS = 0 internal horloge
TOCS = 1 external horloge applied at RA4
TOSE = 0 increment on low-to-high transition
TOSE = 1 increment on high-to-low transition
Prescaler
programmer
28
29
"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".
30
In order to function properly, we must define
several microcontroller parameters such as:
- type of oscillator,
- whether watchdog timer is turned on, and
- whether internal reset circuit is enabled.
_CONFIG _CP_OFF&_WDT_OFF&PWRTE_ON&XT_OSC
31
32
33
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
34
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
35
;*****************************************************************************
; program led_int.asm
; A switch is connected to RB0 (input) and LED to RB1 (output) ; If the switch is set to
1, the LED must light on, if it is set to zero, it must go OFF
;*****************************************************************************
LIST p=16f84A, f=inhx8m, r = dec
INCLUDE "p16f84A.inc"
__CONFIG _CP_OFF & _XT_OSC & _PWRTE_OFF & _WDT_OFF
bsf STATUS,RP0 ; bank 1
movlw B'00000001'
movwf TRISB ; pour configurer RB0 en entrée
bcf STATUS,RP0 ; bank 0
tst btfss PORTB,0
goto off
bsf PORTB,1
goto tst
off bcf PORTB,1
goto tst
end
36
Suppose we work with a quartz of 4MHz. In this case, (4000000/4) =
1.000.000 Cycles per second. Each clock cycle thus lasts 1/1000000ème of
second, is 1µs.
If we decide to use the timer0 in its timer function and in interrupt mode.
We will therefore have an interruption every 256μs, that is to say almost
every quarter of a thousandth of a second.
37
If we want to make a flashing LED at a
frequency of + - 1Hz, we will need a 500ms PSA PS2 PS1 PS0 /tmr0 Temps
time delay, that is 2000 times more. So it's tmr0
0 0 1 0 8 2048 µs
38
We will therefore have to (500.000/65536) = 7,63 times in our
interruption subroutine. 7 or 8 times
39
;*********************************************************************
; INITIALISATIONS
;*********************************************************************
init clrf PORTA ; clear portA
clrf PORTB ; clear portB
bsf STATUS,RP0 ; bank 1
40
Interruption subroutine :
41
;*********************************************************************
; PROGRAMME PRINCIPAL *
;*********************************************************************
start bsf LED ; LED ON
movlw 256 ; load 256 in Workspace
movwf tmr0 ; initialiser tmr0
bcf INTCON , T0IF ; Clear the flag
loop
btfss INTCON , T0IF ; test if counter had finiched
goto loop ; non, go to loop
; if yes to continued of the program
; 256 past events
call inttimer ; call timing of 0.5s
goto start
END ; End of the program
42
Thank you for your attention
43