# Increasing the delay using 'option' register in Pic16f877a

I have learned that the maximum 'delay' possible in pic16f877a running with a source of 11.0592MHz (11059200 / 4 actually) is 23 ms.. without using software delays (delay_ms( ))..

the calculation was max_delay = ((FF-00)xPrescalarxTimeperiod) = (255x256x3.62xe-7) = ~23ms..

My question is how to 'increase this delay to 1 second' without using software delay?

-
Are you setting up the prescaler for the WDT or for a timer? – Thomas O Oct 26 '10 at 9:25
Unfortunately it's not possible without some kind of software delay, so here's one way to do it: every 23 ms run a software interrupt which increments a counter, after 43 counts you will have reached 0.989 seconds; for more precise results use a faster timer setting and increase counts appropriately. – Thomas O Oct 26 '10 at 10:27
Sir, I agree with what you said.. I will work on it.. Thank you very much.. – Vicky Rao Oct 26 '10 at 10:43
I would like to specify, it is possibly to do it without using a software delay similar to delay_ms which is very inefficient. a counter in an interrupt is maybe 30 clock cycles every 20 mS. – Kortuk Oct 26 '10 at 23:13

## Why is it hard?

This is not the easiest thing to do. you have picked a crystal oscillator that is specifically chosen to give easy division to common baud rates.

For example:

11.0592MHz/9600 is 1152

11.0592MHz/115200 is 96

That means you can hit some standard baud rates perfectly with a certain divider. Gives very consistent UART. Now you want to count time.

## Counting 1 Second

If you take a 11.0592MHZ then divide by 256 and 64 you get a value of 675. This means if you want an accurate count of seconds you need to have that interrupt every 256 * 64 clock cycles and count with a variable in your interrupt you can hit it.

## How to make it easier

You have two options, accepting error. First, your equation you divided by 255 since you are counting from 0 to 255, but since the interrupt is on overflow it is actually as you count to 256, so you should be dividing by 256.

### My clock has error?

Decide how much precision you need. Often you do not need spot on precision, if this is the case, accept that it is almost a second and use what Thomas said in comment. Count to as close as you can get and cope. Timer 1 will allow you to divide by 65536 and spend more time doing things other than handling interrupts.

### I need precision in my clock!

You need to pick an oscillator value that is a multiple of 32768 Hz. What you want is something that can be reduced to 1Hz from division by a power of 2.

If you need the nice baud rate and a nice counter, the wiki page has a mark for RTC(good for real time clock). 4.608 MHz is a great thing, it divides to 1Hz pretty easy and is good for 115200 baud also.

-
 As a side note, RTCs still suffer from error and need to be calibrated. – Kortuk♦ Oct 27 '10 at 1:31 With the guidance of Mr.Kortuk, Kellen and Thomas I have increased the delay.. The code goes like this: – Vicky Rao Oct 27 '10 at 4:13 #int_timer0 timer0ISR() { x=x+1; if(x==43) {portd= ~portd; x=0;}}.. followed by 'main'.. I have connected LEDs to portd that blinks at an interval of 1 second, without any software delay.. thank you all.. – Vicky Rao Oct 27 '10 at 4:29