You are on page 1of 61

Getting Started with Embedded Systems

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.

1.2 Some example embedded systems


AA0ZZ PIC-EL v2.1K July 1, 2006 -1Copyright 2006, AA0ZZ

Lets take a look at some example embedded systems, first from everyday life and then from the projects used to illustrate this book.

1.2.1 The domestic refrigerator

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.

1.2.2 A car door mechanism

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

AA0ZZ PIC-EL v2.1K July 1, 2006

-2-

Copyright 2006, AA0ZZ

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

1.4 Microprocessors and microcontrollers

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

AA0ZZ PIC-EL v2.1K July 1, 2006

-4-

Copyright 2006, AA0ZZ

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.

Why Use a Microcontroller?


Being inexpensive singlechip computers, microcontrollers are easy to embed into larger electronic circuit designs. Their ability to store and run unique programs makes them extremely versatile. For instance, one can program a microcontroller to make decisions and perform functions based on situations (I/O line logic) and events. The math and logic functions allow the microcontroller to mimic sophisticated logic and electronic circuits. Programs can also make the microcontroller behave as a neural network and/or a fuzzy logic controller . Microcontrollers are incorporated in
AA0ZZ PIC-EL v2.1K July 1, 2006 -5Copyright 2006, AA0ZZ

consumer electronics and are responsible for the intelligence in these smart electronic devices

1.4.3 Microcontroller families

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

AA0ZZ PIC-EL v2.1K July 1, 2006

-6-

Copyright 2006, AA0ZZ

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

-7-

Copyright 2006, AA0ZZ

1.4.4 Microcontroller packaging and appearance


Integrated circuits are made in a number of different forms, usually using plastic or ceramic as the packaging material. Interconnection with the outside world is provided by the pins on the package. Where possible microcontrollers should be made as physically small as possible, so it is worth asking: what determines the size? Interestingly, it is not usually the size of the integrated circuit chip itself, in a conventional microcontroller, which determines the overall size. Instead, this is set by the number of interconnection pins provided on the IC and their spacing. It is worth, therefore, pausing to consider what these pins carry in a microcontroller. The point has been made that a microcontroller is usually input/output intensive. It is reasonable then to assume that a good number of pins will be used for input/output. Power must also be supplied and an earth connection made. It is reasonable to assume for the sort of systems we will be looking at that the microcontroller has all the memory it needs on-chip. Therefore, it will not require the huge number of pins that earlier microprocessors needed, simply for connecting external data and address buses. It will, however, be necessary to provide pin interconnection to transfer program information into the memory and possibly provide extra power for the programming process. There is then usually a need to connect a clock signal, a reset and possibly some interrupt inputs. Figure 1.10, which shows a selection of microprocessors and microcontrollers, demonstrates the stunning diversity of package and size that is available. On the far right, the massive (and far from recent) 64-pin Motorola 68000 dwarfs almost everything else. The package is a dual-in-line package (DIP), with its pins arranged in two rows along the longer sides of the IC, the pin spacing being 0.1 inches. Because the 68000

AA0ZZ PIC-EL v2.1K July 1, 2006

-8-

Copyright 2006, AA0ZZ

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.

Designer ComputersSo Many Microcontrollers


There are a large variety of microcontrollers on the market. W e will use the versatile microcontroller chips called PIC chips (or PICmicro chips) from Microchip Technology Inc.

- Introduction of pic microcontroller :


PIC microcontrollers have been around for many years and are still very popular, in spite of the arrival of a number of new microcontroller families. One of the reasons PICs are so popular is because there are so many examples of projects that use them. Unfortunately, much of the code is complicated and hard to understand, especially for beginners. On the other hand, with a bit of determination and perseverance, trademarks of amateurs today, many have found PICs to be very useful in a variety of applications such as CW keyers, frequency counters, Direct Digital Synthesis (DDS) controllers, receivers and/or transmitters controllers, repeater controllers, power/SWR meters, antenna control, and station control. The list is endless. I developed the PIC-EL board in late 2004, with consulting help from AmQRP club members, George Heron, N2APB, Joe Everhart, N2CX, John McDonough, WB8RCR, Earl Morris, N8ERO, and Jim Kortge, K8IQY. This was soon after John McDonough volunteered to teach an on-line course to help beginners learn how to use PICs. We wanted to make a project board that the students could use to load the code they just wrote into the PIC and then to immediately try it out. (Immediate feedback does wonders in keeping the experimenter motivated.) Within a three-month period I developed several prototypes, tested them, and finally produced a printed circuit board that the AmQRP club could use in a
AA0ZZ PIC-EL v2.1K July 1, 2006 -9Copyright 2006, AA0ZZ

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

Microchip PIC Family


PIC16 Devices

AA0ZZ PIC-EL v2.1K July 1, 2006

- 10 -

Copyright 2006, AA0ZZ

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 11 -

Copyright 2006, AA0ZZ

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

AA0ZZ PIC-EL v2.1K July 1, 2006

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

13. A 2x6 pin header block (HDR1 - CONFIG)

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.1 PIC system clock (crystal)


The system clock is generated by a 4 MHz crystal with two 22 pf capacitors. A simple RC oscillator or the 16F628s internal oscillator could have been used instead; however, since we are going to be experimenting with several timing-sensitive projects such as frequency counters, an accurate clock is essential so a crystal was used. Figure 1 shows the basic components that are necessary to run a PIC. As you can see, its really very simple.

Figure 1 Basic PIC connections

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 13 -

Copyright 2006, AA0ZZ

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

AA0ZZ PIC-EL v2.1K July 1, 2006

- 15 -

Copyright 2006, AA0ZZ

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.

Figure 6 Gray Code

AA0ZZ PIC-EL v2.1K July 1, 2006

- 16 -

Copyright 2006, AA0ZZ

Determining the direction the encoder is being turned is done by a variety of software algorithms. Figure 7illustrates one way to determine the direction.

Figure 7 Determining Encoder 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

Signal generation with the NJQRP DDS

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 18 -

Copyright 2006, AA0ZZ

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.

2.2.9 CW paddle Input via a Stereo Jack


The CW paddles are attached to the PIC by way of a 1/8 stereo jack. See Figure 11

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

Transmitter Keying via a Stereo Jack

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 19 -

Copyright 2006, AA0ZZ

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

How to Drive a Relay from a PIC

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 20 -

Copyright 2006, AA0ZZ

Figure 14 - Driving a Relay

3 PIC-EL Computer Interface


Now you are ready to program a PIC using the PIC-EL. Before you can do this you need to have a program installed in your PC. There are many good options available (including IC-PROG and WinPIC) for the Windows Operating System but we will concentrate on one good one here, FPP by David Tait, since it is readily available and very easy to use. Similar packages are available for the Linux and Macintosh operating systems but will not be described here.

3.1.1 Installing FPP


You can download FPP from the David Taits site: http://www.people.man.ac.uk/~mbhstdj/piclinks.html If you are using Windows 95 or Windows 98 you can run FPP without any additional drivers. However, if you are using Windows 2000 or Windows XP you need to install two drivers, directio and loaddrv, to allow FPP to access the COM port. These two drivers are available many Internet sites but they are also available by following links from the AmQRP Elmer-160 web site. Detailed instructions for installing FPP are available by downloading Johns Elmer-160 Lesson 10 from the AmQRP Elmer-160 web site.

3.1.2 Using FPP


A serial cable is needed to connect the PC to the PIC-EL board. You need a standard straight-through 9pin DB9M (male) to 9-pin DB9F (female) connector. One end of the cable plugs into the RS-232C serial (COM) port on your PC and the other end plugs into the PIC-EL board. You may need to "disable" other programs also using that same serial port. Be sure that you have a sufficiently high voltage power supply connected to your PIC-EL board. Although the board can run off a 9V battery, you will need to have at least 12V at the PIC-EL CONFIG
AA0ZZ PIC-EL v2.1K July 1, 2006 - 21 Copyright 2006, AA0ZZ

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 22 -

Copyright 2006, AA0ZZ

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.

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 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

AA0ZZ PIC-EL v2.1K July 1, 2006

- 23 -

Copyright 2006, AA0ZZ

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

AA0ZZ PIC-EL v2.1K July 1, 2006

- 25 -

Copyright 2006, AA0ZZ

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 26 -

Copyright 2006, AA0ZZ

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

AA0ZZ PIC-EL v2.1K July 1, 2006

- 27 -

Copyright 2006, AA0ZZ

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

AA0ZZ PIC-EL v2.1K July 1, 2006

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.

The block diagram of the controller is shown below:

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

AA0ZZ PIC-EL v2.1K July 1, 2006

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

PIN 16x PIN 1 PIN 3 PIN 5 PIN 7 PIN 9

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

PIN 2 PIN 4 PIN 6 PIN 8 PIN 10 PIN 12 PIN 14 PIN 16

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

BSF MOVLW MOVWF BSF MOVLW MOVWF

STATUS,RP0 0x1E TRISA STATUS,RP0 0x00 TRISB

;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

3.3 HEADER 16 X. (INLUDES PORT FOR A MATRIX KEYPAD 4 X 4)


AA0ZZ PIC-EL v2.1K July 1, 2006 - 31 Copyright 2006, AA0ZZ

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

FUNCION Ground 5 volts. Intensity control

LCD 8 DB1 9 DB2 10 DB3


- 32 -

GND 1 K

R /W

IN T

R S

EN

EDUPIC RB1 RB2 RB3

FUNCION DATA DATA DATA


Copyright 2006, AA0ZZ

AA0ZZ PIC-EL v2.1K July 1, 2006

4 RS RA1 5 R/W GROU ND 6 EN RA2 7 DB0 RB0

0=command 1=data 0=write in LCD 1=read Enable pulse DATA

11 DB4 12 DB5 13 DB6 14 DB7

RB4 RB5 RB6 RB7

DATA DATA DATA DATA

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:

you choose: NEXT

SOFTWARE LICENSE I ACCEPT SELECT DESTINATION SHORTCUTS? ADD TO DESKTOP? YES YES NO

MPLAB IDE NEXT NEXT NEXT NEXT


- 33 Copyright 2006, AA0ZZ

VIEW READ ME FILES?


AA0ZZ PIC-EL v2.1K July 1, 2006

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:

INSTALATION COMPLETED RESTART SYSTEM

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.

STEP 3: DISABLE WRITE PROTECTION IN FILES.

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 34 -

Copyright 2006, AA0ZZ

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

STEP 4: CONNECT MODULE TO PC.

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.

STEP 5: CONFIGURE WINPIC. TRANSFER AND EXECUTE A FILE

AA0ZZ PIC-EL v2.1K July 1, 2006

- 35 -

Copyright 2006, AA0ZZ

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.

STEP 6: EXECUTION OF PROGRAM IN AUTORUN MODE

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 36 -

Copyright 2006, AA0ZZ

STEP 7: CONFIGURE MPLAB, EDIT, ASSEMBLE AND SIMULATE A PROGRAM.

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.

; File DEMOF84.ASM ; Assembly code for PIC16F84 microcontroller

; 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

AA0ZZ PIC-EL v2.1K July 1, 2006

K equ

H'1E'

; localidad 1E es K

; Programa

org

; origen de memoria flash =0

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

;modify this value to 4 to perform simulation ; J := w ; K := w ; K = K-1, skip next if zero

decfsz J,f goto goto jloop mloop

; J = J-1, skip next if zero

AA0ZZ PIC-EL v2.1K July 1, 2006

- 38 -

Copyright 2006, AA0ZZ

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 39 -

Copyright 2006, AA0ZZ

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 40 -

Copyright 2006, AA0ZZ

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

LCD 8 DB1 9 DB2 10 DB3 11 DB4 12 DB5 13 DB6 14 DB7

GND 1 K

R /W

IN T

R S

EN

EDUPIC RB1 RB2 RB3 RB4 RB5 RB6 RB7

FUNCION DATA DATA DATA DATA DATA DATA DATA

1 2 3 INT 4 RS RA1 5 R/W GROU ND 6 EN RA2 7 DB0 RB0

4. Connecting the system. Step by step.

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 41 -

Copyright 2006, AA0ZZ

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

LCD 8 DB1 9 DB2 10 DB3 11 DB4 12 DB5 13 DB6 14 DB7

GND 1 K

R /W

IN T

R S

EN

EDUPIC RB1 RB2 RB3 RB4 RB5 RB6 RB7

FUNCION DATA DATA DATA DATA DATA DATA DATA

1 2 3 INT 4 RS RA1 5 R/W GROU ND 6 EN RA2 7 DB0 RB0

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:

you choose: NEXT

SOFTWARE LICENSE I ACCEPT SELECT DESTINATION SHORTCUTS? ADD TO DESKTOP? YES YES NO

MPLAB IDE NEXT NEXT NEXT NEXT

VIEW READ ME FILES?

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:

INSTALATION COMPLETED RESTART SYSTEM

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 43 -

Copyright 2006, AA0ZZ

STEP 3: DISABLE WRITE PROTECTION IN FILES.

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

STEP 4: CONNECT MODULE TO PC.


AA0ZZ PIC-EL v2.1K July 1, 2006 - 44 Copyright 2006, AA0ZZ

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.

STEP 5: CONFIGURE WINPIC. TRANSFER AND EXECUTE A FILE

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.

STEP 6: EXECUTION OF PROGRAM IN AUTORUN MODE

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.

STEP 7: CONFIGURE MPLAB, EDIT, ASSEMBLE AND SIMULATE A PROGRAM.

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.

; File DEMOF84.ASM ; Assembly code for PIC16F84 microcontroller

; Enciende leds del puerto B con un patrn de rotacin ; Oscilador Cristal de 4 Mhz. Cada LED enciende durante 0.5 seg.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 46 -

Copyright 2006, AA0ZZ

; 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

; origen de memoria flash =0

movlw B'00000000' ; w := 00000000 tris PORTB ; puerto B=0 (bits son salidas)

movlw B'00000001' ; movwf PORTB bcf STATUS,C ; port B =1 ;carry=0


- 47 Copyright 2006, AA0ZZ

AA0ZZ PIC-EL v2.1K July 1, 2006

mloop:

rlf

PORTB,f

;rotate left PORT B (incluye carry)

movlw D'200' movwf J jloop: kloop: goto movwf K decfsz K,f kloop

;modify this value to 4 to perform simulation ; J := w ; K := w ; K = K-1, skip next if zero

decfsz J,f goto goto jloop mloop

; J = J-1, skip next if zero

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

AA0ZZ PIC-EL v2.1K July 1, 2006

- 48 -

Copyright 2006, AA0ZZ

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

Microchip PIC Family

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

3-16bit Yes 2-8bit

40

31 kHz

PIC18F8622

8-bit

Enhanced Flash

64

1024

3936

70

16

10-Bit

100

3-16bit Yes 2-8bit

40

PIC18F8627

8-bit

Enhanced Flash

96

1024

3936

70

16

10-Bit

100

3-16bit Yes 2-8bit

40

PIC18F8680

8-bit

Enhanced Flash

64

1024

3328

69

16

10-Bit

30

3-16bit Yes 2-8bit

40

PIC18F86J10

8-bit

Standard Flash

64

2048

66

15

10-Bit

100

3-16bit Yes

40

31 kHz

(3: Microcontroller Applications

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

LCD 8 DB1 9 DB2 10 DB3 11 DB4 12 DB5 13 DB6 14 DB7

GND 1 K

R /W

IN T

R S

EN

EDUPIC RB1 RB2 RB3 RB4 RB5 RB6 RB7

FUNCION DATA DATA DATA DATA DATA DATA DATA

1 2 3 INT 4 RS RA1 5 R/W GROU ND 6 EN RA2 7 DB0 RB0

4. Connecting the system. Step by step.

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 53 -

Copyright 2006, AA0ZZ

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

LCD 8 DB1 9 DB2 10 DB3 11 DB4 12 DB5 13 DB6 14 DB7

GND 1 K

R /W

IN T

R S

EN

EDUPIC RB1 RB2 RB3 RB4 RB5 RB6 RB7

FUNCION DATA DATA DATA DATA DATA DATA DATA

1 2 3 INT 4 RS RA1 5 R/W GROU ND 6 EN RA2 7 DB0 RB0

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:

you choose: NEXT

SOFTWARE LICENSE I ACCEPT SELECT DESTINATION SHORTCUTS? ADD TO DESKTOP? YES YES NO

MPLAB IDE NEXT NEXT NEXT NEXT

VIEW READ ME FILES?

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:

INSTALATION COMPLETED RESTART SYSTEM

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 55 -

Copyright 2006, AA0ZZ

STEP 3: DISABLE WRITE PROTECTION IN FILES.

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

STEP 4: CONNECT MODULE TO PC.


AA0ZZ PIC-EL v2.1K July 1, 2006 - 56 Copyright 2006, AA0ZZ

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.

STEP 5: CONFIGURE WINPIC. TRANSFER AND EXECUTE A FILE

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.

STEP 6: EXECUTION OF PROGRAM IN AUTORUN MODE

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.

STEP 7: CONFIGURE MPLAB, EDIT, ASSEMBLE AND SIMULATE A PROGRAM.

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.

; File DEMOF84.ASM ; Assembly code for PIC16F84 microcontroller

; Enciende leds del puerto B con un patrn de rotacin ; Oscilador Cristal de 4 Mhz. Cada LED enciende durante 0.5 seg.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 58 -

Copyright 2006, AA0ZZ

; 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

; origen de memoria flash =0

movlw B'00000000' ; w := 00000000 tris PORTB ; puerto B=0 (bits son salidas)

movlw B'00000001' ; movwf PORTB bcf STATUS,C ; port B =1 ;carry=0


- 59 Copyright 2006, AA0ZZ

AA0ZZ PIC-EL v2.1K July 1, 2006

mloop:

rlf

PORTB,f

;rotate left PORT B (incluye carry)

movlw D'200' movwf J jloop: kloop: goto movwf K decfsz K,f kloop

;modify this value to 4 to perform simulation ; J := w ; K := w ; K = K-1, skip next if zero

decfsz J,f goto goto jloop mloop

; J = J-1, skip next if zero

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

AA0ZZ PIC-EL v2.1K July 1, 2006

- 60 -

Copyright 2006, AA0ZZ

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.

AA0ZZ PIC-EL v2.1K July 1, 2006

- 61 -

Copyright 2006, AA0ZZ

You might also like