5.1 Introduction
This tutorial introduces the idea of interfacing the microcontroller to the outside world. That is, to control switches, LED indicators, other computers and in some cases external memory and I/O. In most cases a microcontroller will not need external memory or I/O. This is because the whole idea behind using a
microcontroller is to take advantage of the integration of microprocessor, memory, I/O and a selection of peripheral devices on a single chip. There are however, a number of situations where a microcontroller may need to have access to external memory or I/O and for this reason a number of microcontrollers (certainly not all) will provide this facility.
A microcontroller that provides this "off chip" memory / I/O option is said to have both microcontroller and microprocessor modes of operation. The microprocessor mode is so named because it is reminiscent of a conventional microprocessor. In microprocessor mode there is no internal program memory available, all program memory being made available as external memory (just like in a conventional microprocessor).
The PIC can be configured in a slightly different arrangement called Extended Microcontroller mode where the internal program memory is available with an extension of external program memory.
The following illustration, taken from the PIC17C42 data sheet, shows the three arrangements.
Tutorial 5 - Interfacing to the Outside World
This tutorial contains the following sections:
5.1 Introduction
5.2 Interfacing to the outside world
5.2.1 Interfacing example TTL Voltage Levels
Need for flywheel diode across a relay coil
5.3 A note about digital signal levels and calculated component values
5.4 Interfacing a number of devices to a microcontroller
5.5 Interfacing external memor
5.5.1 Microprocessor mode
5.6 Using external memory
5.6.1 Port controlled external m Serial EEPROM devices
5.7 External memory write timi Self Assessment Questions
Note that in every case the data memory (RAM) is "on chip".
5.2 Interfacing to the outside world
In most applications the digital I/O ports of the microcontroller are used to interface to the outside world. Typically, we may need to read switches and write to LED indicators, relays, and control LCD displays. We may also need to read and write to external logic and perhaps also communicate with another microcontroller or a PC.
In the latter case we could make use of the serial port to interface with the serial I/O communication port of the PC.
Some examples of interfacing to a microcontroller were included in tutorial 1. You may remember the following diagram:
5.2.1 Interfacing example
The digital I/O of a PIC microcontroller is designed to interface to devices that are
"TTL compatible". This means that they are designed to work at 5V and can source and sink at least one TTL load.
Click on the button below to open a new window with information about TTL voltage levels
The typical interfacing arrangements shown in the example above are quite general and are easy to implement.
For example, to interface an LED indicator, one only needs to know the required forward diode current If and the forward voltage Vf of the LED. This is typically 10mA and 1.6V respectively.
So in the example the value of the current-limiting resistor R1 can be calculated as:
TTL Voltage Levels
The nearest preferred value is 330Ω . This assumes zero VCEsat for the output transistor.
If a low current LED is used (typically If = 2mA) then the value of R1 will be 1700Ω or its nearest preferred value of 1800Ω , giving a diode current of 1.8mA.
The relay can likewise be easily interfaced to the microcontroller by first noting its operating current Ir and operating voltage Vp. Most relays will have an operating voltage greater than 5V (although there are some DIL relays that operate from 5V).
Then
For an operating voltage of 6V and an operating current of 15mA.
Click on the button below to open a new window with information about why there is a need for a diode across the relay coil.
5.3 A note about Digital Signal Levels and Calculating Component Values
In some cases the microcontroller output will not be able to sink the required current so a buffer device will be required.In the typical interfacing example diagram above, the buffer device would be an
"open collector" type so that the output transistor of the buffer would provide a current path to ground when it is turned on.
Switches are very easily interfaced, requiring only a "pull-up" resistor. The
maximum possible value of the pull-up resistor is dictated by the logic one current IiH which flows out of the input pin.
So
R1 = Vdd - Vf= 5V - 1.6V = 340 Ω If 10mA
R2 = Vdd - Vp = 12 - 6 = 400Ω Ir 15ma
Need for a flywheel diode
These figures are taken from the PIC16C84 data sheet. Note that in practice we would need to choose a resistor value less than this to ensure that the volt drop across R3 was much less than the minimum required. A suitable value would be 100KΩ giving a current through R3 when the switch is open of:
(5V - 1.8V) / 100x103 = 32uA
In practice, of course, the current into an input would be no more than 1uA so the volt drop across R3 would be no more than 1uA x 100K = 0.1 Volt. Thus the input would be very close to Vdd (5-0.1) Volts.
This value of R3 will still consume a little current when the switch is closed.
ie. 5V / 100KΩ = 50uA
When interfacing external devices to a microcontroller port, one should first look at the electrical characteristics of the microcontroller.
The following screen shot from the PIC16C84 data sheets illustrates some of the electrical characteristics for the I/O inputs of the PIC16C84 microcontroller.
R3max = Vdd - ViHmin = 5 - 1.8 = 3.2MΩ IiH 1uA
The range of acceptable input voltages. Input logic levels for the digital I/O pins of the PIC.
In particular, the range of input voltages ViH, which range from Vdd (max) to 0.36Vdd (min).
For a nominal value of Vdd = 5V, ViH(min) = 0.36 x 5 = 1.8V.
Note that a permissible input is between 4.5V and 5.5V.
The input low voltage range is between Vss (zero voltage) and 0.8V maximum or between 0V and 0.16xVdd (= 0.8 V).
The leakage currents are very low (it is a CMOS device) at ± 1uA for I/O port inputs.
The following screen shot shows the lower part of the same figure from which you can see the I/O port output voltage levels:
Note the minimum output voltage for logic 1: VoH = Vdd - 0.7V = 5 - 0.7 = 4.3V, for an output current of IoH = 3mA and a minimum supply voltage Vdd of 4.5V.
The PIC devices have what is called a "weak pull-up" resistor connection to all of port B I/O lines. These can be enabled by programming bit 7 of the OPTION register to logic 0. When enabled, any port B I/O bit that is programmed as input will have this weak pull-up resistor connected. The result is that no external pull-up resistors are necessary, thus saving on external components. See Port B and TRISB registers in the PIC16C84 data sheets for details of the internal circuit for each port B bit. Note that port A does not have this feature.
Many newer microcontrollers have this built in "weak pull-up" resistor, but most older microcontroller don't.
5.4 Interfacing a number of devices to a microcontroller
Interfacing more devices to a microcontroller requires more digital I/O ports. Some recent microcontrollers (such as the H8/3048) have a large number of digital I/O ports. (See http://hitachi.com).
Most general purpose microcontrollers however have very limited digital I/O
capability, for example the PIC range of microcontrollers (particularly the base and medium range devices) have between two, three, and four ports. A typical
application might consist of an LCD display, one or two LED indicators, and some panel switches or analogue inputs.
The figure below shows a simple temperature monitor.
Here the requirement is to monitor and store temperature readings over a period of time. If the temperature goes above or below predefined limits, Hi/Low LED
indicators are illuminated.
A four digit LCD display will require eight digital I/O bits, four for the value and one for each digit (digit select).
If we were to use the PIC16C71 for this project we would be able to assign port B for the display parallel interface (8 bits), leaving port A for the digit selection (4 bits) and two bits of port A for the Hi and Lo LEDs. In addition, we require an analogue input, giving a total of 15 bits required.
The PIC16C71 has two I/O ports, Port B (8 bits) and Port A (5 bits) we are short of
two bits to complete the interface. Note that the PIC16C84 does not have an Analogue to Digital Converter, necessary to convert the analogue temperature into a digital value so it would be unsuitable for this application.
We could, of course choose to use a larger PIC device (such as the PIC16C73 which has three I/O ports, Port A - 6 bits, Port B - 8 bits, and Port C - 8 bits) but this is a larger device (28 pin). It would, however, provide for future enhancements to the product.
One way of overcoming the I/O limitation of the PIC16C71 is to use a single I/O port for both input and output, changing the port configuration during program execution to read inputs, or write to outputs as required.
The diagram below illustrates an arrangement showing how a seven segment display can be interfaced along with switch inputs.
Note the RA0 and RA1 port bits are connected to both the A1, A0 switches, and the transistor drivers to select the display digits.
To drive the display, the RA0 and RA1 bits are configured as outputs. To input from the two switches, the RA0 and RA1 bits are configured as inputs. The two 10K resistors prevent damage to the port when configured as outputs and a switch is closed.
5.5 Interfacing External Memory to a Microcontroller
5.5.1 Microprocessor ModeSome microcontroller applications require external memory to supplement the RAM available internally. For example, an application may require additional RAM to store data read in from the external environment in a data logging application. Or more memory may be required to store the application program. Or it may be that the application program is to be updated via a modem and the microcontroller does not support electrically erasable (EEPROM) so external EEPROM program
memory needs to be added. Or an application may require more I/O than is
available on the microcontroller chip. Or a peripheral device may need to be added (such as an external UART) so that the microcontroller can communicate with a modem.
To allow a microcontroller to use external memory, some microcontrollers can be configured in microprocessor mode, also known as expanded mode.
In microprocessor mode some of the internal features of the microcontroller are lost in order to provide an external address and data bus. To see why this is so,
consider the following illustration.
The microcontroller pins for port B double up as a multiplexed Address and Data bus.
For this to work, the microcontroller must contain configuration logic to allow the pins to be used either
z as an I/O port for digital input or output
z as a multiplexed Address/Data Bus.
Note that at any one time these pins can only be used for one of the two functions.
This means that if the microcontroller is to be used in microprocessor mode, then the pins used for the multiplexed Address/Data bus cannot be used for port B I/O and therefore port B I/O is lost. The circuitry for port B is of course still present inside the microcontroller, but it is simply not available for use.
This is a very common problem with microcontrollers and fact it is not just limited to configuration. In most cases, a given microcontroller will contain the devices we have discussed in the previous tutorial (ie. SCI, ADC etc), but they will, in most cases, share the same I/O pins as digital I/O ports. Therefore when we elect to use these devices (SCI/ADC) the digital port features with which they share I/O pins are lost.
This means that when deciding upon the particular microcontroller to use in a particular application, we need to consider which of the internal features we intend to use. Then determine how much digital I/O is left for use in our
application.
Some recent microcontrollers, such as the PIC range, have been designed to provide a more flexible arrangement so that, when an internal peripheral is
selected, not all of the I/O port pins are lost. You will have seen an example of this with the use of the ADC in the PIC16C73 where the use of the ADC does not mean losing all of the I/O for portA if, say, two analogue channels are required rather than four.
So now let us look at a microcontroller that can be configured to access external memory and I/O.
The above diagram shows a microcontroller with two ports, A and B. If these two ports are also used for external data and address bus, then when we configure the chip for microprocessor mode we obtain:
You can see, the port A is now used as part of the address bus, with port B used for both the remaining address bus signals and the data bus. At one time, port B pins form part of the address bus, while, a little later, they are the data bus.
This is a common arrangement, called a multiplexed address/data bus
configuration. It is common because it uses less pins than would be needed for separate address and data bus connections.
In order to obtain the multiplexed address and data signals from the port B pins, an external data latch is required. This is used to hold the address bits, when the port B pins change to become the data bus.
The purpose of the address strobe is to latch the address bits into the data latch at the right moment, before the port B pins change to become the data bus.
The above diagram illustrates the basic arrangement of a multiplexed Address/Data bus. It is actually very similar to that used for the intel 80X86 processor. The basic address/data multiplexed timing is illustrated below.
The main point here is that the address remains fixed until the next address strobe.
Note that the memory read (when the read line is asserted) and the memory write (when the write line is asserted) take place towards the end of the data waveform.
We will have a closer look at a typical example of a microcontroller in microprocessor mode a little later on in this tutorial.
5.6 Using External Memory
Most microcontrollers allow the external memory to be used for either data or program storage. For example, the Motorola MC68HC11 microcontroller can be used with external memory for program or data since it is seen by the microcontroller as an extension of its memory map.
The PIC range of microcontrollers however use a Harvard architecture which has
separate data and address busses for program and data memory. The PIC devices that use external memory use the program data and address busses to interface with the external memory so that this memory is normally used for storing program and is typically used to extend the size of the application program.
The figure below illustrates the arrangement used with the PIC17C4X range.
Typical External Program Memory Connection Diagram
What if you want to use external memory for data rather than program?
It is possible to use external memory to store data but the data cannot be accessed directly.
It should be pointed out that other microcontrollers, such as the 68HC05 and 68HC08 do not have this problem, external memory can be used for either program or data.
To access data in external memory with the PIC (both to write data to external memory and to read it from external memory) requires an indirect approach using a number of the PIC's internal registers (Table pointer and Table Latch registers).
Is there any other way of interfacing external memory to the PIC?
Task
Obtain the data sheet for the PIC17C4X devices (from Microchip website) and read the sections on the External Memory Interface. There is a
schematic diagram showing the connections for a typical external memory interface (the one above). Also read about the Table reads and Table writes. There are some diagrams to explain the operation of these, together with some sample program fragments showing how to read and write data to external memory. Refer to the memory read and memory write interface timing diagrams.
Yes, interface memory directly to the I/O ports of the PIC.
5.6.1 Port Controlled External Memory
The PIC's fast execution time makes it possible to connect an external memory chip to the I/O ports. If a 20MHz crystal is used, memory transfer rates are compatible with microcontrollers using a 1MHz instruction cycle and memory mapped external memory.
Microchip publish an applications paper on this idea that you might like to read.
This arrangement, of course, uses up a large amount of the microcontroller digital I/O but this might not be a problem in some applications.
Another idea is to use a slave microcontroller to control the external data memory device and pass all data to/from the main microcontroller via a dedicated port (or if data speed is not an issue, via serial I/O).
The serial I/O interfacing of external memory is used to interface some kinds of
electrically erasable memory devices. These are called serial EEPROMs and are used to store data that is required to be changed from time to time, but must be held in
memory between power up/power down conditions. Again, there is a data sheet available at Microchip's website for reference to see how it is done.
5.7 External Memory Write Timing
When designing an external memory interface, the external memory write and Additional port bits required for the
Address bus bits of larger memories.
memory read cycle timing must be carefully considered. The following diagram is taken from the PIC17C4X data sheet.
Memory Interface Write Timing
Each memory cycle involves four clock cycles, Q1 to Q4. Most instructions execute in a single memory cycle so the instruction time is simply the clock period multiplied by four. For example, a microcontroller with a 20MHz oscillator has a clock period of 50ns and memory cycle time Tcy = 200ns.
The four states of the memory cycle Q1, Q2, Q3, and Q4 define the four internal states that are an external memory cycle in the PIC.
The address is placed onto the address pins of the PIC during Q2 and latched into the external address latch on the falling edge of ALE.
The multiplexed Address/data bus pins become the data bus during the Q3 phase and the data is written on the rising edge of the write pulse in the following Q1 period.
The time values given in the data sheet are thus:
TcdV2adl (150 in the above figure) indicates a minimum time of 0.25Tcy - 30 which would be;
0.25 x 200ns - 30ns = 50ns - 30ns = 20ns.
Using the values for a 20MHz oscillator we can obtain the maximum access time from
Address Access Time = 0.75 * Tcy - 40ns = 0.75 * 200ns - 40ns = 110ns
However, from this should be subtracted the delay time of any address decoding logic that might be used in the design. So if the worst case delay for the address decoding logic is Tpaddr then
Address Access Time = 0.75 * Tcy - 40ns - Tpaddr = 110ns - Tpaddr.
The Microchip application note TB005 on the use of External RAM gives a specific example of interfacing the PIC17cxx to two MT5c2568 (32Kx8) static RAM chips.
In most applications where there is only additional memory and no additional I/O there will not be any external address decoding logic so Tpaddr will be zero.
Any external memory chips used should have a memory access time which is less than the address access time of the PIC.
So Address Access Time > Memory Access Time.
In general, when calculating the worst case address access time, you should determine the slowest delay path from the microcontroller and the device you are trying to access. This will include any address decoding logic chips.
The indicated timing values below are important and should be studied from the data sheet.
z 150 is the Address valid to ALE address setup time, 20ns in our case
z 151 is the address hold time and is the time required for the address to settle at the output pins of the PIC after the ALE signal has gone low. It is 0ns which means that it is not critical.
z 152 is the data setup time, the time required for the data bus values to settle at the data pins of the PIC. It is 10ns in our case.
z 154 is the Write signal pulse time, in our case 50ns.
z 153 is the time between the end of the write pulse and the point where the data bus outputs become invalid..
Note that the timing values 150 to 152 are minimum values (they will be at least this big), whilst 153 and 154 are typical values (averaged over a number of samples tested).
The timing arrangement for the read cycle is similar and should be studied also.
Task
Go to Microchip's web site at www.microchip.com and find the application sheet TB005 "Using External RAM with PIC17Cxx devices". This contains information on how to use external RAM with the 17C series PICs.
Do the Interactive Self Assessment
(new window)© 2002 Microelectronics Industrial Centre, Northumbria University. Part
funded by the European Union, European Social funds.
A TTL load is defined in terms of the output voltage levels as indicated in the table below.
The standard TTL sinking current is 1.6mA and a TTL output can usually sink ten of these (16mA total). The sourcing current of a standard TTL output is around -
400uA.
Most microcontrollers use CMOS logic and the sinking current levels can be much less than a standard TTL gate, typically one TTL load.
Most new microcontrollers have greater sinking and sourcing capacity. See the data sheet for the PIC16C84 for example where port outputs can source up to 20mA maximum, and sink up to 80mA maximum (the data sheet should be
consulted since different ports sink/source different currents as can be seen in the screen shot below). However, care must be taken to ensure that the total power dissipation of the microcontroller is not exceeded by connecting large
sourcing/sinking load to too many output port bits (see note one in the figure below about maximum power rating for the PIC16C84 device).
TTL Voltage Levels
Logic Level Input Output Nominal
Logic 1 (TTL) Vih(min) = 2.0 V VoH(min) = 2.4 V 5 volts Logic 0 (TTL) ViL(max) = 0.8 V VoL(max) = 0.4 V 0 Volt Logic 1 (CMOS) Vih(min) = 3 V VoH(min) = 4.95
V 5 volts
Logic 0 (CMOS) ViL(max) = 1.5 V VoL(max) = 0.5 V 0 volts
Electrical Characteristics for PIC16C84
© 2002 Microelectronics Industrial Centre, Northumbria University. Part
funded by the European Union, European Social funds.
When interfacing relays to a microcontroller you MUST connect a diode across each relay coil to dissipate the decaying relay current when the output transistor turns off. Current continues to flow as the magnetic field declines. Failure to include this diode will result in damage to the output transistor of the buffer because the voltage will rise to a level where the current is able to flow.
As the figure shows, the diode is normally reverse biased when the relay is energised by the conducting transistor.
When the transistor switches off, the diode conducts and dissipates the relay current through the coil of the relay. This prevents a build up of voltage across the rely coil that would otherwise occur, which would exceed the Vce breakdown value of the transistor.
Why the need for a diode across the relay coil?
© 2002 Microelectronics Industrial Centre, Northumbria University. Part
funded by the European Union, European Social funds.