Microprocessor Interfacing - v1.

05 - J R Smith

MICROPROCESSOR INTERFACING
1 2 INTRODUCTION BINARY LOGIC AND ELECTRONICS 2.1 From voltages to logic 2.2 TRI-STATE logic 2.3 Binary inputs and outputs 3 BINARY INPUT TRANSDUCERS 3.1 3.2 3.3 3.4 3.5 3.6 Mechanical switches. Multiplexed inputs Switch debouncing. Some other switches Non-mechanical switches. Pseudo-binary inputs

4 BINARY OUTPUT TRANSDUCERS 4.1 Solenoids 4.2 Pseudo-binary outputs 5 ENCODING INFORMATION BY VARIATIONS WITH TIME 5.1 5.2 5.3 5.4 5.5 5.6 5.7 Introduction Elapsed Time Frequency Modulation (FM) Pulse Width Modulation (PWM) Bitstream Modulation (BSM) Coding Information coding in Biology (not required for exam)

6 BASIC ANALOGUE COMPONENTS 6.1 Amplifiers 6.2 Comparators 6.3 Using analogue transducers as binary transducers (not required for exam) 7 DIGITAL TO ANALOGUE CONVERSION 7.1 7.2 7.3 7.4 How many bits? Bitstream Binary-weighted resistors R-2R Ladder

8 ANALOGUE TO DIGITAL CONVERSION 8.1 8.2 8.3 8.4 Parallel or Flash Successive Aproximation Integrating Delta - Sigma

1

Microprocessor Interfacing - v1.05 - J R Smith

9 TRANSDUCERS 9.1 TRANSDUCERS FOR TEMPERATURE 9.1.1 Thermocouple 9.1.2 Thermistor 9.1.3 Semiconductor junction 9.1.4 Temperature dependent oscillator 9.1.5 Resistor 9.1.6 Peltier (thermoelectric) module. 9.2 TRANSDUCERS FOR LIGHT 9.2.1 Light Dependent Resistor (LDR) 9.2.2 Photodiode 9.2.3 Phototransistor 9.2.4 Solar cell 9.2.5 Incandescent lamp (Light Emitting Resistor) 9.2.6 Light Emitting Diode 9.3 TRANSDUCERS FOR SOUND 9.3.1. Dynamic microphones 9.3.2. Elecret, capacitor and condensor microphones 9.3.3 Dynamic Speaker 9.3.5 Electrostatic Loudspeaker 9.3.6 Magnetostrictive transducer 9.4 TRANSDUCERS FOR CHEMICAL CONCENTRATIONS 10 INTERACTION SCHEMES 10.1 Programmed interaction or polling 10.2 Interrupts 10.3 Direct Memory Access (DMA) 11 SOME ASPECTS OF COMPUTER ARCHITECTURE 11.1 Types of memory

These notes are written with specific reference to the 'ATOM' microcontroller. However much of the information is also applicable to the 'BASIC STAMP' microcontroller or other microcomputers. Copyright J R Smith 2003 2

Microprocessor Interfacing - v1.05 - J R Smith

1

INTRODUCTION

You should now be familiar with the BASIC MICRO 'ATOM' microcontroller. It is based on the 16F876 PICMicro MCU. It has 8K of FLASH memory used for storing programs, 384 bytes of RAM for storing the variables used in programs and 256 bytes of EEPROM that can store data when the power is removed. What can a device like this do? It turns out that it can do almost anything. However it does have two fundamental limitations - speed and complexity. The internal cycle time (200 ns) and the time taken to execute instructions (of the order of 30 µs) both limit how rapidly the ATOM can respond to external events. This limitation can be overcome for short periods by using external circuitry with a faster response. However for continuous operation the speed is ultimately limited by the instruction execution time. Consequently the ATOM is simply too slow for some tasks (e.g. real time, high fidelity, audio processing). The limited space available for program and variables also imposes an eventual upper limit on the complexity of tasks that the ATOM can reasonably handle. However you are unlikely to approach this limit. I have written large programs (>20 pages of code) that still fit into the 8K memory. Physicists are interested in the behaviour of the real world, however the parameters of interest don't occur in the form of binary signals with voltage levels compatible with the binary logic of microcomputers. Consequently transducers are used to convert various physical parameters to and from suitable electrical signals. These notes aim to provide an introduction to the • interfacing computers to the real world • some common transducers • various techniques used to convert between analogue and digital variables • techniques for synchronising a microcomputer with real world events Suitable circuits and programming examples will be presented wherever possible. They will often be specifically for the ATOM28, but the basic principles are applicable to most microcomputers or microcontrollers.

3

v1.short for Transistor Transistor Logic. A A B E Q 0 0 1 1 x B 0 1 0 1 x E 0 0 0 0 1 Q 0 0 0 1 z x = doesn't matter z = high impedance or 'disconnected' Fig. However before you can interact with signals from the real world you need to which voltage levels correspond to a logical 0 (often called LOW) and which belong to a logical 1 (often called HIGH). Although there are many subspecies with slightly different characteristics.1 From voltages to logic Although you have now been using an electronic implementation of digital logic for several weeks. 2.Microprocessor Interfacing .2. An additional 'enable' input determines whether the output behaves normally or is disconnected.the particular maximum value of VDD depends upon the particular species. .05 . Bus structures TRI-STATE logic is very useful in bus structures where it enables multiple outputs to be connected together. The first is 'TTL' . Currently there are 2 main families of electronic logic that you are likely to meet. you will find that a given pin can sometimes be an input.5 and 5 volts The second main family is CMOS. and sometimes be an output! To understand how this is possible we need to examine a slightly different form of electronic logic . 4 . This has the normal digital logic levels of LOW and HIGH plus an extra output state where the output is essentially disconnected from the internal logic circuitry. their logic levels are approximately logical 0 corresponds to a voltage between 0 and 1. until now you have not needed to concern yourself with what voltages were used to represent binary logic. The ’enable’ input is denoted by E.2 TRI-STATE logic If you examine the data sheet for many microprocessors. This will not give rise to logical contradictions providing only one output is enabled at any given time.J R Smith 2 BINARY LOGIC AND ELECTRONICS 2. Their logic levels are approximately logical 0 corresponds to a voltage between 0 and VDD/2 logical 1 corresponds to a voltage between VDD/2 and VDD There also exist species of CMOS that have inputs that are compatible with TTL voltage levels when VDD = 5V. 2.Complementary Metal Oxide Silicon.TRI-STATE logic. They are almost always powered from a 5 V supply. These are usually powered with a voltage (VDD) in the range 3 to 18V .5 volts logical 1 corresponds to a voltage between 1.1 A 2-input AND gate with tri-state output.

Microprocessor Interfacing .J R Smith Multiplexed inputs Many devices designed for interfacing with microcomputers come with TRI-STATE outputs. input/ output input (direction = 1) output (direction = 0) direction Fig.v1.3 A buffer with tri-state output. The outputs of multiple devices can thus be connected to a common set of inputs.2. A A E Q 0 1 x E 0 0 1 Q 0 1 z x = doesn't matter z = high impedance or 'disconnected' Fig. 2. 5 .4 Schematic diagram of a circuit that allows a single connection to be used as either an input or an output according to the state of an input that specifies the direction of data transfer.consequently it is possible for a given pin to be either an input or an output at different times. 2.05 .2. 2.2.2 Schematic diagram showing how two devices with tri-state outputs can be multiplexed. The state of the outputs P6 and P7 will determine whether the data (D3-D0) from either device A or B will be present at the inputs P3-P0. Bidirectional ports These allow the transfer of data in either direction . DEVICE A D3 D2 D1 D0 E P3 P2 P1 P0 ATOM P6 P7 D3 D2 D1 D0 DEVICE B E Fig. and the desired device selected via its enable input.

the logic levels are different for increasing and decreasing voltages.3. 2.often very useful when interfacing to the real world.05 .2 The input characteristics for conventional TTL (left) and TTL with Schmitt trigger inputs (right). The ATOM has 2 types of input. 6 .1 Circuit for measuring input characteristics of digital inputs. Sometimes the input characteristics are deliberately given a degree of hysteresis .Microprocessor Interfacing . 2.J R Smith 2. whereas the 1->0 transition occurs around 1.3 Binary inputs and outputs The circuit shown below can be used to determine the range of input voltages that correspond to a logical ‘0’ and a logical ‘1’.v1. Inputs 8-15 are Schmitt trigger inputs. +5V ATOM 20k V P0 Fig.5V binary output 1 0 0 1 2 3 4 input voltage 5 binary output 1 0 0 1 2 3 4 input voltage 5 Fig.3. This feature gives the logic a degree of immunity against 'noise' on the input signal . Schmitt trigger inputs. The 0->1 transition occurs around 3V. Inputs 0 to 7 have conventional TTL levels.

On the ATOM the command SETPULLUPS can be used to connect a set of 10k internal pullup resistors on inputs 1 to 7 if required. There are many possibilities and we will limit discussion to some simple basic types. the button has been pushed. They can be wired using the same circuitry as conventional switches.05 .Microprocessor Interfacing . its value can be determined by examining the variable In0. The following code repeatedly tests if the input = 1.1.g. They are often available as 'normally closed' (nc) or 'normally open' (no). Loop1: IF In0 = 1 THEN Loop1 'program reaches this point when In0 = 0 7 .v1.J R Smith 3 BINARY INPUT TRANSDUCERS Some input transducers or devices inherently have only two possible states. Single Throw switch open: P0 = 0 switch closed: P0 = 1 SPDT Single Pole. 3. i. SPDT switches are sometimes called 'changeover'. Single Throw switch open: P0 = 1 switch closed: P0 = 0 SPST Single Pole.usually metal. A pin can be made an input via the INPUT command. Often the transition between states is important. All of the pins on the ATOM are inputs by default when a program starts. Double Throw switch up: P0 = 1 switch down: P0 = 0 Fig. These usually involve contact between two or more pieces of conductive material . push buttons. The diagrams below indicate how they could be connected directly to a digital input. e. The 10k resistor connected between an input and +5V is often known as a 'pull-up resistor' . Some switches only change their state momentarily. in this case P0 of the ATOM.e.1 Mechanical switches. and proceeds to the next instruction once the input = 0. +5V +5V +5V P0 10k P0 10k P0 SPST Single Pole.1 Circuits indicating how switches can be connected to a digital input. It thus effectively detects the 1-> 0 transition.it 'pulls' the input up to +5V when nothing is connected to that input. 3.

1. However it has the great advantage of needing only 5 input/output pins rather than the 16 required if the switches were connected directly.2 Multiplexed inputs The 16 switches on your BS2 development board are connected to the ATOM via a 16 input multiplexer.J R Smith +5V 10k P0 Fig 3.3 Diagram illustrating the multiple transition between states that occur when a switch changes states. The ATOM has a BUTTON instruction is designed to take care of switch debouncing.without multiplexing the ATOM could simultaneously determine the state of 16 switches. Determining the state of a switch is slower with this technique because the address of the desired switch must be supplied to the multiplexer before reading. In most applications you will need to ignore these initial bounces. 3. A 4 bit address determines which of the 16 inputs is connected to the multiplexer output which can then be connected to an ATOM input. Mechanical switch contacts usually 'bounce' for a few milliseconds after they make initial contact.05 . I can't get it to work! 8 . 3. Consequently in the circuits given above for SPST switches you will get a series of multiple rapid transitions between 1 and 0 as the contacts close.3 Switch debouncing. 1 0 Idealised Real world Fig.Microprocessor Interfacing .2 Circuit indicating how a push button can be connected to a digital input. Multiplexing also means that you can only determine the state of a single switch at a time . Question: What is the maximum number of switches that your ATOM could monitor with suitable external multiplexing hardware? 3.v1.

JAYCAR SM-1002 = $2. JAYCAR SM-1035 = $2. the mercury drop moves and an open circuit results. Can also get n.g. Quite cheap (e.45). normal temperature high temperature Thermal cutouts and thermal fuses These are switches that go open circuit (usually permanently) above a predefined temperature. normal temperature high temperature Mercury switches A small drop of mercury maintains electrical contact between two wires when the switch is in the upright position. Compact and cheap (e. Useful for detecting when an object is moved from the vertical position. If the switch is rotated.g.05 .c.v1.80). Widely used for burglar alarms. or changeover contacts. and the circuit is is broken. Transition is either predefined or adjustable.Microprocessor Interfacing . Cheap (e. the strip bends. Have several advantages including: (i) no mechanical contact required (ii) metal contacts are in an isolated environment – no corrosion Trembler switch The end of a thin springy wire is surrounded by a ring of wire. Two springy wires are held together by a waxlike material. Vibration or motion will make the springy wire vibrate and thus make momentary contact with the wire ring. but close when a magnet is near.g. The bi-metallic strip is composed of two metals with different coefficients of thermal expansion.J R Smith 3. or <TILT> on a pinball machine.g. Useful for detecting motion. Compact and relatively cheap ( e. When heated the lower layer of metal expands more than the upper layer. JAYCAR ST-3821/3/5/6 = $4. At a sufficiently high temperature the wax melts and the wires spring apart. JAYCAR ST-3800/4/8 = $2. usually based on a bi-metallic strip.80) glass container bead of mercury 2 wires Magnetic Reed Switch Constructed from magnetic wire sealed inside an evacuated glass enclosure.4 Some other switches Thermostats These are switches that change state at a given temperature. 9 .80). Contacts are normally open.

10 . The inputs 0 to 7 of the ATOM assume that Vin<1.5 V is a logical 1.6 Pseudo-binary inputs Many analogue transducers are often used in a pseudo-binary fashion. This is because it is often sufficient to know if an analogue parameter is greater or less than a certain value. 3.05 .5.5 V is a logical 0 and Vin > 1.J R Smith 3.Microprocessor Interfacing . Some examples use the strength of a magnetic field (Hall Effect switches) or the presence or absence of light (various optical switches).5V. LOOP: IF In6 = 0 THEN LOW P8 ELSE HIGH P8 ENDIF GOTO LOOP A potential divider can be used to increase Vin above 1.5 Non-mechanical switches Switches can operate without moving mechanical parts. Similarly an amplifier (see later) can be used for Vin <1.v1. The following code sets an output high or low depending upon the input voltage.

These operate by passing a current through a coil of wire. It is then possible for the small current from a microprocessor to switch much larger currents.05 . Consequently the current I flowing through the LED will be given by I = (5 . Solenoids are used to move mechanical components between two positions. The voltage across a typical LED is about 2 V. The value chosen for R will depend upon the particular relay and transistor used. The ATOM can only supply a small current from its outputs. The transistor effectively amplifies the current by a factor known as b. Solenoids often require relatively large currents for operation. heating elements. The resultant magnetic field then attracts a magnetizable material into the solenoid. The mechanics of the device are usually designed to ensure that the operation is bistable. BINARY OUTPUT TRANSDUCERS 4.1 Solenoids The most common bistable output transducers are variants of the solenoid. Examples include LEDS. etc.2) / R.2 Pseudo-binary outputs Many analogue transducers are often used as pseudo-binary fashion. or even a Darlington pair. R P0 LED The value of the resistor R can be changed to suit the particular LED. and this depends upon the voltage across it. then I = 14 mA.v1. +V solenoid or relay R P0 4. They also provide electrical isolation between the microprocessor and the controlled circuit.Microprocessor Interfacing .J R Smith 4. and so a single transistor. The function of the diode in the following circuit is to 'short out' any transient high voltages that can occur when the current through the inductance of the solenoid is removed. The intensity of light emitted by a LED depends upon the current flowing through it. 11 . They can also be used to operate levers. If R = 220 ohms. will often be used to amplify the current available from the outputs of the ATOM. typically b = 100. Relays are a solenoid in which the mechanical action is connected to a switch. They are often connected to a hydraulic valve to control water flow in domestic appliances such as dishwashers and washing machines.

v1. 5.1 Introduction Elapsed Time The information is encoded in the elapsed time between some events. The frequency of the binary signal carries the information Pulse Width Modulation (PWM) The frequency is kept constant.05 . etc. ENCODING INFORMATION BY VARIATIONS WITH TIME Up until now we have discussed simple binary operations . semaphore. Some nonelectronic examples include Morse code. Firewire (IEEE1394). etc. Bitstream The running average value of a stream of pulses carries the information. The answer is to consider the past history of binary operations -information can be encoded into the time variation of a binary signal. I2C.Microprocessor Interfacing . but the width of the pulse carries the information. The frequency and pulse widths will usually change to allow the desired waveform to be as precisely as possible. We will look at a couple of possibilities.J R Smith 5. USB.inputs and outputs have only two possible states. Some electronic examples are RS232. Digital Coding Can use the presence or absence of pulses in a specific sequence to carry the information. Surely a microprocessor can do more than this. Frequency Modulation. 12 . The encoding process is often called 'modulation'.

This can be very useful .J R Smith 5. 13 . This counter is stopped once the specified Pin is no longer has the value = State. The ATOM has a useful instruction that allows it to measure the resistance and/or capacitance of an external element . Once Pin is not in State. the maximum time that can be measured is 216 µs = 65.used to store the measurement of ELAPSED time. Before RCTIME is used.8 t For the circuit shown below.t / ln[(V-VFinal )/V] = . Then R can be calculated from C. or C can be calculated from R. +5V 1. This level must be maintained until the capacitor is effectively charged or discharged .t / ln[(5-1.typically 4t will be sufficient . Charging circuit V(t) = V [1 .05 . The time constant t = RC can be calculated from the time taken for the capacitor to charge or discharge. For your ATOM each unit stored in Variable corresponds to 1 µs. It can be used to measure the duration of short pulses.5 V on inputs 0-7 of the ATOM). 2 Elapsed Time One common technique involves measuring the time taken to charge or discharge a capacitor through a series resistor. Variable Pin .5V (the transition from 0->1 occurs around 1.the instruction RCTIME RCTIME Pin.the ATOM can measure the setting of potentiometer.v1. This instruction can be used to measure the time take to charge or discharge an external resistor / capacitor (RC) circuit. When RCTIME commences execution it starts an internal counter.5)/5] = 2. It can even be used to measure voltages. If Variable is a WORD.Microprocessor Interfacing . State. It can also measure the output of many transducers that change their resistance or capacitance in response to changes in the parameter of interest.see manual. the instruction terminates and the value of the counter is stored in Variable. A counter is started once RCTIME starts to execute.5 P0 C voltage R 0 RCTIME time Demonstration code for RC charging. Variable .specifies the desired state for measurement.specifies the pin to be used State . it is essential to use an OUT command to charge or discharge the capacitor to either 0 or 5 V.exp(-t/t)] charging t = . VFinal = 1.535 ms.

RCTIME 0. start to discharge capacitor i. V(t) = V exp(-t/t) discharging t = . both plates at +5V the length of the pause should be ≈ 4RC you should calculate it for your circuit now measure time to recharge 14 . .TIME .5V. a difference of 3.5/5] = 0.TIME .t / ln[1. . and so this approach is only suitable for voltages with a low source impedance. The process of charging the capacitor from the voltage source can drain a significant current. Measuring voltage It's also possible to use the RCTIME command to measure an unknown voltage by connecting components with known values of R and C to the unknown voltage.0. now measure time to recharge Discharging circuit A more accurate approach is to measure the time it takes to discharge a capacitor. This because then VInitial = 5 V and VFinal = 1.83 t +5V 5 P0 voltage C R 1.v1.e. you should calculate it for your circuit RCTIME 0.05 . the length of the pause should be ≈ 4RC . Temp var TIME HIGH 1 . start to discharge capacitor .5 0 RCTIME time Demonstration code for RC discharging.Microprocessor Interfacing .5 V. PAUSE 10 .t / ln[VFinal /V] = .J R Smith Temp var TIME LOW 0 PAUSE 10 .1.

5. The ATOM has an instruction for measuring the frequency of an input signal. period. low freq high freq medium freq Its often convenient to keep the duty cycle constant and equal to 0. The example below shows how three different frequencies might be encoded. The ATOM can measure the VCO frequency. An analogue voltage can be measured by connecting it to a voltage-controlled oscillator (VCO).05 . (The instructions DTMFOUT and FREQOUT generate pseudo-sine waves using a bit-stream technique). Because random noise and interference will generally affect only the amplitude of a signal. 15 .Microprocessor Interfacing . It can measure square wave inputs with frequency < 125 kHz (the pulse width must be ≥ 4 µs). The count is stored in ‘variable’. For example.v1.J R Smith 5. then counts the number of complete cycles (i. unknown voltage Voltage controlled oscillator ATOM (COUNT instruction) There is no single instruction for producing a square wave output at a defined frequency. COUNT pin. variable This instruction makes the selected pin an input. FM is sometimes used to send purely binary information in noisy environments – for example modems. and thus determine the analogue voltage. You will have to write your own if needed using PULSOUT or OUT. compare FM radio with AM (amplitude modulated) radio.3 Frequency Modulation (FM) The frequency of the binary signal carries the information. One example would be a guitar tuning meter. A VCO produces an output signal with a frequency that depends upon the input voltage.e a 0->1->0 or 1->0->1 sequence) during the defined period (in ms). FM is very tolerant of noise and interference. Sometimes the frequency itself of an input signal is important.

The average value of a PWM signal can also be used to carry information.4 Pulse Width Modulation (PWM) The frequency is kept constant.Microprocessor Interfacing . each instruction takes a few ms for execution.v1. The frequency remains constant.05 . However it is not particularly suitable for rapidly varying signals because it takes many complete cycles for the average value to change.25 The ATOM has no simple instruction for producing a ‘correct’ PWM signal with constant frequency.75 duty cycle = 0. The time tH + tL is kept constant. but is hard to predict accurately because extra time is taken by the For loops and the BRANCH instruction. but width of the pulse (tH) carries the information. tH tL tH tL duty cycle = 0. Demonstration code ‘ Program to demonstrate PWM Var VAR WORD Num VAR WORD Denom VAR WORD Num = 2 Denom = 5 Loop: For Var = 1 to Num HIGH 0 NEXT For Var = Num+1 to Denom LOW 0 NEXT BRANCH Loop You will later use a sort of PWM signal to control the angle of servomotors. The following example can produce pulses of different width depending upon the value of 'Num'. The interpreter is slow.5 tH tL duty cycle = 0. 16 .J R Smith 5. The duty cycle is defined as tH / (tH + tL). (The PWM instruction uses what we will call bitstream modulation). A better approach involves allowing the frequency to vary as well as the pulse width .see bitstream modulation.

Microprocessor Interfacing .5 Bitstream Modulation (BSM) The 'running average' value of a stream of pulses carries the information.J R Smith 5. Freq2 17 . 5 // gradually increase brightness NEXT FOR LED = 0 TO 255 PWM 0. ' Program to demonstrate BSM of LEDs ‘ Throbbing LEDs Loop VAR WORD LED VAR BYTE FOR Loop = 0 TO 255 FOR LED = 0 TO 255 PWM 0. 4µs and so are easier to filter out than true PWM). 5 // gradually decrease brightness NEXT NEXT END The FREQOUT instruction uses BSM to produce an output containing one or two pseudosinusoidal signals. FREQOUT Pin. The component pulses are quite short (often approx. LED. The output string of pulses can be filtered to produce different analogue voltages. The frequency and pulse widths will usually change to allow the desired waveform to be as precisely as possible.05 . The example below shows roughly how a triangle wave could be encoded. The PWM instruction effectively performs BSM. 255-LED. Freq1. It thus can act as a Digital to Analogue converter (DAC). Period. Demonstration code.v1.

The binary signal produced by the output nerve thus depends upon the nett effect of the various agonists and antagonists that are released by the ‘input’ nerves. an indicator that program has finished some part of program • Providing feedback (usually via frequency) about some measurement that does not require visual interaction. The biological equivalents of logic gates in the nervous system are implemented by a cunning temporary transition to analogue chemical transmission.e.05 .60 mV in a few ms. it releases small amounts of a chemical messenger into a small region between the nerves – the synaptic cleft. .Microprocessor Interfacing .v1.e.).physiologists call this Fechner's law). Nerves connect to each other at specialised junctions called synapses. This instruction is very useful for generating audio output that is useful for: • Warning ‘beeps’ or ‘pings’ • Provideing feedback on program status.e.7 Information coding in Biology (not required for exam) Frequency modulation is used in the human nervous system. The rate at which nerve impulses occur at the ‘output’ nerve is determined by the concentration of chemical messengers in the synaptic cleft.. • Fun !! (Why not program your ATOM to play ‘Stairway to Heaven’ ? ) 390R P0 SPEAKER 5..J R Smith Sound generation The stream of pulses produced by PWM or FREQOUT needs to be filtered to remove the high frequency components associated with the rapid 0->1 and 1-> 0 transitions. Information about external stimuli is generally carried by the rate at which these nerve impulses occur (usually the rate is approximately proportional to the logarithm of the strength of the stimulus .g. 18 . when a clock is involved) SHIFTOUT – used to send synchronous serial data (i.60 mV to approx + 50 mV and back to . When the nerve impulse reaches the end of an ‘input’ nerve. SERIN – used to read in asynchronous serial data SEROUT – used to send asynchronous serial data SHIFTIN – used to read in synchronous serial data (i. A simple RC filter is usually OK (see manual). USB.6 Coding Can use the prescence or absence of pulses in a specific sequence to carry the information. Firewire (or IEE. Try PHYS2410 if you would like to know more about this 'Biologic'. when a clock is involved) 5. Some examples are RS232. There are two types of chemical messenger – agonists (these tend to increase the firing rate) and antagonists (these tend to decrease the firing rate). Nerve impulses are binary pulses that switch from approx . The ATOM has some instructions designed to extract information from pulse sequences.

e.e.3 Using analogue transducers as binary transducers Often we just want to know if an analogue signal is greater or less than a certain value.2 Comparators Basically a high gain amplifier with analogue inputs and a digital output.VB) This is a differential amplifier. Used to determine whether VA is greater or less than VB VA VB + _ Vout If VA < VB Vout = 0 If VA > VB Vout = 1 6.v1. i.5 V is a logical 0 and Vin > 1. i.Microprocessor Interfacing . it amplifies the difference between the two inputs.1 Amplifiers VA VB + _ Vout Vout = Gain (VA . the output voltage levels are compatible with standard logic. So by amplifying the input voltage Vin with a gain of G. 6.05 . The inputs 0-7 of the ATOM assume that Vin<1. and subtracting a reference voltage VREF it is possible for the 0 –> 1 transition at an ATOM input to correspond to any desired input voltage.J R Smith 6 BASIC ANALOGUE COMPONENTS 6. Amplifier gain = G VIN Comparator GVIN + _ +5V + _ GV _ VREF IN P0 VREF 19 .5 V is a logical 1.

However very precise values require averaging a large number of pulses.000 km and takes perhaps 20 hours flying time. only 20% more than the distance between seats in economy class.1 How many bits? How many bits are required to produce a voltage with sufficient precision? n bits can encode 2^n possible states n= # of states precision 8 256 ~0.06 ppm ppm = parts per million 24 bit precision is incredibly precise.1% 16 65. 24 bit precision would require knowing your absolute position with respect to Sydney with an error of only 1 m.2 Bitstream Could use the PWM instruction to produce a desired DC voltage.5% 10 1024 0. However in many situations we require higher precision.536 ~10^6 16 ppm 1 ppm 20 24 ~16x10^6 0.v1. PWM Pin. 7. whilst minimising noise in the output signal. Vout = (Duty / 255) x 5 V For time varying output voltages. Duty. Some examples can reach 23 bit precision with sample rates below 100Hz.05 .J R Smith 7 DIGITAL TO ANALOGUE CONVERSION We have already seen how an ATOM can output an analogue voltage using PWM or FREQOUT.Microprocessor Interfacing . A plane journey from Sydney to Rome is about 16. very sophisticated algorithms can be used to produce the closest match to the desired waveform. 7. Cycle If output is smoothed with a suitable filter. 20 . These will require extra components external to the ATOM. Bitstream can be very precise because there are essentially no components that need to be calibrated (see later). faster response and multiple outputs. so they are relatively slow.

3 Binary-weighted resistors R1 = 1.Microprocessor Interfacing . 1 mA 2 mA Load 4 mA 8 mA By closing the appropriate switches we can get any current from 0 to 15 mA. The problem: for an n-bit DAC we need n resistors that cover a range from R to 2nR. If each switch were controlled by an appropriate bit.25k S3 R2 = 2. they will slowly experience different shifts in value with temperature variations and aging.v1. 7. each resistor must have a tolerance of at least ± R. where R is the value of the resistor with the lowest value. 21 . Laser trimming of resistors on the same substrate can achieve suitable precision for n <= 16. Another approach generates binary weighted currents by using digital logic to produce pulse sequences with different average values. A functional DAC would generally use some additional transistors or op-amps.05 . In this situation there is nothing to go out of calibration. For n > 10 this is very difficult to achieve.3 An idealized circuit in which the load resistance is assumed to be small in comparison with the resistors R0 to R3. The current can easily be converted into a voltage if required. the current I could be described by I = value of 4-bit word in mA.5k S2 R1 = 5k S1 R0 = 10k S0 Fig. Even if discrete resistors are initially sufficiently precise.J R Smith 7.

and consequently should have similar variations with temperature and time.05 . 16i 8i 8i 2R 8i R 4i 4i 2R 4i R 4i 2i 2i 2R 2i R 2i i 2R i i 2R The resistors can be manufactured on the same substrate.4 R-2R Ladder A cunning method of obtaining binary weighted currents that only needs one or two values of resistor.J R Smith 7. 22 .Microprocessor Interfacing .v1.

23 . digitising video or digital oscilloscopes).Microprocessor Interfacing .A1) is the binary address of the highest asserted input (D0-D3). We now discuss Analogue to Digital converters (ADC). The oscilloscopes in the lab have 8 bit. 8.an n-bit converter requires 2n-1 comparators.J R Smith 8 ANALOGUE TO DIGITAL CONVERSION Our ATOM now needs a method of measuring an analogue voltage using of external circuitry.05 . only requires the time for a comparison plus some digital logic.v1. very high speed situations (e. Becomes prohibitive for n>10. They are usually used for low resolution. Advantages: very fast. The output of the priority encoder (A0.g. Vin 3V + _ D3 Priority encoder 2V + _ D2 A1 A0 1V + _ D1 +5V D0 The above circuit is a 2-bit flash converter. Disadvantages: expensive . 1 GHz converters.1 Parallel or Flash This involves simultaneous comparison with multiple voltage standards.

Again the comparator output will indicate if this bit should remain set at 0 or 1. relatively cheap. (2) the comparator output now indicates whether VDAC<Vin or VDAC>Vin. 24 . only requires one comparator + DAC Disadvantages: slower than flash because an n-bit conversion requires n comparison steps. Slower versions (200 kHz) can have 16-bit precision.05 .2 Successive Approximation This involves a single comparator in sequential comparisons with a voltage standard that is adjusted according to prior comparisons. Can operate at speeds approaching 10 MHz at 12 bits.v1. (3) if VDAC<Vin then msb = 0 or if V DAC>Vin msb = 1 for the rest of the conversion (4) now set the next significant bit = 1. all other bits = 0. (5) repeat for all the bits of the DAC.Microprocessor Interfacing . Vin VDAC + _ Successive approximation register DAC n end of conversion EOC Start The 'start' command initiates a binary search sequence controlled by the Successive Approximation Register (SAR) (1) The most significant bit (msb) of the DAC = 1. Advantages.J R Smith 8. (6) assert the EOC output (End Of Conversion) (6) the digital approximation to Vin is then the final code sent to the DAC.

v1. conversion rates are typically a few Hz. (3) The capacitor then starts to charge up. 25 . so VC increases linearly with time. The comparator output then changes state and stops the counter. the integration process can remove noise and interference.J R Smith 8. (2) The first step is to open the switch and start the counter. (5) The count will then be proportional to Vin.05 . capable of very high precision . (4) Eventually VC = Vin.3 Integrating This measures the time taken for a known capacitor to charge using a known current until its voltage is equal to the unknown voltage. Advantages: very cheap. The current I is constant. Disadvantages: very slow. Vin VC I C + _ stop Binary counter start Start (1) Initially the counter is set to zero and the switch is closed.20 bits or more. so VC = 0.Microprocessor Interfacing .

J R Smith 8. can perform very sophisticated filtering to remove noise and interference.v1.Microprocessor Interfacing . 26 .00 if V3 = 0 The circuit operates at a very fast clock rate.00 if V3 = 1 or VDAC = -1. V1 = Vin . Advantages: cheap.VDAC The integrator output V2 = sum of V1 over many cycles. Consequently a stream of pulses is produced whose average value reflects the value of Vin. then VDAC will be negative and the output of the integrator will be increased. If the value in the integrator is positive the DAC output will be positive and this will be subtracted from Vin. very precise (a one bit DAC has nothing to go out of calibration). Similarly if V2 is negative.4 Delta .05 .Sigma Differential amplifier Comparator Integrator Vin VDAC + _ V1 V2 + _ V3 bitstream output 1-bit DAC The output of the differential amplifier . This then undergoes sophisticated filtering via a decimation filter to become the final serial output. V3 is the output of a comparator: if V2>=0 V3 = 1 else V3 = 0 VDAC = +1. These ADC are increasingly used for digital audio (20 bit resolution at 96 kHz) and very low frequency measurements (23 bits at 20 Hz). Disadvantages: The clock rate must be very much higher than any changes in Vin. reducing the output of the integrator.

1. so amplification will often be required.J R Smith 9 TRANSDUCERS Transducers are devices that convert one form of energy into another.Microprocessor Interfacing .05 . It is possible to get NTC (negative temperature coefficient) or PTC (positive temperature coefficient) devices. This is because a microprocessor or microcontroller can conveniently measure electrical signals via an ADC and output electrical signals via a DAC. To reduce the number of temperature dependent junctions between different metals. A typical value might be 10 kW with a variation of . +5V C ATOM (RCTIME instruction) Thermistor +5V R Thermistor ADC ATOM (SHIFTIN instruction) 27 .v1. The voltage is quite small <100 µV/oC. 9.1 TRANSDUCERS FOR TEMPERATURE 9. A voltage is generated between them that varies with the temperature of the junction.1. Their resistance is usually a non-linear function of temperature.2 Thermistor These use semiconductor materials or oxides with a resistance that varies with temperature.1 Thermocouple A thermocouple is made when two dissimilar metals are placed in contact.1kW/oC. A special integrated circuit can also be used to provide the correct reference voltage. Usually we use them to covert various physical parameters to and from electrical signals. Unknown + _ Reference ADC ATOM (ADCIN instruction) 9. the unknown temperature is often measured with respect to another thermocouple that is kept at a constant reference temperature (usually at 0oC).

2.1. (For a silicon pn junction it is approx.e. they are also sometimes used to measure very low temperatures).5 Resistor When a current I passes through a resistor R.J R Smith 9.6 Peltier (thermoelectric) module. When a voltage V is maintained across a resistor R. 9.1.v1.1. If the polarity of the current is reversed. but calibration is difficult. the Joule heating produced is given by I2R. Temperature controlled oscillator ATOM (COUNT instruction) 9. In these modules a flow of electric current provides the necessary energy for a flow of heat against its thermal gradient (i. heat is extracted from a cold surface and passed to a hot surface). ATOM (PWM instruction) R C amplifier The load in the above circuit could be a resistive heating element or a thermoelectric module.4 Temperature dependent oscillator This transducer uses an oscillator that is sensitive to temperature changes. +5V R ATOM (SHIFTIN instruction) ADC 9.1. (Because the value of a resistor changes slightly with temperature. In theory one can use any diode. the direction of current flow is also reversed.2 mV/oC). the Joule heating produced is given by V2/R. These allow a computer to control the amount of cooling.05 . load 28 .3 Semiconductor junction The voltage across a semiconductor junction varies with temperature.Microprocessor Interfacing . Temperature is measured by counting its output frequency. Consequently it is easier to use specialised integrated circuits that provide either a voltage or a current that is linearly proportional to the temperature.

2.J R Smith 9.and n. but generally requires some amplification.05 .3 Phototransistor The current generated by the electron-hole pairs is now amplified via normal bipolar transistor action. This leaves the absence of an electron in the valence band . with a very low concentration of mobile charge carriers is formed across the region of contact. and can be used to produce a current or a voltage depending upon the external circuitry. They can be connected in the same fashion as a thermistor (see section 9. particularly CdS or CdSe. ADC Solar cell ATOM (SHIFTIN instruction) 29 .1 Light Dependent Resistor (LDR) When a photon of light of a suitable wavelength falls on a semiconductor.Microprocessor Interfacing . This produces a shift in the voltage-current characteristics of the diode.2 Photodiode These are made by placing p.really the absence of electrons). _ + R ADC ATOM (SHIFTIN instruction) 9. The n-type has an excess of mobile negative carriers (electrons) and the p-type an excess of mobile positive carriers (holes . Used mainly to detect the presence/absence of light. An LDR usually consists of a thin. folded. The current increases linearly with illumination. These would normally quickly recombine.1. There are some materials. and a high resistance 'depletion layer'.2.type semiconductor materials in contact and so forming a pn diode.2) 9. A strong electric field is produced across this depletion layer because of the charge movement associated with its formation. Electrons at the edge of the n-region will be attracted to the holes at the edge of the p-region. and vice versa. Typical values might be 1-10k in dim light and 100 ohms in bright light. but the strong electric field across the depletion layer rapidly separates the electron-hole pairs so that they can't recombine. an electron can be excited from the valence band into the conduction band. The resistance of the material then decreases. where the decrease in resistance can be greatly amplified. strip of such material. and the original resistance will be restored. When photons strike a depletion layer they can generate electron-hole pairs. They combine. Their response time is quite slow (typically hundreds of milliseconds).2 TRANSDUCERS FOR LIGHT 9.v1.2. Eventually an electron and a hole will meet and recombine.4 Solar cell These are p-n junctions that are designed to optimise power production.2. 9.usually referred to a positively charged 'hole'.

while the plate separation varies. Can be used in a similar fashion to resistive heating elements and thermoelectric modules.3 Dynamic Speaker Current is sent through a coil of wire (the voice coil) that is placed in a magnetic field. Usually made from a coiled piece of resistance wire in an evacuated bulb.1.3.2. The corresponding voltage across the diode is usually in the range 1. Because of thermal inertia they have a relatively poor frequency response. Capacitor and condensor microphones usually require an external source of charge (48V on standard audio systems).2. Elecret. Magnet Speaker coil Voltage Speaker cone 30 .1. 9.J R Smith 9. Voltage + - Voltage 9. (Air would allow the hot wire to oxidise).Microprocessor Interfacing . The voltage is small (mV or less) and will require amplification before the ADC.v1.2.3.5 to 2. thus producing a voltage.5 volts. Elecret microphones use permanently charged plastic electrodes (electrets). monochromatic light when current flows through them.5 Incandescent lamp (Light Emitting Resistor) If a material is made sufficiently hot it will start to emit visible light. the voltage across the plates will change. Because the charge remains constant. (see section 9. Dynamic microphones The sound pressure wave acts on a diaphragm that moves a coil of wire in a magnetic field.6).3 TRANSDUCERS FOR SOUND 9. capacitor and condensor microphones Sound pressure moves one plate of a very delicate charged capacitor.05 .3.6 Light Emitting Diode These are semiconductor diodes that emit approx. (Shorter wavelengths require higher voltages). Magnet Coil of wire Diaphragm 9. 9. A diaphragm is connected to the voice coil so that a varying current causes motion of the diaphragm that moves the surrounding air.

A voltage is then generated between the inside and outside that depends upon the internal and external proton concentrations.5 Electrostatic Loudspeaker A high voltage is place between two conducting plates.Microprocessor Interfacing .made from terbium. When a current flows through the coil. Causes relative motion of plates that in turn set air in motion. magnetostrictive material Voltage 9.1 pH electrode The electrode tip is made from special glass that is permeable to protons. This in turn vibrates the object to which it is attached and that produces the sound. Their output impedance is very high.4. and thus set air in motion. One example is the 'Soundbug'.4 TRANSDUCERS FOR CHEMICAL CONCENTRATION 9. iron and dysprosium) is surrounded by a wire coil. basically a capacitor microphone in reverse.3.3. 9. A rod of magnetostrictive material (Terfenol . piezo-electric material Speaker cone voltage 9.J R Smith 9. suction cap + _ high input impedance amplifier known [H] unknown [H] proton permeable glass 31 .6 Magnetostrictive transducer Magnetostrictive materials contract in a magnetic field.3. the resulting magnetic field causes the Terfenol to change shape.4 Piezoelectric speaker A voltage across a piece of piezoelectric material causes it to flex. usually > 109 ohms.05 .v1.

10.J R Smith 10 INTERACTION SCHEMES 10. When a specific condition occurs on one of these lines. Disadvantages: Fastest response requires that the microprocessor continually monitor the status flags leaving no time for other tasks. Disadvantages: Requires special hardware Increased latency Harder to program RESETS All microprocessors have another type of interrupt . The subroutine that you wish to be executed (the interrupt handler) should be located at this address. for example checking if a key has been pressed on a computer keyboard. Instruction are available that tell the microprocessor to either ignore or to pay attention to interrupts. If a very fast response is not required.e.1 Programmed interaction or polling In this approach the computer program continually monitors various status flags that indicate the state of real world variables.the RESET.Microprocessor Interfacing .05 . requires minimal hardware Gives the fastest response to an external event. The microprocessor can then always start execution from a well-defined state. When this subroutine has finished processing. then the microprocessor can do other tasks whilst only checking the status flags occasionally. Serious microprocessors also have methods of specifying different priorities for different interrupts. minimum latency.v1. Advantages: Microprocessor can perform useful tasks whilst waiting for interrupt. Advantages: Simple to program. i. 32 .2 Interrupts This involves the external hardware having access to the internal workings of the microprocessor via specific ‘interrupt’ logic lines. the microprocessor acts as if the next instruction in your program was Gosub address This can be thought of as a ‘pseudo-instruction’. the program continues operating from the line in your code after the one it was execution when the interrupt occurred.

The device can then take control of the busses and rapidly transfer data.v1. Also useful for detecting situations when the mains power fails. and any important parameters saved in EEPROM. A ‘LOW BATTERY’ warning can be initiated. 33 . When ready the microprocessor can relinquish control of the busses. 10. or when power is removed and restored Brown out reset – ONBOR: This occurs when the power supply voltage falls below 4. Reset pin reset – ONMOR: This occurs when a 0->1 transition occurs at the ATN/RES pin. Advantages: The microprocessor is free to perform other tasks Very fast data transfers. Requires more complex hardware – the device must be smart enough to become ‘bus master’ Requires more complex programming.05 .1V. particularly when large blocks of data are moved. This can be useful for situations that derive their power from batteries.Microprocessor Interfacing . Often triggered by a ‘RESET’ button.3 Direct Memory Access (DMA) The device that wishes to perform DMA must first undergo an arbitration process with the microprocessor to become the ‘bus master’. Disadvantages: High latency can make it inefficient for transferring small amounts of data. The ATOM28 has no DMA capability (there are no external busses that it can release to another device).J R Smith The ATOM has three types of RESET Power on reset = ONPWR: This occurs when power is first applied to the ATOM.

An ideal computer would use only SRAM.05 . The charge on these capacitors leaks away quite rapidly – in a couple of ms. EEPROM = Electrically EEPROM: The charge on the internal ‘capacitors’.e. A quartz window is provided for this purpose. A copy of sections of the main memory is then kept in caches built using SRAM that can be accessed rapidly by the microprocessor. However it is significantly faster. Cannot be reprogrammed. The common approach is to use SRAM within the microprocessor and DRAM for the main memory. RAM = Random Access Memory: nowadays this refers to read/write memory that ‘forgets’ when the power is removed. SRAM = Static RAM: This memory is implemented using flip-flops. Consequently the memory is ‘refreshed’ by internal logic every ms or so. and hence the stored truth table can be altered by an applied voltage. There are two main species: Dynamic and Static. 34 . This involves using an externally applied current to melt small internal wire links.1 Types of memory ROM = Read Only Memory: The desired truth table is permanently programmed by the manufacturer of the integrated circuit. FLASH = a type of EEPROM that can be easily reprogrammed. and so is more expensive and occupies more space.Microprocessor Interfacing . EPROM = Erasable PROM: The desired truth table is programmed electrically by the user as the presence of charge on internal ‘capacitors’. however this would be too expensive for most situations. DRAM = Dynamic RAM: This memory is implemented via charge on tiny ‘capacitors’. ultraviolet light.J R Smith 11 SOME ASPECTS OF COMPUTER ARCHITECTURE 11. The data can be erased by exposure to photons of sufficiently high energy. Static vs Dynamic Static memory requires more transistors per bit than dynamic.v1. i. PROM = Programmable ROM: The desired truth table is programmed by the user.

Sign up to vote on this title
UsefulNot useful