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

CCP Compare timing errors

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



Joined: 20 Dec 2009
Posts: 23

View user's profile Send private message

CCP Compare timing errors
PostPosted: Wed May 26, 2010 10:32 pm     Reply with quote

Hi Everybody,

I’m learning the CCP system on the 18F2321, and using version 4.105 compiler software.

On one pass through the ISR the hardware resets Timer3. I turn the output on and set how long the signal is high.

On the other pass through the ISR the hardware turns the output off and I set the time of the total waveform. I don’t reset Timer3 because I’m giving it a total time, not an ‘Off‘ time.


Code:


#INT_CCP2
void SyncOutRoutine()
   {
   static int1  Mode;
   if (Mode == 1)
      {
      CCP_2 = DurationTick;
      setup_ccp2(CCP_COMPARE_CLR_ON_MATCH);
      Mode = 0;     
      }
   else
      {
      CCP_2 = TotalWaveformTick;
      setup_ccp2(CCP_COMPARE_SET_ON_MATCH | CCP_COMPARE_RESET_TIMER);
      Mode = 1;
      }
   }



Here's the relevant code form the rest of the program
Code:


#use delay (Clock=10mHz, Oscillator=10mHz)

……………..

   setup_timer_0(T0_OFF);
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);   
   setup_timer_2(T2_DISABLED, 0, 1);   
   setup_timer_3(T3_INTERNAL | T3_DIV_BY_8 | T3_CCP2 );   

…………….

   DurationTick = 2000;
   TotalWaveformTick = 4000;


For testing purposes I set the on time to half the total time, expecting a square wave output. Here are several on time and total time inputs, and the actual times of the output.

Code:

Tick Time inputs         Resulting Times
Total / Duration     Total Time        On Time

4000  /  2000        12.8 mSec          6.3 mSec         
2000  /  1000        6.4 mSec           3.16 msec
1000 /  500          3.20 mSec          1.56 mSec
500  /  250          1.60 mSec         .770 mSec  error with on time
250  / 125          .804 mSec          .380 mSec  error with both
100  / 50            324.0 uSec         140 uSec  error with both
30 / 15              99.0 uSec          27.0 uSec  error with both
20 / 10              The code crashes.


(I had to stick this in a code section because the forum software does not understand tabs and this was the only way to make the table readable)

Where is the error coming from? It can’t be interrupt latency, since the clearing of Timer3 and acting on the compare register = Timer3 condition is done in hardware, and finished long before the ISR is entered.

Any idea what’s going on? I’ve thoroughly been through Microchip’s data sheet on this part and I just can’t figure out what the circuitry is doing.

Thanks, Al
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Thu May 27, 2010 4:41 am     Reply with quote

Big problem, is the value used for the CCP setup.
You can't 'or' these together.
If you look at the values for the VREF for example, you have the comment at the end of the entry:
// Or (with |) the above with a number 0-15

The CCP constants have no such entry.
The available values, are the only ones available. The list is in the data sheet...

You either need to use the set and reset functions seperately as you do, and clear the timer yourself, or reset the timer each time, and handle the pin toggling yourself. This would actually be the 'better' solution, since then the counter timing will be done by the hardware, and you just toggle the pin, 'interrupt latency' uSec after the hardware event. Since the latency will be nearly constant, the result should be OK.

Best Wishes
Allan



Joined: 20 Dec 2009
Posts: 23

View user's profile Send private message

PostPosted: Thu May 27, 2010 6:02 pm     Reply with quote

Quote:

You can't 'or' these together.



D'oh!

Thanks for your help,
Al
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