Pérez et al.
: A demodulator of PWN signalsIngeniare
                                                                        generated-for
                                                                                   Revista
                                                                                      a digital
                                                                                            Chilena
                                                                                                accelerometer
                                                                                                     de Ingeniería,
                                                                                                              is developed
                                                                                                                    vol. 14 Nº
                                                                                                                            using
                                                                                                                               2, 2006,
                                                                                                                                   a microcontroller
                                                                                                                                        pp. 119-123
              A DEMODULATOR OF PWM SIGNALS GENERATED FOR A DIGITAL
              ACCELEROMETER IS DEVELOPED USING A MICROCONTROLLER
    UN DEMODULADOR DE SEÑALES PWM GENERADAS POR UN ACELERÓMETRO
         DIGITAL ES DESARROLLADO USANDO UN MICROCONTROLADOR
                   Eduardo Pérez Lobato1              Marcelo Muñoz Tapia2                 Jorge Ramírez Fernández3
                                  Recibido 14 de marzo de 2005, aceptado 2 de enero de 2006
                                   Received: March 14, 2005 Accepted: January 2, 2006
                                                                RESUMEN
Esta publicación presenta el uso de un microcontrolador para demodular dos señales PWM que están siendo generadas por
un acelerómetro digital, obtener sus anchos y enviarlas en forma serial al puerto paralelo de un computador de propósitos
generales.
Palabras clave: Acelerómetro, microcontrolador, control por computador, PWM.
                                                                ABSTRACT
This paper presents the use of a microcontroller to demodulate two Pulse Width Modulated (PWM) signals which are
being generated by a digital accelerometer, to obtain their pulse widths and transmit them serially to a parallel port of
a general purpose computer.
Keywords: Accelerometer, microcontroller, computer-controlled, PWM.
                     INTRODUCTION                                             2 and Figure 3 respectively. To demodulate PWM signals,
                                                                              Karthaus and Fischer [1] used the envelope detection
Nowadays, there is a new generation of electronic                             technique and Pascual [2] developed a low pass filter.
accelerometers which are more precise, cheaper,                               This proposal deals with developing a PWM demodulator
smaller and with a wider range than their mechanical                          using a microcontroller as a single hardware piece.
counterparts. The scope of its use is very wide, which
usually involves force related applications, such as
mechanical stress, rotary torque, seismic events,
dynamic weight and so on. Thus, the introduction of
these tiny digital accelerometers opens the chances
of dynamically monitoring acceleration in a real time
fashion. In addition, if these devices are integrated with
general purpose computers, it is feasible to go beyond
just displaying them on an screen, calculating their
related energy and forces, etc., saving them up onto disk
for further analysis and distributing them through the
Internet using the Transmission Control Protocol/Internet
Protocol (TCP/IP). These accelerometers, as integrated
circuits, generate two PWM squared-waveforms as shown                         Figure 1. X-Y acceleration signals generated by the
in Figure 1, and the value of the accelerations AX and AY                                accelerometer.
can be calculated through the equations shown in Figure
1   Departamento de Ingeniería Industrial, Facultad de Ingeniería Universidad de Antofagasta, Avenida Angamos 601, Antofagasta, Chile,
    perezlobato@uantof.cl
2   Centro de Computación, Universidad de Antofagasta, Avenida Angamos 601, Antofagasta, Chile, mmunoz@uantof.cl
3   Departamento de Geomensura, Universidad de Antofagasta, Avenida Angamos 601, Antofagasta, Chile, jramirez@unatof.cl
                                              Ingeniare - Revista de Ingeniería, vol. 14 Nº 2, 2006                              119
Ingeniare - Revista Chilena de Ingeniería, vol. 14 Nº 2, 2006
                         ¤ T 1X      ³ 1                                        each, TTL electrically leveled, one designated as the
                    AX  ¥      
 0.5 *                          (1)            ACCEL_X and the other as the ACCEL_Y.
                         ¦ T2X       µ́ 8
                                                                                Microcontroller. The general purpose PIC-16F84 was
Equation that uses the T1X and T2X measured pulse                               chosen because its technical features suits with the
widths to calculate the X-acceleration value                                    requested capabilities and its widespread use makes it a
                                                                                simpler task to integrate it with further projects.
                          ¤ T 1Y      ³ 1                       (2)
                     AY  ¥      
 0.5 *                                        Figure 2 depicts the block diagram between the computer
                          ¦ T 2Y      µ́ 8
                                                                                parallel port, the microcontroller and the accelerometer,
                                                                                giving details only of the inter-connection lines used and
Equation that uses the T1Y and T2Y measured pulse                               their related pin outs.
widths to calculate the Y-acceleration value
    PWM DEMODULATOR REQUIREMENTS
To demodulate a PWM signal it is necessary to know
either the width of the high state pulse and the width of
the cycle. The present work presents the next strategy
      1.    Detect when a signal starts or finishes, either
            if it is in the high or low state. To accomplish
            this, detection either of the falling or raising
            edges is needed.
      2.    Counting slices of time whenever a falling or
            a raising edge has been detected.
                                                                                Figure 2. The general layout shows up the interconnected
To implement the above strategy, a microcontroller                                        signal lines among the computer, the
will be used, mostly because it has the following                                         microcontroller and the accelerometer.
advantages
      q        Flexible detection either of the falling                            SEQUENCE TO DEMODULATE TWO PWM
               or ra ising edges th rough embedded                                              SIGNALS
               programming.
                                                                                To measure the width of the low and the high state of the
      q        Easiness to count slices of time through the                     ACCEL_X and ACCEL_Y signals, a time slice counting
               internal programmable timer.                                     is carried out, using the internal timer TMR0, which was
                                                                                programmed to count slices of one microsecond, because
      q        Fast development of an interface between the                     both ACCEL_X and ACCEL_Y signals have a width time
               computer parallel port and a microcontroller,                    of 200 micro seconds each, so the high and low time are
               because electrical compatibility.                                100 microseconds width. Thus, since TMR0 is an 8-bit
      q        Easy serial communication implementation,                        register, the maximum count is 255, or, 255 microseconds
               which includes protocol developing,                              as maximum width time to record. The sequence to
               handshaking control and data transmission                        accomplish it is described below and the block diagram
                                                                                to demodulate the PWM signals is shown in Figure 3.
               through embedded programming.
                                                                                      1.   To measure the high state of the X-acceleration
  PWM DEMODULATOR HARDWARE SETUP
                                                                                           signal, the software waits until a rising edge
                                                                                           of the ACCEL_X signal is detected. Once
Accelerometer. An ADXL-202 accelerometer was used                                          detected, TMR0 is activated to count until a
as an acceleration sensor. It generates two PWM squared-                                   falling edge of the ACCEL_X signal is detected
waveforms signals, 90º shifted, 1mV/g resolution, ± 2 g                                    and then TMR0 is stopped and its content is
scope and, with a 5 KHz central operation frequency                                        stored into the T1X register.
120                                             Ingeniare - Revista de Ingeniería, vol. 14 Nº 2, 2006
                            Pérez et al.: A demodulator of PWN signals generated for a digital accelerometer is developed using a microcontroller
     2.   To measure the low state of the X-acceleration
          signal, the software waits until a falling edge                                                      Main Program
          of the ACCEL_X signal is detected. Once
          detected, TMR0 is activated to count until a
          rising edge of ACCEL_X is detected and then                                                      ACCEL_X is High?
          TMR0 is stopped an its content is stored into                                                                                 No
          the TPX register.                                                                     Clear internal timer TMR0 & make it count
     3.   To calculate the full width of the waveform, the
          content of the registers T1X and TPX are added                                                    ACCEL_X is Low?
          and the result is stored in T2X register.                                                                                     No
                                                                                                        Store TMR0 into T1_X register
     4.   The above proceeding is repeated in order to                                       Clear internal timer TMR0 & make it count
          have the pulse widths of the ACCEL_Y signal
          stored in the T1Y, TPY and T2Y registers.
                                                                                                           ACCEL_X is High?
     5.   The content of T1X is moved to TTY and                                                                                        No
          thenthe SEND_TTY subroutine is called.                                                        Store TMR0 into TP_X register
                                                                                             Add T1_X and TP_X and store the result onto
                                                                                                         the T2_X register
     6.   The content of T2X is moved to TTY and then
          the SEND_TTY subroutine is called.
     7.   The content of T1Y is moved to TTY and then                                                      ACCEL_Y is High?
          the SEND_TTY subroutine is called.                                                                                          No
                                                                                                    Clear internal timer TMR0 & make it count
     8.   The content of T2Y is moved to TTY and then
          the SEND_TTY subroutine is called
                                                                                                           ACCEL_Y is Low?
                                                                                                                                      No
                                                                                                    Store TMR0 into T1_Y register
 SEQUENCE TO TRANSMIT 32 BITS TO THE
                                                                                               Clear internal timer TMR0 & make it count
      COMPUTER PARALLEL PORT
The SEND_TTY subroutine transmits the 8-bit TTY
                                                                                                           ACCEL_Y is High?
register to the parallel port of the computer, in a bit to bit                                                                        No
fashion, so the next actions are performed and repeated                                               Store TMR0 into TP_Y register
eight times. This subroutine is described below and the                                             Add T1_Y and TP_Y and store result
block diagram is shown in Figure 4.                                                                         into T2_Y register
                                                                                                           Move T1_X onto TTY
          q   The PIC_READY line is raised to let                                                              SEND_TTY
              computer know that the microcontroller is                                                    Move T2_X onto TTY
              ready to send 32-bits.                                                                           SEND_TTY
                                                                                                           Move T1_Y onto TTY
          q   TTY is left-rotated, putting the most                                                            SEND_TTY
              significant bit into the Carry bit of the
                                                                                                           Move T2_Y onto TTY
              microcontroller STATUS register.
                                                                                                               SEND_TTY
          q   The Carry bit is outputted to the DATA
              line.
                                                                            Figure 3. The algorithm shows up the full steps sequence
          q   A falling edge detection of the IO_CLOCK                                of the PWM demodulator in order to obtain
              line is waited.                                                         the AX and AY values and send them to the
                                                                                      parallel port of the computer.
The PIC_READY line is turned to low let computer know
that the microcontroller has already sent the eight-bits.
                                            Ingeniare - Revista de Ingeniería, vol. 14 Nº 2, 2006                                               121
Ingeniare - Revista Chilena de Ingeniería, vol. 14 Nº 2, 2006
                                                                                      q    One chip demodulator means tiny space
                                                                                           and light weight, which makes it extremely
                          Send TTY                                                         portable, either to locate it wherever in the lab
                                                                                           or install it in remote sites, such as in seismic
                                                                                           applications.
                     Raise PIC_READY                                                  q    Old mechanical accelerometers, pendulum style,
                        Counter fl        8                                                along with their heavy carcasses and gears, are
                                                                                           several times more expensive than the digital
                                                                                           ones.
           Rotate left TTY register through carry
                                                                                      q    Use of the parallel port instead of the serial port,
                          DATA fl Carry                                                    because a simpler protocol could be devised.
                    Counter fl Counter - 1
                                                                                                    ACKNOWLEDGMENTS
                                                                                The authors wish to thank to the Universidad de
                      IO_CLOCK is                                               Antofagasta, which funded the Project PEI-1334-01,
                         High?
                                                       No                       named “Construcción de un acelerógrafo prototipo
                                 Yes
                                                                                inteligente para multipropósitos, sismo resistentes…”.
                                                                                Besides we want to thank to the European Southern
                      IO_CLOCK is                                               Observatory organization and Compañía Minera Zaldivar
                         Low?                                                   for help funding this work.
                                                No
                                 Yes                                                                    REFERENCES
          No                                                                    [1] U. Karthaus, M. Fischer. “Fully Integrated Passive
                        Counter = 0
                                                                                    UHF RFID Transponder IC with 16.7 mW minimum
                                                                                    RF input power”. IEEE Journal Of Solid-State
                     Fall PIC_READY
                                                                                    Circuits. Vol. 38. Nº 10. October 2003.
                                                                                [2] C. Pascual, Z. Song, P.T. Krein, D.V. Sarwate. “High-
                              Return
                                                                                    Fidelity PWM Inverter for Audio Amplification
                                                                                    Based On Real-Time DSP”. IEEE Transactions on
                                                                                    Power Electronics. Vol. 18, pp. 473 485. January
Figure 4. Com mu n icat ion rout i ne bet we en t he                                2003.
          microcontroller and the computer, along with
          the related handshaking.                                              [3] J.A. de Lima, S.F. Silva, A.S. Cordeiro. M. Verleysen.
                                                                                    “A CMOS/SOI Single-input PWM Discriminator for
                                                                                    Low-voltage Body-implanted Applications”. VLSI
                       CONCLUSIONS                                                  Design. Volume 15. Nº 1, pp. 469-476(8). January
                                                                                    2002.
Once built, this device was debugged and tested. There                              http://www.dice.ucl.ac.be /~verleyse /papers /
are some outcomes which are interesting to comment                                  vlsidesign02jdl.pdf
on.
                                                                                [4] C. Bolton. “PLD code creates PWM generators”.
      q    Developing, error correcting and modifying                               EDN, pp 108.110. August 8, 2002.
           source code is by far a real easy task, taking                           http://www.edn.com/contents/images/80802di.pdf
           into account it is the embedded hardware that is
           under a correction process. Analogous process                        [5] J. Mahoney. “Circuit converts pulse width to
           with either active or passive components is                              voltage”. EDN, pp.92-94. October 25, 2001.
           clearly a far difficult task to make.                                    http://www.edn.com/contents/images/102501di.pdf
122                                             Ingeniare - Revista de Ingeniería, vol. 14 Nº 2, 2006
                         Pérez et al.: A demodulator of PWN signals generated for a digital accelerometer is developed using a microcontroller
    APPENDIX A. SOURCE CODE EMBEDDED
       INTO THE MICROCONTROLLER                                             Rise_edge_Y nop
                                                                                          btfsc PORTA,1
                                                                                          GoTo Rise_edge_Y
The next assembler list shows the assembler source code
                                                                                          movf TMR0,0
either of the PWM demodulator and the transmitter,
                                                                                          movwf T1Y
which was written onto the PIC16F84 with a EEPROM
                                                                                          clrf TMR0
Writer.
                                                                            Fall_edge_Y nop
                                                                                          btfss PORTA,1
 ASM                                                                                      GoTo Fall_edge_Y
 T1X      equ 0x10                                                                        bsf PORTA,2
 T2X      equ 0x11                                                                        movf TMR0,0
 TPX      equ 0x12                                                                        movwf TPY
                 T1Y        equ 0x13                                                      movf TPY,0
 T2Y     equ 0x14                                                           addwf T1Y,0
 TPY     equ 0x15                                                                         movwf T2Y
 cnt     equ 0x16                                                                         movf T1X,0
 TTY     equ 0x17                                                                         movwf TTY
 TOCS   equ 0x01                                                                          Call PIC_to_PC
 bcf     OPTION_REG, PSA                                                                  movf T2X,0
 bcf     OPTION_REG, TOCS                                                                 movwf TTY
 bcf     OPTION_REG, PS2                                                                  Call PIC_to_PC
 bcf     OPTION_REG, PS1                                                                  movf T1Y,0
 bsf     OPTION_REG, PS0                                                                  movwf TTY
 bcf     STATUS,RP0                                                                       Call PIC_to_PC
 clr    PORTA                                                                             movf T2Y,0
 clrf    PORTB                                                                            movwf TTY
 movlw   0x0B                                                                             Call PIC_to_PC
 movwf  TRISA                                                                             bcf PORTA,2
 movlw   0x00                                                                             GoTo Init_X
 movwf  TRISB                                                               PIC_to_PC
 Init_X         nop                                                                       nop
               bcf    PORTA,2                                               LowSt         nop
               btfss PORTA,0                                                              btfss PORTA,3
               GoTo Init_X                                                                GoTo LowSt
               clrf   TMR0                                                  HiSt           nop
 Rise_edge_X    nop                                                                       btfsc PORTA,3
               btfsc PORTA,0                                                              GoTo HiSt
               GoTo Rise_edge_X                                                           movlw 0x08
               movf TMR0,0                                                                movwf cnt
               movwf T1X                                                    Loop          rlf TTY,0
               clrf TMR0                                                                  btfss STATUS,C
 Fall_edge_X nop                                                                          bcf PORTB,0
               btfss PORTA,0                                                              btfsc STATUS,C
               GoTo Fall_edge_X                                                           bsf PORTB,0
               movf TMR0,0                                                                decfsz cnt,1
               movwf TPX                                                                  GoTo Loop
               movf TPX,0                                                                 nop
               addwf T1X,0                                                  Return
               movwf T2X                                                                  ENDASM
 Init_Y         nop
               btfss PORTA,1
               GoTo Init_Y
               clrf TMR0
                                         Ingeniare - Revista de Ingeniería, vol. 14 Nº 2, 2006                                           123