Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5312
Скачиваний: 0
50 The Quintessential PIC Microcontroller
the Program store (fetched), interpreted and then executed. As any execution memory access will be on the Data store and as each store has its own busses, then the fetch and execution processes can progress in parallel. Thus while instruction n is being fetched, instruction n−1 is being executed. In Fig. 3.3 the instruction codes for both the imminent and current instructions are held in the two Instruction registers IR1 and IR2 respectively. This structure is known as a pipeline, with instructions being fetched into one end and ‘popping out’ into the Instruction decoder at the other end. Figure 3.4 below shows the time line of our 3-instruction exemplar program, quantized in clock cycles. During each clock cycle, except for the first, both a fetch and an execution is proceeding simultaneously.
Cycle 1 |
Cycle 2 |
Cycle 3 |
Cycle 4 |
|||||||||
Fetch stream , IR1 |
Fetch movf 5,w |
Fetch addlw 65h |
Fetch movwf 6 |
Fetch next instr |
||||||||
Execute stream, IR2
Execute movf 5,w |
Execute addlw 65h |
Execute movwf 6 |
||||||
Time
Fig. 3.4 Parallel fetch and execute streams.
In order to illustrate the sequence in a little more detail, let us trace through our specimen program. We assume that our computer (that is the Program Counter) has been reset to 000h and has just finished the Cycle 1 fetch.
Fetch (Fig. 3.3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Cycle 2
•Increment the Program Counter to point to instruction 2.
•Simultaneously move the instruction word 1 down the pipeline (from Instruction register 1 to Instruction register 2).
•Program Counter (001h) to Program address bus.
•The instruction word 2 then appears on the Program data bus and is loaded into Instruction register 1.
Execute (Fig. 3.3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Cycle 2
•The operand address 05h (i.e. NUM_1) to the File Address register and out onto the File address bus.
•The resulting datum at NUM_1 is read onto the File data bus and loaded into the File Data register.
3. Stored Program Processing 51
•The ALU is configured to the Pass Through mode, which feeds the datum through to the Working register.
Fetch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Cycle 3
•Increment the Program Counter to point to instruction 3.
•Simultaneously move the instruction word 2 down the pipeline (from Instruction register 1 to Instruction register 2).
•Program Counter (002h) to Program address bus.
•The instruction word 3 then appears on the Program data bus and is loaded into the pipeline at Instruction register 1.
Execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Cycle 3
•The ALU is configured to the Add mode and the literal (which is part of instruction word 2) is added to the datum in W.
•The ALU output, NUM_1 + 65h, is placed in W.
Fetch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Cycle 4
•Increment the Program Counter to point to instruction 4.
•Simultaneously move instruction word 3 down the pipeline to IR2.
•Program Counter (003h) to Program address bus.
•The instruction word 4 then appears on the Program data bus and is loaded into the pipeline at IR1.
Execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Cycle 4
•The operand address (i.e. NUM_2) 06h to the File Address register and out onto the File address bus.
•The ALU is configured to the Pass Through mode, which feeds the contents of W through to the File Data register and onto the File data bus.
•The datum in the File Data register is written into the Data store at the address on the File address bus and becomes the new datum in NUM_2.
Notice how the Program Counter is automatically advanced during each fetch cycle. This sequential advance will continue indefinitely unless an instruction to modify the PC occurs, such as goto 200h. This would place the address 200h into the PC, overwriting the normal incrementing process, and e ectively causing the CPU to jump to whatever instruction was located at 200h. Thereafter, the linear progression would continue.
Although our program doesn’t do very much, it only takes around 1 µs to implement each instruction. A million unimpressive operations each second can amount to a great deal! Nevertheless, it hardly rates highly in
52 The Quintessential PIC Microcontroller
the annals of software, so we will wrap up our introduction to computing by looking at some slightly more sophisticated examples.
Writing a program is somewhat akin to building a house. Given a known range of building materials, the builder simply puts these together in the right order. Of course there are tremendous skills in all this; poor building techniques lead to houses that leak, are drafty and eventually may fall down!
It is possible to design a house at the same time as it is being built. Whilst this may be quite feasible for a log cabin, it is likely that the final result will not remain rain proof very long, nor will it be economical, maintainable, ergonomic or very pretty. It is rather better to employ an architect to design the edifice before building commences. Such a design is at an abstract level, although it is better if the designer is aware of the technical and economic properties of the available building materials.
Unfortunately much programming is of the ‘on the hoof’ variety, with little thought of any higher-level design. In the software arena this means devising strategies and designing data structures in memory. Again, it is better if the design algorithms keep in mind the materials of which the program will be built; in our case the machine instructions.
At the level of our examples in this chapter, it will be this coding (building) task we will be mostly concerned with. Later chapters will cover more advanced structures which will help this process, and we will get more practice at devising strategies and data structures.
In order to code software we must have a knowledge of the register architecture of the computer/microcontroller and of the individual instructions. Figure 3.5 shows the programming model we will use for our exercises. This shows all registers that can be ‘got at’ by the programmer.
I have added three registers to the previous complement that actually are part of the Data store rather than the CPU but have a special significance for the programmer. File 0 and File 4 are a pair of Address registers working in tandem to point to an object in memory, as described further on in Fig. 3.6. The Status Register6 STATUS comprises two bits in File 3 that are used to tell the software something about the outcome from an instruction. Thus the C flag (bit 0 in File 3) primarily holds the Carry out from the last Addition operation. For instance (in decimal) 5 + 2 = 7C0; 5 +9 = 4C1. It also functions as the complement of the Borrow out from a Subtraction operation. For example 5 − 2 = 3B1; 5 − 9 = 6B0. The Z flag (bit 2 in File 3) is set if the result of the last operation is zero.
Table 3.1 shows all the instructions supported by the BASIC computer. Before looking at these, let us discuss the concept of the address mode. Most instructions act on data, which may be in internal CPU registers or out in the Data store. Thus the location of such operands must be part of the instruction. It isn’t su cient to simply state clr – Clear what? There
6Some processors use the term Code Condition register.
3. Stored Program Processing 53
Table 3.1: Our BASIC computer’s instruction set.
Instruction |
Mnemonic |
Operation |
Flags |
||||||||||
Z |
C |
||||||||||||
Arithmetic |
|||||||||||||
Add W and F |
addwf |
f,d |
(d) <- W + (f) |
√ |
√ |
||||||||
Add Literal and W |
addlw |
L |
W <- #L + W |
√ |
√ |
||||||||
Clear F |
clrf |
f |
(f) <- 00 |
√ |
• |
||||||||
Clear W |
clrw |
f,d |
(f) <- 00 |
√ |
|||||||||
Increment F |
incf |
(d) <- (f) + 1 |
√ |
• |
|||||||||
Decrement F |
√ |
• |
|||||||||||
decf |
f |
(d) <- (f) - 1 |
• |
||||||||||
Subtract W from F |
subwf |
f,d |
(d) <- (f) - W |
√ |
√ |
||||||||
Subtract W from L |
sublw |
L |
W <- #L - W |
√ |
√ |
||||||||
Movement |
|||||||||||||
Move F |
movf |
f,d |
(d) <- (f) |
√ |
• |
||||||||
Move W to F |
movwf |
f |
W <- (f) |
• |
• |
||||||||
Move Literal to W |
movlw |
L |
W <- #L |
• |
• |
||||||||
Logic |
|||||||||||||
AND W and F |
andwf |
f,d |
(d) <- W |
(f) |
√ |
• |
|||||||
AND Literal and W |
andlw |
L |
W <- #L |
· |
·W |
√ |
|||||||
Complement F |
√ |
• |
|||||||||||
comf |
f |
(f) <- (f) |
• |
||||||||||
Inclusive OR W and F |
iorwf |
f,d |
(d) <- W + (f) |
√ |
|||||||||
Inclusive OR Literal and W |
√ |
• |
|||||||||||
iorlw |
L |
W <- #L + W |
• |
||||||||||
Rotate left F |
rlf |
f,d |
(d) <- |
C |
• |
√ |
|||||||
f |
|||||||||||||
Rotate right F |
rlr |
f,d |
(d) <- |
C |
• |
√ |
|||||||
f |
|||||||||||||
eXclusive OR W and F |
xorwf |
f,d |
(d) <- W |
(f) |
√ |
• |
|||||||
eXclusive OR Literal and W |
xorlw |
L |
W <- #L |
√ |
|||||||||
W |
• |
||||||||||||
Skip and Jump
Bit Test F, Skip if Clear |
btfsc |
f,b |
b |
== |
0 ? |
PC++ : |
PC |
|||
Bit Test F, Skip if Set |
btfss |
f,b |
b |
== |
1 ? |
PC++ : |
PC |
|||
Decrement F, Skip if Zero |
decfsz |
f |
--(f) == |
0 ? |
PC++ : |
|||||
Go to address |
goto |
<ea> |
PC <- |
<ea> |
||||||
Increment F, Skip if Zero |
incfsz |
f |
++(f) |
== |
0 |
? |
PC++ : |
|||
• •
• •
PC √ •
• •
PC √ •
√ |
: Flag operates normally |
• |
: Flag not a ected |
||
<ea> |
: E ective address |
b |
: Bit b (0–7) in file |
||
C |
: Carry or |
bit 0 in F3 |
Z |
: Zero, bit 2 in F3 |
|
Borrow, |
|||||
d |
: Destination; 0 = W, 1 = file |
W |
: Working register |
||
L |
: 8-bit Literal |
== |
: Equivalent to |
||
++ |
: Increment |
-- : Decrement |
|||
A?S1:S2 |
: IF A is True THEN DO S1 ELSE DO S2 |
||||
54 The Quintessential PIC Microcontroller
7 |
0 |
|||
Working |
register |
|||
9 |
0 |
|||
F2 |
||||
Program Counter |
||||
7 |
0 |
|||
F0
Indirect Pointer register
F4
File Select register
Address register pair
2 0
Z C
Status register (F3)
Fig. 3.5 Programmer’s model.
are di erent ways of specifying the operand location. These are known as address modes.
Inherent
A few instructions, not shown in Table 3.1, do not explicitly refer to any location. For example return for RETURN from subroutine..
Register Direct
Here the operand is specified to be in the Working register. For example:
clrw |
; Clear the Working register |
Most instructions specify at least one operand should be in W. In many cases W can be both a source and the destination operand. For instance:
addwf f,w ; Add W to a file and put the answer in W
where W initially holds one of the two source datum bytes, and ends up holding the outcome datum.
3. Stored Program Processing 55
Literal
This address mode is used to specify an operand which is constant data rather than a location. For example:
addlw 120 ; Add the constant 120 (#120) decimal to W
Only special literal instructions are used with this type of data, such as movlw and sublw. The destination of this type of instruction is always W. The sublw instruction sometimes causes confusion as it actually takes away the contents of W from the literal byte and not vice versa. Thus sublw 1 does not subtract one from the contents of W (i.e. decrement W) but 1 − W. To decrement the contents of W use addlw -1 (i.e. addlw 0FFh). Note the use of the # symbol in the rtl description to denote that the following number is constant data.
A few instructions can test or modify a single bit in a File. For instance:
btfsc 3Fh,6 ; Test File 3Fh bit 6; skip next instr. IF Clear
in which Operand B is the fixed literal 6, specifying the bit position in Operand A’s specified File.
In both kinds of literal instruction the constant is encoded as part of the instruction word. In the former, eight bits of the 14-bit word is used, and in the latter three bits (see Appendix A).
File Direct
Here the totally fixed file address of the operand, either source or/and destination is directly specified. For example:
clrf 20h ; Clear the byte at File store address 20h
clears the byte out at File 20h.
In many cases the same file can be both source and destination. Thus:
incf 20h,f ; Put the contents of File 20h plus 1 into File 20h
as opposed to the Working register:
incf 20h,w ; Put the contents of File 20h plus one into W
which uses File Direct addressing for Operand A and Register Direct for Operand B.
The main characteristic of this address mode is that the location of the operand is fixed as an integral part of the program, and thus cannot be changed as execution progresses. Although directly specifying its address may seem to be the obvious way to locate an object in the Data store, in some situations this technique is rather inflexible.
Suppose we wished to clear all file registers from 0Ch through to 3Fh in the File store, say to hold an array of 52 byte elements. The obvious
56 The Quintessential PIC Microcontroller
Program 3.1 Clearing a block of files the linear way.
CLEAR_ARRAY |
clrf |
0Ch ; Clear File 12 |
|
clrf |
0Dh ; and File 13 |
||
clrf |
0Eh ; Each clrf |
||
clrf |
0Fh ; uses one instruction word |
||
clrf |
10h |
; in the Program store |
|
clrf |
11h |
; File 17 cleared |
|
clrf |
12h |
; and so on |
|
.... ... |
|||
.... ... |
|||
clrf |
3Eh ; Clear File 62; nearly there |
||
clrf |
3Fh ; Clear File 63; Phew! |
||
way to do this is shown in Program 3.1, which uses a clrf instruction for each byte. Although it works, it is rather ine cient, and the mind boggles if you wanted to clear, say, a 1 Kbyte File store. There has got to be a better way!
File Indirect
Indirect addressing uses an address register to hold the address of an operand. This address register thus acts as a pointer into the Program store. The term indirect is used as this address register does not hold the operand datum itself, only a pointer address to it. The advantage of this seemingly perverse way of accessing data in the Data store, is that the e ective address (ea) is a variable and this can be altered by the program as it progresses. This ea is the contents of this special pointer address register.
In our BASIC computer the File Select Register (FSR) is implemented as File 4 in the File store. The indirect mechanism is evoked when the dummy address File 0 (there is no physical file at location 0 in the Data store) is used for the operand address, as shown in Fig. 3.6.7 Thus, for example, if the contents of File 4 happened to be 20h then:
movwf 0 ; Store datum in W out to place pointed to by File 4
will e ectively copy the contents of W out to File 20h.
This seems rather an obscure way of doing things, but by way of a justification let us revisit our array clearing example of Program 3.1. Repeating the same thing 52 times on successive file locations is a dubious way of doing this. Why not use a pointer into the array, and increment this pointer each time we do a Clear? That is, rather than using a constant address for the destination operand use a variable e ective address.
Program 3.2 follows the scheme by folding the linear structure of the previous program into a loop, shown shaded. The execution path keeps
7Although this all seems rather complicated, I have kept in mind the microcontroller that we will be examining in the following chapters.