• AVR Freaks

Hot!How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE)

Author
ArKayBee
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2017/09/08 23:29:46
  • Location: 0
  • Status: offline
2019/07/14 23:27:47 (permalink)
0

How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE)

I was trying to write a program to measure how long (microseconds) a pin has been set to HIGH. But failed many times now. So I was bound seek help here.
I tried starting and stopping the TIMER1 and reading value from TIMER1 register. Used the following functions in MPLAB XC8 compiler - TMR1_StartTimer(void), TMR1_StopTimer(void), TMR1_ReadTimer(void).
I still couldn't get the output.
I came across this issue while I was trying to measure distance with sonar sensor HC-SR04.
I am using MPLAB XC8 compiler, PIC16f15344 microcontroller and the CURIOSITY LPC board.
Please help.
#1

7 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3227
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE) 2019/07/15 09:55:04 (permalink)
    +1 (1)
    You tried what exactly ?
    Give us the sequence (or the code) !

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    acharnley
    Super Member
    • Total Posts : 389
    • Reward points : 0
    • Joined: 2016/05/01 06:51:28
    • Location: 0
    • Status: offline
    Re: How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE) 2019/07/15 13:42:20 (permalink)
    0
    I do this in a project and this should help. I'm using the ZDC so change that to whatever source. Then use
     
    void TMR1_StartSinglePulseAcquisition(void)
    {
    T1GCONbits.T1GGO = 1;
    }

    The next time the source edge goes high the timer will start, the source goes low and an interrupt will fire. Don't forget to clear the timer before re-enabling the acquisition. 

    You have to massage the timer clock source to ensure it doesn't over-run the counter for the given duration/resolution you're wanting to measure. You can see I've slowed it down to 250Hz which is the maximum I'm measuring at, but also means I can read an 8 bit using:
     
    #define TIMER_PERIOD_VALUE_LOW TMR1L
    #define TIMER_PERIOD_VALUE_HIGH TMR1H

    return TIMER_PERIOD_VALUE_HIGH? 255: TIMER_PERIOD_VALUE_LOW;







    post edited by acharnley - 2019/07/15 13:43:30

    Attached Image(s)

    #3
    ric
    Super Member
    • Total Posts : 24544
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE) 2019/07/15 13:42:37 (permalink)
    +2 (2)
    Even if you get this to work, it will be pretty inaccurate.
    Using the CCP peripheral to capture a timer count would be much more accurate.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    mbrowning
    USNA79
    • Total Posts : 1564
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE) 2019/07/15 13:52:46 (permalink)
    0
    ric
    Even if you get this to work, it will be pretty inaccurate.Using the CCP peripheral to capture a timer count would be much more accurate. 
    not sure what you mean here. Why is using the CCP hardware more accurate than using T1’s built in gating hardware? There’s no software involvement in the actual measurement time.

    Go Navy! Beat Army!
    #5
    ric
    Super Member
    • Total Posts : 24544
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE) 2019/07/15 14:10:39 (permalink)
    +2 (2)
    The OP made no mention of using the timer's gating hardware.
    I agree, if they use that correctly, it will be just as accurate (so long as they handle potential timer rollover correctly, which is required for both methods).
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    acharnley
    Super Member
    • Total Posts : 389
    • Reward points : 0
    • Joined: 2016/05/01 06:51:28
    • Location: 0
    • Status: offline
    Re: How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE) 2019/07/15 14:16:35 (permalink)
    0
    Yes I thought he wasn't using the gate correctly hence I steered in that direction. I use it with the ZCD at low frequency and it's very accurate. 
    #7
    Givi
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2014/02/18 15:26:47
    • Location: 0
    • Status: offline
    Re: How to turn on and off TIMER1 and Read timer value to measure duration (MPLAB IDE) 2019/07/15 17:03:25 (permalink)
    0
    You may want to download the "398_timer_calculator_v4.0.0.0.zip" for free from Mikroelektronica web site.  It can help you with the general setup of a Timer for PICs. See an example below.  I hop this helped.:
     
    //Timer1
    //Prescaler 1:1; TMR1 Preload = 65516; Actual Interrupt Time : 10 us
     
    //Place/Copy this part in declaration section
    void InitTimer1(){
      T1CON  = 0x01;
      TMR1IF_bit  = 0;
      TMR1H  = 0xFF;
      TMR1L  = 0xEC;
      TMR1IE_bit  = 1;
      INTCON  = 0xC0;
    }
     
    void Interrupt(){
      if (TMR1IF_bit){
        TMR1IF_bit = 0;
        TMR1H  = 0xFF;
        TMR1L  = 0xEC;
        //Enter your code here
      }
    }
    #8
    Jump to:
    © 2019 APG vNext Commercial Version 4.5