Help need!! Working out Duty cycle using Timer0 C

Discussion in 'Embedded Systems and Microcontrollers' started by icem, Mar 7, 2011.

  1. icem

    Thread Starter New Member

    Nov 15, 2009
    I'm attempting to use timer0 on the PIC16F877 to work out the duty cycle of an accelerometer(ADXL213).

    I got the basic idea of how to write the code, from an example code which is in assembly and I'm writing my code in C.

    The bit i can not figure out is when the rising edge arrives on the XOUT or YOUT pin, how to get the time and save it, to be used in calculation later.

    The code below is a section from a function, i can't figure how to store the value from TMRO into T1XHi in C language. Also I can't figure out how Timer0H is incremented.
    Code ( (Unknown Language)):
    1. 0020 00123 ReadAccel
    2. 0020 00124 EDGE1
    3. 0020 1805 00125 btfsc PORTA,XOUT ;Wait for low on XOUT
    4. 0021 2820 00126 goto EDGE1
    5. 0022 00127 EDGE2
    6. 0022 1C05 00128 btfss PORTA,XOUT ;Wait for high on XOUT
    7. 0023 2822 00129 goto EDGE2
    8. 0024 0181 00130 clrf TMR0 ;Clear Timer
    9. 0025 01B3 00131 clrf Timer0H
    10. 0026 110B 00132 bcf INTCON,T0IF ;Enable Timer0 overflow interrupt
    11. 0027 168B 00133 bsf INTCON,T0IE
    12. 0028 00134 EDGE3
    13. 0028 1805 00135 btfsc PORTA,XOUT ;Look for falling edge on XOUT
    14. 0029 2828 00136 goto EDGE3
    15. 002A 0801 00137 movf TMR0,W ;Save Timer0H:TMR0 as T1X
    16. 002B 008D 00138 movwf T1XLo
    17. 002C 0833 00139 movf Timer0H,W
    18. 002D 008C 00140 movwf T1XHi
    The PIC used in this example code is PIC16F84 but the method is more or less the same.
    I have attached the full code and data sheet for the components.

    Any help would be great

    Thank you very much for your time
  2. Markd77

    Senior Member

    Sep 7, 2009
    Timer0H will be incremented in the interrupt routine which gets called every time TMR0 overflows. There may be better ways to do it in C and also you could use one of the 16 bit timers on the 16F877 which might simplify it (the F84 only has an 8 bit timer).