Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5315
Скачиваний: 0
72 The Quintessential PIC Microcontroller
28 51 A3 47 8F 1E 3C 79 F2 E4 C8 91 22 45 8B 16 …
The sequence will repeat after 127 output values.
What would happen if the initial value of the random number was zero?
Self-assessment questions
3.1How could you simply with one instruction toggle bit 0 of any file register?
3.2As part of a Data memory testing procedure each file in the range File 0Ch through File 2Fh is to be set to the pattern 01010101b (55h). Using Program 3.2 as a model, write a suitable coding to implement this task.
3.3Write a program to subtract the double-byte datum which is located in File 22:23h, called NUM_2, from NUM_1 in File 20:21h. The doublebyte di erence is to be in File 24:25h. Remember, if there is a borrow from the lower byte subtraction then an additional one must be subtracted from NUM_1 in the upper byte subtraction. Assume that NUM_2 is smaller or equal to NUM_1. If this were not so how could you determine this situation after the routine has been completed?
3.4Write a routine that will determine how many hundreds there are
in a byte in Data memory at File 20h. The outcome, which is to be in W, will either be 02h, 01h or 00h. For example if the contents of File 20 are FFh (decimal 255) then the outcome will be two. Hint: Try subtracting the number 200 from that in W and examining the Carry/borrow flag. If no borrow then try 100. Again if no borrow then the number must be less than 100.
3.5 The binary approximation to the fraction 13 is:
1 1 1 1 1 1 1 1 3 = 2 − 4 + 8 − 16 + 32 − 64 + 128 · · ·
Using this series, write a program that will divide a byte in the Working register by three, with the quotient being in the same register at the end. You can use File 20h and File 21h as temporary storage for the quotient and shifting number respectively. The outcome up to 1281 is 0.3359375, which is within 0.78% of the exact value. With an 8-bit datum there is no point in including any further elements in the series.
3. Stored Program Processing 73
3.6Write a routine that will count the number of ones in the Working register. For example if W were 01110011b then the outcome in File 20h would be 05h. Hint: Continually shift the number, while incrementing the count when the shifted-out bit in the Carry flag is one. Either do this eight times or else exit when the residue is zero. Remember if taking the latter approach that the Carry flag must be cleared before rotating the number! You may use File 21h as a temporary store for the shifting number.
3.7Data from an array of data memory between File 30h and File 4Fh is to be transmitted byte by byte to a distant computer over the internet. In order to allow the receiver to examine the data and check for transmission errors it is proposed to append a single byte which is the 2’s complement (i.e. the negative value, see page 9) of the 8-bit sum of all the data bytes together. If all the received data bytes plus this checksum byte are similarly added then the outcome should be zero if no error has occurred. Code a routine to scan through this data, placing this checksum in File 20h. See Example 3.2 for a template.
3.8One simple way of encrypting a data byte is to reverse the order of bits. For example 10111100b → 00111101b. Write a routine to implement this reversal on a data byte in File 20h. The encrypted outcome is to be in the Working register. You can use location File 21h as a temporary workspace and W as a loop counter. Hint: Use the Rotate Right and Rotate Left File instruction eight times. If you use W as the loop register then use the instruction addlw -1 as a decrement W operation.
3.9Parity is a simple technique to protect digital data from corruption by noise. Odd parity adds a single bit to a word in such a way as to ensure the overall packet has an odd number of 1s. Write a routine that takes an 8-bit byte stored at File 20h and alters its most significant bit to comply with this specification. You can assume that bit 7 is always 0 before the routine begins. Hint: Determine if a binary number is odd or even by counting the number of bits as in SAQ 3.6 and then
examining its least significant bit. All powers of two are even except 20 = 1. Thus if this bit is 1 then the number is odd.
PART II
The Software
In Part I we developed the concept of the Havard architecture, ending up with our somewhat simplified BASIC computer. Although BASIC was entirely fictitious, it was designed with an eye to the MCU that forms the basis for the rest of this book.
This part of the text looks mainly at the software aspects of our chosen MCU, the mid-range Microchip PIC family. We will be covering:
•The internal structure of the MCU.
•The instruction set.
•Address modes.
•The assembly translation process.
•Subroutines and modular program design.
•Interrupt handling.
•The high-level language C.
CHAPTER 4
The PIC16F84 Microcontroller
In this chapter we introduce the PIC16F84 MCU, which we will use as our baseline exemplar for the rest of the text. Here we will primarily look at internal structure, reserving external interfacing considerations for Part 3 of the book.
After reading this chapter you should:
•Recognize the di erence between a microprocessor and microcontroller.
•Understand the Harvard-based architecture with its parallel fetch and execute units.
•Appreciate the function, structure and memory map of the unrelated Program and Data stores.
•Be able to interpret the Status register bits that control memory paging and hold the C, DC and Z flags.
•Know how to manipulate the contents of the Program Counter in conjunction with the PCLATH special-purpose file register.
•Understand the interaction between the clock phases and the internal sequence of micro-operations.
•Appreciates the principle of banking in the Data store and its relationship to the RP0 control bit in the Status register.
•Know what peripheral functions are integral to the PIC16F84.
What exactly is a microcontroller unit? In a nutshell, a microcontroller is a MicroProcessor Unit (MPU) which is integrated with memory and input/output peripheral interface functions on the (usually) one integrated circuit. In essence it is a MPU with on-board system support circuitry. Thus we begin by investigating the origins of the MPU. From a historical perspective the story begins in 1968 when Robert Noyce (one of the inventors of the integrated circuit), Gordon Moore1 and Andrew Grove left the Fairchild Corporation and founded their own company, which they called Intel.2 Within three years, Intel had developed all the basic types of semiconductor memories used today – dynamic and static RAMs and EPROMs.
1Moore’s law stated in 1964 that the number of elements on a chip would double every 18 months, although this was subsequently revised to 2 years.
2Reputed to stand for INTELligence or INTegrated ELectronics.
78 The Quintessential PIC Microcontroller
As a sideline Intel also designed large-scale integrated circuits to customers’ specifications. In 1970 they were approached by the Nippon Calculating Machine Corporation, and asked to manufacture a suitable chip set for a line of calculators to be named Busicom. At that time calculators were a fast-evolving product and any LSI devices were likely to be superseded within a few years. This of course would reduce an LSI product’s profitability and increase its cost. Engineer Ted Ho – reputedly while on a topless beach in Tahiti – came up with a revolutionary way to tackle this project. Why not make a simple computer central computing unit (CPU) on silicon? This could then be programmed to implement the calculator functions, and as time progressed these could be enhanced by developing this software. Besides giving the chip a longer and more profitable life, Intel were in the business of making memories – and computer-like architectures need lots of memory. Truly a brain wave. The Japanese company endorsed the Intel design for its simplicity and flexibility in late 1969, rather than the conventional implementation.
Federico Faggin joined Intel in spring 19703 and by the end of the year had produced working samples of the first chip set. This could only be sold to the Nippon Calculating Machine Corporation, but by the middle of 1971, in return for a price reduction, Intel were given the right to sell the chip set to anyone for non-calculator purposes. Intel was dubious about the market for this device, but went ahead and advertised the 4004 “Micro-Programmable Computer on a Chip” in the Electronic News of November 1971. The term microprocessor unit was not coined until 1972. The 4004 created a lot of interest as a means of introducing ‘intelligence’ into electronic products.
The 4004 MPU featured a von Neumann architecture using a four-bit data bus, with direct addressing of 512 bytes of memory. Clocked at 108 kHz, it was implemented with a transistor count of 2300.4 Within a year the eight-bit 200 kHz 8008 appeared, addressing 16 Kbytes and needing a 3500 transistor implementation. Four bits is satisfactory for the BCD digits used in calculators but eight bits is more appropriate for intelligent data terminals (like cash registers) which need to handle a wide range of alphanumeric characters. The 8008 was replaced by the 80805 in 1974, and then the slightly modified 8085 in 1976. The 8085 is still the current Intel eight-bit device. Strangely, 4-bit MPUs were to outsell all other sizes until the early 1990s.
The MPU concept was such a hit that many other electronic manufactures clambered on to the bandwagon. In addition, many designers jumped ship and set up shop on their own, such as Zilog. By 1976 there
3He was later to found Zilog (last word (Z) in Integrated LOGic) which became notable with the Z80 MPU – a rather superior Intel 8085.
4Compare with the Pentium Pro (also known as the P6 or 80686) at around 5.5 million! 5Designed by Masatoshi Shima, who went on to design the 8080-compatible Z80 for
Zilog.
4. The PIC16F84 Microcontroller 79
were 54 di erent MPUs either available or announced. For example, one of the most successful families was based on the 6800 introduced by Motorola.6 The Motorola 6800 had a clean and flexible architecture, could be clocked at 2 MHz and address up to 64 Kbyte of memory. The 6802 (1977) even had 128 bytes of on-board memory and an internal clock oscillator. By 1979 the improved 6809 represented the last in the line of these eight-bit devices, competing mainly with the Intel 8085, Zilog Z80 and MOS Technology’s 6502.
The MPU was not really devised to power conventional computers, but a small calculator company called MITS,7 faced with bankruptcy, took a final desperate gamble in 1975 and decided to make and market a computer. This primitive machine, designed by Ed Roberts, was based on the 8080 MPU and interacted with the operator using front panel toggle switches and lamps – no keyboard and VDU. The Altair8 was advertised for $500, and within a month MITS had $250,000 in the bank for advance orders.
This first Personal Computer (PC) spawned a generation of computer hackers. Thus an unknown 19-year-old Harvard computer science student, Bill Gates, and a visiting friend, Paul Allen, in December 1975 noticed a picture of the Altair9 on the front cover of Popular Electronics and decided to write software for this primordial PC. They called Ed Robert with a blu , telling him that they had just about finished a version of the BASIC programming language that would run on the Altair. Thus was the Microsoft Corporation born.
In a parallel development, 22 Altair owners in San Francisco set up the Home-brew club. Two members were Steve Jobs and Steve Wozniak. As a club demonstration, they built a PC which they called the Apple.10 By 1978 the Apple II made $700,000; in 1979 sales were $7 million, and then $48 million…
The Apple II was based around the low-cost 6502 MPU which was produced by a company called MOS Technology. It was designed by Chuck Peddle, who was also responsible for the 6800 MPU, and had subsequently left Motorola. The 6502 bore an uncanny resemblance to the Motorola 6800 family and indeed Motorola sued to prevent the related 6501 MPU being sold, as it even had the same pinout as the 6800. The 6502 was one of the main players in PC hardware by the end of the 1970s, being
6Motorola was launched in the 1930s to manufacture motor car radios, hence the name “motor” and “ola” – as in pianola. It has the largest share of the world-wide microcontroller market at the time of writing (1999).
7Located next door to a massage parlor in New Mexico. 8After a planet in Star Trek.
9The picture was just a mock up, they actually were not yet available; an early example of computer ‘vaporware’!
10Jobs was a fruitarian and had previously worked in an apple orchard.
80 The Quintessential PIC Microcontroller
the computing engine of the BBC series and Commodore PETs amongst many others.
What really powered up Apple II sales was the VisiCalc spreadsheet package. When the business community discovered that the PC was not just a toy, but could do ‘real’ tasks, sales took o . The same thing happened to the IBM PC. Reluctantly introduced by IBM in 1981, the PC was powered by an Intel 8088 MPU clocked at 4.77 MHz together with 128 Kbyte of RAM, a twin 360 Kbyte disk drive and a monochrome textonly VDU. The operating system was Microsoft’s PC/MS-DOS version 1.0. The spreadsheet package here was Lotus 1-2-3.
By the end of the 1970s the technology of silicon VLSI fabrication had progressed to the stage that several tens of thousands transistors could be integrated on the one chip. Microprocessor designers were quick to exploit this capability in one of two ways. The better known of these was to increase the size of the ALU and buses/memory capacity. Intel were the first with the 29,000-transistor 8086, introduced in 1978 as a 16-bit version of the 8085 MPU.11 It was designed to be compatible with its eight-bit predecessor in both hardware and software aspects. This was wise commercially, in order to keep the 8085’s extensive customer base from looking at competitor products, but technically dubious. It was such previous experience that led IBM to use the 8088 version, which had a reduced eight-bit data bus and 20-bit address bus12 to save board space.
In 1979 Motorola brought out its 16-bit o ering called the 68000 and its eight-bit data bus version, the 68008 MPU. However, internally it was 32-bit, and this has provided compatibility right up to the 68060 introduced in 1995 and ColdFire RISC device launched in 1997. With a much smaller eight-bit customer base to worry about, the 68000 MPU was an entirely new design and technically much in advance of its 80X86 rivals.
The 68000 was adopted by Apple for its Macintosh series of PCs. However, the Apple Mac only accounts for less than 5% of PC sales. Motorola MPUs have been much more successful in the embedded microprocessor market, the area of smart instrumentation from egg timers to aircraft management systems. Of course, this is just the area which MPUs were developed for in the first place, and the number, if not the profile and value, of devices sold for this purpose exceeds those for computers by more than an order of magnitude.
In this applications area an MPU is ‘buried’ in the application circuit together with memory and various input and output interface circuits. The MPU with its program acts as the controller of the system by virtue of the software in program memory. Over 3.5 billion microprocessor and
11and the Intel 8086 architecture-based MPUs are by far the largest selling MPU for computer-based circuitry.
12A 220 address space is 1 Mbyte, and this is why for backwards compatibility MS-DOS was limited to 1 Mbyte of conventional memory.
4. The PIC16F84 Microcontroller 81
related devices are sold each year for embedded control, making up over 90% of the MPU market.
The second way of using the additional integrated circuit complexity that became available by the end of the 1970s was to keep a relatively simple CPU and use the extra silicon ‘real estate’ to implement on-board memory and input/output interface. In doing so, simple embedded control systems on the one chip became possible and the overall chip count to implement a given function was thereby considerably reduced. The majority of control tasks require relatively little computing power, but the reduction in size (and therefore cost) is vital. A simple example of this is the intelligent smart card, which has a processor integrated into the card itself. Such microprocessor-based devices were called MicroController Units (MCUs).13 For example there are about 100 microcontrollers hidden in every home; in the washing machine, microwave oven, telephones, electronic games and so on. About 20 more lurk in the average family car.14
Microcontroller |
||||
G5 |
MPU |
|||
|
Gearbox |
G4 |
Input |
SRG49 |
|
G2 |
||||
G3 |
||||
|
D |
||||
|
ports |
Odometer |
|||
G1 |
Output |
|||
Reset trip |
ports |
|||
mi/km |
||||
|
SRG28 |
||||
Counter |
||||
Tacho |
pulse |
D |
||
Trip |
||||
program |
||||
memory |
||||
Data memory
Non-volatile
store
Fig. 4.1 An example of a system based on a microcontroller.
13The term microcomputer was an alternative term but was easily confused with early personal computers and has dropped into disuse.
14New Scientist, vol. 59, no. 2141, 4th July 1998, pp. 139.