• AVR Freaks

Hot!Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572

Author
Hj_Chen
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2018/09/09 01:38:57
  • Location: 0
  • Status: offline
2019/06/22 10:16:21 (permalink)
0

Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572

Dear Sir,
1.I used the function of "Timer 1 Module with Gated Control" of PIC12F1572 to design a RPM detector and the RPM input is by pin RA3 as Fig.1.

2. The system clock is 16MHz and the Timer 1 is programmed in "Gated Toggle Mode" for RPM detector in 1MHz clock. The timing of "Gated Toggle Mode" is in Fig.2.

3. My brief program is name proram.txt and whole program is name Frq.X.zip as following:
.proram.txt
.Frq.X.zip
4. My problems are:
(a) There is no interrupt happen if I set the T1GSEL=1; conversely, It will interrupt while T1GSEL=0.
According to the P.101 of data sheet of PIC12F1572, T1G=RA3 if T1GSEL=1.
(b) The load value from TMR1's count is unstable (Period = TMR1; //Get TMR1 Counter).
void TMR1_GATE_ISR(void)
{
Period = TMR1; //Get TMR1 Counter
G_Period = 1; // Set a flag for main routine check
TMR1 = 0; //Reset Counter
PIR1bits.TMR1GIF = 0; // clear TMR1 interrupt flag
}
(c) I will be very appreciated if any one help me to solve those problems.
post edited by Hj_Chen - 2019/06/22 10:21:34

Attached Image(s)

#1

8 Replies Related Threads

    davea
    Senior Member
    • Total Posts : 123
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: 0
    • Status: offline
    Re: Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572 2019/06/23 10:38:57 (permalink)
    +1 (1)
    is RPM a square wave or coil wire signal form a gas engine ?
     
    a different way of doing it is to
    use a interrupt as the input, TMR1 free running 0-65535
     
    void interrupt(void){ 
       uint16_t X;
       uint16_t TMR1_val;
       uint8_t TMr1L = TMR1L; // read order, low then hi byte
       uint8_t TMr1H = TMR1H;
       TMR1_val = (TMr1H << 8) | TMr1L; // FASTEST so far
       X = TMR1_val - TMR1_last; // X = time difference is 1/RPM<<<<<<
       TMR1_last = TMR1_val; // update last
      
      TMR1_last is in main.c 

     
    NOTE: 1 TMR overflow will take care of its self
    2 you will have to take care of
    #2
    Hj_Chen
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/09/09 01:38:57
    • Location: 0
    • Status: offline
    Re: Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572 2019/06/24 00:12:03 (permalink)
    0
    Wow, That's a good ideal and new thought for RPM measurement.
    Thank you very much.
     
    #3
    pcbbc
    Super Member
    • Total Posts : 1108
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572 2019/06/24 08:04:40 (permalink)
    +1 (1)
    Hj_Chen
    Wow, That's a good ideal and new thought for RPM measurement.
    Thank you very much.

    Or better still use a Capture/Compare module to capture the timer value in hardware.  That way you avoid any jitter if you have other interrupts enabled and so the ISR latency is not constant.
    However I don't think your PIC has capture ability...  I could be wrong - I didn't do an exhaustive search of the datasheet.
    #4
    Hj_Chen
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/09/09 01:38:57
    • Location: 0
    • Status: offline
    Re: Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572 2019/06/24 18:46:00 (permalink)
    0
    Dear pcbbc,
    As you said, there is jitter while I do'nt use captured mode. I think The "Timer1 Gated Toggle Mode" is similarly a captured mode.
    After changed RPM input from RA4, the captured values of TMR1 are stable but it  just work only one time (i.e. could not capture another interrupt). That's strangely, I had cleared the PIR1bits.TMR1GIF = 0; in ISR (as above routine), why did not capture next interrupt or capture next period from RA4 ?
     

     

    #5
    ric
    Super Member
    • Total Posts : 22768
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572 2019/06/24 19:31:45 (permalink)
    +2 (2)
    In the code attached to your first post, in interrupt_manager.c the main interrupt service routine is handily turning TMR1 off when the interrupt triggers.
    void __interrupt() INTERRUPT_InterruptManager (void)
    {
        // interrupt handler
        if(INTCONbits.PEIE == 1)
        {
            if(PIE1bits.TMR1GIE == 1 && PIR1bits.TMR1GIF == 1)
            {
                  T1CONbits.TMR1ON = 0;
                TMR1_GATE_ISR();
            }
            else
            {
                //Unhandled Interrupt
            }
        }      
        else
        {
            //Unhandled Interrupt
        }
    }

    I would suggest deleting that line,
    or if you want to leave that file alone, turn it back on in your ISR code.
    post edited by ric - 2019/06/24 19:34:26

    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
    Hj_Chen
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/09/09 01:38:57
    • Location: 0
    • Status: offline
    Re: Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572 2019/06/25 00:19:27 (permalink)
    0
    Dear pcbbc,
    The ISR routine will be interrupted by next coming clock from RPM after deleting the T1CONbits.TMR1ON = 0; line.
    Thank you very much and very appreciation your help to solve my problem.
    #7
    ric
    Super Member
    • Total Posts : 22768
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572 2019/06/25 00:28:27 (permalink)
    +1 (1)
    I think you've mixed up your usernames. :)
     

    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!
    #8
    mpgmike
    Super Member
    • Total Posts : 179
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Using Timer1 Gated Toggle Mode for RPM detector with Pin RA3 of PIC12F1572 2019/06/25 11:57:26 (permalink)
    0
    I find it easier to use TxG for measuring something like an injector pulse width where a high-low turns the Gate ON and a low-high turns the Gate OFF, and sets an IF.  To measure a Period -- low-high to low-high -- the Capture works better.  What you showed was a Period measurement.

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5