Searching \ for '[PIC] PIC16F87 internal clock' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/time.htm?key=clock
Search entire site for: 'PIC16F87 internal clock'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] PIC16F87 internal clock'
2005\05\08@082353 by Dave Turner

picon face
Hi,

I have recently made a clock program in assembler.  I have tested it
with the SIM and the stopwatch, and it is about 0.000041 seconds out
every 2 seconds.  When i download it onto the PIC though, it is a
visible amount out - I have it next to an accurate clock here, and it
can get as far out as half a second every 2 minutes.  I am using the
16F87's internal oscillator, running at 8MHz.  I have checked the
datasheet for the 16F87, and it says the internal oscillator is +- 1%
accurate, I think, at 25 degrees C.  I think something must be very
wrong for it go get that far out.

2005\05\08@083517 by Chris Emerson

flavicon
face
On Sun, May 08, 2005 at 01:23:51PM +0100, Dave Turner wrote:
> Hi,
>
> I have recently made a clock program in assembler.  I have tested it
> with the SIM and the stopwatch, and it is about 0.000041 seconds out
> every 2 seconds.  When i download it onto the PIC though, it is a
> visible amount out - I have it next to an accurate clock here, and it
> can get as far out as half a second every 2 minutes.  I am using the
> 16F87's internal oscillator, running at 8MHz.  I have checked the
> datasheet for the 16F87, and it says the internal oscillator is +- 1%
> accurate, I think, at 25 degrees C.  I think something must be very
> wrong for it go get that far out.

Do the calculation - half a second in 2 minutes is 0.4%, well in spec.
You'll need to use a crystal to keep accurate time.

Cheers,

Chris

2005\05\08@094539 by Jinx

face picon face
> > datasheet for the 16F87, and it says the internal oscillator is
> > +- 1% accurate, I think, at 25 degrees C.  I think something
> > must be very wrong for it go get that far out.
>
> Do the calculation - half a second in 2 minutes is 0.4%, well in
> spec. You'll need to use a crystal to keep accurate time

Or use OSCTUNE or adjust your timing block to compensate
for the 0.4%. Depends on what periods you want to time. Even
a crystal will not give give accurate long-term stability unless it's
calibrated and temperature-compensated. 10ppm is ~ 1s / day

2005\05\08@100043 by Dave Turner

picon face
Hi,

I know I now have a 31.768KHz clock connected to timer1.  I am trying
to set this to be the system clock.  Do I need to adjust OSCCON at
all?  Also will just setting up T1CON correctly do this, or are there
other settings that need to be changed?

On 5/8/05, Jinx <spam_OUTjoecolquittTakeThisOuTspamclear.net.nz> wrote:
{Quote hidden}

> -

2005\05\08@100128 by olin_piclist

face picon face
Dave Turner wrote:
> I have it next to an accurate clock here, and it
> can get as far out as half a second every 2 minutes.

So that's 1/2 a part in 120, or 1 part in 240, or about 1/4 percent.

> I am using the
> 16F87's internal oscillator, running at 8MHz.  I have checked the
> datasheet for the 16F87, and it says the internal oscillator is +- 1%
> accurate, I think, at 25 degrees C.  I think something must be very
> wrong for it go get that far out.

Huh?  It's specified at 1% and your seeing 1/4%.  Looks like everything is
working fine.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

2005\05\08@101151 by Dave Turner

picon face
Oh, i didn't know the RC could be so innacurate.  I think I have
successfully configured OSCCON and T1CON, but now my ICD2 refuses to
program - it says it programs OK, but when it verifies it never
succeeds.  Does anyone know if the ICD is actually compatible with
using T1 as the system clock?

On 5/8/05, Olin Lathrop <.....olin_piclistKILLspamspam@spam@embedinc.com> wrote:
{Quote hidden}

> -

2005\05\08@101318 by olin_piclist

face picon face
Dave Turner wrote:
> I know I now have a 31.768KHz clock connected to timer1.  I am trying
> to set this to be the system clock.  Do I need to adjust OSCCON at
> all?  Also will just setting up T1CON correctly do this, or are there
> other settings that need to be changed?

OSCCON only effects the internal oscillator.  If you're not using it, then
its calibration value doesn't matter.

As for how to make the PIC run from the timer 1 oscillator, I don't remember
off the top of my head.  Read the manual.  Take a close look at the config
bits.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

2005\05\08@103551 by Byron A Jeff

face picon face
On Sun, May 08, 2005 at 03:00:40PM +0100, Dave Turner wrote:
> Hi,
>
> I know I now have a 31.768KHz clock connected to timer1.  I am trying
> to set this to be the system clock.  Do I need to adjust OSCCON at
> all?  Also will just setting up T1CON correctly do this, or are there
> other settings that need to be changed?

Dave,

Having been there and done that you're never going to be pleased with the
results. Even when you get into the low PPM range for drift, it's still
going to drift due to lack of temp compensation and the like.

Also why would you want the 32.768 Khz to be the system clock? Is this
battery powered? If not then just run the PIC from the nanowatt clock
module, and use T1 to keep track of time.

You can find the 16F877 code I used for my sunrise/sunset outdoor light
controller here:

http://www.finitesite.com/d3jsys/clock.asm

It drifts, but because it doesn't need precise edges for turn on/turn off
I've left it alone.

Hope this helps,

BAJ

2005\05\08@104531 by Byron A Jeff

face picon face
On Sun, May 08, 2005 at 03:11:51PM +0100, Dave Turner wrote:
> Oh, i didn't know the RC could be so innacurate.

RC is never used for timing sensitive applications. At the nominal 1%
a RTC will drift 36 seconds an hour.

1% represents 10000 PPM. Most crystals are in the range of 20 PPM.

And each of these will drift differently based on temperature.

My conclusion is that an external source is the best bet. Jinx has posted
about the $2 USD battery powered clock modules. Also one can use the WWVB
60 KHz clock signal broadcast from Colorodo. Finally the easiest and most
stable tick source is the 60 Hz AC signal out of the power outlet. A
transformer and an optosiolator will give you 216000 ticks per hour like
clockwork.

When I get back to my sunrise/sunset controller I plan to use the power
outlet signal to keep accurate time. The 32Khz crystal will be my backup
during power outages as this project has a gel-cell battery backup.

BAJ

2005\05\08@111323 by Dave Turner
picon face
Firstly, I'm not running off battery power.  After much fiddling with
config bits I have discovered that with the PIC running on XT mode, it
runs a lot faster than it should if it is using the 32.768KHz clock.
Also, on INTRC mode it runs too fast.  On EXTRC, LP or HS mode it
doesn't run at all.

The reason I'm trying to use the 32.768KHz clock as the system clock
is that I don't have a clue how to make a delay by checking a clock -
The only method I know is many embedded loops.

Any pointers to good tutorials / examples about using a non-system
clock for a 1 second delay would be extremely welcome.

BTW, does anyone know why the ICD2 sometimes refuses to program unless
the PIC power is turned off for a second - it works fine for a while,
and after a few programs, needs a reset, then it works fine again.

On 5/8/05, Byron A Jeff <byronspamKILLspamcc.gatech.edu> wrote:
{Quote hidden}

> -

2005\05\08@120033 by Byron A Jeff

face picon face
On Sun, May 08, 2005 at 04:13:23PM +0100, Dave Turner wrote:
> Firstly, I'm not running off battery power.

So counting power line ticks is a possibility.

>  After much fiddling with
> config bits I have discovered that with the PIC running on XT mode, it
> runs a lot faster than it should if it is using the 32.768KHz clock.
> Also, on INTRC mode it runs too fast.  On EXTRC, LP or HS mode it
> doesn't run at all.

There's no need for you to run the system clock from the 32 Khz. Run it
from the 8 Mhz INTRC and use the 32 Khz to clock Timer 1.

> The reason I'm trying to use the 32.768KHz clock as the system clock
> is that I don't have a clue how to make a delay by checking a clock -
> The only method I know is many embedded loops.

My sunrise/sunset contoller implements a full 365 day calendar using a
2 second timer 1 heartbeat. Check it out here:

http://www.finitesite.com/d3jsys/clock.asm

>
> Any pointers to good tutorials / examples about using a non-system
> clock for a 1 second delay would be extremely welcome.

It's a two second delay but the idea is sound.

You implement it with a simple state machine. In a single big loop you
increment the smallest value. When that overflows, you reset that value
and increment the next one higher up.

Check out the code for more details.

BAJ

2005\05\08@132348 by olin_piclist

face picon face
Dave Turner wrote:
> Firstly, I'm not running off battery power.  After much fiddling with
> config bits I have discovered that with the PIC running on XT mode, it
> runs a lot faster than it should if it is using the 32.768KHz clock.
> Also, on INTRC mode it runs too fast.  On EXTRC, LP or HS mode it
> doesn't run at all.

Sounds like general confusion about the system oscillator.  Get this figured
out first, because everything else mostly depends on it.  If you're not
running on battery power, why not run the PIC at its maximum clock speed of
20MHz?  All you need is a 20MHz crystal specified for parallel operation and
two caps around 22pF, with the oscillator set to HS mode.

If the issues is pins or board space, then the internal oscillator is a good
choice.  You can use it to run the PIC, and the timer 1 oscillator for
accurate timing.

> The reason I'm trying to use the 32.768KHz clock as the system clock
> is that I don't have a clue how to make a delay by checking a clock -
> The only method I know is many embedded loops.

Fix this first.  Since you're willing to have a crystal anyway, I would make
is a 20MHz crystal on the main oscillator.  Once you've done that, it's very
easy to set up a periodic interrupt of 1mS, for example, using timer 2 and
its period register.  This would happen once every 5000 instructions, so
doesn't add much load to the processor.  As long as 1mS is close enough
timing for everything else in the system, this will work fine.

Once you've got that, there are many ways to implement timers for longer
periods.  One way is to have a counter for each delay event you need.  To
wait, you set the counter to the number of mS to wait.  The interrupt
routine decrements the counter whenever it's not zero, and sets a flag when
it decrements it from 1 to 0.  Now the foreground code can go off and do
other things, and the flag becomes just another event to handle in the main
event loop.

That's only one of many ways to do this.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

2005\05\08@132733 by Dave Turner

picon face
Hmmm.... My problem with the PIC not programming has suddenly got even
worse - I have to pull the plug on it before every single programming,
or else when it verifies, it says that '0' was read instead of the
expected value.

On 5/8/05, Byron A Jeff <.....byronKILLspamspam.....cc.gatech.edu> wrote:
{Quote hidden}

> -

2005\05\08@143753 by Dave Turner

picon face
Hi,

After much reading up, I found some registers related to timer1.
Using my earlier (and finally successful) RS232 program, I tried to
send the contents of TMR1 every time a button is pressed.  When I
tried to send TMR1H (most significant byte), it was always d'8', and
the least significant byte was always changed around, starting at 85,
then after a few resets, 86, then 87, and finally 83.

I setup T1CON with:
00001011
and later tried
00001111

That's: system clock not running off tmr1, no prescaler (1:1), osc
enabled, I tried syncing on and off - no change, tmr1 source is
external, and tmr1 is on.

On 5/8/05, Olin Lathrop <EraseMEolin_piclistspam_OUTspamTakeThisOuTembedinc.com> wrote:
{Quote hidden}

> -

2005\05\08@161022 by Jan-Erik Soderholm

face picon face
Dave Turner wrote :

> After much reading up, I found some registers related to timer1.

"Much reading up" ??
Everything about TMR1 is in (just) *7* pages in the data sheet.
And have you realy been "using" TMR1 without knowing
about the registers "related to timer1" ? Seems a bit hard... :-)

> Using my earlier (and finally successful) RS232 program, I tried to
> send the contents of TMR1 every time a button is pressed.  When I
> tried to send TMR1H (most significant byte),..

Have you read the section called "7.5.1 READING AND WRITING
TIMER1 IN ASYNCHRONOUS COUNTER MODE" ??
(Note, it *was* in uppercase in the data sheet, I'm just quoting,
not shouting... :-) )

{Quote hidden}

The section called  "7.11 Using Timer1 as a Real-Time Clock"
seems to describe a fairly simple method to get a 1 sec real-time
time base. Have you checked if that relates to your needs ?

Jan-Erik.



2005\05\08@200315 by Jinx

face picon face
> > Any pointers to good tutorials / examples about using a non-system
> > clock for a 1 second delay would be extremely welcome.

If you're going to use a 32768 xtal on TMR1 -

32768/4 = 8192Hz incrementation rate (DS30487B, Section 7.1)

Therefore, re-load TMR1 with 0x0000-0x2000 = 0xe000 (=
- d8192) or just re-load TMR1H with 0xe0, as TMR1L has
already gone through 0x00. When TMR1 counts up from 0xe000
to pass through 0x0000 you can either have it generate an interrupt
(by enabling TMR1IE) or poll TMR1IF to see if it's set. In either
case, clear TMR1IF and reload. This is a very simple background
1 second timer. You don't need to do anything other than note that
TMR1 has gone through 0x0000 (and of course re-load it before
TMR1L gets to 0x00 again). Using binary counting like this means
you don't have to worry about reload jitter that can happen when
loading the timer low byte with non-zero values

You can work out the maths if you're using a different xtal as the
main oscillator and using another timer and/or pre-scalers

I'd go along with Byron about using mains cycles as the primary
timing source (RA4/T0CKI - RB0/INT - RB6/T1CKI). Note
though that mains frequency varies slightly during the day due to
demand and you should look at the daily (ie a 24h period) count
for accurate timing. IOW, the number of mains cycles coming in
between 7am and 7pm is more than likely going to be be less
(frequency would be 49.xx or 59.xx Hz) than the period 7pm to
7am (frequency will be catch-up 50.xx or 60.xx Hz)

2005\05\08@202236 by Byron A Jeff

face picon face
On Mon, May 09, 2005 at 12:01:54PM +1200, Jinx wrote:
> > > Any pointers to good tutorials / examples about using a non-system
> > > clock for a 1 second delay would be extremely welcome.
>
> If you're going to use a 32768 xtal on TMR1 -
>
> 32768/4 = 8192Hz incrementation rate (DS30487B, Section 7.1)
>
> Therefore, re-load TMR1 with 0x0000-0x2000 = 0xe000 (=
> - d8192) or just re-load TMR1H with 0xe0, as TMR1L has
> already gone through 0x00. When TMR1 counts up from 0xe000
> to pass through 0x0000 you can either have it generate an interrupt
> (by enabling TMR1IE) or poll TMR1IF to see if it's set. In either
> case, clear TMR1IF and reload. This is a very simple background
> 1 second timer. You don't need to do anything other than note that
> TMR1 has gone through 0x0000 (and of course re-load it before
> TMR1L gets to 0x00 again). Using binary counting like this means
> you don't have to worry about reload jitter that can happen when
> loading the timer low byte with non-zero values

Are you sure there is no jitter if you reset the upper byte only?

BTW I just checked my sunrise/sunset calendar program. It actually uses
the 1:8 prescale and rollover for a 19 bit count (512k). So it ticks every
16 seconds. With a 1:1 prescale it rolls over every 2 seconds.

{Quote hidden}

But it remains accurate over the long term. You'll get 216000 ticks in a
24 hour period though the interval between them changes.

BAJ

2005\05\08@212142 by Jinx

face picon face
> > you don't have to worry about reload jitter that can happen when
> > loading the timer low byte with non-zero values

> Are you sure there is no jitter if you reset the upper byte only?

AFAIK jitter affects just the lower byte. If the timer/counter is
free-running and you're looking at and reloading just the upper byte
then the lower byte is irrelevant for detection (eg IRQ) purposes

Jitter aside, from 7.5.1

"For writes, it is recommended that the user simply stop
the timer and write the desired values. A write contention
may occur by writing to the timer registers while the register
is incrementing. This may produce an unpredictable value in
the timer register"

I infer from this that only a register that is incrementing could be
corrupted, which leads me to conclude that if the low byte is
incrementing untouched by s/w and you're reloading the upper
byte at a time when it can't be incrementing, then that contention
corruption shouldn't (can't ?) cause any problems

> But it remains accurate over the long term

Yes, exactly. Nothing to get upset about if a cycles-based timer is a
second or two out after a few hours. At 24 hours it should be spot on

2005\05\09@031016 by Chris Emerson

flavicon
face
On Mon, May 09, 2005 at 01:21:06PM +1200, Jinx wrote:
> Jitter aside, from 7.5.1
>
> "For writes, it is recommended that the user simply stop
> the timer and write the desired values. A write contention
> may occur by writing to the timer registers while the register
> is incrementing. This may produce an unpredictable value in
> the timer register"
>
> I infer from this that only a register that is incrementing could be
> corrupted, which leads me to conclude that if the low byte is
> incrementing untouched by s/w and you're reloading the upper
> byte at a time when it can't be incrementing, then that contention
> corruption shouldn't (can't ?) cause any problems

Not quite.  Think what happens if TMR1L is at about 0xff when you write
to TMR1H.  You'll be ~0x100 counts off depending on whether you write to
TMR1H before or after the TMR1L rollover.

This might not be a problem if you can guarantee writing to TMR1H early
enough after the TMR1 rollover interrupt.

Chris

2005\05\09@041111 by Jinx

face picon face
> > incrementing untouched by s/w and you're reloading the upper
> > byte at a time when it can't be incrementing, then that contention
> > corruption shouldn't (can't ?) cause any problems
>
> Not quite.  Think what happens if TMR1L is at about 0xff when

Well, er, I did say when TMR1H can't be incrementing. That implies
that you make sure the reload is not during a time when there's the
possibility of a TMR1L rollover. Which, as you say, would probably
be early in an ISR or whatever

2005\05\09@042240 by Chris Emerson

flavicon
face
On Mon, May 09, 2005 at 08:11:03PM +1200, Jinx wrote:
> > > incrementing untouched by s/w and you're reloading the upper
> > > byte at a time when it can't be incrementing, then that contention
> > > corruption shouldn't (can't ?) cause any problems
> >
> > Not quite.  Think what happens if TMR1L is at about 0xff when
>
> Well, er, I did say when TMR1H can't be incrementing. That implies
> that you make sure the reload is not during a time when there's the
> possibility of a TMR1L rollover. Which, as you say, would probably
> be early in an ISR or whatever

Sorry, you're right - I read it too quickly.

Cheers,

Chris

2005\05\09@061354 by Dave Turner

picon face
So, would clearing TMR1L make sure it won't roll over, or do I just
make sure I load TMR1H right after it has rolled over, so TMR1L
doesn't have time to roll over?

BTW, I'm not using interrupts ATM, because I am trying to make just a
1 sec delay function, not increment a clock.

On 5/9/05, Chris Emerson <picspamspam_OUTnosreme.org> wrote:
{Quote hidden}

> -

2005\05\09@063652 by Jinx

face picon face

> So, would clearing TMR1L make sure it won't roll over

Keeping TMR1L at 0x00 would definitely stop it rolling over ;-))

> or do I just make sure I load TMR1H right after it has rolled
> over, so TMR1L doesn't have time to roll over?

Yes. TMR1L will keep incrementing in the background

2005\05\09@064229 by Howard Winter

face
flavicon
picon face
Byron,

On Sun, 8 May 2005 20:22:35 -0400, Byron A Jeff wrote:

>...<
> You'll get 216000 ticks in a 24 hour period though the interval between them changes.

Errr... a touch of brainfade, methinks  :-)  That's ticks *per hour* at 60Hz...

There are 86400 seconds in 24 hours, so at 50Hz that would be 4,320,000 ticks, at 60Hz it would be 5,184,000.  
(Unless you're counting zero-crossings, in which case it's double these, obviously).

Cheers,


Howard Winter
St.Albans, England


2005\05\09@064640 by Howard Winter

face
flavicon
picon face
Dave,

On Sun, 8 May 2005 18:27:32 +0100, Dave Turner wrote:

> Hmmm.... My problem with the PIC not programming has suddenly got even
> worse - I have to pull the plug on it before every single programming,
> or else when it verifies, it says that '0' was read instead of the
> expected value.

At what point in the process do you "pull the plug"?  Before the program cycle or between program and verify?

I believe you're using an ICD2, but what's it connected to?  (ie. what is the PIC plugged in to?)

The problem is less likely to be the ICD2 itself, more likely to be at the other end of the grey cable.

Cheers,


Howard Winter
St.Albans, England


2005\05\09@082321 by Byron A Jeff

face picon face
On Mon, May 09, 2005 at 11:42:25AM +0100, Howard Winter wrote:
> Byron,
>
> On Sun, 8 May 2005 20:22:35 -0400, Byron A Jeff wrote:
>
> >...<
> > You'll get 216000 ticks in a 24 hour period though the interval between them changes.
>
> Errr... a touch of brainfade, methinks  :-)  That's ticks *per hour* at 60Hz...
>

DOH!!!

BAJ

More... (looser matching)
- Last day of these posts
- In 2005 , 2006 only
- Today
- New search...