0% found this document useful (0 votes)
207 views16 pages

Microcontroller - PPT 3

This document discusses microcontroller concepts like delay routines, port configuration, subprograms, and the stack. It provides examples of how to: 1. Create a delay subprogram using a loop with DECFSZ to keep a LED flashing at a visible rate. 2. Configure the ports of a PIC microcontroller for input or output using registers like TRISA. 3. Use the stack to store the return address when calling a subprogram with CALL, and have the program return to the correct location after with RETURN.

Uploaded by

Irvine Chibaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views16 pages

Microcontroller - PPT 3

This document discusses microcontroller concepts like delay routines, port configuration, subprograms, and the stack. It provides examples of how to: 1. Create a delay subprogram using a loop with DECFSZ to keep a LED flashing at a visible rate. 2. Configure the ports of a PIC microcontroller for input or output using registers like TRISA. 3. Use the stack to store the return address when calling a subprogram with CALL, and have the program return to the correct location after with RETURN.

Uploaded by

Irvine Chibaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture 3

Microcontrollers
M.MUNYARADZI
2020
HCT215
Delay Routine

• LED appears to be lit at half power for previous interface.


• The time to execute an instruction is 1 clock cycle except for the
BTFSS/BTFSC instructions
• number clock cycles for a traverse around loop of program is 4
clock cycles
• Assuming a 4MHz crystal for the PIC clock, the clock cycle is 1µs
(oscillator frequency /4).The time to complete the program loop
once is 4µs.
• The LED will be off for 1µs and on for 3µs  and the flash rate will be
250kHz. Clearly, we will not be able to see the LED flashing.
• A delay between turning the LED on and off, and another between
turning it off and on again will allow the LED flashing to be observed.
• Delay routine can be invoked using the instruction CALL delay,
where delay is the name of the sub-program.
Delay Routine
• Our program now becomes:  
• agn
• BSF  portb,0 ;set bit 0 of portB (the LED will turn off)
CALL delay ; call the delay sub-program

• ---  
---
---

The delay sub-program will simply take up processor


time to ensure the LED remains on or off for a
reasonable amount of time.
Using the W Register

– The W register (working register) is particularly


important as it is used to obtain literal data values and
pass them to other registers in the file register.
– NOTE: A literal is simply a data value (not an
address).
– Two instructions used to do this
– 
– MOVLW n   move (copy) the value n into the W
register.
– MOVWF f move (copy) the contents of the W register
into register f
Port Configuration

• The ports of the PIC and other


microcontrollers can be
configured as input or output
ports.  Individual port bits can
be configured as input or
output bits independently.
• The figure illustrates how the
PIC portA can be configured
for input or output operation.
TRIS is the register to set up
the direction of the bits in
PORT A. A 1 indicates and
input bit. A 0 indicates and
ouput bit.
Configuring portA with the
TRISA register
• The TRISA register is
located at address 85h in
the register file map and
is used to configure portA
for input or output by
placing a 1 or a 0 into the
bits of the TRIS register.
• Placing the bit pattern
0110 1110 into the TRISA
register would configure
portA as illustrated
• The PIC instructions to do
this would be
Two Banks of Memory Register
• Files
The default register file
is called page 0. The
TRISA and TRISB
registers are located in
the second bank of
register files known as
page 1. The figure
illustrates the
arrangement of the two
banks of memory
register files.  Note that
in register bank 0
address 05h accesses
porta, while the
configuration register
TRISA is in register file
bank 1 at address 85h.
Access to register banks
• To gain access to register bank 1 we need to set bit 5 of
the STATUS register to 1.  Bit 5 is the register file page
select bit. This is most easily done by using the bit set
instruction:
• BSF      STATUS,5
• To gain access to the default register file bank 0, we
simply clear bit 5 in the status register with:
• BCF      STATUS,5
• You will notice that some registers (such as the STATUS
register) are duplicated in both register banks.
• The most commonly used bits in the status register are
as follows
Bit Setting and Clearing by Masking

• 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

• A sub-program (or subroutine as it is


sometimes called) is a piece of program
that can be called from anywhere from
within a program by using the instruction:

• 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

• The figure shows a stack in


use. The CALL instruction
(ignore word Interrupt in the
diagram for now) places the
return address onto the stack
and the RETURN instruction
gets it back off the STACK and
places it in the Program
Counter register (PC).
• These actions all take place
without any further action
being taken by the
programmer.
• Write the complete program
with the sub-program

You might also like