Top Banner
Getting Started with Embedded Systems 1.1 The main idea – embedded systems in today’s 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. AA0ZZ PIC-EL v2.1K July 1, 2006 - 1 - Copyright 2006, AA0ZZ
78
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embedded Systems (1)

Getting Started withEmbedded Systems

1.1 The main idea – embedded systems in today’s world1.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 needscontrol, 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. Mostpeople, 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 - 1 - Copyright 2006, AA0ZZ

Page 2: Embedded Systems (1)

Let’s 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 refrigeratorA simple domestic refrigerator is shown in Figure 1.1. It needs to maintain a moderately stable, lowtemperature 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 mechanismA 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

Page 3: Embedded Systems (1)

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 Microprocessors and microcontrollers1.4.1 MicroprocessorsThe 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 - 3 - Copyright 2006, AA0ZZ

Page 4: Embedded Systems (1)

1.4.2 MicrocontrollersWhile 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 microcontrollerneeds 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

Page 5: Embedded Systems (1)

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 - 5 - Copyright 2006, AA0ZZ

Page 6: Embedded Systems (1)

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

1.4.3 Microcontroller familiesThere 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

Page 7: Embedded Systems (1)

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

Page 8: Embedded Systems (1)

1.4.4 Microcontroller packaging and appearanceIntegrated 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

Page 9: Embedded Systems (1)

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 Computers—So 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 - 9 - Copyright 2006, AA0ZZ

Page 10: Embedded Systems (1)

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

Page 11: Embedded Systems (1)

2 Hardware DescriptionAppendix 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 Tait’s 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 port’s 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 port’s 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 PIC’s run voltage (Vdd) of 5v.

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.

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

Page 12: Embedded Systems (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 port’s 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-EL’s 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

Allow an outside signal source to be brought in to the “conditioner” and then to the PIC input pin

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

Page 13: Embedded Systems (1)

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

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.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 16F628’s 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, it’s 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

Page 14: Embedded Systems (1)

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 PIC’s 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

Page 15: Embedded Systems (1)

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 PIC’s 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 PIC’s 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 LCD’s 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

Page 16: Embedded Systems (1)

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

Page 17: Embedded Systems (1)

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

Page 18: Embedded Systems (1)

an instruction every microsecond, accurate delay loops can be designed to produce pulses with the desired durations.

2.2.7 Signal generation with the NJQRP DDS 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.

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

Page 19: Embedded Systems (1)

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 plug’s 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

Page 20: Embedded Systems (1)

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

Page 21: Embedded Systems (1)

Figure 14 - Driving a Relay

3 PIC-EL Computer InterfaceNow 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 Tait’s 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 John’s 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 9-pin 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

Page 22: Embedded Systems (1)

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

Page 23: Embedded Systems (1)

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 family2.1.1 A family overviewThe 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

Page 24: Embedded Systems (1)

relative, the 16LF84A, whose extended supply voltage range allows operation at lower voltages. Either ofthese 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

Page 25: Embedded Systems (1)

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 Microchip’s 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 microcontroller’s design and use.

EDU-PIC

16F84 Module

User Manual

Index   : 1. Introduction

22. General Description

23. Digital ports

3

3.1 Leds and microswitches3.2 Relay

3.3 Header 16x (keypad port 4 x 4)

3.4 LCD port

4. Connecting the system. Step by step 6

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

Page 26: Embedded Systems (1)

5. Technical specifications

5.1 General spec85.2 Lay out95.3 Electronics diagram105.4 Contents of CD11

3.1.2.1.1.1.1.1 Apendix: How to connect an LCD, and a 4 x 4 keypad 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 Microchips’s 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 it’s 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: [email protected].

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.

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

Page 27: Embedded Systems (1)

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

Page 28: Embedded Systems (1)

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 it’s 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.

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

Page 29: Embedded Systems (1)

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:

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 Name Type Function16 OSC1/CLKIN Input Signal 1 of Xtal oscilator15 OSC2/CLKOUT Output Señal 2 del Xtal oscilator4 MCLR Input Master reset. In programming mode, 12 to 13 volt signal.17 RA0 Bi Input/Output port bit18 RA1 Bi Input/Output port bit1 RA2 Bi Input/Output port bit2 RA3 Bi Input/Output port bit3 RA4/T0CLK1 Bi Input/Output port bit. May be also configured to be an input clock signal

for TMR0.6 RB0/INT Bi Input/Output port bit. May be also configured to be an external interrupt.AA0ZZ PIC-EL v2.1K July 1, 2006 - 29 - Copyright 2006, AA0ZZ

Page 30: Embedded Systems (1)

7 RB1 Bi Input/Output port bit8 RB2 Bi Input/Output port bit9 RB3 Bi Input/Output port bit10 RB4 Bi Input/Output port bit11 RB5 Bi Input/Output port bit12 RB6 Bi Input/Output port bit. In programming mode, clock input to sincronize

serial data.13 RB7 Bi Input/Output port bit. In programming mode, serial data.5 Vss 5 V. Voltage source.14 Vdd GND Ground

12 3. Digital ports :

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 PIN IN HEADER 16x

FUNCTION

PORT ARA0 PIN 1 ACTIVATE/DEACTIVATE RELAYRA1 PIN 3 MICROSWITCH A1, LCD

CONTROLRA2 PIN 5 MICROSWITCH A2, LCD

CONTROLRA3 PIN 7 MICROSWITCH A3RA4 PIN 9 MICROSWITCH A4PORT BRB0 PIN 2 LED B0, DATA0 LCD, KEYPAD Y1RB1 PIN 4 LED B1, DATA1 LCD, KEYPAD Y2RB2 PIN 6 LED B2, DATA2 LCD, KEYPAD Y3RB3 PIN 8 LED B3, DATA3 LCD, KEYPAD Y4RB4 PIN 10 LED B4, DATA4 LCD, KEYPAD X1RB5 PIN 12 LED B5, DATA5 LCD, KEYPAD X2RB6 PIN 14 LED B6, DATA6 LCD, KEYPAD X3RB7 PIN 16 LED B7, DATA7 LCD, KEYPAD X4

To program direction of bits, the user may use the following lines as an example.

PORTA BSF STATUS,RP0 ;CHOSE REGISTER BANK 1AA0ZZ PIC-EL v2.1K July 1, 2006 - 30 - Copyright 2006, AA0ZZ

Page 31: Embedded Systems (1)

MOVLW 0x1E ;RA0=OUTPUT, RA1..RA4=INPUTSMOVWF TRISA ;PROGRAM DIRECTION OF BITS

PORTB BSF STATUS,RP0 ;CHOSE REGISTER BANK 1MOVLW 0x00 ;RB0..RB7=OUTPUTSMOVWF TRISB

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.

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 it’s contacts and LED L4 will light on.

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

Page 32: Embedded Systems (1)

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 RB0…RB7, thanks to the fact that both devices can operate at different time slots.

LCD EDUPIC

FUNCION LCD EDUPIC FUNCION

1 Ground 8 DB1 RB1 DATA2 5 volts. 9 DB2 RB2 DATA3 INT Intensity control 10 DB3 RB3 DATA4 RS RA1 0=command 1=data 11 DB4 RB4 DATA

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

Page 33: Embedded Systems (1)

5 R/W GROUND

0=write in LCD 1=read 12 DB5 RB5 DATA

6 EN RA2 Enable pulse 13 DB6 RB6 DATA7 DB0 RB0 DATA 14 DB7 RB7 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 it’t 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:

SOFTWARE LICENSE I ACCEPT NEXT

SELECT DESTINATION MPLAB IDE NEXT

SHORTCUTS? YES NEXT

ADD TO DESKTOP? YES NEXT

VIEW READ ME FILES? NO NEXT

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

Page 34: Embedded Systems (1)

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 FINISH

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

Page 35: Embedded Systems (1)

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

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

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

Page 36: Embedded Systems (1)

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.

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

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

Page 37: Embedded Systems (1)

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 patrón de rotación

; Oscilador Cristal de 4 Mhz. Cada LED enciende durante 0.5 seg.

; Configuración 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

K equ H'1E' ; localidad 1E es K

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

Page 38: Embedded Systems (1)

; Programa

org 0 ; origen de memoria flash =0

movlw B'00000000' ; w := 00000000

tris PORTB ; puerto B=0 (bits son salidas)

movlw B'00000001' ;

movwf PORTB ; port B =1

bcf STATUS,C ;carry=0

mloop: rlf PORTB,f ;rotate left PORT B (incluye carry)

movlw D'200' ;modify this value to ‘4’ to perform simulation

movwf J ; J := w

jloop: movwf K ; K := w

kloop: decfsz K,f ; K = K-1, skip next if zero

goto kloop

decfsz J,f ; J = J-1, skip next if zero

goto jloop

goto mloop

end

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

Page 39: Embedded Systems (1)

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

Page 40: Embedded Systems (1)

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 RB0…RB7, 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

Page 41: Embedded Systems (1)

LCD EDUPIC

FUNCION LCD EDUPIC FUNCION

1 Ground 8 DB1 RB1 DATA2 5 volts. 9 DB2 RB2 DATA3 INT Intensity control 10 DB3 RB3 DATA4 RS RA1 0=command 1=data 11 DB4 RB4 DATA5 R/W GROU

ND0=write in LCD 1=read 12 DB5 RB5 DATA

6 EN RA2 Enable pulse 13 DB6 RB6 DATA7 DB0 RB0 DATA 14 DB7 RB7 DATA

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 RB0…RB7, 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

Page 42: Embedded Systems (1)

LCD EDUPIC

FUNCION LCD EDUPIC FUNCION

1 Ground 8 DB1 RB1 DATA2 5 volts. 9 DB2 RB2 DATA3 INT Intensity control 10 DB3 RB3 DATA4 RS RA1 0=command 1=data 11 DB4 RB4 DATA5 R/W GROU

ND0=write in LCD 1=read 12 DB5 RB5 DATA

6 EN RA2 Enable pulse 13 DB6 RB6 DATA7 DB0 RB0 DATA 14 DB7 RB7 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 it’t 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

Page 43: Embedded Systems (1)

Execute file MPLAB660.zip and then the instalation program. Please take note at the following

windows that will appear during instalation:

window: you choose:

SOFTWARE LICENSE I ACCEPT NEXT

SELECT DESTINATION MPLAB IDE NEXT

SHORTCUTS? YES NEXT

ADD TO DESKTOP? YES NEXT

VIEW READ ME FILES? NO NEXT

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 FINISH

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

Page 44: Embedded Systems (1)

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.

STEP 4: CONNECT MODULE TO PC.

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

Page 45: Embedded Systems (1)

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

Page 46: Embedded Systems (1)

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 patrón de rotación

; Oscilador Cristal de 4 Mhz. Cada LED enciende durante 0.5 seg.

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

Page 47: Embedded Systems (1)

; Configuración 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

K equ H'1E' ; localidad 1E es K

; Programa

org 0 ; origen de memoria flash =0

movlw B'00000000' ; w := 00000000

tris PORTB ; puerto B=0 (bits son salidas)

movlw B'00000001' ;

movwf PORTB ; port B =1

bcf STATUS,C ;carry=0

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

Page 48: Embedded Systems (1)

mloop: rlf PORTB,f ;rotate left PORT B (incluye carry)

movlw D'200' ;modify this value to ‘4’ to perform simulation

movwf J ; J := w

jloop: movwf K ; K := w

kloop: decfsz K,f ; K = K-1, skip next if zero

goto kloop

decfsz J,f ; J = J-1, skip next if zero

goto jloop

goto mloop

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

Page 49: Embedded Systems (1)

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

(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

Page 50: Embedded Systems (1)

Air ConditionersCoffee MakersCar Security Systems – E.g. Remote Keyless EntryCar Information system – E.g. Trip ComputerCar Network Systems – E.g. CANHuman Interface device controller – E.g. LCD, mouse, Wireless KeyboardSecurity – E.g. Video SystemsCommunications – 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

Page 51: Embedded Systems (1)

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

Page 52: Embedded Systems (1)

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 Microchip’s 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 microcontroller’s 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

Page 53: Embedded Systems (1)

to the header 16x, using lines RB0…RB7, thanks to the fact that both devices can operate at different time slots.

LCD EDUPIC

FUNCION LCD EDUPIC FUNCION

1 Ground 8 DB1 RB1 DATA2 5 volts. 9 DB2 RB2 DATA3 INT Intensity control 10 DB3 RB3 DATA4 RS RA1 0=command 1=data 11 DB4 RB4 DATA5 R/W GROU

ND0=write in LCD 1=read 12 DB5 RB5 DATA

6 EN RA2 Enable pulse 13 DB6 RB6 DATA7 DB0 RB0 DATA 14 DB7 RB7 DATA

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 RB0…RB7, 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

Page 54: Embedded Systems (1)

LCD EDUPIC

FUNCION LCD EDUPIC FUNCION

1 Ground 8 DB1 RB1 DATA2 5 volts. 9 DB2 RB2 DATA3 INT Intensity control 10 DB3 RB3 DATA4 RS RA1 0=command 1=data 11 DB4 RB4 DATA5 R/W GROU

ND0=write in LCD 1=read 12 DB5 RB5 DATA

6 EN RA2 Enable pulse 13 DB6 RB6 DATA7 DB0 RB0 DATA 14 DB7 RB7 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 it’t 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

Page 55: Embedded Systems (1)

Execute file MPLAB660.zip and then the instalation program. Please take note at the following

windows that will appear during instalation:

window: you choose:

SOFTWARE LICENSE I ACCEPT NEXT

SELECT DESTINATION MPLAB IDE NEXT

SHORTCUTS? YES NEXT

ADD TO DESKTOP? YES NEXT

VIEW READ ME FILES? NO NEXT

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 FINISH

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

Page 56: Embedded Systems (1)

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.

STEP 4: CONNECT MODULE TO PC.

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

Page 57: Embedded Systems (1)

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

Page 58: Embedded Systems (1)

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 patrón de rotación

; Oscilador Cristal de 4 Mhz. Cada LED enciende durante 0.5 seg.

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

Page 59: Embedded Systems (1)

; Configuración 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

K equ H'1E' ; localidad 1E es K

; Programa

org 0 ; origen de memoria flash =0

movlw B'00000000' ; w := 00000000

tris PORTB ; puerto B=0 (bits son salidas)

movlw B'00000001' ;

movwf PORTB ; port B =1

bcf STATUS,C ;carry=0

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

Page 60: Embedded Systems (1)

mloop: rlf PORTB,f ;rotate left PORT B (incluye carry)

movlw D'200' ;modify this value to ‘4’ to perform simulation

movwf J ; J := w

jloop: movwf K ; K := w

kloop: decfsz K,f ; K = K-1, skip next if zero

goto kloop

decfsz J,f ; J = J-1, skip next if zero

goto jloop

goto mloop

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

Page 61: Embedded Systems (1)

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