Electronic – Increasing the delay using ‘option’ register in Pic16f877a

interruptspictimer

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?

Best Answer

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.