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

Accurate timer of 5 minutes, setup and calculation

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







Accurate timer of 5 minutes, setup and calculation
PostPosted: Wed Jul 06, 2005 3:13 am     Reply with quote

Hello,

i am using a PIC 18F458.

I wondered how i could use a timer to count exactly 5 minutes with accuracy.
After these 5 minutes i need to call a function.

I would like to have some examples using timer 0 and timer 1 ...

I don't know how to use setup_counters, setup_timer_1 functions and when i have to use set_timer0(0); set_timer1(0);

Code:
The following is generic code used to issue a quick pulse at a fixed rate:

#include <16Cxx.H>
#use Delay(clock=15000000)
#define HIGH_START 114
byte seconds, high_count;
#INT_RTCC
clock_isr() {
   if(--high_count==0) {
      output_high(PIN_B0);
      delay_us(5);
      output_low(PIN_B0);
      high_count=HIGH_START;
   }
}

main() {
   high_count=HIGH_START;
   set_rtcc(0);
   setup_counters(RTCC_INTERNAL, RTCC_DIV_128);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
   while(TRUE);
}

In this program, the pulse will happen about once a second. The math is as follows:

    The timer is incremented at (CLOCK/4)/RTCC_DIV.

In this example, the timer is incremented (15000000/4)/128 or 29297 times a second (34us).

The interrupt happens every 256 increments. In this example, the interrupt happens 29297/256 or 114 times a second.

The interrupt function decrements a counter (HIGH_START times) until it is zero, then issues the pulse and resets the counter. In this example, HIGH_START is 114 so the pulse happens once a second. If HIGH_START were 57, the pulse would be about twice a second.


I saw that pon the forum but i can't understand very well This calculation ... It is not very clear and i can't find a good explanation.

What is exactly the difference between the setup with an 8 bits timer and a 16 bits
Ttelmah
Guest







PostPosted: Wed Jul 06, 2005 4:12 am     Reply with quote

Start by reading the application notes at MicroChip, in the 'timer' section. Though the code in these generaly uses assembler, the details of how dividers are worked out, and the chip actually 'works', are all here.
The timers are just simple counters (like a clock), counting in the case of 8bit versions, from 0 to 255, and then starting again. In the case of sixteen bit versions, the count goes up to 65535, before resetting.
The timer triggers a signal when it resets, and in some cases this signal directly fires the interrupt, while in others there is then a second 'postscaler' (effectively another counter), which counts this signal, allowing much larger intervals between interrupts.
The setup_timer statements, determine where the clock comes from (internal or external sources), whether prescalers are applied before it comes into the timer itself, and whether postscalers are applied before triggering an interrupt. Details of what prescalers/postscalers are available, are in the chip's data sheet, and the 'names' used to access these, are in the .h file for the processor.
Generally, never use 'set_timer'. The problem here is a thing called latency. The instructions themselves take time, and if they are being used in an interrupt driven routine, it takes even longer to get to this part of the code. Unfortunately this means that code using direct setting of the timers, rarely works as expected. The exception, is where the timer is running very slowly, where this effect becomes a lot smaller. The best way of resetting the timer (if it supports this), is the use of the 'CTC' module, which allows the timer to be accuratel reset when it reaches a particular count.
The 'master clock' of the processor, is the oscillator frequency/4. This is why in the example given, the 15000000 value is divided by four. The setup_counters instruction is saying 'use this clock' (RTCC_INTERNAL), and 'divide it by 128, before feeding into the timer' (RTCC_DIV_128). So the clock into the timer, is (15000000/4)/128.
The timer itself then counts for 256 cycles (0 to 255), giving the 114 times/second figure (in fact 114.44Hz). Now this raises the problem that the figure is not exact. For accurate timing, it is common to deliberately choose crystal frequencies that divide easier. These are also 'good' for serial timings in general. For example, I often use 14745600Hz crystals (which are readily available), and in the example given, this gives 112.5 cycles/second. The '0.5', is easily handled by toggling the value put into the counter on alternate visits to the clock_isr, to give timings limited only by the crystal accuracy.
There have been hundreds of timer applications, including ones that use more complex arithmetic to 'tweak' the results to work from awkward crystal values, published here, and a search in the archives should find more examples than you will know what to do with!. If you then filter these for posters who publish quite well documented code (PCM_programmer, and Neutone, are two good examples), you should find a lot of your questions answered.

Best Wishes
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