Connecing he compuaion capabiliies of a microconroller o exernal signals Transforming variable values ino volages and vice-versa Digial and analog Issues How many signals can be conrolled? How can digial and/or analog inpus be used o measure differen physical phenomena? How can digial and/or analog inpus be used o conrol differen physical phenomena? To conrol or reac o he environmen we need o inerface he microconroller o peripheral devices Microconroller may conain specialized inerfaces o sensors and acuaors Things we wan o measure or conrol ligh, emperaure, sound, pressure, velociy, posiion Sensors e.g., swiches, phooresisors, acceleromeers, compass, sonar Acuaors e.g., moors, relays, LEDs, sonar, displays, buzzers Map analog inpus o a range of binary values 8-bi A/D has oupus in range 0-255 Wha if we need more informaion? linear vs. logarihmic mappings larger range of oupus (16-bi a/d).1 1 10 100 1000 analog 0 64 128 192 255 digial.1 1 10 100 1000 analog! Usually use an op-amp circui Ofen found as a pre-amplifier o ADC circuiry Simple circui o compuer naural logarihm Map binary values o analog oupus (volages) Mos devices have a digial inerface use ime o encode value Time-varying digial signals almos arbirary resoluion pulse-code modulaion (daa = number or widh of pulses) pulse-widh modulaion (daa = duy-cycle of pulses) frequency modulaion (daa = rae a which pulses occur) V IN V OUT = log e ( V IN ) V V V 1
#$%& $ ( $%& $ &) Pulse a digial signal o ge an average analog value The longer he pulse widh, he higher he volage period Pulse-widh raio = on on period average value Mos mechanical sysems are low-pass filers Consider frequency componens of pulse-widh modulaed signal Low frequency componens affec componens They pass hrough High frequency componens are oo fas o figh ineria They are filered ou Elecrical RC-neworks are low-pass filers Time consan (τ = RC) ses cuoff frequency ha separaes low and high frequencies " ' %)+) -&. %)+) Rear wheel conroller/ani-lock brake sysem Normal operaion Regulae velociy of rear wheel Brake pressed Gradually increase amoun of breaking If skidding (fron wheel is moving much faser han rear wheel) hen emporarily reduce amoun of breaking Inpus Brake pedal Fron wheel speed Rear wheel speed Oupus Pulse-widh modulaion rear wheel velociy Pulse-widh modulaion brake on/off brake pedal pressed fron wheel velociy rear wheel velociy micro conroller brake on/off move rear wheel *, /.0 1+)2 #3 $ Check if brake pedal pressed or inerrup brakepressed = read (brakepedalpor) Turn brake on/off wrie (brakepor, onoff) Move rear wheel wrie (rearwheel, onoff) brake pedal pressed fron wheel velociy rear wheel velociy GPIO por micro conroller GPIO por brake on/off move rear wheel Sofware mus repeaedly check Brake pedal por How ofen? Need o make sure no o forge o do so (use imer) Use auomaic deecion capabiliy of processor Connec brake pedal o inpu capure or exernal inerrup pin Inerrup on level change Inerrup handler for brake pedal brake pedal pressed GPIO por micro conroller 2
#$%& $ +) /)$ $ To pump he brakes gradually increase he duy-cycle ( on ) unil car sops Use imer o urn brake on and off Apply brake Se imer o inerrup afer on ime Disengage brake Se ime o inerrup afer off ime Repea How do we ell which inerrup is which? se imer o go off a each edge sar imer running /)$ $1 42 Change value of on ime o change analog average average oupu = ( on + off ) / ( period ) How do we decide on he period of he pulses? Using wo imers One o se period (auo-reload) One o urn i off a he righ duy cycle Need o deermine he rear wheel velociy Use sensor o deec wheel moving Deermine speed of a bicycle Aach baseball card so i pokes hrough spokes Number of spokes is known Coun clicks per uni ime o ge velociy Baseball card sensor is a shaf encoder click! bike wheel se imer o go off a each edge baseball card sar imer running - Insead of spokes we ll use black and whie segmens Black segmens absorb infrared ligh, whie reflecs Coun pulses insead of clicks How many segmens should be used? More segmens give finer resoluion Fewer segmens require less processing Tradeoff resoluion and processing wheel infrared ligh emier deecor pulse " ' 3
5 Use inerrup on GPIO pin Every inerrup, incremen couner Use imer o se period for couning When imer inerrups, read GPIO pin couner velociy = couner known disance per click / judiciously chosen period Rese couner Pulse accumulaor funcion Common funcion Some microconrollers have his in a single peripheral device Basically a couner conrolled by an ouside signal Signal migh enable couner o coun a rae of inernal clock o measure ime Signal migh be he couner s clock o measure pulses ATmega16 has exernal clock source for imer/couner Microconrollers come wih buil-in I/O devices Timers/couners GPIO ADC Ec. Someimes we need more... Opions Ge a microconroller wih a differen mix of I/O Ge a microconroller wih expansion capabiliy Parallel memory bus (address and daa) exposed o he ouside world Serial communicaion o he ouside world *,.0 # The are never enough I/O pors Techniques for creaing more pors por sharing wih simple glue logic decoders/muliplexors memory-mapped I/O por expansion unis Direcion of pors is imporan single direcion por easier o implemen iming imporan for bidirecional pors If signals all in same direcion and have a separae guard signal, hen able o share wihou glue logic Example: connec 5 LCD displays o microconroller can share connecions o RS, RW, and DB bu no E changes on E affec display mus guaranee only one is acive 6!$ 7 Conflic on device signals (e.g., one signal affecs boh) soluion is o inser inervening regisers ha keep signals sable regisers require enable signals which now need pors as well Encode n single-bi device pors using log n bis of a conroller por enabled decoder: one-ho, inpu-only device pors regisered decoder: inpu-only (bu no one-ho) device pors muliplexor: oupu-only device pors µ C device A device B µ C D EN D EN Q Q device A device B µc log(n) bis enable enabled decoder n one-ho signals µc daa µc log(n) bis n signals enable n signals log(n) muliplexor selec bis D EN n regiser selec lines regisered decoder Q 4
8%.0 8%.0 Address bus selecs device Daa bus conains daa Pariion he address space Assign memory-mapped locaions Sofware loads read from he device sores wrie o he device Can exploi unused bis for device inpu-only pors # 7$ $ & Problem of por shorage so common por expansion chips exis Easily connec o he microprocessor Timing on pors may be slighly differen May no suppor inerrups Exploi specialized funcions (e.g., UART, imers) Aemp o connec direcly o a device por wihou adding inerface hardware (e.g., regisers), ry o share regisers if possible bu beware of unwaned ineracions if a signal goes o more han one device If ou of pors, mus force sharing by adding hardware o make a dedicaed por sharable (e.g., adding regisers and enable signals for he regisers) If sill run ou of pors, hen mos encode signals o increase bandwidh (e.g., use decoders) If all else fails, hen backup posiion is memory-mapped I/O, i.e., wha we would have done if we had a bare microprocessor " ' %+ 5%$.0 %+.0 & Suppose we waned a 64-bi I/O por If EN is rue, hen we have an oupu pin If EN is false, hen we have an inpu pin We need 8 8-bi regisers o sore/wrie he 64 bis Selec he EN addresses o be $...000 o $...007 Selec OUT addresses o be $...010 o $...017 Read 15h bi load value a address $...011 (2nd se of OUT regs) logical AND wih 0x80 bi posiion 7 of resul is 15h bi Wrie he 47h bi read OUT regiser a $...015 se bi posiion 7 o desired value ( or wih 0x80) sore in $...015 load EN regiser a $...005 se bi o oupu sore value back o $...005 *, 5
7 #(89 7 #( 868 Design a sysem o conrol a digial Soluion: design a PWM uni! "#$ '&! "#$! "#( %! "#$ %&! "#$ %& '&! "#( % 7 #( 8 & 7.0 // in iniializaion code Wrie off o onoff regiser // do some suff // se up PWM Repea for each moor Wrie hightime and period regisers Sonar range finder Compass IR proximiy deecor Acceleromeer Brigh LED // urn moors on Repea for each moor Wrie on o he onoff regiser // more suff #,, Uses ulra-sound (no audible) o measure disance Time echo reurn Sound ravels a approximaely 343m/sec need a leas a 34.3kHz imer for cm resoluion One simple echo no enough many possible reflecions wan o ake muliple readings for high accuracy Commonly found on old Polaroid cameras, now a frequenly used par in mobile robos Transducer (gold disc) charged up o high volage and snapped disc says senisized so i can deec echo (acs as microphone) Conroller board high-volage circuiry o prepare disc for ransmiing and hen receiving 6
#,,1 42 Only need o connec wo pins o microconroller INIT - sar ransmiing ECHO - reurn signal Some imporan informaion from daa shee INIT requires large curren (greaer han microconroller can provide add exernal buffer/amplifier) ECHO requires a pull-up resisor (deermine curren ha needs o flow ino microconroller pin - size resisor so proper volage is on pin " Four compass direcions (each has hree pins) One-ho/wo-ho encoding one-ho for N, E, S, W wo-ho for NE, SE, SW, NW #!"Ω $ Ω ' # 1 42-7 Deecing a change in compass direcion 4 bis change from 0001 o 0011 o 0010 o 0110 o 0100 Always alernaing beween one bi on and wo bis on Pariy ree can deec difference beween one and wo bis being assered XOR ree of four bis (one TTL SSI package) Oupu mus change a leas once for every change in orienaion Use inerrups o deec changes N E S W e.g., NE E SE 1100 0100 0110 0 1 0 * Oscillaor mus be se o mach deecor, -:$$ #7 urn on emier sleep for 600us val_on = read deecor urn off emier sleep for 600us val_off = read deecor reurn ( val_on & ~val_off ) imer goes off wake imer goes off wake Mosly in main urn on emier se imer sleep imer goes off val_on = read deecor urn off emier rese imer sleep reurn ( val_on & ~val_off ) wake Using inerrup handlers imer goes off (again) val_off = read deecor 7
8 7 Always sending ou IR Deecor drives LED (guaraneed o mach frequency) Micro-elecro-mechanical sysem ha measures force F = ma (I. Newon) Measured as change in capaciance beween moving plaes Designed for a maximum g-force (e.g., 2-10g) 2-axis and 3-axis versions Used in airbags, lapop disk drives, ec. 8
( +$.< = /$ % Average over ime smoohing Sofware filer like swich debouncing Take several readings use average for Tb and Tc or heir raio Running average so ha a reading is available a all imes e.g., updae running average of 4 readings curren average = ¾ * curren average + ¼ * new reading Take readings of boh Tb and Tc o be exra careful Tc changes wih emperaure Usually can do Tc jus once Filer capaciors limied noise frequency bandwidh limiing *,!;,0$ $!;,0 Acceleromeer duy cycle varies wih force 12.5% for each g R SET deermines duraion of period A 1g duy-cycle will be 62.5% (37.5%) Sensiiviy (maximum duy cycle change per degree) is highes when acceleromeer is perpendicular o graviy #( 8$ /! How big a couner do you need? Assume 7.37MHz clock 1ms period yields a coun of 7370 This fis in a 16-bi imer/couner Should you use a prescaler for he couner? Bi precision issues unsigned in posiive; unsigned in period; unsigned in pos_duy_cycle; BAD: pos_duy_cycle = posiive/period; BAD: pos_duy_cycle = ( posiive * 1000 ) / period; OKAY: pos_duy_cycle = ( (long) posiive * 1000 ) / period; Easy o conrol inensiy of ligh hrough pulse-widh modulaion Duy-cycle is averaged by human eye Ligh is really urning on and off each period Too quickly for human reina (or mos video cameras) Period mus be shor enough (< 1ms is a sure be) LED oupu is low o urn on ligh, high o urn i off Acive low oupu 9
! 6 #(8 Varying PWM oupu volaile uin8_ widh; /* posiive pusle widh */ volaile uin8_ delay; /* used o slow he pulse widh changing */ SIGNAL (SIG_OVERFLOW2) { if(delay++ == 20) { OCR2 = widh++; delay = 0; } } in main (void) { /* mus make OC2 pin an oupu for he PWM o visible */ DDRD = _BV(DDD7); /* use Timer 2 FasPWM and he overflow inerrup o updae duy-cycle */ TCCR2 = _BV (WGM21) _BV (WGM20) _BV (COM21) _BV(COM20) _BV(CS21) _BV(CS20); TIMSK = _BV (TOIE2); /* seup iniial condiions */ delay = 0; /* enable inerrups */ sei (); for (;;) { ; /* LOOP FOREVER as he inerrup will make necessary adjusmen */ } reurn (0); } 10