• No results found

Fast฀Switching—Sound฀from฀a฀PIC

So฀far฀in฀this฀chapter,฀our฀emphasis฀has฀been฀on฀relatively฀slow฀switching.฀But,฀if฀we฀switch฀a฀loudspeaker฀

off฀and฀on฀at฀an฀audio฀rate,฀we฀can฀produce฀sound,฀perhaps฀to฀be฀used฀as฀an฀alert฀tone,฀or฀a฀beep฀to฀confirm฀

an฀action฀or฀status.฀(We฀will฀need฀fast฀switching฀to฀control฀a฀DC฀motor’s฀speed฀through฀pulse฀width฀modula-tion,฀and฀to฀control฀stepper฀motors,฀both฀topics฀dealt฀with฀in฀later฀chapters.)

We฀can฀generate฀a฀sound฀either฀through฀a฀self-contained฀sounder,฀such฀as฀the฀Sonalert®฀products฀introduced฀

by฀Mallory,฀or฀through฀the฀PIC฀producing฀the฀audio฀signal฀itself.฀A฀Sonalert฀may฀be฀driven฀by฀a฀PIC฀using฀

any฀of฀the฀techniques฀you฀learned฀earlier฀in฀this฀chapter.฀Later฀chapters฀explore฀in฀some฀detail฀the฀advantages฀

and฀disadvantages฀of฀various฀ways฀to฀generate฀audio฀signals฀using฀MBasic.฀Here,฀however,฀we฀will฀just฀look฀

at฀two฀simple฀interfaces฀and฀one฀of฀the฀many฀audio฀output฀procedures฀available฀in฀MBasic.

We’ll฀assume฀you฀don’t฀intend฀to฀produce฀ear฀splitting,฀high฀fidelity฀output฀from฀a฀PIC.฀Rather,฀you฀are฀in-terested฀in฀beeps฀and฀other฀alerting฀tones.฀In฀some฀cases,฀it฀may฀be฀possible฀

to฀obtain฀adequate฀volume฀levels฀by฀driving฀the฀speaker฀directly฀from฀a฀

PIC,฀as฀illustrated฀in฀Figure฀3-47.฀When฀thinking฀of฀a฀speaker,฀low฀imped-ance฀designs฀most฀often฀come฀to฀mind,฀with฀3.2,฀4฀and฀8฀ohm฀devices฀being฀

common.฀I’ve฀generally฀been฀disappointed฀with฀the฀volume฀levels฀when฀

a฀low฀impedance฀speaker฀is฀directly฀connected฀to฀a฀PIC.฀Indeed,฀a฀series฀

resistor,฀R1฀in฀Figure฀3-47,฀of฀50฀ohms฀or฀so฀is฀necessary฀to฀produce฀useful฀

sound฀output.

Remember,฀however,฀that฀a฀high฀output฀pin฀may฀be฀thought฀of฀as฀a฀5V฀source฀in฀series฀with฀approximately฀

85฀ohms.฀Working฀into,฀for฀example,฀a฀3.2฀ohm฀speaker฀with฀a฀50฀ohm฀series฀resistor,฀approximately฀99.8%฀

of฀the฀theoretical฀maximum฀output฀power฀of฀the฀pin฀will฀be฀lost฀and฀not฀transferred฀to฀the฀speaker.฀This฀still฀

may฀produce฀an฀acceptable฀volume฀level.฀If฀the฀speaker฀has฀higher฀impedance,฀say฀at฀least฀32฀ohms,฀a฀much฀

greater฀proportion฀of฀the฀available฀power฀will฀produce฀useful฀sound.฀If฀necessary,฀a฀simple฀series฀resistor,฀

shown฀as฀R1฀in฀Figure฀3-47,฀can฀serve฀as฀a฀volume฀control.

If฀you’ve฀examined฀a฀loudspeaker฀you฀know฀the฀typical฀construction฀consists฀of฀a฀paper฀cone฀that฀moves฀in฀

or฀out฀in฀response฀to฀current฀through฀the฀voice฀coil.฀Our฀simple฀connection฀of฀Figure฀3-47฀moves฀the฀cone฀

only฀in฀one฀direction,฀either฀in฀or฀out,฀depending฀on฀which฀speaker฀connection฀you฀ground฀and฀which฀you฀

connect฀to฀the฀PIC’s฀pin.฀The฀unidirectional฀motion฀throws฀away฀one฀half฀the฀potential฀sound฀level.฀Depend-ing฀on฀your฀desired฀sound฀level฀and฀speaker฀rating,฀this฀may฀or฀may฀not฀be฀important.฀Figure฀3-48฀shows฀

Figure฀ 3-47:฀ Driving฀ a฀ speaker฀

directly฀from฀a฀PIC.

we฀are฀able฀to฀develop฀a฀peak฀current฀of฀27฀mA฀through฀a฀

3.2฀ohm฀speaker.฀This฀particular฀speaker฀yielded฀a฀weak฀

sound฀with฀27฀mA฀current.฀We฀may฀calculate฀the฀power฀

delivered฀to฀the฀speaker฀by฀recalling฀that฀the฀RMS฀power฀a฀

square฀wave฀is฀equal฀to฀the฀one-half฀the฀peak฀power.฀(The฀

RMS฀of฀the฀on฀period฀is฀equal฀to฀the฀peak;฀but฀since฀half฀

the฀cycle฀is฀off,฀the฀RMS฀reduces฀by฀one฀half.)฀Hence,฀

the฀RMS฀power฀delivered฀to฀the฀speaker฀is฀approximately฀

1.2฀mW.฀(This฀is฀based฀upon฀the฀speaker’s฀nominal฀3.2฀

ohm฀impedance.฀Measurements฀of฀the฀particular฀speaker฀I฀

tested฀showed฀its฀true฀impedance฀at฀1000฀Hz฀is฀3.09฀ohms,฀

representing฀2.95฀ohms฀resistance฀in฀series฀with฀149฀µH฀

inductance.)

Let’s฀look฀at฀a฀higher฀power฀driver฀for฀a฀low฀impedance฀

speaker.฀Since฀we฀are฀not฀overly฀concerned฀with฀the฀sound฀

quality—the฀PIC฀sound฀procedure฀we฀use฀outputs฀a฀square฀

wave,฀after฀all—we฀will฀use฀a฀2N4401฀emitter฀follower฀

to฀drive฀the฀3.2฀ohm฀speaker,฀using฀the฀circuit฀shown฀in฀

Figure฀3-49.฀And,฀to฀permit฀the฀speaker’s฀voice฀coil฀to฀have฀

both฀in฀and฀out฀excursions,฀we฀use฀C1฀to฀block฀the฀DC฀

component.฀

Figure฀3-50฀shows฀the฀resulting฀current฀thorough฀the฀speaker.฀

The฀RMS฀power฀delivered฀to฀the฀speaker฀is฀now฀approximate-ly฀45฀mW,฀yielding฀nearly฀16฀dB฀more฀sound฀output,฀a฀very฀

noticeable฀improvement฀over฀the฀direct฀drive฀connection.฀

Program฀3-3฀uses฀MBasic’s฀sound฀procedure฀to฀output฀a฀

1000฀Hz฀square฀wave฀for฀1,000฀milliseconds฀on฀RB0.฀The฀

tone฀output฀is฀repeated฀endlessly฀through฀the฀GoTo฀Main฀ loop.฀

Program฀3-3฀

;Program฀3-03 Main

฀฀฀฀฀;burst฀of฀1000฀Hz฀for฀1฀second฀฀

w/฀endless฀loop

฀฀฀฀฀Sound฀B0,[1000000\1000]

GoTo฀Main

End

References

[3-1]฀฀ Horowitz,฀Paul฀and฀Hill,฀Winfield,฀The฀Art฀of฀Elec-tronics,฀2nd.฀Ed.,฀(1989).฀If฀you฀have฀only฀one฀book฀on฀

electronics฀in฀your฀library,฀this฀should฀be฀it.฀A฀long-awaited฀3rd฀edition฀is฀rumored฀to฀be฀in฀the฀works,฀but฀

that฀shouldn’t฀discourage฀you฀from฀purchasing฀the฀2nd฀edition.

[3-2]฀฀ American฀Radio฀Relay฀League,฀The฀ARRL฀Handbook฀for฀Radio฀Communications฀2003฀ed.,฀American฀

Radio฀Relay฀League฀(2003).฀Although฀aimed฀at฀radio฀amateurs,฀the฀ARRL฀Handbook฀provides฀good฀

Figure฀ 3-48:฀ Direct฀ drive฀ of฀ 3.2฀ ohm฀ speaker฀

with฀PIC฀and฀56฀ohm฀series฀resistor฀Ch2:฀Speaker฀

current฀(mA).

Figure฀3-49:฀2N4401฀emitter฀follower฀speaker฀driver.

Figure฀3-50฀:฀2N4401฀Follower฀Drive฀of฀3.2฀Ohm฀

Speaker฀with฀PIC฀and฀56฀Ohm฀Series฀Resistor฀Ch2:฀

Speaker฀Current฀(mA).

entry-level฀coverage฀of฀basic฀analog฀and฀digital฀electronics,฀test฀equipment฀and฀construction฀practices.฀

The฀ARRL฀updates฀its฀handbook฀every฀year,฀so฀purchase฀the฀most฀recent฀version฀available.

[3-3]฀฀ Ludeman,฀Robert฀R.,฀Introduction฀to฀Electronic฀Devices฀and฀Circuits,฀Saunders฀College฀Publishing฀

(1990).฀Written฀as฀an฀introductory฀text฀for฀community฀college฀electronic฀technician฀students,฀it’s฀a฀good฀

summary฀of฀basic฀solid-state฀electronics฀without฀requiring฀advanced฀mathematics.฀฀

[3-4]฀฀ Linear฀Technology฀Corp.฀makes฀available฀a฀free฀Microsoft฀Windows-based฀SPICE฀simulator฀and฀sche-matic฀capture฀software,฀“LTSpice/SwitcherCAD฀III.”฀Although฀aimed฀as฀a฀design฀tool฀supporting฀LTC’s฀

products,฀the฀software฀is฀not฀limited฀to฀LTC฀devices.฀It฀may฀be฀downloaded฀at฀http://www.linear-tech.

com/software/.฀In฀addition,฀add-on฀device฀libraries฀and฀explanatory฀material฀for฀LTSpice฀are฀available฀

in฀the฀associated฀Yahoo฀user฀group฀http://groups.yahoo.com/group/LTspice/฀in฀the฀files฀folder.฀฀The฀

schematics฀and฀simulations฀in฀this฀book฀use฀LTSpice.

[3-5]฀฀ Barkhordarian,฀Vrej,฀Power฀MOSFET฀Basics,฀International฀Rectifier฀Corp.฀Technical฀Note฀(undated).

[3-6]฀฀ International฀Rectifier฀Corp.,฀The฀Do’s฀and฀Don’ts฀of฀Using฀MOS-Gated฀Transistors,฀AN-936฀(v.฀Int).฀

(Undated)

[3-7]฀฀ International฀Rectifier฀Corp.,฀Current฀Ratings฀of฀Power฀Semiconductors,฀AN-949฀(v.฀Int),฀(Undated).

[3-8]฀฀ International฀Rectifier฀Corp.,฀Selecting฀and฀Designing฀in฀The฀Right฀Schottky,฀AN-968,฀(Undated)

[3-9]฀฀ Omron฀Electronics,฀Inc.,฀Relay฀User’s฀Guide฀(1990).฀Available฀for฀free฀download฀at฀Omron’s฀reference฀

center฀http://oeiwcsnts1.omron.com/pdfcatal.nsf.฀From฀this฀page,฀select฀Relays.฀From฀the฀relays฀page฀

select฀Manual.

[3-10] ฀Data฀sheets฀for฀the฀devices฀used฀in฀this฀chapter฀are฀available฀for฀downloading฀at฀the฀following฀URL฀ad-dresses:

2N4401:฀http://www.fairchildsemi.com/ds/2N/2N4401.pdf 2N4403:฀http://www.fairchildsemi.com/ds/2N/2N4403.pdf

PS710A-1A:฀http://www.csd-nec.com/opto/english/pdf/PN10268EJ01V0DS.pdf 2N7000:฀http://www.fairchildsemi.com/ds/2N/2N7000.pdf

4N25:฀http://www.fairchildsemi.com/ds/4N/4N25.pdf TIP31:฀http://www.fairchildsemi.com/ds/TI/TIP31.pdf TIP120:฀http://www.fairchildsemi.com/ds/TI/TIP120.pdf

MV5491A฀Dual฀LED:฀http://www.fairchildsemi.com/ds/MV/MV5094A.pdf

IRF510:฀Go฀to฀International฀Rectifier’s฀home฀page฀http://www.irf.com/฀and฀enter฀IRF510฀in฀the฀search฀box.

IRF9510:฀Go฀to฀International฀Rectifier’s฀home฀page฀http://www.irf.com/฀and฀enter฀IRF9510฀in฀the฀

search฀box.

IPS021:฀Go฀to฀International฀Rectifier’s฀home฀page฀http://www.irf.com/฀and฀enter฀IPS021฀in฀the฀search฀box.

IPS511:฀Go฀to฀International฀Rectifier’s฀home฀page฀http://www.irf.com/฀and฀enter฀IPS511฀in฀the฀search฀box.

G5V฀Relay:฀Go฀to฀Omron’s฀home฀page฀for฀US฀relay฀products฀http://oeiweb.omron.com/฀and฀enter฀

G5V-2-H1฀in฀the฀search฀box.

G2RL-24฀Relay:฀Go฀to฀Omron’s฀home฀page฀for฀US฀relay฀products฀http://oeiweb.omron.com/฀and฀enter฀

G2RL-24฀in฀the฀search฀box.

Standex฀JG100฀Relay:฀http://www.standexelectronics.com/serjg.htm

C H A P T E R ฀ 4