PIC Architecture and Assembler Programming: (Including Software Time Delay Subroutine)
PIC Architecture and Assembler Programming: (Including Software Time Delay Subroutine)
PIC Architecture and Assembler Programming: (Including Software Time Delay Subroutine)
S. I. Woolley
Electronic, Electrical and Computer Engineering
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.
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.
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
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
Selection (switch)
13
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
F W 3 F
16