Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5255
Скачиваний: 0
List of Figures XI
15.3 The first 32 bytes of EEPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438 15.4 The PIC16F87X flash and Data EEPROM storage system. . . . . . . 440 15.5 The PIC16F87X EEPROM Control register 1. . . . . . . . . . . . . . . . . . . 441 15.6 View of the flash Program module. . . . . . . . . . . . . . . . . . . . . . . . . . . 445 15.7 Configuration word for the PIC16F87X devices. . . . . . . . . . . . . . . 445 15.8 Watchdog timer period versus temperature. . . . . . . . . . . . . . . . . . 448
16.1 The annunciator hardware. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 16.2 The modular software structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 16.3 The Main process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
16.4Programming the PIC from MPLAB.. . . . . . . . . . . . . . . . . . . . . . . . . . 472
16.5The Microchip PICSTART Plus programmer. . . . . . . . . . . . . . . . . . 473
List of Tables
1.1 7-bit ASCII characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2 Some common bit groupings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.3 Di erent ways of representing the quantities decimal 0…20. . 7
3.1Our BASIC computer’s instruction set. . . . . . . . . . . . . . . . . . . . . . . 53
5.1Move instructions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
5.2Arithmetic.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.3Logic instructions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
5.4 Program Counter instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 6.1 Subroutine and interrupt handling instructions. . . . . . . . . . . . . . 139
6.2The 7-segment lookup table showing byte[N] being extracted. 149
8.1 The listing file root.lst. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
8.2The absolute 8-bit Intel format object-code file root.hex. . . . 206
8.3 The error file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
8.4Part of Microchip’s file p16f84.inc. . . . . . . . . . . . . . . . . . . . . . . . . 209
8.5 The pic16f84.lkr linker command file. . . . . . . . . . . . . . . . . . . . . 212
8.6The output linker map file rms.asm. . . . . . . . . . . . . . . . . . . . . . . . . 218
8.7 The resulting absolute object file rms.hex. . . . . . . . . . . . . . . . . . . 219 9.1 Resulting assembly-level CCS compiler output after linking. . . 240
10.1PIC16F83/4 Special-Purpose Register file reset summary. . . . . 263
10.2Power-up reset and sleep timeouts. . . . . . . . . . . . . . . . . . . . . . . . . 265
10.3 Reset conditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
11.1 Summary of mid-range PIC parallel I/O provision. . . . . . . . . . . . 272 11.2 Energization pattern for the eight field directions. . . . . . . . . . . . 294
12.1 The SSP Mode bits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
14.1 Quantization parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 14.2 ADC clocking frequency versus device crystal frequency. . . . . 401 14.3 Configuring the ADC port pins in the PIC16C73/74 devices. . . 405
List of Programs
3.1 Clearing a block of files the linear way. . . . . . . . . . . . . . . . . . . . . . . 56 3.2 Clearing a block of files using a repeating loop. . . . . . . . . . . . . . . 57 3.3 Simple single-precision addition of two byte variables. . . . . . . . 64
3.4A more accurate single-precision addition. . . . . . . . . . . . . . . . . . . 64
3.5 The double-precision add program. . . . . . . . . . . . . . . . . . . . . . . . . . 66 3.6 Dividing by ten. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 3.7 Multiplying by nine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 3.8 A 7-bit pseudo-random number generator. . . . . . . . . . . . . . . . . . . 71
4.1Incrementing a packed BCD byte. . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
4.2 Adding two packed BCD numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . 103 5.1 Finding the maximum temperature the linear way. . . . . . . . . . . . 111 5.2 Finding the maximum temperature using a loop structure. . . . 113
5.3Division by repetitive subtraction. . . . . . . . . . . . . . . . . . . . . . . . . . . 118
5.4 Shifting to find the highest set bit. . . . . . . . . . . . . . . . . . . . . . . . . . . 124 5.5 Triple-precision shifting to find the number of set bits. . . . . . . 125
5.6Multiplying by three. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
5.7Double-precision decrement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
5.8 Bi-quinary error detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
5.9Binary to 2-digit BCD conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
5.10 Average daily temperature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5.11 multiplication by ten. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 6.1 A 100 ms delay subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 6.2 A K × 100 ms delay subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
6.3An alternative K × 100 ms delay subroutine. . . . . . . . . . . . . . . . . 147
6.4 The software 7-segment decoder. . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 6.5 The byte multiplication subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . 152 6.6 Implementing a byte multiply using a stack model. . . . . . . . . . . 157 6.7 Dividing by three . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 6.8 Coding a 208 µs delay. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
6.9A 1-second delay program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
6.10Binary to 3-digit BCD conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
6.11Coding the square root subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . 163
6.12 Using a software stack to pass parameters. . . . . . . . . . . . . . . . . . . 166 6.13 The software 7-segment decoder revisited. . . . . . . . . . . . . . . . . . . 166
XVI List of Programs
7.1 Background program for the pea canning packer. . . . . . . . . . . . . 181 7.2 Event counting foreground software. . . . . . . . . . . . . . . . . . . . . . . . . 183
7.3Oven safety.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
7.4Saving and restoring the context for the PIC16C74 processor. 191
7.5 Coding the real-time clock ISR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
7.6Incrementing a packed-BCD byte with maximum value of 99.. 194
8.1 Absolute assembly-level code for our square-root module. . . . 200 8.2 The main relocatable source file main.asm. . . . . . . . . . . . . . . . . . . 214
8.3The relocatable source file sqr.asm. . . . . . . . . . . . . . . . . . . . . . . . . 215
8.4 The relocatable source file root2.asm. . . . . . . . . . . . . . . . . . . . . . . 216 9.1 A simple function coded in C.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 9.2 Coding the square root function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
9.3Linearizing a K-type thermocouple. . . . . . . . . . . . . . . . . . . . . . . . . . 246
9.4 Generating the root-mean square value of two variables. . . . . . 247 11.1 Scanning the keypad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 11.2 Noise filtered keypad scanning. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
11.3Interacting with the intruder hardware. . . . . . . . . . . . . . . . . . . . . . 288
11.4A digital comparator with hysteresis. . . . . . . . . . . . . . . . . . . . . . . . 292
11.5Driving a stepper motor.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
11.6 Coding the keypad device driver in C. . . . . . . . . . . . . . . . . . . . . . . . 297 11.7 Displaying the decimal equivalent of a binary byte. . . . . . . . . . . 301 11.8 Displaying a 3-digit decimal number on a scanning readout. . 302 12.1 Displaying the decimal equivalent of a binary byte. . . . . . . . . . . 308 12.2 Input serial byte subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 12.3 Interacting with the MAX549A dual-channel SPI DAC. . . . . . . . . 315 12.4 Using the SSP for SPI data input and output. . . . . . . . . . . . . . . . . . 320 12.5 Interfacing to the MAX549A in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
12.6 A crystal frequency-independent short delay macro. . . . . . . . . . 331 12.7 Low-level I2C subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
12.8 Interacting with the MAX518 dual-channel I2C DAC. . . . . . . . . . 334 12.9 Interfacing to the MAX518 in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 12.10 A baud-rate delay macro. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 12.11 Asynchronous formatted input and output subroutines. . . . . . 340 12.12 The USART-based I/O subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . 345 12.13 Updating Program 11.4’s trip value. . . . . . . . . . . . . . . . . . . . . . . . . . 350 12.14 Reading in a byte using the I2C protocol. . . . . . . . . . . . . . . . . . . . . 351 12.15 Incrementing the non-volatile odometer count. . . . . . . . . . . . . . . 354 12.16 Reading and writing on a 1-Wire system. . . . . . . . . . . . . . . . . . . . . 358 13.1 The bean counter Interrupt Service Routine. . . . . . . . . . . . . . . . . . 368 13.2 Measuring the ECG waveform period to a resolution of 1 ms. . 370 13.3 Generating a 15 minute data logger timebase. . . . . . . . . . . . . . . . 374 13.4 Capturing the instant of time an ECG R-point occurs. . . . . . . . . 378 13.5 Pulse-Width Modulation using Timer 0. . . . . . . . . . . . . . . . . . . . . . . 384 13.6 Tachometer software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
List of Programs XVII
13.7 Measuring the duration of a pulse. . . . . . . . . . . . . . . . . . . . . . . . . . . 388 14.1 Taking a reading from channel n. . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 14.2 Interrupt-driven subroutine to read channel n. . . . . . . . . . . . . . . 410 14.3 The ISR for our interrupt-driven ADC software. . . . . . . . . . . . . . . 411 14.4 Digitizing Channel 1 of a PIC16C71 device. . . . . . . . . . . . . . . . . . . 412 14.5 A digital/analog comparator with hysteresis. . . . . . . . . . . . . . . . . 414 14.6 Bu ered interrupt-driven data acquisition. . . . . . . . . . . . . . . . . . . 421 14.7 Sleep conversion in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 14.8 ECG peak picking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 14.9 An implementation of the ECG peak picker in C. . . . . . . . . . . . . . 427 15.1 Retrieving a byte from the EEPROM Data module. . . . . . . . . . . . . 434
15.2Putting a byte into the EEPROM Data module.. . . . . . . . . . . . . . . . 436
15.3Incrementing the non-volatile odometer count in Data EEPROM. 437
15.4 Reading a word from the flash Program store. . . . . . . . . . . . . . . . 442 15.5 Writing to flash Program memory. . . . . . . . . . . . . . . . . . . . . . . . . . . 443 15.6 Squaring an integer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 15.7 C-based coding for the odometer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 15.8 The Sauna Power-up reset sequence and ISR. . . . . . . . . . . . . . . . . 450 15.9 Reading a new period count. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 15.10 Updating the Sauna EEPROM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 16.1 The timebase software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461 16.2 The data display function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 16.3 The initialization code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 16.4 The Diagnostic process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 16.5 The Set-time process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 16.6 The Main process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
PART I
The Fundamentals
This book is about microcontrollers (MCUs). These are digital engines modelled after the architecture of a stored-program computer and integrated on to a single very large-scale integrated circuit together with support circuitry, memories and peripheral interface devices. Although the MCU is often confused with its better known cousin the microprocessor in its role of the driving force of the ubiquitous personal computer, the vast majority of both microprocessors and microcontrollers are embedded into an assemblage of other digital components. The first microprocessors in the early 1970s were marketed as an alternative way of implementing digital circuitry. Here the task would be determined by a series of instructions encoded as binary code groups in read-only memory. This is more flexible than the alternative approach of wiring hardware integrated circuits in the appropriate manner. The microcontroller is simply the embodiment of this original role of the integrated computer.
We will look at embedded MCUs in a general digital processing context in Parts II and III. Here our objective is to lay the foundation for this material. We will be covering:
•Digital code patterns.
•Binary arithmetic.
•Digital circuitry.
•Computer architecture and programming.
This will by no means be a comprehensive review of the subject, but there are many other excellent texts in this area1 which will launch you into greater depths.
1Such as S.J. Cahill’s Digital and Microprocessor Engineering, 2nd edn., Prentice Hall, 1993.
CHAPTER 1
Digital Representation
To a computer or microprocessor, the world is seen in terms of patterns of digits. The decimal (or denary) system represents quantities in terms of the ten digits 0…9. Together with the judicious use of the symbols +, − and . any quantity in the range ±∞ can be depicted. Indeed non-numeric concepts can be encoded using numeric digits. For example the American Standard Code for Information Interchange (ASCII) defines the alphabetic (alpha) characters A as 65, B = 66…Z = 90 and a = 97, b = 98…z = 122 etc. Thus the string “Microprocessor” could be encoded as “77, 105, 99, 114, 111, 112, 114, 111, 99, 101, 115, 115, 111, 114”. Provided you know the context, that is what is a pure quantity and what is text, then just about any symbol can be coded as numeric digits.1
Electronic circuits are not very good at storing and processing a multitude of di erent symbols. It is true that the first American digital computer, the ENIAC (Electronic Numerical Integrator And Calculator) in 1946 did its arithmetic in decimal2 but all computers since handle data in binary (base 2) form. The decimal (base 10) system is really only convenient for humans, in that we have ten fingers.3 Thus in this chapter we will look at the properties of binary digits, their groupings and processing. After reading it you will:
•Understand why a binary data representation is the preferred base for digital circuitry.
•Know how a quantity can be depicted in natural binary, hexadecimal and binary coded decimal.
•Be able to apply the rules of addition and subtraction for natural binary quantities.
•Know how to multiply by shifting left.
•Know how to divide by shifting right and propagating the sign bit.
•Understand the Boolean operations of NOT, AND, OR and XOR.
The information technology revolution is based on the manipulation, computation and transmission of digitized information. This informa-
1Of course there are lots of encoding standards, for example the 6-dot Braille code for the visually impaired.
2As did Babbage’s mechanical computer of a century earlier. 3And ten toes, but base-20 systems are rare.
4The Quintessential PIC Microcontroller
tion is virtually universally represented as aggregrates of binary digits (bits).4 Most of this processing is e ected using microprocessors, and it is sobering to reflect that there is more computing power in a singing birthday card than existed on the entire planet in 1950!
Binary is the universal choice for data representation, as an electronic switch is just about the easiest device that can be implemented using a transistor. Such 2-state switches are very small; they change state very quickly and consume little power. Furthermore, as there are only two states to distinguish between, a binary depiction is likely to be resistant to the e ects of noise. The upshot of this is that both the packing density on a silicon chip and switching rate can be very high. Although a switch on its own does not represent much computing power; five million switches changing at 100 million times a second, manage to present at least a facade of intelligence!
The two states of a bit are conventionally designated logic 0 and logic 1 or just 0 & 1. A bit may be represented by two states of any number of physical quantities; for example electric current or voltage, light, pneumatic pressure. Most microprocessors use 0 V (or ground) for state 0 and 3 – 5 V for state 1, but this is not universal. For instance, the RS232 serial port on your computer uses nominally +12 V for state 0 and −12 V for state 1.
A single bit on its own can only represent two states. By dealing with groups of bits, rather more complex entities can be coded. For example the standard alphanumeric characters can be coded using 7-bit groups of digits. Thus the ASCII code for “Microprocessor” becomes:
1001101 1101001 1100011 1110010 1101111 1110000 1110010 1101111
1100011 1100100 1110011 1110011 1101111 1110010
Unicode is an extension of ASCII and with its 16-bit code groups is able represent characters from many languages and mathematical symbols.
The ASCII code is unweighted, as the individual bits do not signify a particular quantity; only the overall pattern has any significance. Other examples are the die code on gaming dice and 7-segment code of Fig. 6.6 on page 148. Here we will deal with natural binary weighted codes, where the position of a bit within the number field determines its value or weight. In an integer binary number the rightmost digit is worth 20 = 1, the next left column 21 = 2 and so on to the nth column which is worth 2n−1. For example the decimal number one thousand nine hundred and ninety eight is represented as 1×103 +9×102 +9×101 +8×100 or 1998.
4The binary base is not a new fangled idea invented for digital computer; many cultures have used base 2 numeration in the past. The Harapp¯an civilisation existed more than 4000 years ago in the Indus river basin. Found in the ruins of the Harapp¯an city of Mohenjo-Daro, in the beadmakers’ quarter, was a set of stone pebble weights. These were in ratios that doubled in the pattern, 1,1,2,4,8,16…, with the base weight of around 25g (≈ 1oz). Thus bead weights were expressed by digits which represented powers of 2; that is in binary.
1. Digital Representation 5
Table 1.1: 7-bit ASCII characters.
MS nybble
LS nybble
NUL |
DLE |
SP |
0 |
@ |
P |
‘ |
p |
SOH |
DC1 |
! |
1 |
A |
Q |
a |
q |
STX |
DC2 |
" |
2 |
B |
R |
b |
r |
ETX |
DC3 |
# |
3 |
C |
S |
c |
s |
EOT |
DC4 |
$ |
4 |
D |
T |
d |
t |
ENQ |
NAK |
% |
5 |
E |
U |
e |
u |
ACK |
SYN |
& |
6 |
F |
V |
f |
v |
BEL |
ETB |
’ |
7 |
G |
W |
g |
w |
BS |
CAN |
( |
8 |
H |
X |
h |
x |
HT |
EM |
) |
9 |
I |
Y |
i |
y |
LF |
SUB |
* |
: |
J |
Z |
j |
z |
VT |
ESC |
+ |
; |
K |
[ |
k |
{ |
FF |
FS |
, |
< |
L |
\ |
l |
| |
CR |
GS |
- |
= |
M |
} |
m |
} |
SO |
RS |
. |
> |
N |
^ |
n |
~ |
SI |
US |
/ |
? |
O |
_ |
o |
DEL |
In natural binary the same quantity is 1 ×210 +1 ×29 +1 ×28 +1 ×27 + 1×26 +0×25 +0×24 +1×23 +1×22 +0×21 +1×20, or 11111001101b.
Fractional numbers may equally well be represented by columns to the right of the binary point using negative powers of 2. Thus 1101.11b is equivalent to 14.75. As can be seen from this example, binary numbers are rather longer than their decimal equivalent; on average a little over three times. Nevertheless, 2-way switches are considerably simpler than 10-way devices, so the binary representation is preferable.
An n-digit binary number can represent up to 2n patterns. Most computers store and process groups of bits. For example the first micropro-