03 PIC16F84A Assembly Programming
03 PIC16F84A Assembly Programming
Microcontroller
Features:
1
Features:
2
Peripheral Features:
3
Pin no.1 RA2 Second pin on port A
Pin no.2 RA3 Third pin on port A.
Pin no.3 RA4 Fourth pin on port A. TOCK1 which functions as a
timer
Pin no.4 MCLR Reset input and Vpp programming voltage of a
microcontroller
Pin no.5 Vss Ground of power supply.
Pin no.6 RB0 Zero pin on port B. Interrupt input
Pin no.7 RB1 First pin on port B.
Pin no.8 RB2 Second pin on port B.
Pin no.9 RB3 Third pin on port B.
Pin no.10 RB4 Fourth pin on port B.
4
PIC16F84A
BLOCK
DIAGRAM
5
PROGRAM MEMORY MAP
Program Memory AND STACK
Organization
program counter(13-bit ) capable of
addressing an 8K x 14 program memory
space
1K x 14 (0000h-03FFh) are physically
implemented.
Accessing a location above the
physically implemented address will
cause a wrap around.
Reset Vector
- on reset which occurs on power-up or when the reset
switch is used to pull MCLR low, the PIC will go to
program memory location 0x000 where the first instruction
is stored.
Interrupt Vector
- the interrupt vector points to 0x004, so if interrupts are
used, the first instruction of the interrupt service routine
must be at that location.
6
Data Memory Organization
Data memory
are 8 bits wide register.
contains the data EEPROM memory w/c is indirectly
mapped
The data memory is partitioned into two areas.
Special Function Registers (SFR)
General Purpose Registers (GPR)
accessed either directly using the absolute address of
each register file or indirectly through the File Select
Register (FSR)
7
Data memory is partitioned into two banks which contain the
general purpose registers and the special function registers. Bank
0 is selected by clearing the RP0 bit (STATUS<5>). Setting the
RP0 bit selects Bank 1.
Each Bank extends up to 7Fh (128 bytes). The first twelve
locations of each Bank are reserved for the Special Function
Registers. The remainder are General Purpose Registers,
implemented as static RAM.
8
Special Function Registers
Peripheral.
STATUS REGISTER
9
OPTION REGISTER
7 6 5 4 3 2 1 0
PSA PS2 PS1 PS0
PSA – prescaler assignment
0 = TMR0
1 = WDT
Prescaler Value
PS2 PS1 PS0 +By
0 0 0 2
0 0 1 4
0 1 0 8
0 1 1 16
1 0 0 32
1 0 1 64
1 1 0 128
1 1 1 256
10
PCL and PCLATH
The program counter (PC) specifies the address of the instruction to
fetch for execution. The PC is 13 bits wide. The low byte is called
the PCL register. This register is readable and writable. The high
byte is called the PCH register. This register contains the
PC<12:8> bits and is not directly readable or writable. If the
program counter (PC) is modified or a conditional test is true, the
instruction requires two cycles. The second cycle is executed as a
NOP. All updates to the PCH register go through the PCLATH
register.
INTCON REGISTER
The INTCON register is a readable and writable register that
contains the various enable bits for all interrupt sources.
WORK REGISTER
- an 8-bit wide register that acts like an accumulator in other
microcontrollers.
11
CLOCK OSCILLATOR
RC – resistor capacitor
XT – crystal or ceramic resonator
HS – high speed crystal or ceramic resonator
LP – low power crystal
* Refer to datasheet for resonators and crystal
12
Ceramic resonator is an
oscillator and capacitors can
be packed in joint case with
three pins. Center pins of the
element is the ground, while
end pins are connected with
OSC1 and OSC2 pins on the
microcontroller.
13
Relationship between a clock and a number of instruction
of instruction cycles
14
Computation:
15
Coding Format for literal constants
Creating Variables/Labels
Label
- a mnemonic symbolic name assigned to an address.
- can be a combination of alphanumeric characters and should not
start with a number.
Equates
- an equate statement may serve to assign a label to a specific
address in the PIC16 designated by a literal value (it is
recommended that you use hexadecimal numbers).
ex:
PORTB EQU 0x06
16
Creating Variables/Labels
Cblock / endc
- assigns labels that have contiguous address.
ex:
Cblock 0x0c ;defines the label starting at address 0X0C
Var1
Var2
Var3
Endc ; end of declaration
ORG
- ORG stands for origin.
- used for designing code in absolute mode
ORG defines the address where the program code starts.
syntax: org 0x000
ORG is used to establish the start of the interrupt service for the
PIC16F84.
syntax: org 0x004
Note use CODE for relocatable code as suggested by Microchip for
their new programs
17
End
- is used to tell the assembler it has reached the end of the
program
- must be used to end a program
syntax: end
Configuration bits
Program
End
18
Example:
• Using GPR
• Using PORTs as input or output pins
19
Commands typically used for register
manipulations
bcf f,b ;clears selected bit in selected bit in selected register.
bsf f,b ;sets selected bit in selected register.
movlw k ;loads W with literal
movf f,d ;moves or copy the selected register contents to W or f.
movwf f ;moves or copy the contents of W to selected file
register.
Where:
b= bit position (0-7)
f= file register
d=destination
0=work register
1=file register
20
File and literal manipulation
literal to file
use movlw to copy literal to work register
move/copy the work reg to the target file register using
movwf
file1 to file2
use movf to copy the contents of file1 to work reg
move/copy the work reg to file2 register using movwf
21
Configuring PORTB as output
Example:
22
Configuring PORTB as input
Example:
23
Example: PORTb as input and PORTa as output
bsf STATUS,5 ; select memory bank 1
movlw 0XFF
movwf TRISB ;PORTB as input pins
bcf OPTION_REG,NOT_RBPU ; turn on weak pull ups
movlw 0x00
movwf TRISA ;all PORTA bits output pins
Making Decisions
24
Making decisions
25
;The “main” program starts here
movlw 00 ;clear all bits in port A and B
movwf porta
movwf portb
Loop
bcf portb, 3 ;preclear port B, bit 3
btfss porta, 3
bsf portb, 3 ;but set it if button pressed
bcf portb, 4 ;preclear port B, bit 4
btfss porta, 4
bsf portb, 4 ;but set it if button pressed
goto loop
end
26
The Status Register
Zero Carry
27
Note:
A subtraction is executed by adding the two’s complement
of the second operand.
Steps:
Take the 2’s complement of the subtrahend.
Then it is added to the minuend.
If the final carry over of the sum is 1, it is dropped and
the result is positive.
If there is no carry over, the two’s complement of the
sum will be the result and it is negative.
Example
28
Example
Subtract the two values to compare and use the Carry and
Zero bit flag of the STATUS to evaluate if a number is
greater, less than or equal ti the other number.
29