• AVR Freaks

Helpful ReplyHot!Measure frequency less than 1Hz ( 0.1 to 5 Hz)

Page: 12 > Showing page 1 of 2
Author
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
2019/10/31 10:15:48 (permalink)
0

Measure frequency less than 1Hz ( 0.1 to 5 Hz)

Hi,
I'm trying to measure frequency from 0.1 Hz to 5 Hz (Square wave), Currently I'm able to measure frequency above 2 Hz to 30 Kilo Hz with input capture and compare method. How to read frequency less than 1 Hz.
 
Micro controller: dsPIC30F5011.
 
Thank you and warm regards,
Ravi
post edited by g.ravi - 2019/10/31 10:17:02
#1
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/10/31 11:50:18 (permalink)
5 (1)
Hi,
You may  either use a 32 bit Timer and 32 bit Capture,
that is 2 timers and 2 Capture modules together. Not all dsPIC devices are able to do this.
 
Or  use Interrupts with the 16 bit Timer used for the Capture mdule,
to count the number of times that Timer register roll around to zero,
and add 65536 counts to period time, each time this happen.
 
    Mysil
post edited by Mysil - 2019/10/31 11:54:18
#2
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/10/31 21:18:30 (permalink)
0
Thank you Mysil,
 
Time period for 0.1 Hz is 10 Seconds and we need to average few cycles for accuracy which will test patience of user. so I think using an hardware multiplexer for the signals with lower frequencies ranging from 0Hz to 5 Hz will give better results.
 
Please find the image of 0.147 Hz captured on digital scope.
 
[image]D:\Downloads\p.jpg[/image]
 
Thankyou,
Ravi.
 
 
 
post edited by g.ravi - 2019/10/31 21:37:17

Attached Image(s)

#3
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/10/31 21:34:51 (permalink)
5 (1)
g.ravi
...
so I think using an hardware multiplexer for the signals with lower frequencies ranging from 0Hz to 5 Hz will give better results.

Did you mean a "hardware multiplier" ?
Anything like that would have to be connected to the signal for many cycles to be able to lock to it, and you could have done it more accurately with the PIC in the meantime.
 

Please find the image of 0.147 Hz captured on digital scope.
 
[image]D:\Downloads\0.147 Hz.jpeg[/image]

You can't display an image from your own hard drive like that.
Click the "Open Full Version" link above the "Quick Reply" window, then you can attach a file to your message.
 
 

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
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/10/31 21:47:33 (permalink)
0
Yes, Thank you RIC I have edited and uploaded the image.
#5
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/05 00:55:09 (permalink)
0
Hi Mysil,
 
How to configure 32bit timer and CCP on dspic30f5011.
 
Thank you,
Ravi.
post edited by g.ravi - 2019/11/05 01:29:33
#6
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/05 01:21:21 (permalink)
0
The dspic30f5011 datasheet describes how to use TIMER2/3 or TIMER 4/5 in 32 bit mode.
Are you having trouble understanding the datasheet?
 

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!
#7
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/05 02:07:39 (permalink)
0
Hi,
Sorry for misleading you in message #2,
It is not possible to do 32 bit Capture in dspic30f5011, there is no T32 bit in Input Capture control register. 
It is possible to use Timer2 and Timer3 together with T32 bit in T2CON register,
or Timer4 and Timer5 together with T32 bit in T4CON register.
 
So you may either use Timers in 32 bit mode, read the timer values directly from Timer registers, 
or count timer roll-around interrupts in 16 bit mode, and do the math in software.
 
If using Timer in 32 bit timer mode, note the TMR3HLD or TMR5HLD registers for upper 16 bits.
When reading timer values, they are used this way: First read TMR2 or TMR4 register,
this will trigger an update of the holding register, then read TMR3HLD or TMR5HLD.
 
Doing average calculations at very low RPM is not useful,
depending on inertia and acceleration of your machine, measurement may be obsolete when 2 revolutions have been completed.
 
    Mysil
 
 
#8
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/05 04:13:01 (permalink)
0
Hi,
 
It is mentioned in datasheet that TxCON register for the Type C time base have no effect (T3CON<15:0> are “don’t care” bits). so can I skip configuring T3CON register as I am using Timer2 and Timers for 32  bit timer.
 
As i need to measure frequency less than between 0.147 Hz and 50 Hz I will configure timer with 1ms interrupt.
(Do I really need 1ms timer ?)
 

void INIT_TIMER32(void)
{
 T2CON = 0; // Disable Timer2 and Reset all bits with 0.
 T2CONbits.T32 = 1; // Configure Timer2 & Timer3 as a 32-bit Timer.
 T2CONbits.TCKPS = 0b01; // Set Timer1 Input Clock Prescale Select bits (1:8)
 T2CONbits.TSIDL = 1; // Discontinue module operation when device enters Idle mode

 TMR2 = 0x0000; // "LSW" Clear RTC Timer Register.
 PR2 = 0xFC18; // "LSW" Set Timer1 period register

 TMR3 = 0x0000; // "MSW" Clear RTC Timer Register.
 PR3 = 0xFFFF; // "MSW" Set Timer3 period register for 1s
  
 IFS0bits.T3IF = 0; // Timer3 - Clear Interrupt Flag.
 IPC1bits.T3IP = 7; // Timer3 - Set Interrupt Priority Level.
 IEC0bits.T3IE = 1; // Timer3 - Enable Interrupt.
 
 T2CONbits.TON = 1; // Enable Timer
}

 
is 32 bit timer configuration correct.
 
Below mention funtion is to initialize ICCP
void Init_CCPM32(void)
{
 INIT_TIMER32(); // Initialize Timer3 with 1 ms interrupt
 DDR_IC2 = 1; // Configure RD9 as Input
// Register 13-1: ICxCON: Input Capture x Control Register in datasheet
 IC2CONbits.ICBNE = 0; // Input Capture Buffer Empty Status (Read Only)
 IC2CONbits.ICOV = 0; // Input Capture Overflow Status Flag (Read Only)

 IC2CONbits.ICM = 0b101; // Input Capture mode, every 16th rising edge
 IC2CONbits.ICTMR = 1; // Input Capture Timer Select bits (1 = TMR2 contents are captured on captured event)
 IC2CONbits.ICSIDL = 1; // Input Capture Module Stop in Idle Control(0=Input capture module will continue to operate in CPU Idle mode)
 IPC1bits.IC2IP = 5;
// Register 6-6: IFS1: Interrupt Flag Status Register 1
 IFS0bits.IC2IF = 0; // Input Capture Channel 2 Interrupt Flag Status )0=Interrupt bit is cleared
// Register 6-9: IEC1: Interrupt Enable Control Register 1
 IEC0bits.IC2IE = 1; // Input Capture Channel 2 Interrupt Enable bit (1 = Interrupt request enabled)
}

 
Is ICCP function to initialize 32bit ICCP correct " I'm not sure about IC2CONbits.ICTMR = 1/0; - Timer2/3 ".
 
 I couldn't understand how to capture 32 values during IFS0bits.IC2IF interrupt.
post edited by g.ravi - 2019/11/05 04:50:48

Attached Image(s)

#9
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/05 05:27:26 (permalink)
0
No,
Period register value 4 294 966 296  will Not produce a 1 millisecond value when timer is in 32 bit configuration.
It might create a interval of about 1 hour 11 minutes 35 seconds.
 
When configuring a timer to do full range time measurements,
then it is better to set, or leave the Period registers as PR2 = 0xFFFF; and PR3 = 0xFFFF ;
That will make it possible to use unsigned integer arithmetic to calculate intervals
without having to clear or change the timer value, even when the timer roll around from 4 294 967 295 to 0.
Use uint32_t  type variables for calculations with values from 32 bit timer.
 
Comment about Timer1 period register is wrong, code is for Timer2 period register.
 
    Mysil
 
 
#10
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/05 05:44:12 (permalink)
0
Hi Mysil,
 
as suggested I have modified PRx values with 0xFFFF, but result is not as expected.
 
also updated below as suggested
//volatile unsigned int previous_value = 0, current_value = 0, timePeriod = 0;
volatile uint32_t  previous_value = 0, current_value = 0, timePeriod = 0;
 
I hope I'm missing something in below function

void __attribute__((__interrupt__,no_auto_psv)) _IC2Interrupt(void)
{
 IFS0bits.IC2IF = 0; //Clear bit IC1IF (IFS<2>)

 previous_value = current_value;
 if(IC2CONbits.ICOV)
  current_value = IC2BUF+((unsigned int)T3_Count*65536L);
 else
  current_value = IC2BUF;
  
 if( current_value > previous_value)
   timePeriod = current_value - previous_value;
 else
   timePeriod = ( 65535 - (previous_value + 1)) + current_value;
 T3_Count = 0;

}

 
Please note: T3_Count is incremented on 32 bit Timer interrupt
 
Warm Regards,
Ravi
 
 
#11
pcbbc
Super Member
  • Total Posts : 1373
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/05 05:51:26 (permalink)
0
MysilOr  use Interrupts with the 16 bit Timer used for the Capture mdule,
to count the number of times that Timer register roll around to zero,
and add 65536 counts to period time, each time this happen.

This is possible, but you need to be very careful to make sure that the following race conditions cannot occur in your code:
1. The timer has rolled over and been handled (high order 16 bits incremented in software), but the capture was from prior to the rollover.  The result on the calculated 32 bit captured value is that it is 0x10000 too large.
2. The timer roll over has not yet been processed, but the capture was from after the rollover.  This can happen if the timer rollover fires, and then the capture fires in the meantime while the ISR is being called but before it is handled.  The result on the calculated 32 bit captured value is that it is 0x10000 too small.
 
Note it is not as simple as just the order you handle the interrupts in (it's a race condition).  My usual sequence of processing is:
a) If there has been a capture event...
    i) Copy the low order word (from capture hardware) and high order word (from software counter) into temporary variables.
    ii) Check the timer rollover interrupt flag.  If it is set, AND the MSB of the low order capture word is ZERO (indicating the capture was from AFTER the rollover event), then add ONE to the temporary high order word.
    iii) Combine the temporary high and low order words into a 32 bit result.
b) If there has been a timer rollover event...
    i) Increment the software maintained high order word.
#12
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/05 07:17:53 (permalink)
5 (1)
Hi,
Using 32 bit timer with 16 bit capture
together with T3_Count from 32 bit timer interrupt is not correct.
Either use software counter from 16 bit timer interrupt,
or use  TMR3  register value from 32 bit timer, for high order word.
 
The simplest solution for such very low frequencies, would be to forego the Input Capture value entirely,
and use only the TMR2 and TMR3HLD  timer values from 32 bit Timer.
You may still use the Input Capture Interrupt function to perform the Timer reading, both TMR2 and TMR3HLD.
 
When you get this working satisfactory, then you may try to calculate the difference between 16 bit capture value,
and (copy of) 16 bit TMR2 value.
When this give a higher than normal value, or change sign (if signed), then there is a special case that will need more attention. See message from pcbbc, above. 
 
    Mysil
 
#13
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/06 22:20:54 (permalink)
0
Hi Mysil,
 
Thankyou, After capturing timer values from 32 timer as mentioned below i could see some relevant output.
 

void __attribute__((__interrupt__,no_auto_psv)) _IC2Interrupt(void)
{
 IFS0bits.IC2IF = 0; //Clear bit IC1IF (IFS<2>)

 previous_value = current_value;
 
 current_value = TMR3HLD; // "MSW" is captured in Current Value
 current_value = current_value << 16; // Shift "LSW" to "MSW"
 current_value = current_value + TMR2; // Add 32bit Timer "LSW" Value to "MSW" followed by 0's in "LSW"
 if( current_value > previous_value)
 {
  timePeriod = current_value - previous_value;
  //printf("Tp = %ld \n\r", timePeriod);
 }
 else
  timePeriod = (4294967295 - previous_value) + 1 + current_value ; // if 32bit timer overflowed
 }

Is my function correct,
 
 
is this statement correct to handle timer overflow
timePeriod = (4294967295 - previous_value) + 1 + current_value ; // if 32bit timer overflowed
 
I got below mentioned warning when I compiled
init_CCPM.c: In function '_IC2Interrupt':
init_CCPM.c:88: warning: this decimal constant is unsigned only in ISO C90


 
Thankyou,
Ravi
 
 
#14
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/06 22:33:22 (permalink)
5 (1)
If your variables are unsigned 32 bit, then you don't have to do anything special to handle a single rollover, it just works.

i.e.
timePeriod = current_value - previous_value;
 

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!
#15
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/07 02:34:10 (permalink)
0
Thankyou Mysil, Ric and Pcbbc,
 
Now my output is much closer I could see some errors mean in-between, below is the debug information for better understanding.
 

Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 70304, Fcy = 14.223942 Hz
Timeperiod = 135850, Fcy = 7.361060 Hz // Error
Timeperiod = 70297, Fcy = 14.225358 Hz
Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 70301, Fcy = 14.224548 Hz
Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 70302, Fcy = 14.224346 Hz
Timeperiod = 70318, Fcy = 14.221109 Hz
Timeperiod = 4774, Fcy = 209.467957 Hz // Blunder
Timeperiod = 70303, Fcy = 14.224144 Hz
Timeperiod = 70317, Fcy = 14.221313 Hz
Timeperiod = 70307, Fcy = 14.223335 Hz
Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 70306, Fcy = 14.223537 Hz
Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 135843, Fcy = 7.361439 Hz // Error
Timeperiod = 70317, Fcy = 14.221313 Hz
Timeperiod = 70305, Fcy = 14.223740 Hz
Timeperiod = 70313, Fcy = 14.222121 Hz
Timeperiod = 70305, Fcy = 14.223740 Hz
Timeperiod = 70314, Fcy = 14.221919 Hz
Timeperiod = 70312, Fcy = 14.222323 Hz
Timeperiod = 135840, Fcy = 7.361602 Hz // Error
Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 70306, Fcy = 14.223537 Hz
Timeperiod = 70317, Fcy = 14.221313 Hz
Timeperiod = 70304, Fcy = 14.223942 Hz
Timeperiod = 70322, Fcy = 14.220301 Hz
Timeperiod = 70306, Fcy = 14.223537 Hz
Timeperiod = 135853, Fcy = 7.360898 Hz // Error
Timeperiod = 70298, Fcy = 14.225156 Hz
Timeperiod = 70312, Fcy = 14.222323 Hz
Timeperiod = 70308, Fcy = 14.223132 Hz
Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 70296, Fcy = 14.225560 Hz
Timeperiod = 70316, Fcy = 14.221515 Hz
Timeperiod = 4776, Fcy = 209.380234 Hz // Blunder
Timeperiod = 70317, Fcy = 14.221313 Hz
Timeperiod = 70310, Fcy = 14.222728 Hz
Timeperiod = 70317, Fcy = 14.221313 Hz
Timeperiod = 70304, Fcy = 14.223942 Hz
Timeperiod = 70322, Fcy = 14.220301 Hz
Timeperiod = 70304, Fcy = 14.223942 Hz
Timeperiod = 4782, Fcy = 209.117523 Hz // Blunder
Timeperiod = 70312, Fcy = 14.222323 Hz
Timeperiod = 70307, Fcy = 14.223335 Hz
Timeperiod = 70318, Fcy = 14.221109 Hz
Timeperiod = 70317, Fcy = 14.221313 Hz
Timeperiod = 70321, Fcy = 14.220503 Hz
Timeperiod = 70320, Fcy = 14.220705 Hz
Timeperiod = 4765, Fcy = 209.863586 Hz // Blunder
Timeperiod = 70315, Fcy = 14.221717 Hz
Timeperiod = 70318, Fcy = 14.221109 Hz
Timeperiod = 70315, Fcy = 14.221717 Hz
Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 70307, Fcy = 14.223335 Hz
Timeperiod = 70315, Fcy = 14.221717 Hz
Timeperiod = 4783, Fcy = 209.073807 Hz // Blunder
Timeperiod = 70310, Fcy = 14.222728 Hz
Timeperiod = 135844, Fcy = 7.361385 Hz // Error
Timeperiod = 70310, Fcy = 14.222728 Hz
Timeperiod = 70318, Fcy = 14.221109 Hz
Timeperiod = 70307, Fcy = 14.223335 Hz
Timeperiod = 70320, Fcy = 14.220705 Hz
Timeperiod = 4787, Fcy = 208.899109 Hz // Blunder
Timeperiod = 70310, Fcy = 14.222728 Hz
Timeperiod = 70305, Fcy = 14.223740 Hz
Timeperiod = 70322, Fcy = 14.220301 Hz
Timeperiod = 70323, Fcy = 14.220098 Hz
Timeperiod = 70321, Fcy = 14.220503 Hz
Timeperiod = 70307, Fcy = 14.223335 Hz
Timeperiod = 70319, Fcy = 14.220907 Hz
Timeperiod = 70321, Fcy = 14.220503 Hz
Timeperiod = 70321, Fcy = 14.220503 Hz
Timeperiod = 70305, Fcy = 14.223740 Hz
Timeperiod = 70313, Fcy = 14.222121 Hz
Timeperiod = 70318, Fcy = 14.221109 Hz
Timeperiod = 70308, Fcy = 14.223132 Hz
Timeperiod = 4771, Fcy = 209.599670 Hz // Blunder
Timeperiod = 70302, Fcy = 14.224346 Hz
Timeperiod = 70299, Fcy = 14.224954 Hz
Timeperiod = 70287, Fcy = 14.227382 Hz
Timeperiod = 70320, Fcy = 14.220705 Hz
Timeperiod = 70302, Fcy = 14.224346 Hz
Timeperiod = 70294, Fcy = 14.225965 Hz
Timeperiod = 4767, Fcy = 209.775543 Hz // Blunder
Timeperiod = 70332, Fcy = 14.218279 Hz
Timeperiod = 135838, Fcy = 7.361710 Hz // Error
Timeperiod = 70302, Fcy = 14.224346 Hz
Timeperiod = 70321, Fcy = 14.220503 Hz
Timeperiod = 70336, Fcy = 14.217470 Hz

 

Why do we get
Timeperiod = 4771, Fcy = 209.599670 Hz // Blunder
Timeperiod = 135838, Fcy = 7.361710 Hz // Error

I have commented Blunder and Error manually for easy reference.

 
Warm regards,
Ravi.
#16
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/07 02:36:38 (permalink)
0
I would suggest you add debug printouts of the raw timer counts BEFORE the subtraction is done.

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!
#17
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/07 03:36:17 (permalink) ☄ Helpfulby g.ravi 2019/11/07 09:03:47
5 (1)
Hi,
There are mistakes in code shown in message #14.
As explained in message #8, when reading 32 bit timer values,
make sure to read value from TMR2 before TMR3HLD , otherwise value in TMR3HLD will be stale.
You may try it like this:
               /* Global timePeriod measurement. */
volatile uint32_t  timePeriod;      /* also use extern keyword if storage is defined in another file. */

void __attribute__((__interrupt__,no_auto_psv)) _IC2Interrupt(void)
{
    static uint32_t current_value;
           uint32_t previous_value;

    IFS0bits.IC2IF = 0; //Clear bit IC1IF (IFS<2>)

    previous_value = current_value;

    current_value  = TMR2; // First read 32bit Timer "LSW"
    current_value |= TMR3HLD << 16; // Shift and add "MSW" from holding register.

// if( current_value > previous_value)    /* This test not needed when doing unsigned 32 bit calculation. */
// {
    timePeriod = current_value - previous_value;    /* This is correct in unsigned 32 bit calculation. */
                                                    /* when 32 bit timer with 0xFFFF FFFF  period register is used. */
    //printf("Tp = %ld \n\r", timePeriod);
// }
// else
//  timePeriod = (4294967295 - previous_value) + 1 + current_value ; // if 32bit timer overflowed
}  

 
Regards,
    Mysil
#18
g.ravi
Senior Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2005/12/19 02:14:26
  • Location: India
  • Status: offline
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/07 09:04:44 (permalink)
0
Hi Mysil,
 
Do we need to clear IC2Buff value upon IC2 interrupt though the values are directly fetched from 32 bit timer. will this have any unknown effect on IC2 interrupt.
 
Warm Regards,
Ravi.
#19
Mysil
Super Member
  • Total Posts : 3403
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: Measure frequency less than 1Hz ( 0.1 to 5 Hz) 2019/11/07 12:20:32 (permalink)
0
Hi,
In the Input capture hardware, there is a FIFO buffer, 4 values,
and a couple of status bits that tell if the FIFO is empty ICBNE, and if there is a Overflow happened: ICOV.
If you experience a interrupt that will not go away when there is FIFO overflow,
or interrupts not happening as expected,
then you may try to keep the FIFO empty when processing the IC2 interrupt.
You may try something like
    while (IC2CONbits.ICBNE)
        IC2BUF;   

Another possible use for this, might be to test for FIFO overflow.
If ICOV bit is set, something strange have happened that may cause previous value to be wrong,
and cause invalid measurement.
You might then read the timer to get a new current_value, before emptying the buffer, but do not calculate a new timePeriod,
since it may possibly be wrong, or mark the measurement as invalid.
Something like:
    if (IC2CONbits.ICOV)
    {   current_value  = TMR2; // First read 32bit Timer "LSW"
        current_value |= TMR3HLD << 16; // Shift and add "MSW" from holding register.
        while (IC2CONbits.ICBNE)        // Empty FIFO buffer
            IC2BUF;
        return;
    }  

There should be a Family Reference Manual document for the Input Capture hardware: 
DS70046 or   http://ww1.microchip.com/downloads/en/DeviceDoc/Section1370060c.pdf 
that may have more description.
 
    Mysil
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5