Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

EE1A2

PIC Architecture and Assembler Programming


(including Software Time Delay Subroutine)

S. I. Woolley
Electronic, Electrical and Computer Engineering

Architecture 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.
o 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 1s. o 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 1s to cross the road!) The PIC16F84 also has a hardware timer, TMR0, which we shall consider later.

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.

Architecture - continued

Microcontroller 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.

Architecture of the PIC16F84

Microchips architectural block diagram of the PIC16F8X


(For information/reference only - not assessed material)

Program Documentation
o Good code documentation is essential if programs are to be maintained. o 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. o 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). o 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). o 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.

Code 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 GOTO INT_SER

Code 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
NOTE: The actual code order in this documentation example slide was modified 13 Feb 2005 to satisfy a PORTB change interrupt requirement. The line BCF INTCON, RBIF was moved below the PORTB write. This is because PORTB needs to be read or written to clear the change mismatch prior to clearing the flag. This requirement is explained in the data sheets pg68 in the 2005 handbook. My thanks to Alex Smith for pointing this out.)

Code Structure and Documentation


; ----------------------- MAIN PROGRAMME -------------------------------START MOVLW 0XFF ;- CONFIGURE PORTA AS INPUTS TRIS PORTA ;/ MOVLW 0X80

END

Subroutines
o Subroutines are a sequence of instructions for performing a particular task. They generally make code more efficient because their functions can be re-used. o Subroutines are normally placed before the main program after the ORG and GOTO lines. o They are implemented using CALL and RETURN (or RETLW). o 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. o When a RETURN or RETLW instruction is encountered, the program counter is restored by popping the stack. o You should use a subroutine when you need to perform a task and then continue with a previous task (otherwise, use GOTO.) o 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.

10

Time 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 GET_N DEC_N MOVLW MOVWF MOVLW MOVWF DECFSZ GOTO DECFSZ GOTO RETURN 0XFF MCOUNT 0XFF NCOUNT NCOUNT,F DEC_N MCOUNT,F GET_N

11

Using Time Delays


Debouncing Switch and Key Inputs o A problem with mechanical switches is that the contacts often bounce, making contact then oscillating between open and closed for a short time. o 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 o 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 o When running our software time delay the processor is not free to attend other tasks. o It is also difficult to time and control several different events concurrently. o We will look at the PIC hardware timer, TMR0, later.

DELAY GET_N DEC_N

MOVLW MOVWF MOVLW MOVWF DECFSZ GOTO DECFSZ GOTO RETURN

0XFF MCOUNT 0XFF NCOUNT NCOUNT,F DEC_N MCOUNT,F GET_N

12

Flowcharts
o Flowcharts are simple graphical representations of program steps. o Flowcharts are not appropriate for use in all circumstances. For example, interrupts and resets cannot easily be represented. o Other design representations exist for specific application domains. But, for assembler programming, flowcharts are generally preferred.
First task Condition T F Next task Task #1 Case #2 T F Task #2 Case #1 T Condition F

Condition

Task sequence
... T ... Loop Task

Selection (if..then.else)

Condition T

Case #N Condition Condition F T F T Loop Task

Selection (switch)

Repetition (do..while) Selection (while)

13

Time Delay with Flowchart


MCOUNT NCOUNT DELAY GET_N DEC_N EQU EQU MOVLW MOVWF MOVLW MOVWF DECFSZ GOTO DECFSZ GOTO RETURN 0x0C 0x0D 0XFF MCOUNT 0XFF NCOUNT NCOUNT,F DEC_N MCOUNT,F GET_N

14

Timed 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? o The loop contains a DECFSZ (normally 1 clock cycle) and a GOTO (always two clock cycles.) o 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 = 3mn+4m+1 (without the call and return).

15

More Example Instructions


The W and TEMP registers are both clear (=0) at the start of the code segment below. 1. Write down their values for each of the executed lines of code. 2. Write down suitable equate statements for these lines of assembler.
W MOVLW MOVWF MOVLW DECF ADDWF BTFSC CLRF INCF 0X26 TEMP 0x3B TEMP, TEMP, TEMP, TEMP TEMP, TEMP

F W 3 F

;BITS ARE NUMBERED FROM 7..0

16

You might also like