The PIC Architecture
The PIC Architecture
The PIC Architecture
Page 1 of 9
John J. McDonough, WB8RCR
Architectural Overview
Introduction Let me apologize in advance for this section. You may find this particular section especially dull. There is a bunch of background information to get to you, and here it is. In later sections we will actually do something, and you will likely find that a lot more fun. This section is about a number of basic concepts. If there are some details that dont quite gel for you, dont get overly concerned. As we begin to poke at the PIC and do things with it, some of what we are talking about will become more clear. The PIC is a self-contained, Harvard architecture microcontroller. It can be highly effective at replacing relatively complex discrete logic circuits. The PIC is not a microprocessor in the sense of a Pentium or 80386. The PIC is targeted at embedded applications where there is a fixed program which is relatively simple. The PIC is a RISC (Reduced Instruction Set Computer) processor. It has only 35 instructions. While this means that it may take more instructions to accomplish a simple task than a Pentium, which has hundreds of instructions, it also means that there are only 35 instructions to learn. This makes the PIC simple to apply. Harvard vs. von Neumann Architecture The traditional microprocessor is what is called a von Neumann architecture. The significant characteristic is that there is a single memory. Programs and data are stored in the same memory, which makes it possible for a program to load another program into memory. This is a key characteristic of what we generally think of as a computer. In most practical computers, some portion of the address space is implemented in ROM so that the computer has some sense when it is first powered up, but from the point of view of a program, its all the same. In contrast, a Harvard architecture machine has multiple memories. In the case of the PIC there are three; the program memory, the file register, and the EEPROM. The EEPROM is almost like an I/O device, although it is implemented on-chip. We wont talk about the EEPROM much until later. One of the issues with von Neumann machines is that the instructions and data generally need to be different sizes. A lot of data is character data, which is typically 8 bits long. To be effective, a processor needs to be able to fetch a character from memory, so the memory wants to be organized in 8 bit pieces. However, if the instructions were only 8 bits, the processor would be awfully limited. The designer would be faced with, for example, allocating 2 bits to the instruction and 6 bits to the address of the operand. This would allow a machine with 4 instructions which could address 64 bytes of memory not a pleasant prospect. The way around it is to make the instructions multiple bytes. This slows things down a lot, and adds a lot of complexity to the chip, but its the price of the flexibility the von Neumann architecture affords. Harvard machines dont have that limitation. Since the program is stored in its own memory, the program memory doesnt have to be friendly to storing data. There is no reason to make multiple memories the same width, and in most Harvard machines they arent. In the case of the PIC, there are three series of PICs with 12, 14, and 16 bit wide program memories. We will largely concentrate on the 14 bit parts. Continued on next page Page 2 of 9
John J. McDonough, WB8RCR Revised: 16 Nov 2003 - 01:44 PM Printed: 16 Nov 2003 - 01:44 PM
Addressing
Page 3 of 9
John J. McDonough, WB8RCR
TMR0
Page 4 of 9
John J. McDonough, WB8RCR
Program Memory
Introduction The program memory is, well, where the program is stored. There really isnt a lot to say about it. In the 16F84, the program memory is 14 bits wide by 1024 locations long. The program memory is nonvolatile memory, which means we can program it and it will retain its value even when power is removed. There are two series of PIC parts, those with an F in the part number and those with a C. The F parts, like the 16F84, have FLASH program memory. The C parts have CMOS ROM. The C parts are less expensive, but they can be programmed only once. The F parts can be programmed thousands of times. Programming the Unlike your PC, the PIC itself cannot change its program memory. This requires Program memory special circuitry. In the case of the 16F84, a supply of over 12 volts is required to enable programming, in addition to the normal 5 volt logic supply. Some other PIC parts can be programmed with 5 volts only, but currently, all of the PICs can be programmed with 12 volt programmers. However, only a few pins are required to program a PIC. Because of this, it is practical to integrate some of the program circuitry into the application circuit. This is called in-circuit programming. There may be some cases where it isnt realistic to provide the isolation needed to program in-circuit, but in most cases it only adds a handful of parts, and it is a great convenience when you are experimenting.
EEPROM
Introduction The final type of memory in the PIC is Electrically Erasable PROM. This is data memory that the PIC itself can alter, and which retains its value even when the power is removed. EEPROM takes multiple steps to access, so we wont deal with it until later in the course. It is useful, though, to remember things like code speed or the last frequency set.
Page 5 of 9
John J. McDonough, WB8RCR
Thats basically all any computer does. It just relentlessly picks one instruction after another to execute. Instructions may tell it to load the working register with something, do some math, or store the result somewhere. Those I/O pins allow that somewhere to affect the outside world, that is, the circuit we want it to control.
Page 6 of 9
John J. McDonough, WB8RCR
Assembler
Introduction The PIC, or any computer for that matter, simply stores combinations of bits. It gets tough looking at long strings of ones and zeroes, so we need some sort of help. An assembler is a program that takes representations of these numbers that are more readable to a human, and translates them into a form that the computer can understand. You may also have heard of a compiler. The main difference between an assembler and a compiler is that everything you do in the assembler translates one to one into something in the computer. One thing you do in a compiler may translate into hundreds of things in the computer. The advantage of a compiler is that, if you are doing something the compiler knows how to do, it is a lot simpler than an assembler. The advantage of an assembler is that you control precisely what the computer will do. When we are using the computer for an embedded controller, we are often very concerned about timing, and this is a huge advantage. Number Representation As we said, to the computer, any computer, the world is a series of ones and zeroes. To us humans, though, its a real pain looking at these strings. In the assembler, we can, if we choose, represent a ten as B00001010, but thats kind of tough to look at. In the assembler, we can represent a ten as a decimal number by putting D in front of it. So the value D10 means exactly the same thing as B00001010. More often, we use hexadecimal representation for numbers, because decimal numbers dont map nicely into the bits. Hexadecimal numbers are like decimal numbers, except there are 16 values for each digit instead of 10. They run from 0 to F. Thus, 6 decimal would be represented as H06 in hexadecimal. But 10 decimal would be represented as H0a in hex. There is quite a good discussion of number representations on the web at: http://vwop.port5.com/beginner/bhextut.html if this is a little unclear. Continued on next page
Page 7 of 9
John J. McDonough, WB8RCR
Assembler, Continued
Machine Instructions Within the PIC, or any computer for that matter, any number that is pointed to by the program counter is interpreted as an instruction. Generally, these instructions include an operator and an operand. Also, in general, different operators use different numbers of bits, thus leaving a different number of bits for the operand. We could manually translate the operation we want into a number and store it in the machines program memory, but this is a big pain. The assembler deals with all this stuff for us. Lets assume that we want to place the number 4 into the working register. We could refer to the programming card and recognize that we need to put: 11000000000100 Into the program memory to cause this to happen. Its a lot easier, though, to say: MOVLW H04 The MOVLW tells the assembler that we want to move a literal constant into the W register. The H04 tells it that the literal constant we want to move is a 4. The assembler figures out that the left 6 bits being 110000 tells the chip to load the right 8 bits into the W register. Symbols OK, so the assembler helps us with the instructions, and it allows us to represent numbers in a variety of ways. Perhaps the most important feature, though, is that it allows us to associate symbols with values. Imagine, for example, that we were building a keyer. We decide to store the current code speed in location H3b in the file register. Now, if we wanted to load the current code speed into the working register, we could tell the assembler: MOVF H3b,0 The zero tells the assembler that the target of the move is the W register. However, we could define the symbol CodeSpd to mean 03bh. Now we can write: MOVF CodeSpd,W We not only dont need to be constantly remembering where we put things, but later on, when we come back to look at this program, it will make a lot more sense.
Page 8 of 9
John J. McDonough, WB8RCR
Wrap Up
Summary In this lesson we have gotten exposed to some of the basic concepts of microcontrollers in general, and the Microchip PIC in particular. At this point, these concepts are pretty theoretical, but they will become clearer as we begin to use them in our programs. For some of you, this has been pretty old news. For others, some of it may be confusing, and even a bit strange. There is no need to feel like you need to memorize everything in this lesson. It is sufficient that you have seen it, so that it can gel as we practice. Coming Up In the next lesson we will download and install Microchips Integrated Development Environment or IDE. This program will become our primary tool, and will be used heavily in the following lessons.
Page 9 of 9
John J. McDonough, WB8RCR