Tachomet

Embed Size (px)

Citation preview

  • 8/8/2019 Tachomet

    1/4

    ConstruCtion

    9 2 J an uar y 2008 electronics for you w w w . e f y m a g . c o m

    Prof. K. PADMANABHAN

    MicrocoNtroller-BAseDtAcHoMeter

    SUNI L KUMAR

    Atachometer is nothing but asimple electronic digital trans-ducer. Normally, it is used for

    measuring the speed of a rotating shaft.The number of revolutions per minute(rpm) is valuable information for un-derstanding any rotational system. Forexample, there is an optimum speedfor drilling a particular-size hole in aparticular metal piece; there is an ideal

    sanding disk speed that depends onthe material being nished. You mayalso want to measure the speed of fansyou use.

    This easy-to-make photoelectrictachometer measures the rpm of mostshop-oor tools and many householdmachines without any mechanical orelectrical interface.

    Hw wk?

    Just point the light-sensitive probe tip

    atop the spinning shaft towards thespinning blade, disk or chuck and readthe rpm. The only requirement is that

    rotates.Each time the tape spins past

    the probe, the momentary increasein reected light is detected by thephototransistor. The signal processorand microcontroller circuit countsthe increase in the number of such lightreections sensed by it and therebyevaluates the rpm, which is displayed

    on the 4-digit, 7-segment display.The phototransistor is kept inside

    a plastic tube, which has a convex lenstted at one end. A convex lens ofabout 1cm diameter and 8-10cm focallength is a common item used by watchrepairers and in cine lm viewer toys.It can be obtained from them to set upthe experiment. The phototransistor isxed on a piece of cardboard such thatit faces the lens at a distance of about8 cm. The leads from the phototransis-

    tor are taken out and connected in thecircuit shown in Fig. 1. Fig. 2 showsthe suitable arrangement of phototran-

    you rst place a contrasting colourmask. A strip of white adhesive tape isideal on the spinning object. Position itsuch that the intensity of light reectedfrom the objects surface changes as it

    Fig. 1: Circuit of microcontroller-based tachometer

    PArts List

    Semiconductors:IC1 - AT89C2051

    microcontrollerIC2 - ULN2003 current bufferIC3 - CA3140 operational

    amplierT1-T4 - BC557 pnp transistorsT5 - 2N2222 npn transistorT6 - L14F1 photo-transistor

    D1 - 1N4007 rectier diodeDIS1 - KLQ564 4-digit,7-segment display

    Resistors (all -watt, 5% carbon):R1-R4 - 1-kilo-ohmR5, R6 - 10-kilo-ohmR7-R11 - 1.2-kilo-ohmRNW1 - 10-kilo-ohm resistor

    networkVR1 - 4.7-kilo-ohm preset

    Capacitors:C1 - 10F, 16V electrolyticC2, C5, C6 - 0.1F ceramic diskC3, C4 - 22pF ceramic disk

    Miscellaneous:

    S1 - Push-to-on switchS2 - On/Off SwitchX

    TAL- 12MHz

    BATT. - 6V battery

  • 8/8/2019 Tachomet

    2/4

    ConstruCtion

    9 4 J an uar y 2008 electronics for you w w w . e f y m a g . c o m

    sistor.The detected sig-

    nal is amplified bytransistor 2N2222 (T5)and further ampliedby operational ampli-er CA3140 (IC3). Thereference voltage point

    for the operational am-plier is obtained byresistor divider net-

    work comprising R2 and R3. Theoutput from pin 6 of IC3 is fed to pin12 of microcontroller AT89C2051. Notethat pins 12 and 13 of microcontrollerAT89C2051 are the inputs (+ and -) ofits internal analogue comparator. Pin13 is adjusted to nearly half the sup-ply voltage using a potential dividercomprising resistor R7 and preset VR1

    across the supply.The pulses picked up by the

    phototransistor are sensed by theinternal comparator of AT89C2051and, through software, each pulserepresenting one rotation of the objectis detected. By counting the number ofsuch pulses, on an average per minutebasis, the RPM is evaluated. It is dis-played by a software routine to lightup the LED segments of the 4-digit,7-segment display.

    cu dpn

    Fig. 1 shows the circuit of the mi-crocontroller-based tachometer. Thetachometer comprises AT89C2051microcontroller, ULN2003 high-currentDarlington transistor array, CA3140operational amplier, common-anode7-segment (4-digit multiplexed) displayand its four anode-driving transistors.

    The AT89C2051 is a 20-pin, 8-bitmicrocontroller of Intels 8051 familymade by Atmel Corporation. Port-1

    pins P1.7 through P1.2, and port-3 pin P3.7 are connected to inputpins 1 through 7 of ULN2003.Port-1 pins are pulled up with10-kilo-ohm resistor networkRNW1. They drive all the sevensegments of the display with thehelp of internal inverters.

    Port-3 pins P3.0 through P3.3of the microcontroller are con-nected to the base of transistorsT1 through T4, respectively, to

    Fig. 2: Suitable arrangement of phototransistor

    Fig. 3: Pin

    configuration oftransistor BC557

    select one digit out of the four at a timeand to supply anode-drive currents tothe common anode pin of respectivedigit. Pin conguration of transistorBC557 is shown in Fig. 3.

    When pin P3.0 of microcontrollerIC1 goes low, it drives transistorT1 into saturation, which provides

    the drive current to anode pin 6 of4-digit, 7-segment, common-anodedisplay DIS1. Similarly, transistors T2through T4, respectively, provide sup-

    ply to common-anode pins 8, 9and 12 of DIS1.Thus microcon-troller IC1 drivesthe segment inmultiplexed man-ner using its port

    pins. This is time-division multi-plexing process.

    Segment dataand display-en-able pulse fordisplay are re-freshed every5 ms. Thus, thedisplay appearsto be continuouseven though itlights up one byone.

    Switch S1 isused to manuallyreset the micro-controller, whilethe power-on-re-set signal for themicrocontrolleris given by C1and R6. A 12MHz

    crystal is connect-ed to pins 4 and 5of IC1 to generatethe basic clockfrequency for themicrocontroller.The circuit usesa 6V battery forpower supplyor alternativelya mains derivedlow voltage sup-

    ply. An actual-size, single-sideFig. 5: Component layout for the PCB

    Fig. 4: A single-side, actual-size PCB layout for microcontroller-basedtachometer

  • 8/8/2019 Tachomet

    3/4

    ConstruCtion

    9 6 J an uar y 2008 electronics for you w w w . e f y m a g . c o m

    Tacho.asm

    $mod51ORG 0HAJMP 30HORG 0BH ;TIMER 0 INTERRUPT VECTOR

    AJMP TIMER0ISR ;Timer 0 Interrupt service routineaddressORG 30HMOV SP,#60H ;set stack pointerMOV P3,#0FFH ;set all port 3 bits high to enable

    inputs alsoMOV P1,#03 ;set port 1 to all zeros expect bits 0,1MOV TMOD,#01100001B ;TIMER 1 - MODE 2

    COUNTER,TIMR-0 TO 16 bit timer

    BEG: MOV TH0,#0ffH ;TIMER REG.0 IS SET TO 0,GIVES 64msMOV TL0,#-99 ; timer low reg. is also sosetb et0setb eamov 44h,#0mov 45h,#0acall delayajmp lowsigdelay: mov r2,#10djnz r2,$ ;wait 20 usretlowsig: jb p3.6,lowsigcall delayjnb p3.6,$setb tr0 ; start timermov c,p3.6 ;high beginsmov p3.5,cacall delayjb p3.6, $mov c,p3.6 ;low nowmov p3.5,cacall delayjnb p3.6,$mov c,p3.6 ;high begins againmov p3.5,cclr tr0 ;stop timerclr et0 ;and interrupt by timermov r3,#0 ;number 600000 or 927c0 hex as Dividendmov r2,#09h ; 9mov r1,#27h ;27mov r0,#0c0h ; c0mov r5,45h ;divisor is time for one cyclemov r4,44hcall UDIV32 ;divide 60000/tmov 40h,r0mov 41h,r1

    mov r1,41hmov r2,40hCALL HEX2BCD

    mov 50h,#0FFHcall refresh

    disp: call refresh1djnz 50h,disp ; so many times for a visible time limitjmp beg;16 Bit Hex to BCD Conversion for 8051 Microcon -troller;This routine is for 16 bit Hex to BCD conversion;;Accepts a 16 bit binary number in R1,R2 and returns 5digit BCD in ;R7,R6,R5,R4,R3(upto 64K )Hex2BCD: ;r1=high byte, r7 most signicant digit, R2

    = LSByteMOV R3,#00D

    MOV R4,#00DMOV R5,#00DMOV R6,#00DMOV R7,#00DMOV B,#10DMOV A,R2DIV ABMOV R3,B ;MOV B,#10 ; R7,R6,R5,R4,R3DIV ABMOV R4,BMOV R5,ACJNE R1,#0H,HIGH_BYTE ; CHECK FOR HIGH

    BYTESJMP ENDD

    HIGH_BYTE: MOV A,#6ADD A,R3MOV B,#10DIV ABMOV R3,BADD A,#5ADD A,R4MOV B,#10DIV ABMOV R4,BADD A,#2ADD A,R5MOV B,#10DIV ABMOV R5,BCJNE R6,#00D,ADD_ITSJMP CONTINUE

    ADD_IT: ADD A,R6CONTINUE: MOV R6,A

    DJNZ R1,HIGH_BYTEMOV B, #10DMOV A,R6

    DIV ABMOV R6,BMOV R7,A

    ENDD: retDISP1:REFRESH:; content of 18 to 1B memory locations areoutput on LEDs; only numbers 0 to 9 and A to F are valid data in theselocations

    MOV 18H,r3 ; least signicant digitMOV 19H,r4 ; next signicant digitMOV 1AH,r5MOV 1BH,R6 ; most signicant digit (max:9999)

    RETrefresh1:

    MOV R0,#1bh ; 1b,1a,19,18, holds values for 4 dig-

    itsMOV R4,#8 ; pin p3.3_ 0 made low one by one starts

    wth 18mov r7,#2 ; decimal pt.on 3rd digit from left (2 nd

    fromright)PQ2: CALL SEGDISPdec R0mov a,r4rrc amov r4,ajnc pQ2PV3:RETSEGDISP:mov dptr,#ledcodeMOV A,@R0ANL A,#0FHMOVC A,@A+dptrsegcode:MOV R5,AORL A,#03H ; WE WANT TO USE PORT 1 BITS 0

    AND 1 FOR INPUT ANLOG; so retain them highS3: MOV P1,A ; SEGMENT_PORTMOV A,R5 ;we use p3.7 for the segment a of displayRRC A ;so get that bit D0into carryrrc amov p3.7,c ;segment a;S1: MOV A,R4 ; get digit code from r4 00001000cpl a ;11110111rrc a ;11111011-1mov p3.0,c ; output to drive transsitors for digit

    lightingrrc a ;11111101-1mov p3.1,crrc a ;11111110-1mov p3.2,crrc a ;1111111-0 yes low makes left most digit show

    msdigitmov p3.3,c

    S5:S4: ACALL DELAY1 ; let it burn for some timeMOV A,#0ffH ; extinguish the digit after that time

    PCB layout for the tachometer (Fig. 1)is shown in Fig. 4 and its componentlayout in Fig. 5.

    tng

    The source code of this article is avail-able at http://www.electronicsforu.com/efycodes/efy-codes.zip at code

    file tacho.hex. Using a program-mer, load the code into the new chipAT89C2051. (Refer theMay 2005 issueof EFY for article on programmer for89C51 and 2051.) Then, t it into thecircuit board and after powering up thecircuit, test it.

    For testing, point the probe us-ing torchlight for illumination ofthe rotating object. For fans, use thelight from behind. Hold the probefirmly so as to provide a steady,

    bright illumination on the object.Even an LED pen torch could beused here. Avoid the fluctuatingbackground light from sources suchas tubelight.

    swa

    The software is written in Assembly

    language and assembled using 8051cross-assembler. It is well comment-ed and easy to understand. It usesAT89C2051s internal timer for mea-suring the period of one cycle of therotation in units of 100 microseconds.Thus if the speed is 1500 rpm, it is 25rps, and the time taken for one cycleis 40 ms.

    The timer uses an interrupt tocount overows every 100 microsec-onds and so the number counted by

    the timer program in this case will be400. This is divided by 600,000 (somany 100/s present in a minute),giving a result of 1500. This gives therpm. These digits are displayed on the4-digit, 7-segment display. To performthe division, subroutine UDIV32 isemployed, which is a standard sub-

    routine available for 8051 family for32-bit number by 16-bit number divi-sion. It has an accuracy of 5 rpm in a6000rpm count.

    EFY note. The source code of thisarticle is available at http://www.elec-tronicsforu. com/efycodes/efy-codes.zip and will also be included in EFY-CD of February 2008 issue.

    Prof. K. Padmanabhan retired from Alagappa Col-lege of Technology, Guindy, Chennai

  • 8/8/2019 Tachomet

    4/4

    ConstruCtion

    9 8 J an uar y 2008 electronics for you w w w e f y m a g c o m

    MOV P3,A ; to prevent shadows6: RETledcode:DB 7EH,0CH,0B6H,9EH,0CCH,0DAH,0FAH

    ;these are code for the numbers 0 to 9 and A to FDB 0EH,0FEH,0CEH,0EEH,0F8H,72H,0BCH,0F6H,

    0E2HDELAY1:MOV 55h,#0ffH ; 1msN: NOPDJNZ 55h,NRETTIMER0ISR:mov th0,#0ffhmov tl0,#-90 ; in 100 us stepspush acc

    mov a,#1clr cadd a, 44h ;count time btwn pulsesmov 44h,amov a,#0addc a,45h ;add carry to most sign. bytemov 45h,apop accreti; subroutine UDIV32;32 bit /16 bit to 32 bit quotient and remainder un-signed;input r3,r2,r1,r0 = dividend X;input r5,r4 = divisor y;output r3-r0 = quotient Q of X/Y;r7,r6,r5,r4 =remainder;alters acc, agsUDIV32: push 08 ;save reg. bank 1

    push 09

    push 0AHpush 0BHpush 0CHpush 0DHpush 0EHpush 0Fhpush dplpush dphpush Bsetb RS0 ;select reg.bank 1mov r7,#0

    mov r6,#0mov r5,#0mov r4,#0mov B,#32 ;set loop countdiv_lp32:clr RS0 ;selet reg.bank 0clr Cmov a,r0 ;shift highestbit of Xrlc amov r0,amov a,r1 ;shift next bit of Xrlc amov r1,amov a,r2 ;shift next bit of X

    rlc amov r2,amov a,r3 ;shift next bit of Xrlc amov r3,asetb rs0 ;reg. bank 1mov a,r4 ;lowest bit of remainderrlc amov r4,amov a,r5 ;shift next bit of remrlc amov r5,amov a,r6 ;shift next bit of remrlc amov r6,amov a,r7 ;shift next bit of remrlc amov r7,amov a,r4

    clr Csubb a,04mov dpl,amov a,r5subb a,5mov dph,amov a, r6subb a,#0mov 06,amov a,r7subb a,#0

    mov 07,acpl Cjnc div_321mov r7,7mov r6,6mov r5,dphmov r4,dpldiv_321: mov a,r0rlc amov r0,a ; shift result bit into partial quotientmov a,r1rlc amov r1,a

    mov a,r2rlc amov r2,amov a,r3rlc amov r3,adjnz B,div_lp32mov 7,r7mov 6,r6mov 5,r5mov 4,r4mov 3,r3mov 2,r2mov 1,r1mov 0,r0clr rs0pop Bpop dphpop dpl

    pop 0Fhpop 0EHpop 0Dhpop 0Chpop 0bhpop 0ahpop 09pop 08retEND