Hot!Input Capture for counting pulses

Author
luker
Senior Member
  • Total Posts : 165
  • Reward points : 0
  • Joined: 2015/04/17 06:24:04
  • Location: 0
  • Status: offline
2018/09/07 08:32:21 (permalink)
0

Input Capture for counting pulses

Hello forum,
 
I need to count pulses of ~12us period that keep coming in for ~14ms every ~100ms (the interface of a temperature sensor).

I set up an Input Capture module to count the pulses, falling edges only. And a timer to inspect the measurement every millisecond.

    IC3CON1 = 0;
    IC3CON2 = 0;
    
    IC3CON1bits.ICSIDL = 0;
    IC3CON1bits.ICTSEL = 0b111;                 // ClockSource = Peripheral Clock.
    IC3CON1bits.ICI = 0;                        // Doesn't matter with disabled interrupt.
    IC3CON2bits.IC32 = 0;                       // 16 bit mode.
    IC3CON2bits.ICTRIG = 0;                     // Input source (none) synchronizes instead of triggering.
    IC3CON2bits.SYNCSEL = 0;                    // No sync source selected.
    
    _IC3IP = 0;
    _IC3IF = 0;
    _IC3IE = 0;
    
    RPINR8bits.IC3R = 0b0101001;                // RP41 (=RB9)
    IC3CON1bits.ICM = 0b010;                    // InputCaptureMode = every falling edge.
    IC3CON2bits.TRIGSTAT = 1;                   // Start operation now.

Every millisecond, I poll _IC3IF to see if there were new pulses, and reset the flag if there were. But I don't read the amount until there were no more pulses for a whole millisecond. Then, I read IC3BUF. Reading it does also clear it, right?

I'm expecting to see ~160 pulses per 1ms up until ~1200 pulses cumulated. Then nothing until the next series of pulses starts. But I keep seeing a random number of pulses in IC3BUF in the debugger.
Of cause I had a look at the oscilloscope. Everything hardware-related seems in very good condition.

Now my question: How would I set up an interrupt capture to count pulses? Or do I evaluate them wrong?
 
Here's the code that runs every millisecond.
void Temperature_HandleMillisecondElapsed(void)
{
    switch(_measurementState)
    {
        case MeasurementState_Idle:
        {
            if(_IC3IF)
            // No more idling. Counting has started.
            {
                _IC3IF = 0;
                _measurementState = MeasurementState_Counting;
            }
            break;
        }
        case MeasurementState_Counting:
        {
            if(_IC3IF)
            // Continue counting.
            {
                _IC3IF = 0;
            }
            else
            // No new pulse in 1 ms. We have reached the end of counting.
            //   Process result, reset for next run.
            {
                uint16_t newValueDigits = IC3BUF;
                
                //
                //         / digits         \
                // Temp = { -------- * 256°C } - 50°C
                //         \  4096          /
                //
                int16_t temperature =  (int16_t)(newValueDigits >> 4);
                temperature -= 50;
                if(MeasurementReadyHandler != NULL)
                {
                    MeasurementReadyHandler(temperature);
                }
                _measurementState = MeasurementState_Idle;
            }
            break;
        }
    }
}

post edited by luker - 2018/09/07 08:34:19
#1

11 Replies Related Threads

    MBedder
    Circuit breaker
    • Total Posts : 6553
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/07 08:53:56 (permalink)
    0
    Go learn how input capture works. And use a timer configured as a counter to count pulses.
    post edited by MBedder - 2018/09/07 10:44:24
    #2
    du00000001
    Just Some Member
    • Total Posts : 2234
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/07 09:40:01 (permalink)
    0
    Depending on how exactly the temperature is coded, setting up a counter to count these pulses - eventually only checking twice every 100 ms might suffice.
    See MBedder's reply!

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #3
    luker
    Senior Member
    • Total Posts : 165
    • Reward points : 0
    • Joined: 2015/04/17 06:24:04
    • Location: 0
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/08 01:32:42 (permalink)
    0
    MBedder: Does that mean that input capture cannot count pulses?
    Going back to the data sheet, I indeed must confess that I misread Input Capture as being configurable as timer/counter. That is not mentioned there.
     
    So I will have to either use the Quadrature Encoder Interface or Timer2 (which is already in use). I guess it's easier to derive my internal times from another timer than to get QEI up and running. Correct?
    #4
    luker
    Senior Member
    • Total Posts : 165
    • Reward points : 0
    • Joined: 2015/04/17 06:24:04
    • Location: 0
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/12 07:07:13 (permalink)
    0
    Timer2 is indeed the only option, since QEI is not present on GP types, only MC.
     
    And Timer2 works just fine.
     
    Thanks, forum.
    #5
    MBedder
    Circuit breaker
    • Total Posts : 6553
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/12 08:03:45 (permalink)
    0
    lukerTimer2 is indeed the only option
    Who on Earth told you that bullshìt?? Which particular dsPIC are you talking about?
    #6
    luker
    Senior Member
    • Total Posts : 165
    • Reward points : 0
    • Joined: 2015/04/17 06:24:04
    • Location: 0
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/13 00:17:46 (permalink)
    0
    @MBedder Why so agitated? OK, I'm shocked, too, that I didn't mention the dsPIC in question but let's calm down and have a chat.
    I'm using a dsPIC33EP256GP502. The data sheet states on page 1 that there are four possibilities to count
    1) Timer
    2) Output Compare
    3) Peripheral Trigger Generator
    4) Quadrature Encoder Interface
     
    Page 177 states that the inputs for QEI are on Motor Control devices only, not on General Purpose, which we have here. So 4) is inapplicable.
    For 3) I'm too stupid to get how to configure that thing.
    2) doesn't have any inputs except for a Fault signal. Don't know how to count with that. Maybe if combined with an Interrupt Capture module. But I think it's way easier to use
    1) Timer1, Timer3, Timer4 and Timer5 all have fixed clock inputs, which are not the one I need to count on.
    That leaves Timer2 with its remappable clock input to use.
     
    Maybe 2) or 3) could also work with more configuration effort. Thank you for pointing me there.
    For now, I'm happy with Timer2 counting input pulses.
    #7
    MBedder
    Circuit breaker
    • Total Posts : 6553
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/13 01:10:56 (permalink)
    0
    luker1) Timer1, Timer3, Timer4 and Timer5 all have fixed clock inputs, which are not the one I need to count on.
    Just another bullshìt. Perhaps this is due to your religious affilation? LoL


     
    #8
    luker
    Senior Member
    • Total Posts : 165
    • Reward points : 0
    • Joined: 2015/04/17 06:24:04
    • Location: 0
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/13 02:03:51 (permalink)
    0
    @MBedder: I'm quite baffled by your tone. Such harsh comments, even with taking measures to circumvent the swearword filter, are highly unusual in this forum.
     
    But back to topic: What's wrong with my claim that the timers have fixed inputs?
    How could I change Timer1's input, for example?
    #9
    du00000001
    Just Some Member
    • Total Posts : 2234
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/13 02:23:20 (permalink)
    0
    @ luker
    You don't give away too much about yourself, but your status seems to be "interested EE student" or alike.
     
    If you had wasted an eye on the datasheet, you should have found T1CK ... T5CK - the external lock inputs for all timers.
    Don't troll around trying to let others do your very basic work.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #10
    luker
    Senior Member
    • Total Posts : 165
    • Reward points : 0
    • Joined: 2015/04/17 06:24:04
    • Location: 0
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/13 03:16:50 (permalink)
    0
    [...] datasheet [..] T1CK ... T5CK - the external lock inputs for all timers [...]

    I found the external clock inputs for those timers. And I found that with the exception of Timer2, all of them are fixed. And I'm happy that Timer2 allows to use the one pin I need to count pulses on.
     
    In the light of this whole thing working perfectly since Post#5, I want to state that we're no longer searching for a solution. Even less do I ty to let others do my work.
    My question how to relocate Timer1's input, which I think is what triggered you, was a rhethorical one for MBedder, who seemed to think that Timer1's input is not fixed to some pin. And since his post lacked the usual friendliness that we're all used to in this forum, I really would have liked to see what I had said or done wrong while it is so easy to get right.
     
    But for me, this thread is too far from solving a problem. I'm off.
    #11
    MBedder
    Circuit breaker
    • Total Posts : 6553
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    Re: Input Capture for counting pulses 2018/09/13 03:50:56 (permalink)
    0
    lukerI'm off.
    Happy jorney LoL


    #12
    Jump to:
    © 2018 APG vNext Commercial Version 4.5