You are on page 1of 22

PIC- Assembly Programming

Addition of Two Eight bit numbers


#include<p16f877a.inc>
cblock 0x20
Sum
Carry
endc
org 0x00
Goto start
org 0x05
start
Contd…
• #include<p16f877a.inc> is the header file
containing the register mapping.
• Cblock 0x20 – similar to using #define in C
program (Macro).
– Starting from 0x20 address of data memory, each
locations will be represented using a name.
– Sum = 0x20, carry =0x21
– Endc indicates the end of cblock representation.
Contd…
• Instead of Cblock, one could use EQU
Eg. EQU sum 0x20
Eg. EQU carry 0x21
• Org 0x00 – indicates the address of program
memory (reset vector)
• Goto start - start is a label indicating an address
• Org 0x05 – indicates addr of program memory
Start  means 0x05 is indicated using the label start
Contd…
• Cblock, endc, EQU, Labels etc.. are collectively
called as Assembler directives.
• This is a feature provided by the IDE( MPlab).
• Directions to the assembler.
– Eg. Instead of 0x20 it is represented as Sum
Addition of 8 bit numbers
#include<p16f877a.inc> MOVLW 0xFF
cblock 0x20 ADDLW 0xDD
Sum MOVWF sum
Carry
endc BTFSC STATUS,0
Org 0x00
INCF carry,1 ; ack carry
Goto start
Loop GoTo Loop ; infinite loop
Org 0x05
Start End
CLRF sum
CLRF Carry
Program memory

Address Content
Org 0x00
00 Goto Start ( address 05) Goto start
01
02
03
04
Start
05 CLRF sum CLRF sum
06 CLRF carry CLRF carry

Address 04 is interrupt vector- if the program is using interrupts this location should
point to ISR. In this case it is not using any interrupts so this could be used( next slide).
Another option- Program memory

Address Content Org 0x00

00 CLRF sum CLRF sum


01 CLRF carry CLRF carry
02 MOVLW 0xFF
03 ADDLW OXDD
04 MOVWF sum
05 BTFSC STATUS,0
06 INCF carry,1

BTFSC STATUS,C also could be used. – specify the bit name


Subtraction of two numbers
#include<p16f877a.inc> BTFSS STATUS,C
num1 EQU 0x20 Call Compliment ; stack push
Num2 EQU 0x21 GoTo Loop1
Diff EQU 0x22
Borrow EQU 0x23 Compliment
COMF Diff,1 ; I’s complement
Org 0x00 INCF Diff,1 ; 2’s complement
MOVLW 0X62
INCF Borrow, 1 ; ack borrow
MOVWF num1
Return ; stack pop
MOVLW 0XA2
MOVWF num2
SUBWF num1, 0 ; W= num1- W Loop1 GoTo Loop1 ; infinte loop
MOVWF Diff End
Example 1
MOVLW 0x20
Clear 8 successive locations
MOVWF FSR starting from 0x20- indirect
addressing mode.
MOVLW 0x08
MOVWF count
L1 CLRF INDF
INCF FSR,1
DECFSZ count,1
GOTO L1
Example2
Org 0x00
MOVLW 0x04 Address 0x00

MOVWF 0x30 Address 0x01

DECFSZ 0x30,1 Address 0x02

GOTO 0x02
GOTO 0x01
Continuously count from 4 till 0
Example 3
High EQU 0x30 MOVWF N_high
Low EQU 0x40 Nu MOVLW Low
Cblock 0x25 MOVWF N_low
N_low Cu DECFSZ N_low,1
N_high Goto Cu
Endc DECFSZ N_high,1
Org 0x00 Goto Nu
MOVLW High Goto $
end
Delay generation using 16 bits
8 bit Multiplication
#include<p16f877a.inc> Movwf multiplicant
Cblock 0x20 Movf multiplier,0
Multiplier = 3
Count BTFSC STATUS,Z Multiplicant=4
Multiplier Goto terminate
Multiplicant Count =3
Movf multiplicant,0
Product_msb
BTFSC STATUS,Z 4 is added 3 times
Product_lsb
Goto terminate
Endc
Loop
Org 0x00
ADDWF product_lsb,1
CLRF product_msb
BTFSC STATUS,C
CLRF product_lsb
Movlw 0x03 INCF Product_msb,1
Movwf count DECFSZ count,1
Movwf multiplier Goto Loop
Movlw 0x04 Terminate
Goto terminate
End
Division Logic
Dividend = 5 , divisor =2, Q=0 , Rem=0

Dividend= dividend – divisor


Dividend = 5-2 =3 ; C=1 indicating no borrow
Check C flag for borrow, if no borrow then increment quotient and repeat; Q=1
Dividend= 3-2 =1 ; C=1  no borrow so increment quotient Q=2
Dividend= 1-2 = FF ; 2’s complement addition, C=0  borrow present
 Division is over
 Dividend = FF
 Remainder = dividend + divisor
 Rem= FF + 2 = 1
 5/2 Q=2, Rem =1
Division Program
#include<p16f877a.inc> Goto terminate
Cblock 0x20
Loop
dividend
SUBWF dividend,1 ; div=div-divisor
divisor
Quotient BTFSC STATUS,C ; borrow check no borrow
Remainder INCF quotient,1
endc BTFSS STATUS,C ; borrow exist
Org 0x00 Goto out
MOVLW 0x05 Goto Loop
MOVWF dividend
Out
MOVLW 0x02
CLRW
MOVWF divisor
CLRF remainder MOVF dividend,0
CLRF quotient ADDWF divisor,0
MOVF divisor,0 MOVWF remainder
BTFSC STATUS,Z Terminate Goto Terminate
End
Addition of numbers using indirect Addressing
Watch Dog Timer (WDT)
• Safety mechanism to recover from
software/hardware faults.
• It is a timer , that runs in the background using a
dedicated clock for reliability. For each cycle the timer
register increments.
• If the timer overflows i.e exceeds the maximum count
, it is considered as a fault and microcontroller resets
as to recover.

https://en.wikipedia.org/wiki/Watchdog_timer
Contd…
• The user program has to take care of WDT.
• The programmer should know when the WDT will
overflow( after how many instructions) and
CLRWDT instruction should be used. So that WDT is
cleared and reinitialized to 0.
• WDT as a feature could be enabled or disabled.
• In PIC16f877a WDT uses an internal RC oscillator
for reliable operation.
• WDT is independent of the Crystal oscillator.
Contd…
• If the program has infinite loops - the
controller will be executing the infinite loop,
but the WDT will still continue to increment
and overflow, resulting in a reset.
• Such cases you can disable WDT , as the
infinite loop is intentionally put by the
programmer.
SLEEP
• Dedicated instruction for SLEEP mode.
• Power saving.
• Microcontrollers goes to sleep mode
suspending all the activities.
• The external crystal oscillator is not used,
saving power.
• Few peripherals could be working, based on
internal RC oscillator.
Contd…
• Wake up from SLEEP could be initialized
through an external interrupt.
• WDT can wake up the device from SLEEP
mode. ( not as a reset, instead as wake up)
• Some peripherals can also wake the device.
– Eg. UART- serial communication module.
– If the peripheral receives a bit from another
device it wakes up PIC.
STATUS- register

TO - time out bit (bar) – logic is inverted

=1 after power up , CLRWDT instruction, or SLEEP instruction


=0 indicates that a WDT time out has occurred.

After power up i.e the microcontroller has turned on this bit is set as 1,
CLRWDT and SLEEP instruction also sets this bit.

PO – power down bit (bar) – logic is inverted

= 1 after power up, CLRWDT instruction


=0 when SLEEP is executed

You might also like