Documente Academic
Documente Profesional
Documente Cultură
1.1 The main idea embedded systems in todays world 1.1.1 What is an embedded system?
The basic idea of an embedded system is a simple one. If we take any engineering product that needs control, and if a computer is incorporated within that product to undertake the control, then we have an embedded system. An embedded system can be defined : system whose principal function is not computational, but which is controlled by a computer embedded within it. These days embedded systems are everywhere, appearing in the home, office, factory, car or hospital. Table 1.1 lists some example products that are likely to be embedded systems, all chosen for their familiarity. While many of these examples seem very different from each other, they all draw on the same principles as far as their characteristics as an embedded system are concerned. The vast majority of users will not recognise that what they are using is controlled by one or more embedded computers. Indeed, if they ever saw the controlling computer they would barely recognise it as such. Most people, after all, recognise computers by their screen, keyboard, disc drives and so on. This embedded computer would have none of those.
Lets take a look at some example embedded systems, first from everyday life and then from the projects used to illustrate this book.
A simple domestic refrigerator is shown in Figure 1.1. It needs to maintain a moderately stable, low temperature within it. It does this by sensing its internal temperature and comparing that with the temperature required. It lowers the temperature by switching on a compressor. The temperature measurement requires one or more sensors, and then whatever signal conditioning and data acquisition circuitry that is needed. Some sort of data processing is required to compare the signal representing the measured temperature to that representing the required temperature and deduce an output. Controlling the compressor requires some form of electronic interface, which accepts a low-level input control signal and then converts this to the electrical drive necessary to switch the compressor power.
A very different example of an embedded system is the car door, as shown in Figure 1.2. Once again there are some sensors, some human interaction and a set of actuators that must respond to the requirements of the system. One set of sensors relates to the door lock and another to the window. There are two actuators, the window motor and the lock actuator. It might appear that a car door could be designed as a self-contained embedded system, in a similar way to the fridge. Initially, one might even question whether it is worthy of any form of computer control
-2-
whatsoever, as the functions seem so simple. Once again, by creating it as an embedded system, we see the opportunity to enhance functionality. Now we have the door status and actuators under electronic control, they can be integrated with the rest of the car. Central locking can be introduced or an alarm sounded if the door is not locked when the driver tries to pull away. There is therefore considerable advantage in having a network which links the humble actions of the door control to other functions of the car. We will see in later chapters that networked interaction is an important feature of the embedded system.
1.4.1 Microprocessors
The first microprocessors appeared in the 1970s. These were amazing devices, which for the first time put a computer CPU onto a single IC. For the first time, significant processing power was available at rather low cost, in comparatively small space. At first, all other functions, like memory and input/output interfacing, were outside the microprocessor, and a working system still had to be made of a good number of ICs. Gradually, the microprocessor became more self-contained, with the possibility, for example, of including different memory types on the same chip as the CPU. At the same time, the CPU was becoming more powerful and faster, and moved rapidly from 8-bit to 16- and 32-bit devices. The development of the microprocessor led very directly to applications like the personal computer.
AA0ZZ PIC-EL v2.1K July 1, 2006 -3Copyright 2006, AA0ZZ
1.4.2 Microcontrollers
While people quickly recognised and exploited the computing power of the microprocessor, they also saw another use for them, and that was in control. Designers started putting microprocessors into all sorts of products that had nothing to do with computing, like the fridge or the car door that we have just seen. Here the need was not necessarily for high computational power, or huge quantities of memory, or very high speed. A special category of microprocessor emerged that was intended for control activities, not for crunching big numbers. After a while this type of microprocessor gained an identity of its own, and became called a microcontroller. The microcontroller took over the role of the embedded computer in embedded systems. So what distinguishes a microcontroller from a microprocessor? Like a microprocessor, a microcontroller needs to be able to compute, although not necessarily with big numbers. But it has other needs as well. Primarily, it must have excellent input/output capability, for example so that it can interface directly with the ins and outs of the fridge or the car door. Because many embedded systems are both size and cost conscious, it must be small, self-contained and low cost. Nor will it sit in the nice controlled environment that a conventional computer might expect. No, the microcontroller may need to put up with the harsh conditions of the industrial or motor car environment, and be able to operate in extremes of temperature. A generic view of a microcontroller is shown in Figure 1.8. Essentially, it contains a simple microprocessor core, along with all necessary data and program memory. To this it adds all the peripherals that allow it
-4-
to do the interfacing it needs to do. These may include digital and analog input and output, or counting and timing elements. Other more sophisticated functions are also available, which you will encounter later in the book. Like any electronic circuit the microcontroller needs to be powered, and needs a clock signal (which in some controllers is generated internally) to drive the internal logic circuits.
consumer electronics and are responsible for the intelligence in these smart electronic devices
There are thousands of different microcontroller types in the world today, made by numerous different manufacturers. All reflect in one way or another the block diagram of Figure 1.8. A manufacturer builds a microcontroller family around a fixed microprocessor core. Different family members are created by using the same core, combining with it different combinations of peripherals and different memory sizes. This is shown symbolically in Figure 1.9. This manufacturer has three microcontroller families, each with its own core. One core might be 8-bit with limited power, another 16-bit and another a sophisticated 32-bit machine. To each core is added different combinations of peripheral and memory size, to make a number of family members. Because the core is fixed for all members of one family, the instruction set is fixed and
-6-
users have little difficulty in moving from one family member to another.
While Figure 1.9 suggests only a few members of each family, in practice this is not the case; there can be more than 100 microcontrollers in any one family, each one with slightly different capabilities and some targeted at very specific applications.
-7-
-8-
depends on external memory, many of its pins are committed to data and address bus functions, which forces the large size. Second from right is the comparatively recent 40-pin PIC 16F877. While this looks similar to the 68000, it actually makes very different use of its pins. With its on-chip program and data memory it has no need for external data or address buses. Its high pin count is now put to good use, allowing a high number of digital input/output and other lines. In the middle is the 52-pin Motorola 68HC705. This is in a square ceramic package, windowed to allow the on-chip EPROM (Erasable Programmable Read-Only Memory) to be erased. The pin spacing here is 0.05 inches, so the overall IC size is considerably more compact than the 68000, even though the pin count is still high. To the left of this is a 28-pin PIC 16C72. Again, this has EPROM program memory and thus is also in a ceramic DIP package. On the far left is the tiny 8-pin surface-mounted PIC 12F508 and to the right of this is an 18-pin PIC 16F84A.
kit. Hundreds of these kits were distributed by the club in early 2005 and the reception was very positive. Since John called his on-line PIC course Elmer-160, I named this board the PIC-EL. The standard PIC for the PIC-EL is a 16F628, a very common, mid-range PIC, selected for this project because of its balance of architectural simplicity, power, and low cost. It is one of the most common PICs used in amateur applications these days. The Elmer-160 lessons start with an even lower-end PIC, the 16F84, because it is even easier to for beginners to understand. The 16F628 is nearly compatible with the 16F84 in that only a few lines of code need to be changed. The PIC-EL can use either of these PICs interchangeably. However, the diagnostic program, pre-loaded on a 16F628 that is supplied with the kit, is slightly too large for a 16F84. PIC-EL kit sales by the AmQRP club ended after a few months. Recently I updated the board to replace obsolete parts, correct a few deficiencies, and make the kits available on a long-term basis. The result is the PIC-EL Version 2.
1.1
- 10 -
2 Hardware Description
Appendix A has the schematic of the PIC-EL board. The PIC-EL board has two parts - a PIC programmer and a test / demonstration portion.
2.1
PIC programmer
The PIC serial port programmer design is an adaptation of the classic programmer by David Tait. See Taits PIC-related home page on the Internet at: http://people.man.ac.uk/~mbhstdj/piclinks.html The PIC programmer has an RS232 (DB9F) serial port connection. Pin assignments are as follows: Data-Out is on RS-232 pin 4 (DTR) Clock is on RS-232 pin 7 (RTS) Data-In is on RS-232 pin 8 (CTS) MCLR is on RS-232 pin 3 (TD)
The RS-232 lines representing Data-Out and Clock are inverted and converted to PIC signal levels via NPN (2N3904) transistor switches (Q1 and Q2) before going to the PIC microcontroller. Thus, for these two computer output signals passed via the serial ports DTR and RTS lines, low serial port signal levels (12v) get converted to high PIC signal levels (+5v) and high serial port signal levels (+12v) get converted to low PIC signal levels (0v). The MCLR signal is another PC output signal, and it is sent to the programmer via the serial ports TD line. This signal is generated by the PC programming software in order to put the PIC in high-voltage programming mode. In this case, the PIC needs the two levels to be zero and approximately +12v. Transistor Q3 operates in a manner that is very similar to Q1 and Q2, except its collector voltage is higher. As before, it inverts the serial port signal levels, but this time the low level (12v) results in a MCLR signal of approximately 12.5v (depending on the power supply voltage) and the serial port high level (+12v) results in a MCLR signal of approximately zero volts. One 2N2907 (PNP) transistor switch (Q4) is used in the programmer. It brings the Data-In signal from the PIC microcontroller to the serial port connector. Again, its high and low levels are inverted. Since the serial port expects to see voltages which are in the range of +4v to +12v (high) and 4v to 12v (low), Q4, two resistors (R7 and R8), a diode (D4), and a negative voltage (TD), are used to adjust the levels. When the PIC generates a low level (0v), the voltage at the base of Q4 will be about 5v, as supplied by the 5v regulator. The voltage at RS-232 pin 4 (TD) is 12v when in active programming mode. This level gets inverted by transistor Q2 to produce +12.5v as Vpgm to the HDR1 header and then to PIC pin 4 (MCLR). This meets the PIC requirement for high-voltage programming. High voltage programming requires the programmer to quickly raise the voltage on the MCLR pin from zero to at least 4.5v higher than the PICs run voltage (Vdd) of 5v.
- 11 -
No provision is made at this time for connecting to a USB port or a parallel port to the programmer. However, external USB or parallel port programmers can be attached to the PIC-EL board via HDR-1. The PIC-EL board cannot be run directly from a PC parallel port because of the difficulty is getting the Data In signal to work properly. The parallel ports voltages are 0v for a low level and +5v for a high level. Since the TD signal never goes negative, the circuitry that was used to pull the Q4 collector below zero for a low level does not work. In fact, the low level only goes down to approximately 3.2v when TD is asserted (0v). Additional circuitry could be added, but it is not included in this PIC-EL board. How about a USB-to-serial converter between the computer and the PIC-ELs serial interface? It would be nice, but so far we have not found one that works in this application. The reason is because RS232 ports are intended to perform asynchronous serial communications while we are directly controlling the serial port's control lines and doing synchronous communications to the PIC-EL. The PIC-EL requires a perfect 1-to-1 correspondence between signals generated by the computer programmer software and the signals presented to the PIC-EL serial interface. Since USB-to-serial interfaces do not allow this closely controlled synchronous activity, it cannot interface properly with the PIC programmer and does not work properly.
2.2
Project/Demonstration
The project / demonstration portion of the PIC-EL board was designed to allow the experimenter to understand how a PIC microcontroller can be used in a variety of applications. It allows the person to progress from controlling very basic components to more advanced components and projects. In RUN mode, PIC experimenters have an opportunity to use and understand the following hardware functions: 1. A 18-pin PIC microcontroller (16F84/A, 16F628/A, 16F88, etc) Includes 4 MHz crystal 2. A 2x16 LCD (two lines of 16 characters) 3. A rotary encoder (ENC-1) 4. Three general-purpose pushbuttons (PB1 through PB3) 5. A dedicated pushbutton (PB4) for master clear (reset) of the PIC microcontroller 6. Three LEDs (LED1 through LED3) 7. A speaker (SPKR-1) with transistor driver. 8. All connections necessary to drive the NJQRP DDS Daughtercard (DDS-30 or DDS-60) 9. A stereo jack for connection to CW paddles. 10. A stereo jack with transistor driver for transmitter keying 11. A transistor conditioner for converting low-level signals to levels required for PIC input detection. 12. A multi-purpose BNC connector Selectable via a jumper at header HDR-2 Allow DDS output to be routed to the BNC Allow DDS output to be routed to a conditioner and then to a PIC input pin
- 12 Copyright 2006, AA0ZZ
Allow an outside signal source to be brought in to the conditioner and then to the PIC input pin Allows attachment of a foreign programmer to this PIC project board Allows attachment of this programmer to a foreign project board
The PIC-EL Schematic (Appendix A) may look quite complicated because many of the PIC pins have multiple usages. However, we can break down the schematic into its core pieces to understand the individual functions. This will also show how to use these basic components in other projects.
2.2.2
LEDs
Two direct ways of lighting an LED from a PIC microcontroller are illustrated in Figure 2. The first is to connect a PIC output pin to a resistor and then to the anode of the LED with the cathode grounded. To light the PIC, the program needs to assert a logical high (+5v nominal) on the output PIC pin. The PIC sources the current to light the LED. The other way is to connect a PIC output pin to a resistor and then to the cathode of the LED with the anode connected to +5v. In this case, to illuminate an LED from the PIC, the PIC pin needs to be brought to a low level. The PIC is a current sink. One minor drawback of this method is that the PIC programmer must remember that the logic is reversed; i.e., the LED is illuminated when the PIC pin is set to a logical low, and it is dark when the PIC pin is logical high. The method used in the PIC-EL board is to sink current with a PIC rather than to source the current.
- 13 -
Figure 2 Lighting an LED Ideally, to illuminate an LED, the current flow through it should be between 1 ma and 20 ma. In this design the current flow is determined by the size of the series resistors. The series resistors (R16, R17, and R18) are each 2.2k ohms. These values were selected in order to keep the circuit loading to a minimum, since the PIC pins to which they are connected are used for multiple functions. Since the voltage drop across each LED is about 1.8v, the voltage drop across the 2.2k resistors is about 3.2v. This means the current through the resistors and these LEDs is about 1.4 ma. This amount of current illuminates the LEDs sufficiently. In some cases you may want to increase this current for brighter illumination. You could use a 1k resistor (3.2 ma), for example.
2.2.3
Pushbuttons
Figure 3 shows how these switches are used in a PIC and how PB3 is implemented in the PIC-EL.
Figure 3 General Usage and Master Clear Pushbuttons Three stand-alone normally-open SPST pushbuttons (PB1, PB2 and PB3) are connected to PIC pins (RA4, RA3, and RA2) in the PIC-EL. They can be used for any type of control functions that the programmer wants to use them for. One other normally-open SPST pushbutton (PB4) is connected to the PICs Master Clear pin and is used to reset the PIC program (make it start over). The three PIC pins
AA0ZZ PIC-EL v2.1K July 1, 2006 - 14 Copyright 2006, AA0ZZ
that have general-purpose pushbuttons (PB1, PB2 and PB3) also have 10k pull-up resistors (R22, R21 and R34 respectively) attached to Vdd (+5v). In general, using pull-up resistors is a good design principle and provides a good stiff pull-up. In some cases, no pull-up resistor is used because some PIC pins (Port B in a 16F628) can have internal weak pull-ups activated via PIC software instructions. (This is done by executing a PIC instruction which clears bit 7 of the PICs OPTION register.) In this mode, the PIC in effect puts a 50k ohm resistor between each of these pins and +5v. This means the PIC is able to source .1 ma of current on each of those pins. This is sufficient for a simple pushbutton operation. Note that the PICs Master Clear pin (pin 4) has a 10k ohm pull-up resistor (R30) to +5v and is switched via a normally-open SPST pushbutton (PB5) to near ground. This is also illustrated in Figure 5. The pull-up resistor is essential here, since the PIC needs +5v on MCLR for normal PIC operation. The 10k resistor is sufficient here, since the Master Clear pin draws very little current. Pushbutton PB4 also has a 100 ohm resistor to prevent voltages transients from locking up the PIC.
2.2.4
LCD
The LCD used in the PIC-EL demonstration board has two rows of 16 characters. It is a standard 5x10 dot matrix LCD that has a standard Hitachi 44780 controller. It is attached in such a way that it minimizes interaction with other functions of the PIC-EL. In particular, the PIC programmer (also using PIC pins 12 and 13 - RB6 and RB7) still works properly when the LCD is connected in this manner. The values of the voltage divider resistors (R14, R15) were selected to put the proper voltage on the LCDs contrast pin (pin 3). Also, the LCD backlight is activated with the resistor to +5v connected to LCD pin 15 along with the ground connection to LCD pin 16. The backlight of the 2x16 LCD used in the PIC-EL kit draws about 75 ma; if the LCD used in a different application draws a different amount of backlight current, the size of this resistor must be adjusted. Figure 4 shows how the LCD is implemented in the PIC-EL board.
Figure 4 LCD
- 15 -
2.2.5
Rotary Encoder
A mechanical rotary encoder is attached to two PIC pins, RA3 and RA4, as shown in Figure 5. R23 and R24 are typical pull-up resistors, since the rotary encoder is, essentially, just a pair of switches that open and close as the shaft rotates. Capacitors C3 and C4 are filters for removing noise which comes from contact bounce. The series resistors, R25 and R26, help in the signal filtering. Without the noise filtering, operation is very erratic.
Figure 5 Rotary Encoder For the mechanical encoder included in the PIC-EL kit, each of the signal lines produce 24 pulses per revolution, so a total of 96 up-or-down voltage transitions per revolution are generated and can be detected by the PIC microcontroller. Since the pulses of the two data lines overlap (gray code), the PIC program can use an algorithm to determine which direction the shaft is being turned. See Figure 6 for a simple explanation of how this works.
- 16 -
Determining the direction the encoder is being turned is done by a variety of software algorithms. Figure 7illustrates one way to determine the direction.
2.2.6
Speaker
A miniature speaker (SPKR-1) is attached to a PIC pin by way of a simple transistor (Q5) driver, as shown in Figure 8.
Figure 8- Speaker The transistor driver gives more punch to the speaker than could be attained by directly attaching it to the PIC pin to the speaker. The capacitor and diode in the path to the base of the driver transistor would be optional in most PIC speaker applications but are very important in the PIC-EL board because they prevent the speaker from being inadvertently left on if the PIC-EL application happens to leave that pin in a high state. Q5 acts as a switch, allowing current to flow through the speaker when Q5 is turned on and not flow when Q5 is turned off. Pulses are generated by the PIC software and pass through capacitor C11 to turn Q5 on and off. The PIC program produces different tones by changing the duration of the pulses it generates. Since audio tones are relatively low frequency and the PIC executes
AA0ZZ PIC-EL v2.1K July 1, 2006 - 17 Copyright 2006, AA0ZZ
an instruction every microsecond, accurate delay loops can be designed to produce pulses with the desired durations.
2.2.7 Daughtercard
The AmQRP DDS Daughtercard can be plugged into the PIC-EL board by way of a socket (J6). Appropriate PIC connections are made to the PIC and the required +12v is also supplied to the Daughtercard socket. Details of how the Daughtercard operates can be found on the AmQRP web page at: http://www.amqrp.org/kits/dds60/ . The PIC connections to the DDS daughtercard are illustrated in Figure 9
Figure 9- DDS Daughtercard The output of the Daughtercard is supplied back to the socket (J6) at pin 6. The PIC microcontroller can drive the DDS Daughtercard to produce an amplitude of approximately 600 mv with a frequency within the range of zero to 30 MHz or zero to 60 MHz, depending on which version of the DDS Daughtercard you have.
2.2.8
Signal Conditioner
A signal conditioner, shown in Figure 10 is provided to increase small amplitude signals to voltage levels which are detectable by the PIC. The output amplitude of the DDS Daughtercard is too low to be fed directly back into a PIC pin for the demonstration of frequency counting. To make this work, the amplitude is increased by the signal conditioner circuitry. Notice that this conditioner is not a linear amplifier in that it does not attempt to keep a distortion-free sine-wave output. For purposes of frequency measurement, a square wave would be just as good as a sine wave.
- 18 -
Figure 10 Signal Conditioner Note that a header HDR2 is used to select the source of the signal which goes into the conditioner. In one position, the output of the DDS Daughtercard is fed into the conditioner while in another configuration a signal from an external source can be brought into the PIC-EL board via the BNC connector (J7) and routed through the conditioner before going to the PIC.
Figure 11 CW Paddles The jack connects one of the paddle connections to the stereo plugs tip and the other to the ring. Both pins have pull-up resistors (R21 and R22) connected to +5v. The PIC is then able to detect the paddle closures just as if they were two SPST switches. A demonstration example of a CW keyer is available on my web site or the FILES section of the PIC-EL yahoo group.
2.2.10
Figure 12 shows how to key a transmitter with the output of the demonstration keyer. Another 1/8 stereo jack is provided for this purpose.
- 19 -
Figure 12 Transmitter Keying The output of a PIC pin goes to a transistor driver which then goes to the tip connection of the stereo jack. When keyed, the transistor driver drives the voltage at the tip connection from approximately 5v to ground potential. When the PIC pin is not keyed the tip-to-ground connection looks like an open circuit so the tip remains at approximately 5v. This keying mechanism will work for most modern rigs because they are positive-keyed transmitters. Some older style transmitters (tube style in particular) used negative keying. Positive keying means that the radio has approximately +3 to +5 volts on the tip connection and the radio is keyed when this pin is shorted to ground. Negative keying transmitters often have something on the order of 30v on the tip connection and are keyed when this connection is shorted to ground. This keying circuit is for positive keying only, but if your radio requires a negative keying scheme, it can be accomplished by adding a few extra components.
2.2.11
Frequency Counter
A frequency counter can be implemented in the PIC-EL by a using the signal conditioner which was mentioned in a previous section. The conditioner feeds its output into PIC pin 3 (RA4/T0CKI). This PIC pin may be configured to be a general purpose input/output pin but also has the unique characteristic of being configured as the as a counter input to a PIC register, TMR0. The TMR0 register is used by frequency counter applications.
2.2.12
Although the PIC-EL does not demonstrate this item, people often wonder how they can drive a relay or some other device which requires more current than a PIC pin can deliver (about 20 ma). Figure 14 shows how it can be done. The resistor (Rx) in series with the relay coil must be sized to pass the proper amount of current. The 2N7000 MOSFET is a good general-purpose part but in some applications, such as LCD backlight activation, an LRLML2502 is a good choice since its Drain-to-Source resistance when turned on is very low (about 0.045 ohms). This Rds(on) resistance is between 2 and 5 ohms in the 2N7000.
- 20 -
header (HDR1) in order to generate the minimum "programming voltage", called Vpgm. 1) Set up After the serial cable is connected, you've got FPP loaded and turned on, your PIC-EL board is powered by at least 12V, and you've manually wiggled the lines as described in Lesson 10, it should be a piece of cake to program a PIC on the PIC-EL board. 2) Obtain the .HEX program The HEX file is the new software you will be burning into the PIC. You can download the PIC-EL V2 Diagnostic program from my web page or the FILES section of the PIC-EL YAHOO group. The PICELv2diag.HEX file contains the Test Program in "HEX ASCII" format. This file format is the standard output produced by PIC assembly programs and is a specific data format which is expected by the FPP program. 3) Load the PICELv2diag.HEX file into FPP. Click on the LOAD button and navigate to wherever you unzipped the PIC-EL V2 Diagnostic files on your PC. You will see the PICELv2diag.HEX file listed there. Double-click on it and the HEX ASCII code will load into the FPP buffer. You will see that code in the FPP window. 4) Slide mode switch S1 DOWN to PGM MODE. You need to move the slide switch S1 to the DOWN position in order to put the PIC-EL board into the PGM Mode. LED4, next to the switch, will turn on when you do this. 5) Erase the PIC currently plugged into the PIC-EL board. You MAY want to "erase", or clear out the software program currently in the PIC's flash memory before you burn a new program into the PIC. Click the ERASE button to do this, and if successful you will see a simple message pop up saying "PIC is erased". 6) Burn the new code into the PIC. Now that the PIC memory is empty and you have the new program (HEX file) in the FPP buffer window, you are all set to burn the program into the PIC. Click PROGRAM on the FPP application window and confirm your desire again in the pop-up window. It will take a few moments for this short program to be burned into the PIC, but when complete FPP will display "Device programmed!". If it says "Programming failure", you obviously have a problem like the PIC was not first erased, power supply wasn't connected or sufficiently high, cable was not plugged in, etc. 7) Slide the mode switch UP to go into RUN MODE. Now that the programming is complete, you next need to put the PIC-EL board back into RUN Mode. Do this by sliding Mode Switch S1 UP, and the PGM LED will turn off once again.
- 22 -
8) RESET the board to start up the new program. Although not always necessary, just press the RESET pushbutton on the PIC-EL board to start up the new program just programmed into the PIC.
Table 2.1 gives summary details of some members of the 16 Series family, choosing the ones we meet in this book. Even with this limitation, there is considerable diversity. Within the 16 Series extended family, we find groupings of very closely related controllers, of which two are represented here, the 16F84A and the 16F87XA. The 16F84A is listed first, with features we are about to explore in detail. It has a very close
Table 2.1 Some members of the PIC 16 Series family
- 23 -
relative, the 16LF84A, whose extended supply voltage range allows operation at lower voltages. Either of
these controllers is available in different packages, different operating temperature ranges, and different clock speed ranges. For example, the 16F84A is available in 4 and 20MHz versions. The 16F87XA is a diverse grouping, as can be seen. There are two package sizes and two memory sizes. It is easy to see that package size is driven by the number of input/outputs that are available. The 40-pin versions have five parallel ports (which translates to 33 lines of parallel digital input/output), as
AA0ZZ PIC-EL v2.1K July 1, 2006 - 24 Copyright 2006, AA0ZZ
well as more analog input, compared with their 28-pin relatives. There is otherwise not much difference. Each package size, however, comes with two different memory sizes. The bigger memory of course gives the opportunity for longer programs and more data storage, but also costs a little more. As is normal Microchip practice, each member of the 16 Series family has its own comprehensive data sheet, available from Microchips website. Reference 2.1 is the data sheet for the 16F84A. As well as this, there is a manual covering all the features that are common to all members of the family [Ref. 2.2]. While it is not necessary to refer to these while reading this chapter, it is worth knowing they are there, and extremely useful for looking up the finer details of a microcontrollers design and use.
EDU-PIC
16F84 Module
User Manual
Index : 1. Introduction 2 2. General 2 3. Digital 3 ports Description
- 25 -
3.1 Leds and microswitches 3.2 Relay 3.3 Header 16x (keypad port 4 x 4) 3.4 LCD port 4. 5. Connecting the system. Step by step Technical specifications 5.1 General spec 8 5.2 Lay out 9 5.3 Electronics diagram 10 5.4 Contents of CD 11 3.1.2.1.1.1.1.1 Apendix: How to connect an LCD, and a 4 x 4 keypad 6
11
1. Introduction : The philosophy of design of the EDUPIC 16F84 module, was to put in one board many useful interfaces available to the user, and, at the same time diminish costs using only the most widely used components. The EDUPIC module integrates a FLASH memory programmer for the 16F84 but may function as well in AUTORUN mode for field applications. Making use of Microchipss MPLAB software tool, EDUPIC connects to a PC COM1 serial port via a DB9 cable. MPLAB integrates an editor, assembler, simulator and linker, but it may as well integrate more tools, like a programmer, an emulator and a compiler. WINPIC is another freeware tool which is used to program the 16F84 FLASH memory. Both software tools function on any Windows system, including 98, 2000, Millenium and XP. EDU-PIC is offered as a KIT, and includes all needed software and hardware items for its immediate functioning: a wall transformer, a serial DB9 cable and a CD. This manual includes all technical specification of the EDUPIC module, such as an electronic diagram, and a lay out diagram. To have your EDUPIC system immediately functioning, please go directly to Chapter 4: Connecting the system. In case of any question regarding this system, please call us at (52)(55) 56-53-58-01. We also have an online help in msn: puntoflotante@hotmail.com.
- 26 -
2. General Description The EDUPIC system was designed to provide the user with a programmer and also a field applications system. Typical applications include robotics, access control systems, home security and industrial automation. The LAY OUT diagram of the module, and the pin assignment in 16F84 are shown below.
L S PRO G L 4 3 AUTORUN B 1 Y V N L
L
1 4
N 1
S T
1 S L 5
C R
O E
N L A
A Y
C R E
16F84
L A
1 5 1 6 7 . . . . . . . . . . . . . . . . L E D H S E . . A . . D . . E . . R . . . 1. . 6. . X . . C O N N E C T O R K E Y P A D 4 X 4 X T A L 4 M A H Z 1 1 DB9 S e s 2 E R I A L P O R
S 3
E D
. .
G R
4 m i c r o s w i t c h U L A T O R A 1 E S E T L 2
LS L L L
1: 2 3 4
: : : :
R S 5 P
E L A Y A C T I V A T E D E R I A L P O R T C O N N E V O L T L E D . R O G R A M M I N G L E D
- 27 -
Block diagram of the 16F84A (supplementary labels in shaded boxes added by the author)
The module design is based on the 18 pin 16F84 microcontroller. It has two sections: the 16F84 programmer, and the microcontroller with interfaces such as leds, microswitches, and a relay connected to its ports. The programmer is controlled from the PC via the serial port, using the standard ICSP (in circuit serial programming). ICSP uses three pins of the 16F84: RB6, RB7 and MCLR. The jumpers S1, S2, and S3 connects these three pins either to the programmer section or the microcontroller section. The overal characteristics of the module are listed below: 16F84 microcontroller operating at 4 Mhz. 1K bytes FLASH, 64 bytes EEPROM, 68 bytes RAM y 1 timer/counter. CMOS technology with very low consumption: only 2 ma. at 5 volt. Harvard arquitecture, with an instruction set of only 35 instructions. 16F84 programmer integrated to module, via a serial cable with DB9 to connect a PC. 8 leds connected to port B, useful for testing or filed applications purposes. 4 microswitches connected to port A, useful for testing or field applications purposes Low power RELAY contacts, ready to activate external devices.
- 28 Copyright 2006, AA0ZZ
LCD port with an standard 14 pin connector. Keypad 4 x 4 port with an 8 pin connector 16x header to provide conection to external interfaces. Watch dog circuit ready to be programmed to provide an automatic reset if the 16F84 loses control. SLEEP mode for very low consumtion, <1 uA of the module. Timer/counter to provide for delays, real time clock and event counter. Interrupt system, generated from several sources, among them the timer, the EEPROM and the port B signals. CODE PROTECTION programmming mode, to avoid possible stealing of code.
M R
E A
MD M
A O
TR E
AY E
M E MC O P R O M
D R
YE F
M L
E A
M S
O H
I M
/ C
CONTENTS OF CD Included in CD is MPLAB IDE, a professional development software tool, and WINPIC, a 16F84 programmer software. Also included, are complete data sheets and documentation of module. EDUPIC connects to a PC COM1 port through a serial cable. Any Windows plataform may be used, including 98, 2000, Millenium and XP. 16F84 pin signal description is as follows: Pin 16 15 4 17 18 1 2 3 Name OSC1/CLKIN OSC2/CLKOUT MCLR RA0 RA1 RA2 RA3 RA4/T0CLK1 Type Input Output Input Bi Bi Bi Bi Bi Function Signal 1 of Xtal oscilator Seal 2 del Xtal oscilator Master reset. In programming mode, 12 to 13 volt signal. Input/Output port bit Input/Output port bit Input/Output port bit Input/Output port bit Input/Output port bit. May be also configured to be an input clock signal for TMR0.
- 29 Copyright 2006, AA0ZZ
6 7 8 9 10 11 12 13 5 14
RB0/INT RB1 RB2 RB3 RB4 RB5 RB6 RB7 Vss Vdd 3. Digital ports:
Bi Bi Bi Bi Bi Bi Bi Bi 5 V. GND
Input/Output port bit. May be also configured to be an external interrupt. Input/Output port bit Input/Output port bit Input/Output port bit Input/Output port bit Input/Output port bit Input/Output port bit. In programming mode, clock input to sincronize serial data. Input/Output port bit. In programming mode, serial data. Voltage source. Ground
The 16F84 chip has two digital ports: port A with 5 available pins and port B with 8 available pins. Both are bidirectional signals and may be configured as inputs or outputs, as needed, through registers TRISA and TRISB. In the EDUPIC module, 4 microswitches has been connected to port A and 8 leds to port B. Please note that all these signal are also available in the HEADER 16x of module, so that the user may connect external devices. Finally, a relay has been connected to bit RA0 in port A. Many of the pins are used for multiple functions, depending on the interface used. However, an LCD and a 4 x 4 keypad may be simultaneously connected without interferance with each other.
PORT PORT A RA0 RA1 RA2 RA3 RA4 PORT B RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7
IN
HEADER FUNCTION ACTIVATE/DEACTIVATE RELAY MICROSWITCH A1, LCD CONTROL MICROSWITCH A2, LCD CONTROL MICROSWITCH A3 MICROSWITCH A4 LED B0, DATA0 LCD, KEYPAD Y1 LED B1, DATA1 LCD, KEYPAD Y2 LED B2, DATA2 LCD, KEYPAD Y3 LED B3, DATA3 LCD, KEYPAD Y4 LED B4, DATA4 LCD, KEYPAD X1 LED B5, DATA5 LCD, KEYPAD X2 LED B6, DATA6 LCD, KEYPAD X3 LED B7, DATA7 LCD, KEYPAD X4
To program direction of bits, the user may use the following lines as an example.
AA0ZZ PIC-EL v2.1K July 1, 2006 - 30 Copyright 2006, AA0ZZ
PORTA
;CHOSE REGISTER BANK 1 ;RA0=OUTPUT, RA1..RA4=INPUTS ;PROGRAM DIRECTION OF BITS ;CHOSE REGISTER BANK 1 ;RB0..RB7=OUTPUTS
PORTB
13.1LEDS AND MICROSWITCHES: Electronic diagrams of leds and microswitches are shown below. All port pins in A and B ports are also available in header 16x for use in external interfaces.
5 R B O R B 1 k 7 v .
4 7 k R R R R S W 4 S W A A A A 1 1 2 3 4
13.2
RELAY
A relay of 12 volts, with contacts of 220 VCA @ 0.2 amperes may be activated from bit RA0 as shown. If RA0 = 1, the relay will close its contacts and LED L4 will light on.
5 L L R A 0 R B C R 5 4 7 E A L S A 1 2 1 0 Y E 4 V D . SCREW CO NNECTO R
5 . 6 K
R R R R R R R R
B 0 B 1 B 2 B 3 B 4 B 5 B 6 B 7
2 4 6 8 1 1 1 1
0 2 4 6
1 3 5 7 9 1 1 1 3 1 5
R R R R R 5 G
A A A A A v N
0 1 2 3 4 . D
3.4. LCD PORT There is an standard connector fot an LCD in the EDUPIC module. This connector has 14 pins as shown in the table below. It is possible to connect an LCD to this port and, in parallel, also connect a keypad to the header 16x, using lines RB0RB7, thanks to the fact that both devices can operate at different time slots.
I C
2 0 0 6 ,
Y 5 V O LTS
D C L O C N D N 1 4 E C T O R
A 1
R A 2
R B7
R B6
R B5
R B4
R B2
R B0
R B3
R B1
R A 1
100K
LCD 1 2 3 INT
EDUPI C
GND 1 K
R /W
IN T
R S
EN
4. Connecting the system. Step by step. STEP 1: COMPONENT CHECK AND INICIAL TEST OF MODULE Please check that the kit includes: EDUPIC module, wall transformer, serial cable and CD. You will need a PC with Windows 98, Proffesional, Millenium or XP operating system. In the wall transformer, move the 220 VDC selector to itt proper position and the output voltage selector to 7.5 volts. Connect the wall transformer to EDUPIC. Jumpers S1, S2 and S3 must be in the AUTORUN position. Once the module is with its power on, a program loaded in the FLASH memory should start running, flashing the leds. STEP 2: INSTALL PROGRAMS MPLAB IDE AND WINPIC FROM CD TO YOUR PC: Create a folder with the name EDUPIC in the desktop of your PC. Insert CD in drive. Open the contents and copy all folders and files to the EDUPIC folder.
Execute file MPLAB660.zip and then the instalation program. Please take note at the following windows that will appear during instalation:
window:
SOFTWARE LICENSE I ACCEPT SELECT DESTINATION SHORTCUTS? ADD TO DESKTOP? YES YES NO
Now, the program looks for hardware devices such as programmers or emulators in the USB ports. As this is not the case, simply ignore every warning refering to these USB drivers. Then the following text will appear:
FINISH OK
Once your PC is restarted, open the WINPIC folder and execute winpicpr.zip. Do the extract to the subfolder WINPIC. Make a direct access of WINPIC.exe in your desktop.
Before editing or assembling any program in MPLAB, you must disable the write protection in the folder files16F84. This protection is enabled because the files were copied directly from an only readable CD.
Open folder files16F84. Select edit and select all. Once all files are shadowed, right click your mouse in any file and select properties. Disable de only read protection in the small square. Then give a click in apply and close. If you skip this step, MPLAB would be unable to edit or assemble any file and will display an arror message.
- 34 -
M PRO G
M AUTORUN
I N
GL B
CL
DE
D P
R E
T D S E S . . . . . . . . . . . B R I A L P O 0 R T
1 S
7 . . . . . . . . . . L 16F84
A L
N 3
. L L E O N D 2 S E R I A L P O R T
Please take a look at the picture above and identify the components, the 16F84 microcontroller, the wall transformer connector, the serial port DB9 connector and the jumpers S1, S2 and S3. S1, S2 and S3 must be now in the PROGRAM position. Connect the wall transformer. The power LED L3 should light on. The DB9 connector should fit on COM1 or COM2 in the PC side and in the serial connector in EDUPIC on the other side.
- 35 -
Execute WINPIC from your desktop. Select option device,config and then part=16F84A. Then select oscillator= XT,XTAL, code protection=off, power up timer=disabled, and watch dog enable=disabled. In option interface, interface type=JDM programmer, on port=COM1.
Working now on the option code, chose file, and load and program device and then select path C:\EDUPIC\files16F84. Select file contador.hex. First, the file is transferred to WINPIC buffer, then automatically, data is transferred to the FLASH memory. Once the memory is succesfully programmed, WINPIC will display: program finished, no errors. In case that an error message is displayed, please revise your connections, the position of jumpers S1, S2 and S3, and the configuration of WINPIC.
Select option device and reset/disconnect ICSP/go. This command will execute the program. You will immediately see a binary count flashing in the LEDS of EDUPIC. You may as well push F9 to execute the program. Following the same procedure, load the file demof84.hex. This program will display a rotating pattern in LEDS. Take note that when you load a new program, the FLASH memory is automatically erased before being programmed.
The last program loaded in the FLASH memory of the 16F84 may be now executed in AUTORUN mode. Disconnect the serial cable from the computer and move jumpers S1, S2, and S3 to the Autorun position. If necessary, push the reset button. The program will run automatically.
- 36 -
Execute MPLAB IDE from your desktop. Chose configure, select device and 16F84. Now select file, open and select the path C.\EDUPIC\files16f84. select file demof84.asm. Listing of this file is shown below.
; Enciende leds del puerto B con un patrn de rotacin ; Oscilador Cristal de 4 Mhz. Cada LED enciende durante 0.5 seg.
; Configuracin del CPU ; 16F84, Oscilador cristal 4 Mhz, ; watchdog timer off, power-up timer on ; incluye archivo p16f84.inc
processor 16f84 include <p16f84.inc> __config _XT_OSC & _WDT_OFF & _PWRTE_ON J equ H'1F' ; localidad 1F es J
- 37 Copyright 2006, AA0ZZ
K equ
H'1E'
; localidad 1E es K
; Programa
org
movlw B'00000000' ; w := 00000000 tris PORTB ; puerto B=0 (bits son salidas)
movlw B'00000001' ; movwf PORTB bcf mloop: STATUS,C rlf ; port B =1 ;carry=0 ;rotate left PORT B (incluye carry)
PORTB,f
movlw D'200' movwf J jloop: kloop: goto movwf K decfsz K,f kloop
- 38 -
end
This program will flash LEDS in EDUPIC with a rotating pattern. Before performing simulation, modify the value shown from 200 to 4. This is necessary to escalate the time base, otherwise the simulation would last too long because the speed of the simulator is much lower that the real time speed of the program.
Select project, quickbuild demof84.asm. Once the program is assembled, select debugger, select tool, MPLAB SIM. Now push F6 to position a green arrow in the starting line of the program and push F7 to run step by step each instruction. To look at the file registers, select view, file registers and a window will display the value of each file register in the system. Go on with the simulation, looking at the change in the value of the file registers.
To finish simulation, modify again the value 4 to 200 and assemble the program again. Once the program is assembled without errors, you may use WINPIC to transfer it to EDUPIC, as explained in former paragraphs.
- 39 -
Block diagram of the 16F84A (supplementary labels in shaded boxes added by the author)
3.4. LCD PORT There is an standard connector fot an LCD in the EDUPIC module. This connector has 14 pins as shown in the table below. It is possible to connect an LCD to this port and, in parallel, also connect a keypad to the header 16x, using lines RB0RB7, thanks to the fact that both devices can operate at different time slots.
- 40 -
I C
2 0 0 6 ,
Y 5 V O LTS
D C L O C N D N 1 4 E C T O R
A 1
R A 2
R B7
R B6
R B5
R B3
R B2
R B1
R B0
R B4
R A 1
100K
LCD
EDUPI C
FUNCION Ground 5 volts. Intensity control 0=command 1=data 0=write in LCD 1=read Enable pulse DATA
GND 1 K
R /W
IN T
R S
EN
3.4. LCD PORT There is an standard connector fot an LCD in the EDUPIC module. This connector has 14 pins as shown in the table below. It is possible to connect an LCD to this port and, in parallel, also connect a keypad to the header 16x, using lines RB0RB7, thanks to the fact that both devices can operate at different time slots.
- 41 -
I C
2 0 0 6 ,
Y 5 V O LTS
D C L O C N D N 1 4 E C T O R
A 1
R A 2
R B7
R B6
R B5
R B3
R B2
R B1
R B0
R B4
R A 1
100K
LCD
EDUPI C
FUNCION Ground 5 volts. Intensity control 0=command 1=data 0=write in LCD 1=read Enable pulse DATA
GND 1 K
R /W
IN T
R S
EN
4. Connecting the system. Step by step. STEP 1: COMPONENT CHECK AND INICIAL TEST OF MODULE Please check that the kit includes: EDUPIC module, wall transformer, serial cable and CD. You will need a PC with Windows 98, Proffesional, Millenium or XP operating system. In the wall transformer, move the 220 VDC selector to itt proper position and the output voltage selector to 7.5 volts. Connect the wall transformer to EDUPIC. Jumpers S1, S2 and S3 must be in the AUTORUN position. Once the module is with its power on, a program loaded in the FLASH memory should start running, flashing the leds. STEP 2: INSTALL PROGRAMS MPLAB IDE AND WINPIC FROM CD TO YOUR PC: Create a folder with the name EDUPIC in the desktop of your PC. Insert CD in drive. Open the contents and copy all folders and files to the EDUPIC folder.
AA0ZZ PIC-EL v2.1K July 1, 2006 - 42 Copyright 2006, AA0ZZ
Execute file MPLAB660.zip and then the instalation program. Please take note at the following windows that will appear during instalation:
window:
SOFTWARE LICENSE I ACCEPT SELECT DESTINATION SHORTCUTS? ADD TO DESKTOP? YES YES NO
Now, the program looks for hardware devices such as programmers or emulators in the USB ports. As this is not the case, simply ignore every warning refering to these USB drivers. Then the following text will appear:
FINISH OK
Once your PC is restarted, open the WINPIC folder and execute winpicpr.zip. Do the extract to the subfolder WINPIC. Make a direct access of WINPIC.exe in your desktop.
- 43 -
Before editing or assembling any program in MPLAB, you must disable the write protection in the folder files16F84. This protection is enabled because the files were copied directly from an only readable CD.
Open folder files16F84. Select edit and select all. Once all files are shadowed, right click your mouse in any file and select properties. Disable de only read protection in the small square. Then give a click in apply and close. If you skip this step, MPLAB would be unable to edit or assemble any file and will display an arror message.
M PRO G
M AUTORUN
I N
GL B
CL
DE
D P
R E
T D S E S . . . . . . . . . . . B R I A L P O 0 R T
1 S
7 . . . . . . . . . . L 16F84
A L
N 3
. L L E O N D 2 S E R I A L P O R T
Please take a look at the picture above and identify the components, the 16F84 microcontroller, the wall transformer connector, the serial port DB9 connector and the jumpers S1, S2 and S3. S1, S2 and S3 must be now in the PROGRAM position. Connect the wall transformer. The power LED L3 should light on. The DB9 connector should fit on COM1 or COM2 in the PC side and in the serial connector in EDUPIC on the other side.
Execute WINPIC from your desktop. Select option device,config and then part=16F84A. Then select oscillator= XT,XTAL, code protection=off, power up timer=disabled, and watch dog enable=disabled. In option interface, interface type=JDM programmer, on port=COM1.
Working now on the option code, chose file, and load and program device and then select path C:\EDUPIC\files16F84. Select file contador.hex. First, the file is transferred to WINPIC buffer, then automatically, data is transferred to the FLASH memory. Once the memory is succesfully programmed, WINPIC will display: program finished, no errors. In case that an error message is displayed, please revise your connections, the position of jumpers S1, S2 and S3, and the configuration of WINPIC.
Select option device and reset/disconnect ICSP/go. This command will execute the program. You will immediately see a binary count flashing in the LEDS of EDUPIC. You may as well push F9 to execute the program. Following the same procedure, load the file demof84.hex. This program will display
AA0ZZ PIC-EL v2.1K July 1, 2006 - 45 Copyright 2006, AA0ZZ
a rotating pattern in LEDS. Take note that when you load a new program, the FLASH memory is automatically erased before being programmed.
The last program loaded in the FLASH memory of the 16F84 may be now executed in AUTORUN mode. Disconnect the serial cable from the computer and move jumpers S1, S2, and S3 to the Autorun position. If necessary, push the reset button. The program will run automatically.
Execute MPLAB IDE from your desktop. Chose configure, select device and 16F84. Now select file, open and select the path C.\EDUPIC\files16f84. select file demof84.asm. Listing of this file is shown below.
; Enciende leds del puerto B con un patrn de rotacin ; Oscilador Cristal de 4 Mhz. Cada LED enciende durante 0.5 seg.
- 46 -
; Configuracin del CPU ; 16F84, Oscilador cristal 4 Mhz, ; watchdog timer off, power-up timer on ; incluye archivo p16f84.inc
processor 16f84 include <p16f84.inc> __config _XT_OSC & _WDT_OFF & _PWRTE_ON J equ H'1F' H'1E' ; localidad 1F es J ; localidad 1E es K
K equ
; Programa
org
movlw B'00000000' ; w := 00000000 tris PORTB ; puerto B=0 (bits son salidas)
mloop:
rlf
PORTB,f
movlw D'200' movwf J jloop: kloop: goto movwf K decfsz K,f kloop
end
This program will flash LEDS in EDUPIC with a rotating pattern. Before performing simulation, modify the value shown from 200 to 4. This is necessary to escalate the time base, otherwise the simulation would last too long because the speed of the simulator is much lower that the real time speed of the program.
Select project, quickbuild demof84.asm. Once the program is assembled, select debugger, select tool, MPLAB SIM. Now push F6 to position a green arrow in the starting line of the program and push F7 to run step by step each instruction. To look at the file registers, select view, file registers and a
- 48 -
window will display the value of each file register in the system. Go on with the simulation, looking at the change in the value of the file registers.
To finish simulation, modify again the value 4 to 200 and assemble the program again. Once the program is assembled without errors, you may use WINPIC to transfer it to EDUPIC, as explained in former paragraphs.
5. Technical specifications
3.2
4
Name Architectu Program re Memory KBytes Data EEPROM RAM I/O ADC Channels ADC Bit ADC Sample Comparat Timers/W ors DT
2-8bit
Interface
2 EUSART 2 MIC Compatibl e/SPI 2 EUSART 2 MIC Compatibl e/SPI 2 EUSART 2 MIC Compatibl e/SPI EUSART MIC Compatibl e/SPI CAN 2.0B 2 EUSART 2 MIC Compatibl e/SPI
Max. Speed
Int Oscillator
PIC18F85J15
8-bit
Standard Flash
48
2048
66
15
10-Bit
100
40
31 kHz
PIC18F8622
8-bit
Enhanced Flash
64
1024
3936
70
16
10-Bit
100
40
PIC18F8627
8-bit
Enhanced Flash
96
1024
3936
70
16
10-Bit
100
40
PIC18F8680
8-bit
Enhanced Flash
64
1024
3328
69
16
10-Bit
30
40
PIC18F86J10
8-bit
Standard Flash
64
2048
66
15
10-Bit
100
3-16bit Yes
40
31 kHz
There are many different areas of application that a microcontroller can be used for. In general they are used in the Area of Appliances, Automotive, Human Interface controllers, Alarm and Video Systems and Communications. Some more specific examples of the applications that a microcontroller is required for:
AA0ZZ PIC-EL v2.1K July 1, 2006 - 49 Copyright 2006, AA0ZZ
Air Conditioners Coffee Makers Car Security Systems E.g. Remote Keyless Entry Car Information system E.g. Trip Computer Car Network Systems E.g. CAN Human Interface device controller E.g. LCD, mouse, Wireless Keyboard Security E.g. Video Systems Communications E.g. Switches, Routers, Hubs Although it would be possible to use either a 8051 chip or a PIC chip, to do any of these jobs the exact chip most suited to the required job would be used. There is no point using a chip that has more functionality than is required for the job. This is because it would not be an economic investment to use the best microcontroller with every sort of interface just to control the functions of a coffee maker. In the above example of a Trip Computer of a car, care must be taken to choose a micro controller that will be able to withstand the physical environment in which it will be situated. Depending on the placement of the microcontroller within the car will greatly affect the requirements of the chip. For example if the chip is to be situated within the engine compartment it must be capable of functioning correctly at temperatures in excess of 100oC. However if the chip is to be situated within the passenger compartment the temperature will not be as high. In both situations however it would be required that the chip be able to function in temperatures below 0oC, as the internal temperature of a car can be below the external temperature on start up. An example of a PIC microcontroller that is used within an Air conditioner to monitor the airflow is the PIC16C55A. This particular Microcontroller can operate without degradation in a temperature range of -55 to +125oC. This means that the chip can be put in the heart of the air conditioning unit without having to worry about temperature. An equivalent 8051 chip that could be used in air conditioning units is the 87C752, which only has a temperature range of -45 to +85oC. This means this chip cannot be used in temperatures that the PIC16C55A would be able to operate in. In this chapter you will therefore learn about: The PIC 16 Series family, in overview The overall architecture of the 16F84A The 16F84A memory system, along with a review of memory technologies Other hardware features of the 16F84A, including the reset system. If you wish, you will also learn about: Alternative approaches to microcontroller structure, through an example from another microcontroller family. 2.1 The main idea the PIC 16 Series family 2.1.1 A family overview The PIC 16 Series family is growing rapidly, with a huge and almost bewildering diversity of members. Therefore, when we talk of family here, we are applying the concept of extended family, and a very large one at that. Nevertheless, the 16 Series stays true to the concept that all family members have identical core and instruction set, with the difference arising from different peripherals and other features being
AA0ZZ PIC-EL v2.1K July 1, 2006 - 50 Copyright 2006, AA0ZZ
implemented, and different package sizes. Hence the pattern of Figure 1.9 is followed. Table 2.1 gives summary details of some members of the 16 Series family, choosing the ones we meet in this book. Even with this limitation, there is considerable diversity. Within the 16 Series extended family, we find groupings of very closely related controllers, of which two are represented here, the 16F84A and the 16F87XA. The 16F84A is listed first, with features we are about to explore in detail. It has a very close
Table 2.1 Some members of the PIC 16 Series family*For DIP package only.
AA0ZZ PIC-EL v2.1K July 1, 2006 - 51 Copyright 2006, AA0ZZ
ADC, analog-to-digital converter; PWM, pulse width modulation. relative, the 16LF84A, whose extended supply voltage range allows operation at lower voltages. Either of these controllers is available in different packages, different operating temperature ranges, and different clock speed ranges. For example, the 16F84A is available in 4 and 20MHz versions. The 16F87XA is a diverse grouping, as can be seen. There are two package sizes and two memory sizes. It is easy to see that package size is driven by the number of input/outputs that are available. The 40-pin versions have five parallel ports (which translates to 33 lines of parallel digital input/output), as well as more analog input, compared with their 28-pin relatives. There is otherwise not much difference. Each package size, however, comes with two different memory sizes. The bigger memory of course gives the opportunity for longer programs and more data storage, but also costs a little more. As is normal Microchip practice, each member of the 16 Series family has its own comprehensive data sheet, available from Microchips website. Reference 2.1 is the data sheet for the 16F84A. As well as this, there is a manual covering all the features that are common to all members of the family [Ref. 2.2]. While it is not necessary to refer to these while reading this chapter, it is worth knowing they are there, and
extremely useful for looking up the finer details of a microcontrollers design and use. 3.4. LCD PORT There is an standard connector fot an LCD in the EDUPIC module. This connector has 14 pins as shown in the table below. It is possible to connect an LCD to this port and, in parallel, also connect a keypad
AA0ZZ PIC-EL v2.1K July 1, 2006 - 52 Copyright 2006, AA0ZZ
to the header 16x, using lines RB0RB7, thanks to the fact that both devices can operate at different time slots.
I C
2 0 0 6 ,
Y 5 V O LTS
D C L O C N D N 1 4 E C T O R
A 1
R A 2
R B7
R B6
R B5
R B3
R B2
R B1
R B0
R B4
R A 1
100K
LCD
EDUPI C
FUNCION Ground 5 volts. Intensity control 0=command 1=data 0=write in LCD 1=read Enable pulse DATA
GND 1 K
R /W
IN T
R S
EN
3.4. LCD PORT There is an standard connector fot an LCD in the EDUPIC module. This connector has 14 pins as shown in the table below. It is possible to connect an LCD to this port and, in parallel, also connect a keypad to the header 16x, using lines RB0RB7, thanks to the fact that both devices can operate at different time slots.
- 53 -
I C
2 0 0 6 ,
Y 5 V O LTS
D C L O C N D N 1 4 E C T O R
A 1
R A 2
R B7
R B6
R B5
R B3
R B2
R B1
R B0
R B4
R A 1
100K
LCD
EDUPI C
FUNCION Ground 5 volts. Intensity control 0=command 1=data 0=write in LCD 1=read Enable pulse DATA
GND 1 K
R /W
IN T
R S
EN
4. Connecting the system. Step by step. STEP 1: COMPONENT CHECK AND INICIAL TEST OF MODULE Please check that the kit includes: EDUPIC module, wall transformer, serial cable and CD. You will need a PC with Windows 98, Proffesional, Millenium or XP operating system. In the wall transformer, move the 220 VDC selector to itt proper position and the output voltage selector to 7.5 volts. Connect the wall transformer to EDUPIC. Jumpers S1, S2 and S3 must be in the AUTORUN position. Once the module is with its power on, a program loaded in the FLASH memory should start running, flashing the leds. STEP 2: INSTALL PROGRAMS MPLAB IDE AND WINPIC FROM CD TO YOUR PC: Create a folder with the name EDUPIC in the desktop of your PC. Insert CD in drive. Open the contents and copy all folders and files to the EDUPIC folder.
AA0ZZ PIC-EL v2.1K July 1, 2006 - 54 Copyright 2006, AA0ZZ
Execute file MPLAB660.zip and then the instalation program. Please take note at the following windows that will appear during instalation:
window:
SOFTWARE LICENSE I ACCEPT SELECT DESTINATION SHORTCUTS? ADD TO DESKTOP? YES YES NO
Now, the program looks for hardware devices such as programmers or emulators in the USB ports. As this is not the case, simply ignore every warning refering to these USB drivers. Then the following text will appear:
FINISH OK
Once your PC is restarted, open the WINPIC folder and execute winpicpr.zip. Do the extract to the subfolder WINPIC. Make a direct access of WINPIC.exe in your desktop.
- 55 -
Before editing or assembling any program in MPLAB, you must disable the write protection in the folder files16F84. This protection is enabled because the files were copied directly from an only readable CD.
Open folder files16F84. Select edit and select all. Once all files are shadowed, right click your mouse in any file and select properties. Disable de only read protection in the small square. Then give a click in apply and close. If you skip this step, MPLAB would be unable to edit or assemble any file and will display an arror message.
M PRO G
M AUTORUN
I N
GL B
CL
DE
D P
R E
T D S E S . . . . . . . . . . . B R I A L P O 0 R T
1 S
7 . . . . . . . . . . L 16F84
A L
N 3
. L L E O N D 2 S E R I A L P O R T
Please take a look at the picture above and identify the components, the 16F84 microcontroller, the wall transformer connector, the serial port DB9 connector and the jumpers S1, S2 and S3. S1, S2 and S3 must be now in the PROGRAM position. Connect the wall transformer. The power LED L3 should light on. The DB9 connector should fit on COM1 or COM2 in the PC side and in the serial connector in EDUPIC on the other side.
Execute WINPIC from your desktop. Select option device,config and then part=16F84A. Then select oscillator= XT,XTAL, code protection=off, power up timer=disabled, and watch dog enable=disabled. In option interface, interface type=JDM programmer, on port=COM1.
Working now on the option code, chose file, and load and program device and then select path C:\EDUPIC\files16F84. Select file contador.hex. First, the file is transferred to WINPIC buffer, then automatically, data is transferred to the FLASH memory. Once the memory is succesfully programmed, WINPIC will display: program finished, no errors. In case that an error message is displayed, please revise your connections, the position of jumpers S1, S2 and S3, and the configuration of WINPIC.
Select option device and reset/disconnect ICSP/go. This command will execute the program. You will immediately see a binary count flashing in the LEDS of EDUPIC. You may as well push F9 to execute the program. Following the same procedure, load the file demof84.hex. This program will display
AA0ZZ PIC-EL v2.1K July 1, 2006 - 57 Copyright 2006, AA0ZZ
a rotating pattern in LEDS. Take note that when you load a new program, the FLASH memory is automatically erased before being programmed.
The last program loaded in the FLASH memory of the 16F84 may be now executed in AUTORUN mode. Disconnect the serial cable from the computer and move jumpers S1, S2, and S3 to the Autorun position. If necessary, push the reset button. The program will run automatically.
Execute MPLAB IDE from your desktop. Chose configure, select device and 16F84. Now select file, open and select the path C.\EDUPIC\files16f84. select file demof84.asm. Listing of this file is shown below.
; Enciende leds del puerto B con un patrn de rotacin ; Oscilador Cristal de 4 Mhz. Cada LED enciende durante 0.5 seg.
- 58 -
; Configuracin del CPU ; 16F84, Oscilador cristal 4 Mhz, ; watchdog timer off, power-up timer on ; incluye archivo p16f84.inc
processor 16f84 include <p16f84.inc> __config _XT_OSC & _WDT_OFF & _PWRTE_ON J equ H'1F' H'1E' ; localidad 1F es J ; localidad 1E es K
K equ
; Programa
org
movlw B'00000000' ; w := 00000000 tris PORTB ; puerto B=0 (bits son salidas)
mloop:
rlf
PORTB,f
movlw D'200' movwf J jloop: kloop: goto movwf K decfsz K,f kloop
end
This program will flash LEDS in EDUPIC with a rotating pattern. Before performing simulation, modify the value shown from 200 to 4. This is necessary to escalate the time base, otherwise the simulation would last too long because the speed of the simulator is much lower that the real time speed of the program.
Select project, quickbuild demof84.asm. Once the program is assembled, select debugger, select tool, MPLAB SIM. Now push F6 to position a green arrow in the starting line of the program and push F7 to run step by step each instruction. To look at the file registers, select view, file registers and a
- 60 -
window will display the value of each file register in the system. Go on with the simulation, looking at the change in the value of the file registers.
To finish simulation, modify again the value 4 to 200 and assemble the program again. Once the program is assembled without errors, you may use WINPIC to transfer it to EDUPIC, as explained in former paragraphs.
- 61 -