victorcastro89
Joined: 31 Oct 2011 Posts: 11
|
Helpful tip for input capture |
Posted: Sat Aug 11, 2012 11:18 pm |
|
|
Hello guys, i'm working with the input capture to measure frequency from 2 Hz to thousand Hz.
I was using dsPIC33, 80Mz, 40 MIPS, with 16bit timer 2.
If prescaler = 64 theoretically I can measure 10hz minimum.
If prescaler = 256 theoretically I can measure 2hz minimum.
But when I tried this code,
Code: |
#int_ic1
void ic1(void){
t1=get_capture(1,0);
t2=get_capture(1,0);
if(t2>t1)
timePeriod = t2-t1;
else
timePeriod = (65535- t1) + t2;
}
|
Time period varies between correct period and zero.
It occurs because when frequency is too low, capture buffer is empty. When we read then on second time(t2) and get_capture return the last capture, so timePeriod=0
To avoid this just verify buffer status before read it.
On dspic33fj12mc202:
Code: |
//ICBNE: Input Capture Buffer Empty status flag bit
#BIT ICBNE = 0x142.3
#int_ic1
void ic1(void){
while(!ICBNE); //Wait Capture
t1=get_capture(1,0);
while(!ICBNE); //Wait Capture
t2=get_capture(1,0);
if(t2>t1)
timePeriod = t2-t1;
else //If timer Overflow
timePeriod = (65535- t1) + t2;
}
|
Enjoy! |
|