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

Accuracy Timer2

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



Joined: 20 Sep 2003
Posts: 47
Location: Brussel - Belgium

View user's profile Send private message

Accuracy Timer2
PostPosted: Sun Jun 19, 2005 1:48 am     Reply with quote

I try to have one interrupt each 1 mSec with the greather accuracy posssible.
I have try with Timer0, without succes. I follow the advice of Mark and I use Timer2.
The test program below give me pulses of 0.99981 mSec ( measured with a counter/frequence meter).
Is it possible to increase this accuracy ?
Test with Pic 16F876A, quartz of 16 Mhz + 2 x 27 pF, PCWH 3.216

Code:

#include <16F876A.h>
#device *=16

#use delay(clock=16000000)
#define CRYSTAL_FREQ    16000000

#fuses HS,NOWDT,PUT,BROWNOUT,NOWRT,NOLVP

#use fast_io ( A )
#use fast_io ( B )
#use fast_io ( C )


/****  Interrupts   ******************************************/

#INT_TIMER2
   void Timer2Interrupt ( void )
      {
      output_toggle(PIN_A1);
      }

/*   Main  */

void main ( void )
 {
  /* INITIALIZE */
 setup_adc_ports ( NO_ANALOGS );
 set_tris_b(0xff);
 set_tris_a(0x20);
 set_tris_c(0b10011010);

 setup_timer_2(T2_DIV_BY_16,249,1);
 set_timer2(0);
 
  /* INTERRUPTS */
  enable_interrupts (INT_TIMER2);    // enable timer2 interrupts
  enable_interrupts ( GLOBAL );       // enable all interrupts

  /* MAIN LOOP */

   while ( TRUE )
      {
      }
 }


Thanks a lot for your advice. Have a nice sunday.
Ttelmah
Guest







PostPosted: Sun Jun 19, 2005 4:52 am     Reply with quote

Get a more accurate crystal!....
Seriously the final accuracy, is completely determined by the crystal. If you want a more accurate result, change one of the crystal caps into a trimmer, program the chip to give a pwm output at (say) the crystal/16, measure this with your dfm, and adjust the trimmer till the frequency is right. Unfortunatel, remember that if you just try to check the crystal frequency with the dfm, the capacitance of the meter itself will change the raw frequency. Also how recently was you dfm calibrated?. Assuming the dfm is accurate, your current error is 2 parts in 10000, or 200ppm. Normally I'd expect a good quality crystal to do better than this (up to perhaps 20ppm, before you start to need therma control), but I'd also be checking whether your cap values are reasonable. Remember you should include board and pin capacitances in the calculation.

Best Wishes
GDetienne



Joined: 20 Sep 2003
Posts: 47
Location: Brussel - Belgium

View user's profile Send private message

Thanks.
PostPosted: Sun Jun 19, 2005 9:33 am     Reply with quote

Thanks for your answer. Not easy ...
What the best, Xtal or ceramic resonator ?

Thanks.
Ttelmah
Guest







PostPosted: Sun Jun 19, 2005 2:56 pm     Reply with quote

Crystal.
Seriously, your figure is below what a reasonable crystal should achieve. If you get the data sheets from various crystal manufacturers, you will find types ranging from perhaps 100ppm down to 20ppm in the 'standard' ranges, then high accuracy types (which will cost more), going down to perhaps 2ppm. Then versions with temperature control, pushing down to perhaps 0.1ppm over short/medium terms.
I would be very suspicious that your capacitances are not correct for the crystal involved (when you include the chips own capacitances). with the right crystal chosen, and right capacitors, and careful board layout, you should be able to improve by at least a factor of ten.
You should also be aware that the internal PIC oscillator, can result in overdriving certain types of crystal (and give very poor frequency accuracy as a result). Look at the MicroChip application notes about crystal selection.
Also consider pre-built crystal oscillator modules instead of a simple crystal. These are available 'pre-trimmed' to much higher accuracies. C-mac for example, do a unit with 20ppm accuracy, that is specified for 15years, over 0 to 70C.

Best Wishes
GDetienne



Joined: 20 Sep 2003
Posts: 47
Location: Brussel - Belgium

View user's profile Send private message

Thanks
PostPosted: Sun Jun 19, 2005 3:10 pm     Reply with quote

Thanks for ideas. I can start to try with others values of capacitances.

Have a nice week.
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

Re: Accuracy Timer2
PostPosted: Sun Jun 19, 2005 8:08 pm     Reply with quote

GDetienne wrote:

Test with Pic 16F876A, quartz of 16 Mhz + 2 x 27 pF, PCWH 3.216


Have you checked the crystal specs to ensure you are loading it correctly? 27pF seems large for a generic 16M crystal but may be correct for the one you are using.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
GDetienne



Joined: 20 Sep 2003
Posts: 47
Location: Brussel - Belgium

View user's profile Send private message

tests ...
PostPosted: Mon Jun 20, 2005 12:23 pm     Reply with quote

I search and let you know when I have found.

Thanks.
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