Title: PIC Architecture and Assembler Programming including Software Time Delay Subroutine
1PIC Architecture and Assembler
Programming(including Software Time Delay
Subroutine)
EE1A2
- S. I. Woolley
- Electronic, Electrical and Computer Engineering
2Architecture of the PIC16F84
- Buses Communication lines for transferring data
within the processor. -
- Oscillator Used to drive the microprocessor,
clocking data and instructions in the processor. -
- Timing The PIC has an internal divide by 4
whereby 4 oscillator pulses form one clock pulse.
This makes instruction times easy to calculate.
- Most instructions (except calls and returns and
other instructions involving jumps and branches)
take one clock cycle, so with a 4MHz oscillator
(divided by 4), instructions take 1?s. - The calculation of execution times is important.
We will use timed delay subroutines in the
laboratory to slow down traffic light LED
sequences (otherwise pedestrians will have 1?s to
cross the road!) The PIC16F84 also has a
hardware timer, TMR0, which we shall consider
later.
3Architecture - continued
- Program counter The program counter stores the
current program position. After each instruction
the program counter is incremented automatically
so that it points to the location of the next
instruction or data in memory. - Stack The stack is used to save program counter
contents when subroutines are called. The
PIC16F84 has an 8-level stack. -
- Reset vector On power-up or reset, the PIC16F84
will go to program memory and begin executing
instructions sequentially. -
- Interrupt vector In the PIC16F84, this points to
0x04, so that if an interrupt occurs, the first
instruction to be executed will be at this
location. Interrupts are configured in the
interrupt control register. -
- Status register The status register is a very
important register which contains all the
arithmetic status of the ALU and Reset status.
The contents of the status register are updated
after certain instructions which modify the W
(working) register.
4Microcontroller Features
- Microcontrollers now come with a wide range of
features, for example, watchdog timers,
sleep/wakeup modes, power management, powerful
I/O channels, and so on. -
- Watchdog timer
-
- A watchdog timer provides a means of graceful
recovery from a system problem. This could be a
program that goes into an endless loop, or a
hardware problem that prevents the program from
operating correctly. - If the program fails to reset the watchdog at
some predetermined interval, a hardware reset
will be initiated. The bug may still exist, but
at least the system has a way to recover. This
is particularly useful for unattended systems. - See the CLRWDT instruction.
-
-
-
5Architecture of the PIC16F84
Microchips architectural block diagram of the
PIC16F8X (For information/reference only - not
assessed material)
6Program Documentation
- Good code documentation is essential if programs
are to be maintained. - The header should provide all the important
processor details and identify the programmer.
Most importantly, it should contain a FUNCTION
statement which tells the reader what the
processor needs to be connected to, exactly which
I/O pins are connected to which devices and what
the program does. - Labels should be meaningful. They should help to
make your code more readable. Try to avoid using
labels which may be reserved words (see assembler
directives). - Comments should be clear and concise. They
should summarise important functionality.
Comments often summarise the function of several
lines by using \ and / characters to tie lines
together (see code examples). - A clear columnar structure also helps code to be
more readable. Separating equate and sub-routine
components and providing short headings for each
also makes the code easier to understand.
7Code Structure and Documentation
- ----- GENERAL EQUATES --------------------------
--------------------- - W EQU 0
- F EQU 1
- RBIF EQU 0
- RBIE EQU 3
- GIE EQU 7
- ----- I/O EQUATES ------------------------------
------------------ - PORTA EQU 0X05
- PORTB EQU 0X06
- ----- REGISTER EQUATES -------------------------
---------------------- - INTCON EQU 0X0B
- MCOUNT EQU 0X0C
- NCOUNT EQU 0X0D
- LED_VAL EQU 0X0E
- TEMP_W EQU 0X0F
- ------------------------------------------------
----------------------- - ORG 0X00
- GOTO START
- ORG 0X04
8Code Structure and Documentation
- ---------------- INTERRUPT SERVICE SUBROUTINE
------------------------ - INT_SER MOVWF TEMP_W COPY CONTENTS OF W
- MOVLW 0X00
- MOVWF LED_VAL \
- MOVWF PORTB -RESET LEDS
- MOVF TEMP_W,W RESTORE W
- BCF INTCON,RBIF CLEAR INTERRUPT FLAG
- RETFIE
- ----------------------- SUBROUTINE 'DELAY'
---------------------------- - DELAY MOVLW 0X0F DELAY OF X SECONDS
- MOVWF MCOUNT
- GET_N MOVLW 0XFF
- MOVWF NCOUNT
- DEC_N DECFSZ NCOUNT,F
- GOTO DEC_N
- DECFSZ MCOUNT,F
- GOTO GET_N
- RETURN
9Code Structure and Documentation
- ----------------------- MAIN PROGRAMME
-------------------------------- - START MOVLW 0XFF - CONFIGURE PORTA AS INPUTS
- TRIS PORTA /
- MOVLW 0X80
-
- END
10Subroutines
- Subroutines are a sequence of instructions for
performing a particular task. They generally
make code more efficient because their functions
can be re-used. - Subroutines are normally placed before the main
program after the ORG and GOTO lines. - They are implemented using CALL and RETURN (or
RETLW). - When a CALL instruction is encountered, the
program counter is pushed onto the stack. A
new value is loaded into the program counter and
instruction execution starts from the new
address. - When a RETURN or RETLW instruction is
encountered, the program counter is restored by
popping the stack. - You should use a subroutine when you need to
perform a task and then continue with a previous
task (otherwise, use GOTO.) - Can a subroutine be called from within another?
Yes. The limit to the depth of nesting is the
depth of the program counter stack. The PIC16F84
has a program stack depth of 8.
11Time Delays
- A subroutine called delay can be used (CALLed)
to generate a time delay. -
- On a subroutine CALL the program counter is
pushed onto the stack, and on a RETURN
instruction the program counter is retrieved
(popped) and program continues from where it left
off. - Example-
- DELAY MOVLW 0XFF
- MOVWF MCOUNT
- GET_N MOVLW 0XFF
- MOVWF NCOUNT
- DEC_N DECFSZ NCOUNT,F
- GOTO DEC_N
- DECFSZ MCOUNT,F
- GOTO GET_N
- RETURN
12Using Time Delays
- Debouncing Switch and Key Inputs
- A problem with mechanical switches is that the
contacts often bounce, making contact then
oscillating between open and closed for a short
time. - A simple way to avoid multiple bounce inputs is
to wait a short time (a few msecs) after the
initial closure for the switch to stop bouncing
and then test for release. - Traffic Light Control
- The basic delay subroutine will be used to create
variable amounts of delay for your lights to
change and pedestrians to cross the road etc. - Hardware Timers
- When running our software time delay the
processor is not free to attend other tasks. - It is also difficult to time and control several
different events concurrently. - We will look at the PIC hardware timer, TMR0,
later. -
-
DELAY MOVLW 0XFF MOVWF MCOUNT GET_N MOVLW 0XFF M
OVWF NCOUNT DEC_N DECFSZ NCOUNT,F GOTO DEC_N DEC
FSZ MCOUNT,F GOTO GET_N RETURN
13Flowcharts
- Flowcharts are simple graphical representations
of program steps. - Flowcharts are not appropriate for use in all
circumstances. For example, interrupts and
resets cannot easily be represented. - Other design representations exist for specific
application domains. But, for assembler
programming, flowcharts are generally preferred.
14Time Delay with Flowchart
- MCOUNT EQU 0x0C
- NCOUNT EQU 0x0D
- DELAY MOVLW 0XFF
- MOVWF MCOUNT
- GET_N MOVLW 0XFF
- MOVWF NCOUNT
- DEC_N DECFSZ NCOUNT,F
- GOTO DEC_N
- DECFSZ MCOUNT,F
- GOTO GET_N
- RETURN
15Timed Delays
- The routine contains two (nested) loops. The
time taken for the delay can be changed by
altering the contents of MCOUNT and NCOUNT. The
time taken is approximately 3x(255x255) clock
cycles. -
- Why the 3?
- The loop contains a DECFSZ (normally 1 clock
cycle) and a GOTO (always two clock cycles.) - This is repeated approximately (MCOUNT x NCOUNT)
times, to give a time delay of approximately
0.2s. -
- A useful private study exercise ..
-
- Calculate the delay exactly. Substitute low
values (2 or 3) for m and n, and manually step
through the code line by line decrementing the
counters. You should find that the delay
3mn4m1 (without the call and return). -
-
-
-
-
16More Example Instructions
- The W and TEMP registers are both clear (0) at
the start of the code segment below. - Write down their values for each of the executed
lines of code. - Write down suitable equate statements for these
lines of assembler. - W TEMP
- MOVLW 0X26
- MOVWF TEMP
- MOVLW 0x3B
- DECF TEMP, F
- ADDWF TEMP, W
- BTFSC TEMP, 3 BITS ARE NUMBERED FROM 7..0
- CLRF TEMP
- INCF TEMP, F
-