Interfacing PIC Microcontrollers 4 pot - Pdf 16

code protection bits (CP1:CP0) disable reads from selected program areas.
Program memory may also be written from within the program itself, so that
data tables or error checking data can be modified. Obviously, this needs some
care, and this option can be disabled via the WRT bit. Data EEPROM may also
be protected from external reads in the same way via the CPD bit, while inter-
nal read and write operations are still allowed, regardless of the state-of-the-
code protection bits.
IN-CIRCUIT DEBUGGING
In-circuit debugging (ICD) allows the program to be downloaded after the
chip has been fitted in the application circuit, and allows it to be tested with
the real hardware. This is more useful than the previous method, which re-
quires the chip to be programmed in a separate programmer unit before in-
sertion in its socket on the board. With ICD, the chip can be programmed,
and reprogrammed during debugging, while avoiding possible electrical and
mechanical damage caused by removal from the circuit. The normal debug-
ging techniques of single stepping, breakpoints and tracing can be applied in
ICD mode. This allows a final stage of debugging in the prototype hardware,
where problems with the interaction of the MCU with the real hardware can
be resolved.
Interfacing PIC Microcontrollers
16
Bit Label Function Default Enabled Typical
15 – None 0 x 0
14 – None 0 x 0
13 CP1 Code protection 1 0 1
12 CP0 (4 levels) 1 0 1
11 DEBUG In-circuit debugging (ICD) 1 0 0
10 – None 1 x 1
9 WRT Program memory write enable 1 1 1
8 CPD EEPROM data memory write protect 1 0 1
7 LVP Low-voltage programming enable 1 1 0

Brown out refers to a short dip in the power-supply voltage, caused by mains
supply fluctuation, or some other supply fault, which might disrupt the
program execution. If the Brown-Out Detect Enable bit (BODEN) is set, a
PSU glitch of longer than about 100 µs will cause the device to be held in reset
until the supply recovers, and then wait for the power-up timer to time out,
before restarting. The program must be designed to recover automatically.
WATCHDOG TIMER
The watchdog timer is designed to automatically reset the MCU if the program
malfunctions, by stopping or getting stuck in loop. This could be caused by an
undetected bug in the program, an unplanned sequence of inputs or supply
fault. A separate internal oscillator and counter automatically generates a reset
about every 18 ms, unless this is disabled in the configuration word. If the
watchdog timer is enabled, it should be regularly reset by an instruction in the
program loop (CLRWDT) to prevent the reset. If the program hangs, and the
watchdog timer reset instruction not executed, the MCU will restart, and (pos-
sibly) continue correctly, depending on the nature of the fault.
RC OSCILLATOR
The MCU clock drives the program along, providing the timing signals for
program execution. The RC (resistor–capacitor) clock is cheap and cheerful,
requiring only these two inexpensive external components, operating with the
PIC Hardware
17
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 17
internal clock driver circuit, to generate the clock. The time constant (product
R ϫ C) determines the clock period. A variable resistor can be used to give a
manually adjustable frequency, although it is not very stable or accurate.
CRYSTAL OSCILLATOR
If greater precision is required, especially if the program uses the hardware
timers to make accurate measurements or generate precise output signals, a
crystal (XTAL) oscillator is needed. Normally, it is connected across the clock

18
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 18
19
PIC INSTRUCTION SET
F ϭ Any file register (specified by address or label), example is labelled GPR1
L ϭ Literal value (follows instruction), example is labelled num1
W ϭ Working register, W (default label)
Labels Register labels must be declared in include file or by register label equate (e.g. GPR1 EQU 0C)
Bit labels must be declared in include file or by bit label equate (e.g. bit1 EQU 3)
Address labels must be placed at the left margin of the source code file (e.g. start, delay)
Operation Example
Move
Move data from F to W MOVF GPR1,W
Move data from W to F MOVWF GPR1
Move literal into W MOVLW num1
Test the register data MOVF GPR1,F
Register
Clear W (reset all bits and value to 0) CLRW
Clear F (reset all bits and value to 0) CLRF GPR1
Decrement F (reduce by 1) DECF GPR1
Increment F (increase by 1) INCF GPR1
Swap the upper and lower four bits in F SWAPF GPR1
Complement F value (invert all bits) COMF GPR1
Rotate bits Left through carry flag RLF GPR1
Rotate bits Right through carry flag RRF GPR1
Clear ( ϭ 0) the bit specified BCF GPR1,but1
Set ( ϭ 1) the bit specified BSF GPR1,but1
Arithmetic
Add W to F, with carry out ADDWF GPR1
Add F to W, with carry out ADDWF GPR1,W

Note 2: General Purpose Register 1, labelled ‘ GPR1’, represents all file registers (00-4F). Literal value ‘num1’ represents all 8-bit values 00-FF. File
register bits 0–7 are represented by the label ‘but1’.
Note 3: The result of arithmetic and logic operations can generally be stored in W instead of the file register by adding, ‘W’ to the instruction. The full syntax
for register operations with the result remaining in the file register F is ADDWF GPR1,F etc. F is the default destination, and W the alternative, so the
instructions above are shortened to ADDWF, GPR1, etc. This will generate a message from the assembler that the default destination will be used.
Table 1.3 PIC instruction set by functional groups
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 19
register, but the working register W is sometimes an option. Each instruction
is described in detail in the MCU data sheet, Section 13.
Instruction Types
The functional groups of instructions, and some points about how they work,
are described below. The use of most of these instructions will be illustrated in
due course within the demonstration programs for each type of interface.
MOVE
The contents of a register are copied to another. Notice that we cannot move a
byte directly from one file register to another, it has to go via the working
register. To put data into the system from the program (a literal) we must use
MOVLW to place the literal into W initially. It can then be moved to another
register as required.
The syntax is not symmetrical; to move a byte from W to a file register,
MOVWF is used. To move it the other way, MOVF F,W is used, where F is any
file register address. This means that MOVF F,F is also available. This may
seem pointless, but in fact can be used to test a register without changing it.
REGISTER
Register operations affect only a single register, and all except CLRW (clear W)
operate on file registers. Clear sets all bits to zero (00h), decrement decreases
the value by 1 and increment increases it by 1. Swap exchanges the upper and
lower four bits (nibbles). Complement inverts all the bits, which in effect
negates the number. Rotate moves all bits left or right, including the carry flag
in this process (see below for flags). Clear and set a bit operate on a selected bit,

Using GOTO label simply transfers the program execution point to some other
point in the program indicated by a label in the first column of the source code
line, but CALL label means that the program returns to the instruction following
the CALL when RETURN is encountered at the end of the subroutine.
Another option, which is useful for making program data tables, is RETLW
(Return with Literal in W). See the KEYPAD program later for an example of
this. RETFIE (Return From Interrupt) is explained below.
CONTROL
NOP simply does nothing for one instruction cycle (four clock cycles). This
may seem pointless, but is in fact very useful for putting short delays in the
program so that, for example, external hardware can be synchronised or a
delay loop adjusted for an exact time interval. In the LCD driver program
(Chapter 4), NOP is used to allow in-circuit debugging to be incorporated later
when the program is downloaded, and to pad a timing loop so that it is exactly
1 ms.
SLEEP stops the program, such that it can be restarted with an external
interrupt. It should also be used at the end of any program that does not loop
back continuously, to prevent the program execution continuing into unused
locations. The unused locations contain the code 3FFF (all 1 s), which is a
valid instruction (ADDLW FF). If the program is not stopped, it will run
through, repeating this instruction, and start again when the program counter
rolls over to 0000.
CLRWDT means clear the watchdog timer. If the program gets stuck in a
loop or stops for any other reason, it will be restarted automatically by the
watchdog timer. To stop this happening when the program is operating nor-
mally, the watchdog timer must be reset at regular intervals of less than, say,
10 ms, within the program loop, using CLRWDT.
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 21
Interfacing PIC Microcontrollers
22

Subroutines are used to create functional blocks of code, and provide
good program structure. This makes it easier for the program to be understood,
allows blocks of code to be re-used, and ultimately allows ready-made
library routines to be created for future use. This saves on programming
time and allows us to avoid ‘re-inventing the wheel’ when writing new
applications.
A label is used at the start of the subroutine, which the assembler then
replaces with the actual program memory address. When a subroutine is
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 22
called, this destination address is copied into the program counter, and the
program continues from the new address. At the same time, the return ad-
dress (the one following the CALL) is pushed onto the stack, which is a
block of memory dedicated to this purpose. In the PIC, there are 8 stack ad-
dress storage levels, which are used in turn. The return addresses may thus
be viewed as a stack of items, which must be added and removed in the same
sequence.
The subroutine is terminated with a RETURN instruction, which causes the
program to go back to the original position and continue. This is achieved by
pulling the address from the top of the stack and replacing it in the program
counter. It should be clear that CALL and RETURN must always be used in
sequence to avoid a stack error, and a possible program crash. Conventional
microprocessor systems often use general RAM as the stack, in which case it
is possible to manipulate it directly. In the PIC, the stack is not directly acces-
sible (Figure 1.7).
A delay subroutine is included in the program BIN4. The stack mechanism
and program memory arrangement is shown in Figure 2.1 in the data sheet, and
a somewhat simplified version is shown in Figure 1.6.
INTERRUPTS
The stack is also used when an interrupt is processed. This is effectively a call
and return which is initiated by an external hardware signal which forces the

instructions
13 bit
hex address
Instruction 1 0000h (RESET)
Instruction 2 0001h
Instruction 3 0002h
Instruction 4 0003h
Instruction 5 0004h (INTERRUPT)
Instruction 6 0005h
Page 0








(2k)
Instruction 2048 07FFh (END PAGE 0)
Instruction 2049 0800h (START PAGE 1)
Page 1 (2k)
Instruction 4096 0FFFh (END PAGE 1)
Instruction 4097 1000h (START PAGE 2)
Page 2 (2k)
Instruction 6144 17FFh (END PAGE 2)
Instruction 6145 1800h (START PAGE 3)
Page 3 (2k)
Instruction 8192 1FFFh (END PAGE 3)
Program Counter (13)

not zero. The full instruction set must be consulted to confirm which operations
affect the Z flag. Bit test and skip instructions use this flag for conditional
branching, but remember that there are dedicated instructions for decrement or
increment and skip if zero. Curiously, these do not affect the zero flag itself. A
typical use of the zero flag is to check if two numbers are the same by
subtracting and applying bit test and skip to the Z bit.
CARRY FLAG (C)
This flag is only affected by add, subtract and rotate instructions. If the result of
an add operation generates a carry out, this flag is set; that is, when two 8-bit
numbers give a 9-bit sum. The carry bit must then be included in subsequent
calculations to give the right result. When subtracting, the carry flag must be set
initially, because it provides the borrow digit (if required) in the most signifi-
cant bit of the result. If the carry flag is cleared after a subtraction, it means the
result was negative, because the number being subtracted was the larger. An
example of this is seen later in the calculator program.
Taken together, the zero and carry flags allow the result of an arithmetic
operation to be detected as positive, negative or zero, as shown in Table 1.4.
PIC Hardware
25
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 25


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status