CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Helpful tip for input capture

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
victorcastro89



Joined: 31 Oct 2011
Posts: 11

View user's profile Send private message

Helpful tip for input capture
PostPosted: Sat Aug 11, 2012 11:18 pm     Reply with quote

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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group