# Accurate 1-second periods with any xtal 2011-02-13

Status
Not open for further replies.

#### Mr RB

##### Well-Known Member
Mr RB submitted a new article:

Accurate 1-second periods with any xtal - Here are some easy C code examples to generate 1-second (or any period) from any xtal value and any

Here are some easy C code examples to generate 1-second (or any period) from any xtal value and any interrupt period.

The code is performed in an interrupt, and only needs one variable to generate the desired period.

A simple example is a PIC running at 4MHz xtal, which is 1MHz on Timer0 (set to 1:1 prescale). In this example the interrupt occurs every 256 counts, and a second would be 1 million counts;

Code:
	// C code for a 1 second period with a 1MHz timer (4MHz xtal);
//...

#### birdman0_o

##### Active Member
I disagree that it is not as tunable in ASM, also, it is much more elegant in C

#### Mr RB

##### Well-Known Member
Hi Birdman, sorry I didn't state that very clearly. I meant that the third example is easier to convert to assembler because the main variable fits in a 2byte (unsigned int) variable instead of 3 or 4 bytes needed for the unsigned long var in assembler.

But with a 2byte variable 1 second = 62500 so it is not as finely tuneable as the top example. It is still tunable to within about 1.5 seconds a day which is better than most household clocks.

#### Catnamedog

##### New Member
Hi
I'm a complete newbie when it comes to C. Are there any "packages" of code which I might use to vary timings related to bottle filling? I was thinking of using an old (ancient!) laptop to give a more accurate fill-level than the timer circuit I currently use.
Many thanks, and best regards
Ken

Status
Not open for further replies.