• No results found

Switch฀Bounce฀and฀Sealing฀Current

Beyond฀assuring฀that฀an฀open฀switch฀is฀correctly฀read,฀a฀pull-up฀resistor฀provides฀the฀switch฀with฀enough฀

current฀to฀reliably฀operate.฀In฀addition฀to฀the฀mechanical฀wiping฀action฀of฀switch฀contacts,฀a฀small฀DC฀cur-rent฀greatly฀assists฀in฀maintaining฀reliable฀conduction฀between฀moving฀switch฀contacts.฀And,฀if฀the฀switch฀

is฀connected฀through฀wiring฀with฀mechanical฀splices฀and฀crimp฀or฀screw฀pressure฀terminals,฀the฀current฀

helps฀clean฀oxidizing฀film฀from฀the฀mating฀conductors.฀From฀telephone฀terminology,฀we฀often฀refer฀to฀this฀

as฀a฀“sealing”฀current฀or฀a฀“wetting”฀current.฀Hence฀switch฀circuits฀are฀often฀referred฀to฀as฀wet—carrying฀a฀

current฀well฀in฀excess฀of฀that฀necessary฀to฀pull฀an฀input฀pin฀high฀or฀low—or฀dry—carrying฀only฀a฀minimal฀

signal฀current.฀For฀most฀mechanical฀switches,฀unless฀we฀are฀trying฀to฀save฀power฀such฀as฀in฀a฀battery฀pow-ered฀system,฀select฀the฀pull-up฀resistor฀to฀supply฀5฀to฀20฀mA฀current฀flow฀through฀the฀switch฀when฀closed.฀

For฀5฀V฀systems,฀use฀a฀pull-up฀resistor฀of฀1K฀ohm฀to฀250฀ohms.฀Finally,฀the฀smaller฀the฀pull-up฀resistor฀

value,฀the฀less฀stray฀voltage฀will฀be฀induced฀into฀the฀wiring฀connecting฀the฀PIC฀with฀the฀switch.

When฀a฀mechanical฀switch฀operates,฀the฀same฀“contact฀

bounce”฀phenomena฀we฀observed฀in฀Chapter฀3฀with฀relay฀

contacts฀is฀seen.฀In฀most฀switches,฀the฀contact฀separation฀

operation฀(“break”)฀is฀relatively฀clean,฀but฀the฀contact฀closure฀

(“make”)฀exhibits฀multiple฀bounce฀events.฀Figure฀4-11฀shows฀

nearly฀1.5฀ms฀is฀required฀to฀reach฀a฀steady฀state฀closed฀condi-tion฀in฀the฀microswitch฀SPDT฀switch฀being฀tested.

Why฀should฀we฀be฀worried฀about฀switch฀bounce?฀The฀

answer฀is฀that฀in฀many฀cases,฀we฀don’t฀care.฀Bounce฀isn’t฀

a฀concern,฀for฀example,฀where฀a฀baud฀rate฀switch฀is฀read฀

only฀at฀start-up,฀or฀where฀a฀limit฀switch฀senses฀the฀position฀

of฀a฀part฀and฀activates฀a฀software฀stop฀sequence.฀In฀the฀first฀

case,฀the฀switch฀isn’t฀operated฀while฀the฀program฀executes฀

and฀in฀the฀second฀multiple฀activations฀of฀stop฀sequence฀

isn’t฀a฀concern.฀฀Suppose,฀however,฀the฀switch฀counts฀the฀

number฀of฀times฀an฀action฀is฀performed.฀Clearly฀we฀want฀

one฀switch฀operation฀to฀increment฀the฀count฀once,฀not฀

once฀for฀each฀of฀a฀dozen฀or฀so฀bounces.฀To฀prevent฀

switch฀bounce฀from฀repeatedly฀triggering฀an฀operation,฀

we฀must฀debounce฀the฀switch.

We฀can฀debounce฀a฀switch฀either฀with฀external฀electron-ics,฀or฀in฀software.฀

Hardware฀Debouncing

Although฀specialized฀integrated฀circuit฀“debouncers,”฀such฀

as฀Fairchild’s฀FM809฀microprocessor฀supervisor฀devices,฀

exist฀we’ll฀look฀at฀a฀simple฀circuit฀described฀in฀a฀recent฀is-sue฀of฀EDN฀Magazine,฀as฀shown฀in฀Figure฀4-12.[4-1]

Figure฀ 4-11:฀ Contact฀ bounce฀ upon฀ closure฀ of฀

microswitch.

Figure฀4-12:฀Hardware฀debounce.

When฀the฀switch฀is฀open,฀C1฀charges฀to฀VDD ฀and฀RC0฀is฀read฀as฀high.฀When฀the฀switch฀makes,฀C1฀is฀dis-charged฀through฀R2฀(D1฀is฀reversed฀biased฀and฀may฀be฀neglected)฀and฀will฀be฀read฀as฀low฀when฀the฀voltage฀

across฀C1฀drops฀below฀high-to-low฀transition฀voltage,฀approximately฀1.8฀V.฀If฀the฀time฀constant฀of฀R2-C1฀

is฀long฀compared฀with฀the฀individual฀bounce฀intervals,฀the฀decay฀will฀be฀smooth฀and฀only฀one฀transition฀

through฀VTHRESHOLD฀will฀occur.฀But,฀even฀if฀spikes฀of฀several฀hundred฀millivolts฀occur฀at฀RC0฀the฀output฀will฀

stay฀low,฀as฀in฀order฀to฀change฀its฀read฀state,฀RC0฀must฀see฀the฀low-to-high฀transition฀voltage฀of฀approxi-mately฀3.1฀V.฀

When฀the฀switch฀is฀closed,฀the฀input฀pin฀is฀connected฀to฀ground฀through฀R2.฀The฀leakage฀current฀from฀the฀

PIC฀input฀pin฀is฀rated฀not฀to฀exceed฀1µA,฀so฀in฀the฀worst฀case฀with฀R2฀at฀15K฀ohm,฀the฀input฀pin฀will฀be฀at฀

0.015฀V,฀well฀within฀the฀logical฀low฀range.

When฀the฀switch฀is฀opened฀after฀closure,฀C1฀charges฀through฀R1฀and฀R2฀in฀parallel฀with฀D1.฀Until฀the฀volt-age฀across฀C1฀reaches฀0.7฀V฀from฀VDD,฀C1฀charges฀mostly฀through฀R1฀and฀D1.฀Hence,฀the฀charge฀cycle฀is฀

significantly฀shorter฀than฀the฀discharge฀cycle.฀This฀design฀assumes฀that฀the฀switch฀has฀bounce฀problems฀only฀

on฀make฀and฀therefore฀little฀or฀no฀anti-bounce฀effect฀is฀required฀on฀break.

The฀relationship฀between฀C1,฀R2฀and฀the฀desired฀debounce฀time฀TB฀is฀given฀by:

฀ ฀

R C T

LnV V

B THRESHOLD

DD

2 1= −

If฀we฀design฀for฀a฀Schmitt฀input฀where฀VTHRESHOLD฀for฀a฀

high฀to฀low฀transition฀is฀approximately฀1.8฀V,฀and฀if฀we฀

make฀C1฀0.1฀µF,฀a฀convenient฀value,฀we฀may฀simplify฀this฀

equation฀and฀solve฀for฀R2฀in฀terms฀of฀TB:

฀ R2 10≈ TB

R2฀is฀in฀Kohm,฀and฀TB฀is฀in฀milliseconds.฀If฀the฀desired฀

debounce฀time฀is฀1.5฀ms,฀R2฀should฀be฀15K฀ohm.฀Figure฀฀

4-13฀shows฀how฀well฀this฀simple฀circuit฀removes฀the฀

bounce฀from฀the฀same฀switch฀shown฀in฀Figure฀4-11.

To฀study฀the฀effect฀of฀the฀debounce฀circuit,฀we฀simply฀make฀

RB0฀equal฀to฀RC0฀and฀repeat฀the฀test฀in฀an฀endless฀loop.

Program฀4-2

;Program฀04-02

;Program฀to฀echo฀read฀of฀C0฀to฀B0

;Variables

Temp฀ Var฀ Byte Input฀฀C0

Output฀B0 Main

PortB.Bit0฀=฀PortC.Bit0 GoTo฀Main

End

The฀key฀statement฀in฀the฀program฀is฀PortB.Bit0฀=฀PortC.Bit0฀where฀the฀assignment฀operator฀forces฀a฀

read฀of฀RC0฀and฀a฀subsequent฀write฀of฀the฀resulting฀value฀to฀RB0.฀This฀read/write฀sequence฀is฀repeated฀every฀

time฀the฀loop฀executes.฀Program฀4-2฀runs฀quite฀a฀bit฀slower฀than฀Program฀4-1,฀with฀the฀switch฀pin฀RB0฀being฀

read฀once฀every฀68฀µs,฀compared฀with฀the฀once฀every฀1.2฀µs฀in฀the฀assembler฀code.

Figure฀4-13:฀External฀debounce฀circuit฀operation฀

Ch1:฀RC0฀PIC฀input;฀Ch2:฀RB0฀PIC฀output.

Software฀Debouncing

We฀may฀also฀debounce฀the฀switch฀in฀software.฀In฀the฀simplest฀case,฀we฀simply฀pause฀program฀execution฀to฀

allow฀the฀switch฀contacts฀to฀stabilize.

Program฀4-3

;Program฀04-03

;Variables

Temp฀ Var฀ Byte Input฀฀B0

Output฀B1 Main

฀฀฀PortB.Bit1฀=฀PortB.Bit0

฀฀฀Pause฀2 GoTo฀Main End

Figure฀4-14฀shows฀the฀effect฀of฀pausing฀2฀m฀after฀reading฀

the฀input฀pin.฀The฀actual฀reading฀of฀RB0฀in฀the฀statement฀

PortB.Bit1฀=฀PortB.Bit0฀still฀occurs฀very฀quickly;฀in฀

0.2฀µs฀for฀a฀20฀MHz฀clocked฀PIC,฀even฀though฀the฀entire฀

statement฀requires฀approximately฀14฀µs฀to฀execute.฀How-ever,฀these฀quick฀reads฀are฀now฀spaced฀approximately฀every฀

2฀ms฀apart.฀Hence,฀if฀the฀read฀happens฀to฀detect฀a฀high฀dur-ing฀the฀bounce฀period,฀the฀output฀stays฀high฀until฀at฀least฀

the฀next฀read฀period.฀By฀that฀time,฀the฀switch฀will฀have฀had฀

time฀to฀stabilize฀and฀the฀next฀read฀will฀correctly฀be฀a฀low.฀

If,฀on฀the฀other฀hand,฀RB0฀reads฀a฀low฀during฀a฀bounce,฀

the฀output฀drops฀to฀low฀immediately.฀The฀next฀read,฀2฀ms฀

later,฀will฀also฀read฀a฀low,฀since฀the฀switch฀will฀have฀by฀

then฀stabilized.฀฀In฀either฀case฀the฀switch฀is฀successfully฀

debounced.

MBasic฀also฀provides฀a฀built-in฀switch฀debounce฀procedure฀Button,฀invoked฀as:

Button฀pin,฀downstate,฀delay,฀rate,฀bytevariable,฀targetstate,฀address

Pin—is฀the฀address฀of฀the฀pin฀to฀be฀read฀and฀may฀be฀either฀a฀pre-defined฀constant,฀such฀as฀B0,฀or฀a฀variable฀

set฀to฀the฀pin’s฀address.

Downstate—Button฀tests฀for฀a฀specific฀input,฀either฀high฀or฀low.฀Downstate฀is฀a฀constant฀or฀variable฀

(either฀0฀or฀1)฀that฀defines฀which฀condition฀represents฀the฀switch฀being฀operated.฀If฀the฀switch฀operation฀

causes฀a฀low฀to฀be฀applied฀to฀pin,฀Downstate฀should฀be฀0;฀if฀operation฀causes฀a฀high,฀Downstate should฀be฀1.

Delay—a฀constant฀or฀variable฀(0…255)฀that฀controls฀how฀long฀Button฀waits฀before฀starting฀auto-repeat.฀

Auto-repeat฀means฀that฀Button฀acts฀as฀if฀the฀switch฀is฀repeatedly฀cycled,฀similar฀to฀the฀repeated฀letters฀

obtained฀when฀you฀hold฀a฀key฀down฀on฀your฀computer฀keyboard.฀If฀Delay฀is฀set฀to฀0,฀debounce฀and฀

auto-repeat฀are฀disabled;฀if฀Delay฀is฀set฀to฀255,฀debounce฀is฀enabled,฀auto-repeat฀is฀disabled.฀Delay฀is฀

in฀units฀of฀the฀time฀button฀is฀called.฀For฀example,฀if฀button฀is฀used฀in฀a฀loop฀that฀requires฀2฀ms฀to฀

execute,฀and฀if฀Delay฀is฀set฀to฀10,฀the฀net฀delay฀is฀20฀ms.

Rate—is฀a฀constant฀or฀variable฀(0…255)฀that฀determines฀the฀time฀between฀auto-repeats.฀Rate฀is฀also฀in฀units฀

of฀time฀button฀is฀called.฀

Figure฀4-14:฀Debounce฀via฀Pause2฀Ch1:฀RB0฀PIC฀

input;฀Ch2:฀RB1฀PIC฀output.

ByteVariable—is฀a฀byte฀variable฀used฀by฀Button฀for฀workspace.฀You฀must฀declare฀this฀variable.

TargetState—is฀a฀constant฀or฀variable฀(0฀or฀1)฀that฀specifies฀which฀state฀pin฀must฀be฀in฀to฀cause฀program฀

execution฀to฀branch฀to฀Address.

Address—A฀label฀to฀which฀execution฀will฀jump฀upon฀pin฀=฀TargetState.฀

Program฀4-4฀illustrates฀how฀Button฀may฀be฀used.

Program฀4-4

;Program฀04-04

;Variables

Temp฀ Var฀ Byte Input฀฀B0

Output฀B1

Main

High฀B1

Button฀B0,฀0,฀255,฀100,฀Temp,฀1,฀UpButton

GoTo฀Main

UpButton:

฀฀ Low฀B1

฀฀ Pause฀10 GoTo฀Main End

Program฀4-4฀reads฀the฀status฀of฀a฀switch฀connected฀as฀

shown฀in฀Figure฀4-15.฀Program฀4-4฀makes฀an฀output฀

pin,฀B1,฀track฀the฀debounced฀input฀pin฀B0.฀We฀first฀set฀

B1฀high฀and฀test฀to฀see฀if฀B0฀is฀low฀via฀the฀Button฀ debounce฀procedure.฀If฀the฀switch฀is฀open,฀B0฀remains฀

high,฀and฀program฀execution฀resumes฀after฀Button฀ with฀the฀GoTo฀Main ฀statement฀which฀loops฀back฀to฀set-ting฀B1฀high฀and฀reading฀B0.฀If,฀however,฀the฀switch฀is฀

closed฀and฀Button ฀reads฀a฀low฀on฀B0,฀program฀execu-tion฀branches฀to฀UpButton.UpButton฀is฀a฀dummy฀

procedure฀that฀simply฀makes฀B1฀low฀for฀10฀ms,฀after฀

which฀execution฀resumes฀with฀setting฀B1฀high฀and฀read-ing฀B0฀through฀the฀GoTo฀Main฀statement.฀If฀you฀wish฀the฀

button฀press฀to฀do฀something฀useful,฀the฀appropriate฀code฀

would฀be฀at฀the฀procedure฀UpButton.

Figure฀4-16฀shows฀the฀output฀of฀Program฀4-4฀where฀ But-ton฀is฀set฀for฀automatic฀repeat฀operation฀with฀Delay฀250฀ and฀Rate฀250.฀Since฀the฀program฀loop฀reading฀Button฀ takes฀140฀µs฀to฀execute,฀it฀will฀start฀repeating฀after฀250฀×฀

140฀µs,฀or฀35฀ms.฀It฀will฀continue฀to฀repeat฀every฀250฀×฀140฀

µs฀or฀35฀ms.฀Upon฀activation฀(either฀initially฀or฀following฀

each฀auto-repeat)฀program฀execution฀branches฀to฀a฀simple฀

routine฀that฀drops฀the฀output฀low฀for฀10฀ms.

Figure฀4-15:฀Switch฀connection.

Figure฀ 4-16:฀ Button฀ with฀ Delay=250,Rate=250฀

Ch1:฀RB0฀PIC฀Input;฀Ch2:฀RB1฀PIC฀Output.

In฀many฀cases,฀we฀need฀debounce฀only,฀with฀one฀operation฀for฀every฀button฀push.฀Figure฀4-17฀shows฀the฀ef-fectiveness฀of฀Button฀at฀debouncing.฀

Figure฀4-18฀shows฀three฀possible฀switch฀connections.฀SW1฀and฀SW2฀correspond฀to฀State฀0฀and฀State฀1฀

designs฀in฀MBasic’s฀User’s฀Guide฀button฀discussion.฀When฀SW1฀is฀closed,฀RB0฀goes฀low฀while฀when฀SW2฀

is฀closed,฀RB1฀goes฀high.฀If฀SW2฀is฀installed฀on฀the฀same฀printed฀circuit฀board฀as฀the฀PIC,฀the฀connection฀

shown฀in฀Figure฀4-18฀is฀acceptable.฀However,฀if฀the฀switch฀is฀remote฀from฀the฀PIC,฀perhaps฀even฀connected฀

by฀a฀lengthy฀cable,฀the฀design฀associated฀with฀SW2฀requires฀unprotected฀+5฀V฀to฀be฀run฀to฀SW2,฀thus฀expos-ing฀it฀to฀possible฀problems.฀If฀a฀normally฀closed฀switch฀is฀available—perhaps฀the฀NC฀contacts฀of฀a฀SPDT฀

switch—the฀alternate฀configuration฀of฀SW3฀is฀better.฀In฀the฀worst฀case฀should฀either฀side฀of฀SW3฀be฀inadver-tently฀grounded฀no฀damage฀will฀occur.

Isolated฀Switching

Just฀as฀with฀switching฀isolated฀loads,฀there฀are฀occasions฀when฀you฀may฀wish฀to฀read฀a฀switch฀closure฀that฀

cannot฀have฀a฀common฀ground฀with฀your฀PIC฀circuit.฀Or,฀you฀may฀have฀a฀long฀run฀of฀cable฀between฀the฀

switch฀and฀the฀PIC฀and฀wish฀to฀ensure฀against฀stray฀induced฀voltages฀or฀ground฀currents.

We฀can฀apply฀several฀techniques฀developed฀in฀Chapter฀3฀isolating฀PIC฀inputs฀from฀switches.฀Figure฀4-19฀

implements฀two฀simple฀approaches.฀RB0฀is฀switched฀through฀a฀relay,฀while฀RB1฀is฀switched฀through฀an฀

optical฀coupler.฀Both฀RB0฀and฀RB1฀

need฀debouncing฀treatment.

SW1฀makes฀or฀breaks฀current฀

through฀relay฀K1,฀thereby฀operating฀

the฀contacts฀connected฀to฀RB0.฀When฀

SW1฀is฀open,฀RB0฀is฀high;฀when฀

SW1฀is฀closed,฀RB0฀is฀low.฀R1฀is฀se-lected฀to฀permit฀a฀reasonable฀wetting฀

current,฀5฀mA฀in฀the฀example,฀to฀flow฀

through฀K1’s฀contacts.฀฀D1฀is฀a฀snub-bing฀diode฀to฀reduce฀arcing฀when฀

SW1฀is฀opened.฀For฀small฀relays฀

operated฀from฀low฀voltage฀sources,฀

D1฀may฀be฀safely฀omitted.

Figure฀ 4-17:฀ Button฀ with฀ delay=255,฀ rate=100฀

Ch1:฀RB0฀PIC฀input;฀Ch2:฀RB1฀PIC฀output

Figure฀ 4-18:฀ Alternative฀ Switching฀

Connections.

Figure฀4-19:฀Isolating฀remote฀switches฀from฀a฀PIC.

SW2฀makes฀or฀breaks฀current฀through฀the฀LED฀half฀of฀optoisolator฀U1.฀When฀illuminated,฀U1’s฀LED฀satu-rates฀the฀phototransistor฀and฀pulls฀its฀collector—and฀RB1—low.฀When฀not฀illuminated,฀U1’s฀phototransistor฀

is฀off฀and฀RB1฀is฀taken฀high฀R2.฀R3฀should฀be฀selected฀to฀ensure฀the฀phototransistor฀is฀fully฀saturated฀when฀

the฀LED฀is฀illuminated,฀with฀15฀to฀20฀mA฀being฀a฀typical฀current฀value฀for฀a฀4N25฀optoisolator.฀R2฀should฀

be฀selected฀to฀ensure฀the฀phototransistor’s฀collector฀voltage฀is฀under฀0.5฀V฀when฀the฀LED฀is฀illuminated.฀For฀

a฀4N25,฀we฀can฀accomplish฀this฀by฀a฀collector฀current฀of฀1฀mA฀or฀so.฀The฀component฀values฀in฀Figure฀4-19฀

result฀in฀an฀LED-on฀voltage฀of฀0.15฀V฀at฀the฀phototransistor’s฀collector.