Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
1K views

03 PIC16F84A Assembly Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

03 PIC16F84A Assembly Programming

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Introduction to PIC16F84A

Microcontroller

Source: PIC16F84A Data Sheet

Features:

 Only 35 single word instructions to learn


 All instructions single-cycle except for program branches
which are two-cycle
 Operating speed: DC - 20 MHz clock input
DC - 200 ns instruction cycle
 1024 words of program memory
 68 bytes of Data RAM
 64 bytes of Data EEPROM
 14-bit wide instruction words
 8-bit wide data bytes

1
Features:

 15 Special Function Hardware registers


 Eight-level deep hardware stack
 Direct, indirect and relative addressing modes
 Four interrupt sources:
 External RB0/INT pin
 TMR0 timer overflow
 PORTB<7:4> interrupt-on-change
 Data EEPROM write complete

Pins and Functions:

a PIC16F84A is an 18-pin Dual In-line Package (DIP) IC.

2
Peripheral Features:

 13 I/O pins with individual direction control


 High current sink/source for direct LED drive
 25 mA sink max. per pin
 25 mA source max. per pin
 TMR0: 8-bit timer/counter with 8-bit programmable
prescaler

Special Microcontroller Features:


 10,000 erase/write cycles Enhanced FLASH Program memory
typical
 10,000,000 typical erase/write cycles EEPROM Data memory
typical / EEPROM Data Retention > 40 years
 In-Circuit Serial Programming™ (ICSP™) – via two pins
 Power-on Reset (POR), Power-up Timer (PWRT), Oscillator Start-
up Timer (OST)
 Watchdog Timer (WDT) with its own On-Chip RC Oscillator for
reliable operation
 Code protection
 Power saving SLEEP mode
 Selectable oscillator options

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.

 Pin no.11 RB5 Fifth pin on port B.


 Pin no.12 RB6 Sixth pin on port B. 'Clock' line in program
mode.
 Pin no.13 RB7 Seventh pin on port B. 'Data' line in program
mode.
 Pin no.14 Vdd Positive power supply pole.
 Pin no.15 OSC2 Pin assigned for connecting with an oscillator
 Pin no.16 OSC1 Pin assigned for connecting with an oscillator
 Pin no.17 RA2 Second pin on port A.
 Pin no.18 RA1 First pin on port A.

4
 PIC16F84A
BLOCK
DIAGRAM

PIC 16F84A MEMORY ORGANIZATION:

The PIC 16 microcontrollers have separate blocks of memory:


Program memory and Data memory.
 Program memory
- starts at 0x000 address.
- it is a Flash memory, can be erased and reprogrammed.
- read-only at run time.
 Data memory
 Further be broken down into the general purpose RAM and the
Special Function Registers (SFRs).
 also contains the data EEPROM memory. This memory is not
directly mapped into the data memory

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)

Data Memory Organization


REGISTER FILE MAP -PIC16F84A

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.

General Purpose Register (GPR)

 is 8-bits wide and is accessed either directly or indirectly


through the FSR
 addresses in Bank 1 are mapped to addresses in Bank 0.
 As an example, addressing location 0Ch or 8Ch will
access the same GPR.

8
Special Function Registers

 are used by the CPU and Peripheral functions to control


the device operation.
 These registers are static RAM.
 The special function registers can be classified into two
sets:
 Core
 STATUS, OPTION, INTCOM, PCL and PCLATH

 Peripheral.

STATUS REGISTER

 The Status register contains the arithmetic status of the


ALU, the RESET status and the bank select bit for data
memory.

9
OPTION REGISTER

 The OPTION register is a readable and writable register


which contains various control bits to configure the
TMR0/WDT prescaler, the external INT interrupt, TMR0,
and the weak pull-ups on PORTB.

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

4 different types of clock oscillators may be used with


PIC16F84.

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

 Crystal oscillator is kept in metal


housing with two pins where you
have written down the frequency
at which crystal oscillates. One
ceramic capacitor of 30pF
whose other end is connected to
the ground needs to be
connected with each pin.

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.

 RC Oscillators used in applications where great time


precision is not necessary
 R< 2.2k can stop the oscillation.
 R >> example 1M oscillator becomes very sensitive to
noise and humidity
 Recommended value of 3>R>100k.
 capacitor above 20pF should still be used for noise and
stability.

13
 Relationship between a clock and a number of instruction
of instruction cycles

One possible clock circuit for experimentation is a 4 MHz


crystal controlled oscillator in a can.

The external clock frequency is divided by 4 internally, so


the internal clock memory is 1 MHz.

Almost all instructions are executed in one instruction


(internal clock) cycle or 1 microsecond in this case.

14
Computation:

time (T) = 1 / frequency (f)


= 1 / 1MHz
= 1 /1 000 000
= .000001 sec
= 1 microsecond

Getting Started with PIC


Assembly Programming

15
Coding Format for literal constants

 Notations (literal constants)


hex 0xFF or H’FF’
decimal .255
binary b’11111111’
 Comments
 Comments in assembly starts with a semicolon
 Example: ;this is a comment

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

label equate hex address

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

Program Format: Standard Header

Configuration bits

File Registers definition

Program

End

18
Example:

#include P16F84A.inc ; processor library inclusion


__config 0xFFF1 ; refer to datasheet for configuration word setup
;PortA equ 0x05 ;optional since it is already defined in the inc file
cblock 0x0c
num1
num2
num3
endc
num4 equ 0x4f
org 0x000
movlw 0xff
end

A Simple Data Transfer


Program

• 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

Using the GPR

 Its is recommended that you assign labels to the GPR


address that will be used.
 Use EQU for single declaration or CBLOCK/ENDC for
multiple contiguous address assignment

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

PORT and TRIS

 PORTA is 5 bits/lines wide and PORTB is a byte-wide.


 Each PORT line may be individually programmed as an
input line or output line. This is done by changing TRIS
Register,
 A “0” associated with a PORT bit makes it an output,
 A “1” makes it an input.

21
Configuring PORTB as output

 Switch bank to where PORTB is located


 Initialize PORTB such that initial state is OFF
 Switch bank to where TRISB is located
 Clear the bit of the TRISB Register if the pin is to be used
as output
 Switch bank to where PORTB is located to generate the
signal required

Example:

bcf STATUS, RP0 ; Come back to Bank 0


clrf PORTB ; given that dives connected
; to the PORT is active high
bsf STATUS, RP0 ; Go to Bank 1
movlw 0x00 ; Put 00000 into W
movwf TRISB ; Move 0 onto TRISB
;– all pins set to output
bcf STATUS,RP0 ; Come back to Bank
movlw 0xXX ; XX what ever data
movwf PORTB

22
Configuring PORTB as input

 Switch bank to where TRISB is located


 Set the bit 1 of the TRISB Register if the pin is to be used
as input
 You may want to enable the weak pull up of PORTB while
you are in BANK1
 Switch bank to where PORTB is located to start reading
from the newly configure input pin

Example:

; PORTB configured as input with weak pull ups enabled

bsf STATUS,RP0 ; select memory bank 1


movlw 0xFF
movwf TRISB ;all port B bits output
bcf OPTION_REG,NOT_RBPU ; turn on weak pull ups
bcf STATUS,5 ;select bank 0

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

bcf STATUS,5 ;select bank 0


loop
movf PORTA,W ;move port A to W register
movwf PORTB ;move W register to port B
goto loop

Making Decisions

24
Making decisions

 The PIC 16 Series microcontrollers do not have branch


instructions as such but instead it have four conditional
“skip” instructions.
 DECFSZ
 INCFSZ
 BTFCS f,b
 BTFSS f,b

These test for a certain condition, and skip just


one instruction if the condition is met and
continue normal program execution if it is not.
The first of these tests bit b in memory
location f and skips just one instruction if
the bit is set(i.e., at Logic 1).
The second does a similar thing, but skips if
the tested bit is clear (i.e., at Logic 0).

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

Comparing Register Values

26
The Status Register

Zero Carry

The Status Register


 Z: Zero bit
1 = The result of an arithmetic or logic operation is zero
0 = The result of an arithmetic or logic operation is not
zero
 C: Carry/borrow bit
-ADDWF, ADDLW,SUBLW,SUBWF instructions
-for borrow, the polarity is reversed
1 = A carry-out from the Most Significant bit of the result
occurred
0 = No carry-out from the Most Significant bit of the result
occurred

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

Given : 110110 – 10110


 Now, 2’s complement of 010110 is (101101 + 1)
i.e.101010. Adding this with the minuend.
110110 Minuend
101010 2’s comp of subtrahend
Carry over 1 1 0 0 0 0 0 Result of addition
 After dropping the carry over we get the result of
subtraction to be 100000.

28
Example

Given : 10110 – 11010


 2’s complement of 11010 is (00101 + 1) i.e. 00110. Hence
Minued - 10110
2’s comp of subtrahend - 00110
Result of addition - 11100

 As there is no carry over, the result of subtraction is


negative and is obtained by writing the 2’s complement of
11100 i.e.(00011 + 1) or 00100.
 Hence the difference is – 100.

How then can we compare values ?

 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

You might also like