Microcontroller - PPT 3
Microcontroller - PPT 3
Microcontrollers
M.MUNYARADZI
2020
HCT215
Delay Routine
• ---
---
---
• The logical AND and OR operators can be used to clear or set bits
according to the bit pattern specified as a literal.
• The literal is known as a mask.
• Logical ANDing is used to clear selected bits in a byte to zero.
•
• ANDLW n The value n is logically ANDed to the contents of the W
• register and the result placed into the W register.
• Example:
•
• ANDLW 7Bh would logically AND the contents of W with literal 7Bh
• W 1001 1101 the contents of W (assumed)
• n 0111 1011 the literal value 7Bh (the mask)
• Result 0001 1001 bits 7 and 2 have been cleared by the AND
operation
Instructions Using the Destination
Flag Bit
• Many of the PIC instructions can place their result into either the W
register or into one of the selected file registers.
• The destination is determined by the value of d (destination select).
• d=0 destination W register
• d=1 destination file register f (this is the default)
• eg.MOVF f,d move/copy the contents of the file register f into the
destination d
• Example
• MOVLW FCh
MOVWF 35h ; move literal FC hex into location 35h
• INCF 35h,1 ; increment contents of memory location 35h
and store it there
• MOVF 35h,0 ; move contents of 35 hex into W register
The Auto-decrement and Auto-
increment Instructions
• There are two instructions that can be used to create loops that execute for a
particular number of times.
• DECFSZ f,d ; decrement register f and skip the next instruction if
• zero
• INCFSZ f,d ;increment register f and skip the next instruction if zero
• Example
•
• Next DECFSZ count
• GOTO next
• BSF portb,1
• The first instruction decrements the contents of memory location count. If it is
not equal to zero, the GOTO instruction will be executed. This of course
causes the program to loop back to the DECFSZ instruction to decrement
count once more.
The Auto-decrement and Auto-
increment Instructions
• This will continue until the DECFSZ instruction
decrements count to zero, at which point the
DECFSZ instruction will skip the GOTO
instruction and execute the next instruction
• The two instructions DECFSZ and GOTO take
up processor cycles and thus constitute a
"delay".
• The DECFSZ instruction can be used to keep a
count of some event taking place, for example
we may wish to keep a count of the number of
times an input changes from 1 to 0.
The Purpose of Sub-programs
• CALL name_of_sub_program
• E.g. CALL delay
Sub-programs
• The program instructions comprising the delay sub-program are:
•
• Delay MOVLW FFh
• MOVWF count,F
next
• DECFSZ
GOTO next
RETURN
• The sub-program needs to start with a label (delay) and end with the
instruction RETURN.
• When the CALL instruction calls sub-program, the program
branches off to the group of instructions following the label delay.
On completing the sub-program (ie. on reaching the instruction
RETURN) the program branches back to the next instruction in the
main program following the CALL instruction.
The Stack
• In order to know where to return from a sub-
program the PIC microcontroller must know
where to branch back to in the main program.
This is achieved by storing the next instruction
address after the CALL instruction in a special
area of memory called the STACK.
• The RETURN instruction will look at the stack
and obtain the return address which it will load
into the PIC's program counter (which keeps
track of where the next instruction is to be
executed in program memory).
The Stack and its use