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 CCS Technical Support

Using CCP in capture mode!??

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



Joined: 09 Mar 2008
Posts: 3

View user's profile Send private message

Using CCP in capture mode!??
PostPosted: Sun Nov 22, 2009 4:12 am     Reply with quote

Dear forum,

I've looked at numerous examples given on this forum on this exact same topic and still I am battling - tunnel vision? I don't know. This is my first time using the CCP in capture mode.

I use timer0 to generate a 50Hz signal on pin B0. This signal is fed to the CCP2 input that is configured in capture mode. I want to measure the frequency.

I display 49Hz instead of 50Hz. Where o where am I missing the bus?

Here is my code (as minimal as I could make it).

Is the problem in my calculation, or is it in the test signal I'm trying to generate. I unfortunately do not have access to a function generator. Sad.

Any advice??

Regards,
Madri
Code:

#include <18F4620.h>

#device HIGH_INTS=true
#fuses HS,NOPROTECT,NOWDT,BROWNOUT,PUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

/* Public interface */
//....
/* Constants */
//...
/* Global variables */
int timer1RolloverCount = 0;
int16 frequency;
int32 isrCCPdelta;
int1 captureFlag = FALSE;


void init(void)
{
   init_pins();
   
   set_timer0(40536); //set up timer 0 to interrupt every 10ms   
   setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
   set_timer0(0);
   
   setup_ccp2(CCP_CAPTURE_RE);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); //period of timer 1 = 800ns per tick
   set_timer1(0);   
   
   clear_interrupt(INT_TIMER1); //ensure interrupt flag is cleared
   clear_interrupt(INT_CCP2); //ensure interrupt falg is cleared
   
   enable_interrupts(INT_TIMER0); //servo and contact sensor time base
   enable_interrupts(INT_CCP2);
   enable_interrupts(INT_TIMER1); //used to provide time base for speed related calculations
      enable_interrupts(GLOBAL);
      
} /* init */

/*****************************************************************************
 * TIMER 0 interrupt. Provides the 20ms period for the test signal on pin B0.
 ****************************************************************************/
#int_TIMER0
void TIMER0_isr()
{   
   set_timer0(40536); //create 10ms timebase thus creating a signal with freq 50Hz
   output_toggle(PIN_B0); //to test frequency reading...
}/* TIMER0_isr */

/*****************************************************************************
 * TIMER 1 interrupt. Keeps track of number of roll over events. TIMER 1 is also the timer associated with the
 * CAPTURE mode  ****************************************************************************/
#int_TIMER1
void TIMER1_isr()
{
   ++timer1RolloverCount; //increment count when a timer rollover event occurs
}

/*****************************************************************************
 * Capture interrupt. Occurs when encoder sensor goes high. Calculates pulse length.
 * This is then used to calculate the distance and speed.
 ****************************************************************************/
#INT_CCP2
void CCP2_isr()
{
   int32 endTime;
   static int32 startTime = 0;
   
   endTime = CCP_2;
   isrCCPdelta = (int32)0x10000 * timer1RolloverCount - (int32)startTime + (int32)endTime;
   
   startTime = endTime; //end of one pulse is start of next;
   timer1RolloverCount = 0; //restart rollover count

   captureFlag = TRUE;
}

/*****************************************************************************
 * Main routine.
 ****************************************************************************/
void main(void)
{

int16 externCCPdelta;

init();

   while (TRUE)
   {
       if(captureFlag==TRUE) //tesing rpm only...
       {
          disable_interrupts(GLOBAL);
          externCCPdelta = isrCCPdelta;
          enable_interrupts(GLOBAL);
          frequency = (int16)(1250000L / externCCPdelta);
          
          printf("Freq = %Lu\r\n",frequency);
          captureFlag = FALSE;
      }
      else
      {
         printf("No signal...\r\n");
      }
   delay_ms(200);
    }//end while(true)
}/* main */
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 22, 2009 10:18 am     Reply with quote

Look at this post which explains how to do the frequency calculation
with proper round-off:
http://www.ccsinfo.com/forum/viewtopic.php?t=29963&start=31

Look at this thread which shows how to do the frequency calulation
with floating point to get more precise results:
http://www.ccsinfo.com/forum/viewtopic.php?t=33153&start=45
Madri



Joined: 09 Mar 2008
Posts: 3

View user's profile Send private message

thanks!
PostPosted: Sun Nov 22, 2009 11:37 am     Reply with quote

Many thanks! Now it's working 100%. Very Happy

Madri
Madri



Joined: 09 Mar 2008
Posts: 3

View user's profile Send private message

o' no!???
PostPosted: Sun Nov 22, 2009 12:47 pm     Reply with quote

When I change the code in timer0 to generate a 5Hz signal (closer to my real-world signal), the frequency displays 23. I am very confused!?

Why is this happening? How can I fix it?

Madri

I changed frequency calculation to:

Code:
frequency = (int16)((1250000L + (int32)(externCCPdelta>>1))/ (float)externCCPdelta);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 22, 2009 1:04 pm     Reply with quote

Study all the threads on using the CCP to measure frequency.
Some of them discuss the changes required to measure a low frequency.
http://www.ccsinfo.com/forum/viewtopic.php?t=33153
http://www.ccsinfo.com/forum/viewtopic.php?t=29963
http://www.ccsinfo.com/forum/viewtopic.php?t=36852
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