[PIC] making a quartz clock

56 views
Skip to first unread message

picnoob

unread,
Feb 23, 2007, 4:16:22 PM2/23/07
to pic...@mit.edu

ok, i am doing a new project, u guessed it...a clock.

now, i have read some guides about this on the www, but the give different
info. Is it true that the frequency of the oscillator used is divided by 4,
and instructions are executed at this new frequency? im using a pic16f84a.

also, i wanted to know, what is different between 16f and 16c etc....?

ty.
--
View this message in context: http://www.nabble.com/making-a-quartz-clock-tf3281293.html#a9126956
Sent from the PIC - [PIC] mailing list archive at Nabble.com.

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

Harold Hallikainen

unread,
Feb 23, 2007, 4:31:09 PM2/23/07
to Microcontroller discussion list - Public.

>
> ok, i am doing a new project, u guessed it...a clock.
>
> now, i have read some guides about this on the www, but the give different
> info. Is it true that the frequency of the oscillator used is divided by
> 4,
> and instructions are executed at this new frequency? im using a pic16f84a.
>
> also, i wanted to know, what is different between 16f and 16c etc....?
>
> ty.

Yes, Fosc is divided by 4. So, if you have a 4MHz oscillator, the PIC runs
at 1,000 instructions per second (some instructions take twice as long,
like those that modify PC).

16F uses flash memory. It can be erased electrically. 16C uses eprom. If
in a ceramic case, it can be erased with UV. If in a plastic case, it
cannot be erased and is considered "one time programmable."

Harold


--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

Maarten Hofman

unread,
Feb 23, 2007, 4:35:15 PM2/23/07
to Microcontroller discussion list - Public.
Redwood Shores, 23 februari 2007.

Clock sounds nice. First the answers to your questions: on the 16F84A the
speed of each instruction is indeed 1/4 of the clock speed. In 18F this
isn't always the case, but most 12F and 16F are like that. 16F means it uses
some form of flash memory, allowing you to rewrite the program a number of
times (100-1000x for some types, 100000x for other types). 16C usually means
that the part can be written only once (it is OTP - One Time Programming)
although there are exceptions.

Now for additional comments:
1) Don't use the 16F84A. I personally like the 16F628A, but you could
immediately switch to the 16F648A, or the very universal 16F88 if you want
something similar. If you don't care about similar, you might want to focus
on the 18F and skip the 16F alltogether. Personally I also like using the
16F688, which is a very neat 14-pin device.
2) A clock based on a 4 MHz resonator won't work very well. A 4 MHz crystal
might be better, but is still quite temperature sensitive and consumes a lot
of power. I believe the consensus is that a 32768 Hz crystal is ideal.
3) There still is a power issue, however. I personally really like
interfacing with a DS1302 or DS1307 and have that part handle the time
keeping. If you add a supercap it can keep the time for very long periods.
The PIC can then turn itself off while the DS130x keeps the time and date
(and possibly 128 bytes of additional information).

Greetings,
Maarten Hofman.

Herbert Graf

unread,
Feb 23, 2007, 4:50:16 PM2/23/07
to Microcontroller discussion list - Public.
On Fri, 2007-02-23 at 13:16 -0800, picnoob wrote:
> ok, i am doing a new project, u guessed it...a clock.
>
> now, i have read some guides about this on the www, but the give different
> info. Is it true that the frequency of the oscillator used is divided by 4,
> and instructions are executed at this new frequency? im using a pic16f84a.

Yes, the instruction clock is /4 the oscillator clock.

Normally ever clock tick of the instruction clock equals one instruction
being run, however, ANY instruction that changes the PC (program
counter) will result in that instruction taking TWO instruction clocks
to run.

Examples are the GOTO or RET op codes. Also, BSFSZ and BSFSC will result
in 2 instruction clocks being needed if the "skip" is done.

> also, i wanted to know, what is different between 16f and 16c etc....?

If you are referring to the 16f84 and 16c84 the differences are very
minimal. Since you mention the 16f84a you've got the "newest" f84 and
you don't have to worry about what the differences were.

That said, many here will recommend switching to a newer PIC, the 84 is
very old and things can be done much easier with the newer parts.

TTYL

Herbert Graf

unread,
Feb 23, 2007, 4:51:39 PM2/23/07
to Microcontroller discussion list - Public.
On Fri, 2007-02-23 at 13:31 -0800, Harold Hallikainen wrote:
> >
> > ok, i am doing a new project, u guessed it...a clock.
> >
> > now, i have read some guides about this on the www, but the give different
> > info. Is it true that the frequency of the oscillator used is divided by
> > 4,
> > and instructions are executed at this new frequency? im using a pic16f84a.
> >
> > also, i wanted to know, what is different between 16f and 16c etc....?
> >
> > ty.
>
> Yes, Fosc is divided by 4. So, if you have a 4MHz oscillator, the PIC runs
> at 1,000 instructions per second (some instructions take twice as long,
> like those that modify PC).

Oops, I think you mean 1,000,000 instructions per second.

> 16F uses flash memory. It can be erased electrically. 16C uses eprom. If
> in a ceramic case, it can be erased with UV. If in a plastic case, it
> cannot be erased and is considered "one time programmable."

True, except for the 16c84, which was EEPROM based (basically same
features as the 16f84).

TTYL

Jinx

unread,
Feb 23, 2007, 5:19:41 PM2/23/07
to Microcontroller discussion list - Public.
> ok, i am doing a new project, u guessed it...a clock.

'noob, good h/w - s/w project to start with. Endless possibilities
for mechanical and functional imagination

> and instructions are executed at this new frequency? im using a
> pic16f84a

The f84 was the first PIC I used and all of my clocks built with it
at that time (1996 ? and similar Motorolas before that) are still out
there, working away as good as new. The f84 is fine to use if you
have one, but for future projects there are better and cheaper
alternatives (628, 88, 688, 18F, 12F etc). For a simple clock or
timer, you can get by with just TMR0

I'd suggest you not use a 4MHz time-base, but 32768Hz. This
makes counting and rollover IRQs from TMR0 very easy. A crystal
will drift though and is not a good long-term solution. Fine for power
outages though. If you see this drift, and you will, make the reset
feature set a particular time, eg 6pm

The best you can expect is that the crystal works at an average of
32768Hz. But 1Hz high will be (32769/32768) * 86400 = 86402.6
or 2.6 seconds per day fast

If you want this to keep good time, use an AC wall-wart so that you
can count mains cycles. A loaded unregulated DC one may also
have enough ripple to use for this, or you could get into the wall-
wart and remove the filter cap and bridge. These can be used in your
circuit for rectification, but you'll have the AC accessible to the PIC
before that

William Chops

unread,
Feb 23, 2007, 5:29:19 PM2/23/07
to Microcontroller discussion list - Public.

On Feb 23, 2007, at 1:50 PM, Herbert Graf wrote:

>> i wanted to know, what is different between 16f and 16c etc....?
>
> If you are referring to the 16f84 and 16c84 the differences
> are very minimal.

> That said, many here will recommend switching to a newer PIC...

Note that the 16F84 vs 16C84 is the ONLY instance where there are
only "minimal" differences between 16F and 16C; the 16C84 was the
very first pic with "electrically erasable" program memory, and
microchip hadn't yet settled on the "F" designator for that...

For ALL other "C" chips, you're stuck with one-time-programmable,
or at best "expensive but UV erasable" chips.

Summarizing:

A microcontroller beginner should choose a flash-based electrically
erasable and reprogrammable chip.

Among Microchip PIC chips, electrically erasable chips are designated
by including an "F" in the part name.

Except for the 16C84, which is so old that it should be avoided anyway.

BillW

Byron A Jeff

unread,
Feb 23, 2007, 5:36:23 PM2/23/07
to Microcontroller discussion list - Public.
On Fri, Feb 23, 2007 at 01:16:22PM -0800, picnoob wrote:
>
> ok, i am doing a new project, u guessed it...a clock.

Been there. Done that. Got the tee shirt.

> now, i have read some guides about this on the www, but the give
> different
> info. Is it true that the frequency of the oscillator used is
> divided by 4,
> and instructions are executed at this new frequency?

That's true for PICs.

>im using a pic16f84a.

What exactly is this clock supposed to do? There's probably better
choices than the 16F84 if you want to perform this activity. One
reason is that other PICs have multiple timers. In particular 16F
PICS with timer 1 can be driven from an external oscillator independant
of the PIC oscillator

> also, i wanted to know, what is different between 16f and 16c etc....?

Marketing. Nothing really worth talking about.

I'd advise you take a read on newer PICs if PIC are where you are
planning to work. I've argued over the years that the 16F84 limitations
makes getting tasks done difficult.

Finally just so you won't go away empty handed you can take a look
at my code for my sunrise/sunset outdoor light controller, which is
basically a calendar/clock that turns outdoor lights on about dusk
and off around dawn. Code is here:

http://www.finitesite.com/d3jsys/clock.asm
It's written for a 16F877 and drives timer 1 with a 32 khZ watch
crystal to generate a 2 second counter.

Accuracy is a bit problematic. Crystals will vary in oscillation
rates based on temperature. I always planned to update the project
to use the 60 Hz power line signal, but just never got back to it.

Hope this helps,

BAJ

Byron A Jeff

unread,
Feb 23, 2007, 5:43:37 PM2/23/07
to Microcontroller discussion list - Public.
On Fri, Feb 23, 2007 at 05:36:23PM -0500, Byron A Jeff wrote:
> > also, i wanted to know, what is different between 16f and 16c etc....?
>
> Marketing. Nothing really worth talking about.

Actually let me amend this. I read the question as the difference
between the 16C84 and the 16F84. That's marketing.

The 16C series was Microchip's initial PIC offering. They used
EPROM for program memory. They came in two packages: plastic one
time programmable (OTP) and ceramic EPROM style cases with the
quartz window that allowed for erasure.

The point is somewhat moot because virtually all of Microchip's
current PIC offerings are flash based. But from what I understand
you can still buy 16C parts if you need them. As a hobbyist, you
probably won't ever need 16C parts.

> I'd advise you take a read on newer PICs if PIC are where you are
> planning to work. I've argued over the years that the 16F84
> limitations
> makes getting tasks done difficult.

Worth reiterating. For a tutorial on newer 16F parts take a look
a Nigel Goodwin's PIC tutorial at http://www.winpicprog.com. That
tutorial uses 16F628 and 16F876/877 parts with has much better
peripheral packages than the anemic 16F84.

Harold Hallikainen

unread,
Feb 23, 2007, 5:48:13 PM2/23/07
to Microcontroller discussion list - Public.
Thanks for catching my typo! I wasn't aware of the 16c84. Have never used
it. Most of my stuff is now 18F or 24H unless it's a very simple cost
sensitive device, when I go back to 16F.

Thanks!

Harold

--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

Jinx

unread,
Feb 23, 2007, 6:16:29 PM2/23/07
to Microcontroller discussion list - Public.
I wrote -

> I'd suggest you not use a 4MHz time-base, but 32768Hz. This
> makes counting and rollover IRQs from TMR0 very easy

The other advantage is power consumption. The slower the PIC
runs, the less it uses

eg ~ 1000 - 2000uA 4MHz 5.5V (XT mode) versus 35uA 32kHz
3V (LP mode), a substantial saving

Bob Axtell

unread,
Feb 23, 2007, 6:35:26 PM2/23/07
to Microcontroller discussion list - Public.
Byron A Jeff wrote:
> On Fri, Feb 23, 2007 at 01:16:22PM -0800, picnoob wrote:
>
>> ok, i am doing a new project, u guessed it...a clock.
>>
>
> Been there. Done that. Got the tee shirt.
>
Get the DVD, too?
If you want a clock that is ALWAYS accurate to a second or two, you
might consider
using WWVB (if you are in the US/CA at 60Khz), DCF77 (Germany at
77.5Khz), HGB (switzerland
at 75Khz), MSF60 (UK at 60Khz), or two Japanese transmitters, one at
40Khz and the second at
60Khz. They provide worldwide coverage. Tests indicate that WWVB reaches
well into South America,
for exanple, and Japanese signals reach Australia easily.

C-MAX sells modules that will extract the time information, or even
smaller modules that act as receivers
only, and you can extract the time code.

I am designing a solar tracking system that uses accurate time and a
known position to determine where
the sun is at all times. The tracking math has all been solved, but an
accurate time source was undetermined. I have
now concluded that the above time service is cheaper and more reliable
than GPS, as GPS requires an
expensive antenna & GPS receiver, at about $90 USD, while this module
and antenna in a plastic box
costs less than $10 USD. For reliability, I only use WWVB once a day in
the middle of the night to SET
the time, and a separate timekeeping chip maintains it during the day.

So, funnel this time data into your clock once a day to re-calibrate the
clock.

--Bob

Rikard Bosnjakovic

unread,
Feb 24, 2007, 12:43:42 AM2/24/07
to Microcontroller discussion list - Public.
On 2/23/07, Harold Hallikainen <har...@hallikainen.org> wrote:

> Yes, Fosc is divided by 4. So, if you have a 4MHz oscillator, the PIC runs
> at 1,000 instructions per second (some instructions take twice as long,
> like those that modify PC).

I read about this (and some other crystal-based text) in my PIC-book,
but what I cannot understand is why a 32768Hz-crystal is magical
enough to use for clock-applications. Dividing 32768 with 4 gives me
8192, and that's a value that I certainly don't connect with anything
that's got to do with timing.

On the other hand, I haven't yet got a good grip of why to use
crystals. To time the controller, sure, but...how? I have read that
an xtal oscillates due to some quartz-magic in it, but that's all. No
clue of how it works.

This is probably a good incitament to start this day with a visit to Wikipedia.


--
- Rikard.

Richard Prosser

unread,
Feb 24, 2007, 1:55:01 AM2/24/07
to Microcontroller discussion list - Public.
Rikard,
Yes, but if you divide 8192 by 2^11 you get 1. i.e 1Hz and that is
useful. And dividing by 2, 11 (or 15) times is easy.

32.767kHz is commonly used I think because a small crystal cut in the
right way can be very stable and requires low drive power and so is
useful for all manner of clocks, watches and computer RTCs.

RP

scott larson

unread,
Feb 24, 2007, 2:05:29 AM2/24/07
to Microcontroller discussion list - Public.
On 2/23/07, Rikard Bosnjakovic <rikard.bo...@gmail.com> wrote:

> I read about this (and some other crystal-based text) in my PIC-book,
> but what I cannot understand is why a 32768Hz-crystal is magical
> enough to use for clock-applications. Dividing 32768 with 4 gives me
> 8192, and that's a value that I certainly don't connect with anything
> that's got to do with timing.

2^16 = 65536.
2^15 = 32768.

Timer1 in PICs is a 16 bit timer which can be driven by an external
source, in this case a 32768 Hz crystal. Preloading the timer1 high
register with 0x80 (set the 7th bit), and running the timer will
overflow after 32768 cycles. Configure an interrupt on overflow and
you get an interrupt every 1 second.

-Scott

Jinx

unread,
Feb 24, 2007, 3:46:15 AM2/24/07
to Microcontroller discussion list - Public.
> but what I cannot understand is why a 32768Hz-crystal is magical

As others have said, it's divisible by binary powers, as can be seen
if you look at it in hexadecimal

32768 = $8000

This is good for a clock as a large number of cycles is a whole or
easy fraction of a second when used to clock one of the PIC's timer/
counters. On the F84 you have TMR0. When assigned to count
instruction cycles, it will count 256 (00 to FF) and then rollover to
00, setting T0IF, which can be used as an interrupt flag. A 32768
crystal will cause this rollover 32768/256 = 128 times per second.
If you set the pre-scaler to 1:128, then it now takes 32768 cycles
to set T0IF, or IOW a 1Hz interrupt

Note that the $8000 above can be broken down like this -

The 00 is the low byte, and in the above example is what is counted
by the TMR0 register. The 80 (= d128) is the high byte, and that
is what is counted by the pre-scaler

Another hex one is 9.8304MHz = $960000, which is good for high-
speed RS232 (most baud rates are hexadecimally-based), especially
if used with the 4 x PLL on an 18F

4MHz, 10MHz or 20MHz is better for shorter, exact periods, for
example 100us, because the cycle times are respectively 1us, 400ns
and 200ns. You might use something like this in a timer capture to
measure a fast event (eg ultrasonic sounding) or generate pulses. A
crystal like a 19.6608MHz, good for a clock/RS232, has a cycle
time of 203.45052083ns, which is not a pretty number to work with
if you want exactly 10us, 50us or 100us

So you choose your crystal based on the jobs the micro has to do

Rikard Bosnjakovic

unread,
Feb 24, 2007, 4:30:11 AM2/24/07
to Microcontroller discussion list - Public.
On 2/24/07, Jinx <joeco...@clear.net.nz> wrote:

> So you choose your crystal based on the jobs the micro has to do

Thank you for your very descriptive post. I will print it out and keep
it for reference when I start with my first PIC-circuit. Still waiting
(too long...) for the programmer to arrive by post.

--
- Rikard.

Jinx

unread,
Feb 24, 2007, 4:51:55 AM2/24/07
to Microcontroller discussion list - Public.

> Thank you for your very descriptive post

You're most welcome. Good luck with the programming, I'm
sure you'll just rocket along when you can see the PIC working

If you get stuck, the list is here for you

Tony Smith

unread,
Feb 24, 2007, 5:37:17 AM2/24/07
to Microcontroller discussion list - Public.
> Another hex one is 9.8304MHz = $960000, which is good for
> high- speed RS232 (most baud rates are hexadecimally-based),
> especially if used with the 4 x PLL on an 18F
>
> 4MHz, 10MHz or 20MHz is better for shorter, exact periods,
> for example 100us, because the cycle times are respectively
> 1us, 400ns and 200ns. You might use something like this in a
> timer capture to measure a fast event (eg ultrasonic
> sounding) or generate pulses. A crystal like a 19.6608MHz,
> good for a clock/RS232, has a cycle time of 203.45052083ns,
> which is not a pretty number to work with if you want exactly
> 10us, 50us or 100us


Something to add to your "may come in handy but unlikey to" list is that
some car tools (diagnostics stuff etc) use RS232, and do so at 8192 baud.

Tony

Rikard Bosnjakovic

unread,
Feb 24, 2007, 6:15:28 AM2/24/07
to Microcontroller discussion list - Public.
On 2/24/07, Tony Smith <ajs...@rivernet.com.au> wrote:

> Something to add to your "may come in handy but unlikey to" list is that
> some car tools (diagnostics stuff etc) use RS232, and do so at 8192 baud.

This recalls another question I had in mind. Given that timing is not
a critical issue, does it matter what crystal you chose for your
particular project (provided it's within the range specified by
Microchip)?


--
- Rikard.

Jinx

unread,
Feb 24, 2007, 6:58:01 AM2/24/07
to Microcontroller discussion list - Public.
> This recalls another question I had in mind. Given that timing is
> not a critical issue, does it matter what crystal you chose for your
> particular project (provided it's within the range specified by
> Microchip)?

If you're just like turning things on and off ? No (apart from the
power consideration of fast vs slow). And if you want to cut
costs you might use one of the RC modes

Byron A Jeff

unread,
Feb 24, 2007, 7:45:15 AM2/24/07
to Microcontroller discussion list - Public.
On Sat, Feb 24, 2007 at 06:43:42AM +0100, Rikard Bosnjakovic wrote:
> On 2/23/07, Harold Hallikainen <har...@hallikainen.org> wrote:
>
> > Yes, Fosc is divided by 4. So, if you have a 4MHz oscillator, the PIC runs
> > at 1,000 instructions per second (some instructions take twice as long,
> > like those that modify PC).
>
> I read about this (and some other crystal-based text) in my PIC-book,
> but what I cannot understand is why a 32768Hz-crystal is magical
> enough to use for clock-applications. Dividing 32768 with 4 gives me
> 8192, and that's a value that I certainly don't connect with anything
> that's got to do with timing.

It's magical because it's a power of 2. You divide it enough and you get a
nice round number to work with without having to do anything special.

Let me talk about my clock application. I applied a 32768 Hz watch crystal
to timer 1 of a 16F877, which is a 16 bit timer that counts up to 65536.
If you divide 32768 Hz/65536 you get exactly 0.5 Hz. Note that Hz is the
inverse of seconds, so if you invert 0.5 Hz you get 2 seconds. And if the
crystal is exactly on time at 32768 Hz then that time would be exactly 2
seconds.

Now the other point is that by using timer 1, I can decouple that 2 Hz clock
from the speed of the controller, which can now use a separate clock.
It's useful to be able to compute things faster without having to worry about
counting oddball frequencies in order to get exactly one second.

That's the magic of the 32768 Hz crystal.

> On the other hand, I haven't yet got a good grip of why to use
> crystals.

Supposedly they are a stable time base. That's the theory. I can tell you
that in practice crystal timebases drift especially based on temperature.

> To time the controller, sure, but...how? I have read that
> an xtal oscillates due to some quartz-magic in it, but that's all. No
> clue of how it works.

You can treat it as a black box in this application. The PIC has all of the
necessary circuitry to make the crystal do it's job, with the exception of
the crystal capacitors required to get the circuit to oscillate.

>
>This is probably a good incitament to start this day with a visit to Wikipedia.

Don't bite off more than you can chew here. All you need to know right now is
that the crystal provides a stable time base. Over researching minute details
is a project killer.

BTW you never did tell me your application, which I think I asked about in
a previous post. All I've seen so far is you're using a 16F84 to build a
clock.

I have a page that describes why the 16F84 may not be your best starting point.
You can find it here:

http://www.finitesite.com/d3jsys/16F88.html

The peripheral packages (including the multiple timers which you should use
for this clock project) extends across many of the new PIC parts. The 16F84
will limit you in a lot of ways.

I'm starting to test what I think should be the gold standard for the PIC 16F
family: the 16F886 and 16F887. Full peripheral packages, nanowatt features
which means a decent internal oscillator up to 8 Mhz and the ability to add
up to 3 more I/O pins (well 2 I/O and one input) by repurposing the MCLR and
CLK pins, and bigger hobbyist friendly DIP packages (28 and 40 pins). Also
for the bootloader crowd (of which I am a member), they are self programmable.

I reiterate my thought that a newbie should start with the biggest, best
package and work their way down instead of the other way around. By doing so,
when you need a feature (more timers, more I/O pins, PWM, ADC, serial, etc)
then it's already there for you to use instead of having to hunt up another
part with the feature you need. Hobbyist generally build a series of one off
projects, so the bigger packages and the slightly higher costs are small
factors in the usage decision.

The upshot is that if you start with the 16F84, then you'll learn from
tutorials and webpages all of the twisted, convoluted ways to program stuff
around the part's limitations. Then you'll start thinking that's the normal
right way to go about handling serial, multiple timers, PWM and the like.
However, if you start with the best, right part from the start, then you'll
start designing with best practices from the start.

One last point. You need to download, print, and read the 16F midrange manual.
You can find it here:

http://ww1.microchip.com/downloads/en/DeviceDoc/33023a.pdf

It'll be a much better use of your study time than trying to understand the
ins and out of Colpitts and Pierce crystal oscillators.

BAJ

Byron A Jeff

unread,
Feb 24, 2007, 8:02:42 AM2/24/07
to Microcontroller discussion list - Public.
On Sat, Feb 24, 2007 at 10:30:11AM +0100, Rikard Bosnjakovic wrote:
> On 2/24/07, Jinx <joeco...@clear.net.nz> wrote:
>
> > So you choose your crystal based on the jobs the micro has to do
>
> Thank you for your very descriptive post. I will print it out and keep
> it for reference when I start with my first PIC-circuit. Still waiting
> (too long...) for the programmer to arrive by post.

My impatience is one of the reasons why I developed the Trivial PIC
programming series. You can get started with a single I/C and a handful of
discretes in a short period of time. It's not a good permanent programming
soluion (I'm partial to bootloaders myself), but it does have the quality
of near instant gratification.

http://www.finitesite.com/d3jsys

BAJ

Byron A Jeff

unread,
Feb 24, 2007, 8:04:25 AM2/24/07
to Microcontroller discussion list - Public.
On Sun, Feb 25, 2007 at 12:58:01AM +1300, Jinx wrote:
> > This recalls another question I had in mind. Given that timing is
> > not a critical issue, does it matter what crystal you chose for your
> > particular project (provided it's within the range specified by
> > Microchip)?
>
> If you're just like turning things on and off ? No (apart from the
> power consideration of fast vs slow). And if you want to cut
> costs you might use one of the RC modes

This is also another good point in favor of the multispeed internal
oscillator that many PIC parts carry. You can pick a speed and you
don't even need to add any external parts.

BAJ

peter green

unread,
Feb 24, 2007, 10:12:58 AM2/24/07
to Microcontroller discussion list - Public.

> This recalls another question I had in mind. Given that timing is not
> a critical issue, does it matter what crystal you chose for your
> particular project (provided it's within the range specified by
> Microchip)?
the exact speed isn't critical

in general selecting the clock speed is a compromise between power consuption and processing speed.

peter green

unread,
Feb 24, 2007, 10:12:59 AM2/24/07
to Microcontroller discussion list - Public.

> > On the other hand, I haven't yet got a good grip of why to use
> > crystals.
>
> Supposedly they are a stable time base. That's the theory. I can tell you
> that in practice crystal timebases drift especially based on temperature.
They are FAR better than ceramic resonators or RC cuircuits.

One thing to remember is that we put much tighter requirements on timebases than on almost anything else. A clock whose speed was 1% off (and for most components 1% is considered good) would be gaining or losing about a quarter of an hour per day which most people would find totally unacceptable!

peter green

unread,
Feb 24, 2007, 10:12:58 AM2/24/07
to Microcontroller discussion list - Public.

> -----Original Message-----
> From: piclist...@mit.edu [mailto:piclist...@mit.edu]On Behalf
> Of Rikard Bosnjakovic
> Sent: 24 February 2007 05:44
> To: Microcontroller discussion list - Public.
> Subject: Re: [PIC] making a quartz clock
>
>
> On 2/23/07, Harold Hallikainen <har...@hallikainen.org> wrote:
>
> > Yes, Fosc is divided by 4. So, if you have a 4MHz oscillator,
> the PIC runs
> > at 1,000 instructions per second (some instructions take twice as long,
> > like those that modify PC).
>
> I read about this (and some other crystal-based text) in my PIC-book,
> but what I cannot understand is why a 32768Hz-crystal is magical
> enough to use for clock-applications. Dividing 32768 with 4 gives me
> 8192, and that's a value that I certainly don't connect with anything
> that's got to do with timing.

you clearly aren't used to thinking in powers of two yet

1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536

devide 32768hz by 2 15 times and you end up with 1hz.

michael brown

unread,
Feb 24, 2007, 10:19:13 AM2/24/07
to Microcontroller discussion list - Public.
Rikard Bosnjakovic wrote:
> On 2/24/07, Tony Smith <ajs...@rivernet.com.au> wrote:
>
>> Something to add to your "may come in handy but unlikey to" list is
>> that some car tools (diagnostics stuff etc) use RS232, and do so at
>> 8192 baud.
>
> This recalls another question I had in mind. Given that timing is not
> a critical issue, does it matter what crystal you chose for your
> particular project (provided it's within the range specified by
> Microchip)?
Not really, it's more a matter of convenience, need and personal
prefference. When I first started, I used nothing but 4MHz xtals with the
16F84. Now, the main 18 pin PIC that I tinker with is the 16F88, I suggest
you look into those as they are way nicer (and allot cheaper) than the F84.
They have an internal oscillator that is trimmed to 1% accuracy. It can run
from 31kHz to 8MHz and can be changed on the fly, much more convenient than
external crystals, but still accurate enough to do serial comms and the
like. For small stuff, I like the 12F683 (8 pins) but it lacks a built in
UART.

Now, I only use an external xtal when I need the accuracy. For example, I
have a circuit used for timing mechanical clocks and watches that I have
managed to tweak within a couple of ppm (at room temperature) using an
external xtal and by playing with the loading caps. I used a quartz analog
watch of known accuracy to tweak it all out by measuring the spacing of the
impulses to the stepper motor. In this case I wanted uS precision with high
accuracy.

By using the extra oscillator that is part of the TMR1 module, you can use a
32kHz watch crystal to clock TMR1 so that the PIC can shut everything else
down (including its internal oscillator) and go to sleep, only waking up on
TMR1 rollovers. This makes for very, very low power consumption (as in
batteries lasting for many months). When awake, you can run at any speed
from 31kHz to 8MHz using the INTOSC module.

Right now I'm tinkering with a propellor clock type thingy. For that
project, I'm likely to use a Dallas 1307 real-time clock chip in the "final"
version.

picnoob

unread,
Feb 24, 2007, 10:56:09 AM2/24/07
to pic...@mit.edu

ok, i think im gettin this.. 2 more questions: if the crystal is connected to
t0clk, will the freq be divided by 4. and also, how do i wire the 32768Hz
crystal into the t0clk pin?


i cant just execute instructions at 32768Hz, due to my design.

current wiring: http://www.nabble.com/file/6750/2%20xtals.bmp 2 xtals.bmp
--
View this message in context: http://www.nabble.com/making-a-quartz-clock-tf3281293.html#a9135079
Sent from the PIC - [PIC] mailing list archive at Nabble.com.

Jan-Erik Söderholm

unread,
Feb 24, 2007, 11:37:58 AM2/24/07
to Microcontroller discussion list - Public.
picnoob skrev:

> how do i wire the 32768Hz
> crystal into the t0clk pin?

Was this an F84A ?
It has no crystal osc on tmr0...

Jan-Erik.

Byron A Jeff

unread,
Feb 24, 2007, 11:47:51 AM2/24/07
to Microcontroller discussion list - Public.
On Sat, Feb 24, 2007 at 05:37:58PM +0100, Jan-Erik S?derholm wrote:
> picnoob skrev:
>
> > how do i wire the 32768Hz
> > crystal into the t0clk pin?
>
> Was this an F84A ?
> It has no crystal osc on tmr0...

Which is yet another good reason not to use it.

Jan-Erik,

IIRC picnoob is in Sweden, right? Can you drop him some newer parts in the
post?

BAJ

Byron A Jeff

unread,
Feb 24, 2007, 11:46:48 AM2/24/07
to Microcontroller discussion list - Public.
On Sat, Feb 24, 2007 at 07:56:09AM -0800, picnoob wrote:
>
> ok, i think im gettin this.. 2 more questions: if the crystal is connected to
> t0clk, will the freq be divided by 4.

There's a prescaler you can use IIRC. I haven't looked at 16F84s in a long
long time.


> and also, how do i wire the 32768Hz crystal into the t0clk pin?

I don't think you can. That's why many of us have been telling you to abandon
the 16F84 and work with a newer part.

Where are you again? Maybe some kind soul out there can drop you some 16F88s
in the post. Or even better some 16F887s.

> i cant just execute instructions at 32768Hz, due to my design.

That's where the timer 1 that most modern PICs are useful. It can have its
own separate crystal from the main processor crystal.

Please consider it. It'll make your work a lot easier.

BAJ

Dario Greggio

unread,
Feb 24, 2007, 12:11:00 PM2/24/07
to Microcontroller discussion list - Public.
I can add that Higher frequency Clocks call for higher Power
consumption. Don't know if it's an issue...

--
Ciao, Dario

Jan-Erik Söderholm

unread,
Feb 24, 2007, 1:03:12 PM2/24/07
to Microcontroller discussion list - Public.
Sure,
he can check my web site :
http://www.sodjan.se
http://www.jescab.se/Prod_PIC.html (PIC's I resell)
http://www.jescab.se/Kontakt.htm (mail and other info)
http://www.jescab.se/Kontakt1.htm (english page)

And (*if* he's in Sweden) he should also register on the
Swedish "Elektronikforum" :
http://elektronikforumet.com/forum/index.php
There is a separate forum part for uC/uP.

Jan-Erik.


Byron A Jeff skrev:

picnoob

unread,
Feb 24, 2007, 3:21:10 PM2/24/07
to pic...@mit.edu


Jan-Erik Söderholm-2 wrote:
>
> And (*if* he's in Sweden)
>

nope, UK. sweeden would be nice to live in, or south korea.
--
View this message in context: http://www.nabble.com/making-a-quartz-clock-tf3281293.html#a9137456


Sent from the PIC - [PIC] mailing list archive at Nabble.com.

picnoob

unread,
Feb 24, 2007, 3:21:20 PM2/24/07
to pic...@mit.edu


Jan-Erik Söderholm-2 wrote:
>
> And (*if* he's in Sweden)
>

nope, UK. sweeden would be nice to live in, or south korea:)
--
View this message in context: http://www.nabble.com/making-a-quartz-clock-tf3281293.html#a9137457


Sent from the PIC - [PIC] mailing list archive at Nabble.com.

picnoob

unread,
Feb 24, 2007, 3:21:20 PM2/24/07
to pic...@mit.edu

Jan-Erik Söderholm-2 wrote:
>
> And (*if* he's in Sweden)
>

nope, UK. sweeden would be nice to live in, or south korea :)

so, i take it that using a pic16f84a isnt the best idea, im considering a
16f628a, does it have an analogue to digital converter or not, i cant find
out.
--
View this message in context: http://www.nabble.com/making-a-quartz-clock-tf3281293.html#a9137457


Sent from the PIC - [PIC] mailing list archive at Nabble.com.

Jan-Erik Söderholm

unread,
Feb 24, 2007, 3:42:22 PM2/24/07
to Microcontroller discussion list - Public.
picnoob wrote :

> so, i take it that using a pic16f84a isnt the best idea, im considering a
> 16f628a, does it have an analogue to digital converter or not, i cant find
> out.

Of course you can! Just check the datasheet.
Concider the 16F88 for a full-function 16F in 18-pin DIP
*including* an A/D converter.

Byron A Jeff

unread,
Feb 24, 2007, 4:02:31 PM2/24/07
to Microcontroller discussion list - Public.
On Sat, Feb 24, 2007 at 09:42:22PM +0100, Jan-Erik S??derholm wrote:
> picnoob wrote :
>
> > so, i take it that using a pic16f84a isnt the best idea, im considering a
> > 16f628a, does it have an analogue to digital converter or not, i cant find
> > out.
>
> Of course you can! Just check the datasheet.

Even better you can check out the PIC parameter page here:

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=74

> Concider the 16F88 for a full-function 16F in 18-pin DIP
> *including* an A/D converter.

I still think a larger package would be better. The 16F886 and 16F887 look
like really excellent parts.

BAJ

Jinx

unread,
Feb 24, 2007, 4:05:30 PM2/24/07
to Microcontroller discussion list - Public.
> > and also, how do i wire the 32768Hz crystal into the t0clk pin?
>
> I don't think you can

On my first clocks, because of that and the lack of the fully-featured
PICs of today, I used (and still do occassionally in logic circuits) the
HA7210 as a driver for T0CKI

http://www.datasheetcatalog.com/datasheets_pdf/H/A/7/2/HA7210.shtml

michael brown

unread,
Feb 24, 2007, 5:25:42 PM2/24/07
to Microcontroller discussion list - Public.
picnoob wrote:
> Jan-Erik Söderholm-2 wrote:
>>
>> And (*if* he's in Sweden)
>>
>
> nope, UK. sweeden would be nice to live in, or south korea :)
>
> so, i take it that using a pic16f84a isnt the best idea, im
> considering a 16f628a, does it have an analogue to digital converter
> or not, i cant find
> out.

The F628 does not have ADC, it does have a comparator though. The 16F88 is
what you want for an 18 pin part. You can think of it like this, sort of,
the F84 was superceded by the F628 which was superceded by the 16F88. The
F88 has the most memory and peripherals, the lowest power requirements and
the best internal oscillator of the bunch. It also costs the least.

Rikard Bosnjakovic

unread,
Mar 1, 2007, 10:49:08 PM3/1/07
to Microcontroller discussion list - Public.
On 2/24/07, peter green <plug...@p10link.net> wrote:

> in general selecting the clock speed is a compromise between power consuption and processing speed.

Higher clock speed = higher power consumption?


--
- Rikard.

peter green

unread,
Mar 2, 2007, 2:15:53 AM3/2/07
to Microcontroller discussion list - Public.

> -----Original Message-----
> From: piclist...@mit.edu [mailto:piclist...@mit.edu]On Behalf
> Of Rikard Bosnjakovic
> Sent: 02 March 2007 03:49
> To: Microcontroller discussion list - Public.
> Subject: Re: [PIC] making a quartz clock
>
>

> On 2/24/07, peter green <plug...@p10link.net> wrote:
>
> > in general selecting the clock speed is a compromise between
> power consuption and processing speed.
>
> Higher clock speed = higher power consumption?

yep

Jinx

unread,
Mar 2, 2007, 3:30:15 AM3/2/07
to Microcontroller discussion list - Public.

> Higher clock speed = higher power consumption?

That's the beauty of multi-speed PICs. Low when you want
to save power, high when you want quick instructions

There's some debate though about a case like when the PIC's
asleep and you wake it to do a task. Wake it up in low gear
and it will use less power but it's on for longer. Wake it up in
top gear and it uses more power but it's on for a shorter time.
Sometimes the maths will prove the opposite of what you
instinctively think

Howard Winter

unread,
Mar 3, 2007, 6:49:52 AM3/3/07
to Microcontroller discussion list - Public.
BAJ,

On Sat, 24 Feb 2007 07:45:15 -0500, Byron A Jeff wrote:

>...


> > On the other hand, I haven't yet got a good grip of why to use
> > crystals.
>
> Supposedly they are a stable time base. That's the theory. I can tell you
> that in practice crystal timebases drift especially based on temperature.

And in the Good Old Days you could get things called "Crystal Ovens", a little thermostatically-controlled housing that the crystal sat in, at a constant
temperature so at a much more stable/accurate frequency than one just sitting in the air. Unfortunately they seemed to go the way of Thermionic
Valves ("vacuum tubes" for the left-ponders) so we're stuck with trying to do temperature compensation in other ways.

Jinx also came up with the idea of using the solenoid pulses from an MSF/DCF/WWVB wall-clock as a source of accurate 1 second pulses, but using a
clock to provide the signal to a clock seems a bit strange in this context! :-)

Cheers,


Howard Winter
St.Albans, England

Marsh James

unread,
Mar 3, 2007, 7:11:16 AM3/3/07
to Microcontroller discussion list - Public.
Has anyone tried using a temperature compensating capacitor across the xtal?

Howard Winter <HD...@H2Org.demon.co.uk> wrote: BAJ,

Cheers,


Howard Winter
St.Albans, England

Jim. w9...@sbcglobal.net

If you can read this, thank a teacher. If you are reading it in English thank a Veteran.

Byron A Jeff

unread,
Mar 3, 2007, 9:44:50 AM3/3/07
to Microcontroller discussion list - Public.
On Sat, Mar 03, 2007 at 11:49:52AM +0000, Howard Winter wrote:
> BAJ,
>
> On Sat, 24 Feb 2007 07:45:15 -0500, Byron A Jeff wrote:
>
> >...
> > > On the other hand, I haven't yet got a good grip of why to use
> > > crystals.
> >
> > Supposedly they are a stable time base. That's the theory. I can tell you
> > that in practice crystal timebases drift especially based on temperature.
>

> And in the Good Old Days you could get things called "Crystal Ovens", a
>little thermostatically-controlled housing that the crystal sat in, at a
>constant temperature so at a much more stable/accurate frequency than one
>just sitting in the air. Unfortunately they seemed to go the way of
>Thermionic Valves ("vacuum tubes" for the left-ponders) so we're stuck with
>trying to do temperature compensation in other ways.

My controller is in an unheated basement, so it is subject to varied
temperature swings.


> Jinx also came up with the idea of using the solenoid pulses from an
>MSF/DCF/WWVB wall-clock as a source of accurate 1 second pulses, but using a
>clock to provide the signal to a clock seems a bit strange in this
>context! :-)

I had similar ideas. My first crack was using a $2 quartz clock movement to
adjust my clock. I tried passing the hour hand through a LED/photodiode
interruptor. Proved to be mechanically unstable.

If I ever decide to redo the project, I plan to use the 60 Hz power line
frequency as a time base with the 32 Khz crystal as backup when power fails.

As it is I simply go an reset the time every 5-6 weeks or so after it's
drifted far enough that the lights are coming on while it's still bright and
going off when still dark. Not enough of an annoyance to redo the project...
yet.

BAJ

Mark Rages

unread,
Mar 3, 2007, 12:21:45 PM3/3/07
to Microcontroller discussion list - Public.
On 3/3/07, Howard Winter <HD...@h2org.demon.co.uk> wrote:
> BAJ,
>
> On Sat, 24 Feb 2007 07:45:15 -0500, Byron A Jeff wrote:
>
> >...
> > > On the other hand, I haven't yet got a good grip of why to use
> > > crystals.
> >
> > Supposedly they are a stable time base. That's the theory. I can tell you
> > that in practice crystal timebases drift especially based on temperature.
>
> And in the Good Old Days you could get things called "Crystal Ovens", a little thermostatically-controlled housing that the crystal sat in, at a constant
> temperature so at a much more stable/accurate frequency than one just sitting in the air. Unfortunately they seemed to go the way of Thermionic
> Valves ("vacuum tubes" for the left-ponders) so we're stuck with trying to do temperature compensation in other ways.
>

Could you make a simple oven by epoxying the crystal to a PTC
thermistor run in self-heating mode? Not a good choice for battery
operation.

Digital cell phones use a "temperature-compensated crystal oscillator"
that is good to a PPM or so across temperature. One of these could
be salvaged for a hobby project, although you would have to design
around the frequency you get. These parts are reasonably low-current.

Regards,
Mark
--
You think that it is a secret, but it never has been one.
- fortune cookie

Rikard Bosnjakovic

unread,
Mar 9, 2007, 12:58:33 AM3/9/07
to Microcontroller discussion list - Public.
On 2/24/07, Byron A Jeff <by...@cc.gatech.edu> wrote:

> There's a prescaler you can use IIRC. I haven't looked at 16F84s in a long
> long time.

>From what I can read of the datasheets, a prescaler is some sort of
black magic that scales down Hz to some other value. For a 16F628a
(which I'm going to work on) I can select either 1:1, 1:2, 1:4 or 1:8
in T1CON. I'm going to use a 32768Hz-crystal for counting secs in my
app.

I want a timer-interrupt each second. 32768/4 = 8192. If I pick
prescaler 1:8 I (think I) get 1024, but I'm not sure what this value
is or how I can use it to measure one second of elapsed time.

What should I set the prescaler at, and what does it really do?


--
- Rikard.

Marcel Birthelmer

unread,
Mar 9, 2007, 2:16:05 AM3/9/07
to Microcontroller discussion list - Public.
Rikard,
you can think of the prescaler as a counter. For example, at 1:4, the
you get one count out when you put 4 counts in - it counts to 4
internally and increases the output register (TMR1) when it gets
there, and then starts over.
So you have 32768Hz on the input. If your prescaler is 1:8, you get a
TMR1 interrupt every 4096 cycles. Since 32768 ticks are 1 second, 4096
cycles are 1/8th of a second. So you get eight interrupts per second.
You can then count it down in software to react only to the 8th such
interrupt, and there you go.

- Marcel

Jinx

unread,
Mar 9, 2007, 2:57:36 AM3/9/07
to Microcontroller discussion list - Public.
> >From what I can read of the datasheets, a prescaler is some sort of
> black magic that scales down Hz to some other value. For a 16F628a
> (which I'm going to work on) I can select either 1:1, 1:2, 1:4 or 1:8
> in T1CON. I'm going to use a 32768Hz-crystal for counting secs in
> my app

A pre-scaler is a binary ripple counter (or divider if you want to call it
that) which can have the count limit or division set by s/w. If set to 1:2,
you get one "increment" out for every two that go in, similarly with 1:4,
1:8 and so on. There are diagrams showing the phase relationships but
it's not necessary to understand those to make it work

> I want a timer-interrupt each second. 32768/4 = 8192. If I pick
> prescaler 1:8 I (think I) get 1024, but I'm not sure what this value
> is or how I can use it to measure one second of elapsed time.

When Timer1 is used with an external crystal, the crystal frequency
is not divided by 4, as the main oscillator is. Therefore you want to
count the full 32768 cycles for 1 second. This means you don't have
to use the pre-scaler at all

To take your example above, any count less than 0x0000 (65536) can
be done with a 16-bit timer like TMR1. Very close to 0x0000 can be
tricky, time-wise, but when you're hundreds or thousands of counts
below roll-over it's not generally a problem. The 8192 (0x2000) is
loaded into the timer as -8192, which is 0x100000 - 0x2000 = 0xE000.
So TMR1L would be set to 0x00 and TMR1H to 0xE0, and the pre-
scaler is not needed

The easiest way to get 1 second interrupts out is to clear TMR1L and
set TMR1H to 0x80. ie TMR1 = 0x8000. It will take 32768 (0x8000)
to get TMR1 back up to 0x0000 and generate an interrupt. Then you
reload TMR1H with 0x80 (If the reload is done early in the ISR, then
TMR1H will still be 0x00, and simply BSF TMR1H,7 will make it 0x80,
but I can't imagine you'd be so short of time you'd need to save an
instruction cycle)

TMR1L has already been 0x00 (at the instant of TMR1IF being set)
and is being incremented as part of the second timing, so don't touch
TMR1L

Oh, and don't forget to clear TMR1IF

michael brown

unread,
Mar 9, 2007, 7:13:44 AM3/9/07
to Microcontroller discussion list - Public.
Rikard Bosnjakovic wrote:
> On 2/24/07, Byron A Jeff <by...@cc.gatech.edu> wrote:
>
>> There's a prescaler you can use IIRC. I haven't looked at 16F84s in
>> a long long time.
>
> From what I can read of the datasheets, a prescaler is some sort of
> black magic that scales down Hz to some other value. For a 16F628a
> (which I'm going to work on) I can select either 1:1, 1:2, 1:4 or 1:8
> in T1CON. I'm going to use a 32768Hz-crystal for counting secs in my
> app.
>
> I want a timer-interrupt each second. 32768/4 = 8192. If I pick
> prescaler 1:8 I (think I) get 1024, but I'm not sure what this value
> is or how I can use it to measure one second of elapsed time.
>
> What should I set the prescaler at, and what does it really do?

The prescalers is just some extra bits that can be tacked onto the
counter that act like a clock divider. Using a 32kHz crystal to clock
TMR1 will generate an interrupt every 2 seconds with the prescaler set
to 1. If you were to set the prescaler to 8, then TMR1 would overflow
every 16 seconds instead. IOW, you don't need to use the prescaler (set
it to 1), what you need to do is modify TMR1H every time an interrupt
occurs. You should not touch TMR1L if you want accurate timekeeping.

You will simply set the MSB of TMR1H i.e. bsf TMR1H, 7 (or you could
just move 0x80 to TMR1H. This will make TMR1 overflow after 32768 ticks
instead of 65,536. The result is that you will get one interrupt per
second.

Here's how to do it: [ Note how TMR1H is only modified right after
TMR1L increments, this is important for perfect timekeeping ]

;---------------------------------------------------------------------------------------------
; HEADER:
;---------------------------------------------------------------------------------------------
LIST P=16f88
RADIX DEC
INCLUDE "p16f88.inc"
;Program Configuration Register 1
__CONFIG _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF &
_WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF &
_PWRTE_ON & _WDT_OFF & _INTRC_IO

;Program Configuration Register 2
__CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF

;---------------------------------------------------------------------------------------------
; EQUATES:
;---------------------------------------------------------------------------------------------


STARTOFRAM equ 0x20 ;First Usable RAM Location
(Bank 0)
ENDOFRAM equ 0x6F ;Last Usable RAM Location
(Bank 0)
FIXEDAREA equ 0x70 ;Start of shared memory
between banks
BANK1RAM equ 0xA0 ;Start of Bank 1 RAM
ENDOFBANK1 equ 0xEF ;End of Bank 1 RAM


cblock STARTOFRAM
;Application specific variables
temp
endc

cblock FIXEDAREA

; Make sure we can always get to these
;
; ISR Register Save Areas
;

INT_FLAGS
W_TEMP
STATUS_TEMP
FSR_TEMP
PCLATH_TEMP

endc

;---------------------------------------------------------------------------------------------
; ENRTY POINTS:
;---------------------------------------------------------------------------------------------
org 0x000
goto Start

;IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
;
; ISR
;
;IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

org 0x004
; Interrupt handler right here
movwf W_TEMP ;Save everything
swapf STATUS, W
movwf STATUS_TEMP
movfw FSR
movwf FSR_TEMP
movfw PCLATH
movwf PCLATH_TEMP

bcf STATUS, RP1
bcf STATUS, RP0 ; Bank 0

btfsc PIR1, TMR1IF ;Is it a TMR1 interrupt
goto Timeout ;Yes

goto IntExit


;-----------------------------------------------------------------------------
;
; ISR routines
;
;-----------------------------------------------------------------------------


;------------------------------------------------------------------------------
Timeout

; toggle LED pin
movfw PORTA
xorlw 0x01
movwf PORTA

; wait for TMR1L to increment so that we only change on TMR1H when the
clock input is high
movfw TMR1L
movwf temp ; Save a copy of TMR1L
WaitForTMR1ToChange:
movfw TMR1L ;
xorwf temp,W ; Has TMR1L changed?
skpnz
goto WaitForTMR1ToChange

; Reload TMR1H for rollovers
; movlw 0xC0 ; Preload for 1/2 second
rollover
movlw 0x80 ; Preload for 1 second
rollover
; movlw 0x00 ; Preload for 2 second
rollover
movwf TMR1H

bcf PIR1, TMR1IF ; Clear interrupt flag

goto IntExit


;-------------------------------------------------------
IntExit
movfw PCLATH_TEMP
movwf PCLATH
movfw FSR_TEMP
movwf FSR
swapf STATUS_TEMP, W
movwf STATUS
swapf W_TEMP, F
swapf W_TEMP, W
retfie

;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;
; Main Level Code
;
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

Start
bcf STATUS, RP1
bsf STATUS, RP0 ; Switch to Bank 1

movlw b'00000000' ; All pins output
movwf TRISA & 0x7F

movlw b'00000000' ; All pins output
movwf TRISB & 0x7F
; movwf WPU & 0x7F

movlw 0x00 ; All pins digital i/o for
now
movwf ANSEL & 0x7F

; movlw b'01110000' ;Set internal osc to 8Mhz
movlw b'01100000' ;Set internal osc to 4Mhz
; movlw b'00000000' ;Set internal osc to
31khz
movwf OSCCON & 0x7F

bcf STATUS, RP1
bcf STATUS, RP0 ; Switch to Bank 0

clrf PORTA ; Drive all pins low
clrf PORTB

call TMR1Init

bsf INTCON, GIE ; Turn on GLOBAL
Interrupts

Main
; sleep ; Your choice, makes no difference
nop
goto Main


;------------------------------------------------------------------------
;
; Timers and Stuff
;
;------------------------------------------------------------------------


TMR1Init
;

; Setup for using external crystal
movlw b'00001110' ; TMR1 oscillator off, and timer
stopped, prescale /1
; |||||||+-------------- TMR1ON (started later)
; ||||||+--------------- TMR1CS (external 32768 crystal)
; |||||+---------------- /T1SYNC (unsynchronized for
sleep)
; ||||+----------------- T1OSCEN (enabled)
; |||+------------------ T1CKPS0
; ||+------------------- T1CKPS1
; |+-------------------- TMR1GE (T1RUN on 16F88)
; +--------------------- T1GINV (Unused on 16F88)

movwf T1CON

; Clear the timer registers
clrf TMR1H
clrf TMR1L

; Clear the interrupt flag
bcf PIR1, TMR1IF

; Enable peripheral interrupts
bsf INTCON, PEIE

; Enable TMR1 interrupts
bcf STATUS, RP1
bsf STATUS, RP0 ; Select Bank 1

bsf PIE1 & 0x07F, TMR1IE ; Turn on TMR1 ints

bcf STATUS, RP1
bcf STATUS, RP0 ; Select Bank 0

; Turn on Timer 1
bsf T1CON, TMR1ON ; Start timer running

return

end

Tony Antoniou

unread,
Mar 9, 2007, 11:36:04 AM3/9/07
to Microcontroller discussion list - Public.
Interesting solution. I've never considered using an interrupt to engage a
subroutine. Particularly with my smaller 12C50x projects, I've always used
the 1:32 prescaler with a 32kHz crystal and checked the change in status of
TMR0,7. It's a pretty short routine with x number of goto routines
proportional to the amount of pseudo-timers I have coded into it to give me
more flexibility than just the one timer. Of course, this concept would
translate to an interrupt-based timer as well.

In the grand scheme of things, I'd say that your solution is more elegant
but for the sake of the argument, I figured I'd throw in my sample solution
as well for all to decide on which would be best for them. It's worked for
me and very accurately at that.


LIST P=12C508A,F=INHX8M
INCLUDE <P12C508A.INC>
__CONFIG _MCLRE_OFF & _CP_OFF & _WDT_ON & _LP_OSC

#DEFINE ALERT GPIO,3
#DEFINE INPUT1 GPIO,2
#DEFINE OUTPUT1 GPIO,1
#DEFINE OUTPUT2 GPIO,0

;General purpose RAM
TIMER1 EQU 07H
TIMER2 EQU 08H
COUNT EQU 09H
WAIT_HIGH_LOW EQU 10H

ORG 00

;First instruction now begins here

INITIALISE
;Set GP0 and GP1 as outputs, all others inputs
CLRF GPIO
MOVLW B'00111100'
TRIS GPIO

;Setup OPTION: No wakeup on pin change, disable weak pull ups (GP0,1,3)
; Fosc/4 clock source, timer0 clock on low - high transition
; Prescaler assigned to TMR0, /32
MOVLW B'01000100'
OPTION

CLRF TMR0
CLRF WAIT_HIGH_LOW
; BCF WAIT_HIGH_LOW,0
BSF WAIT_HIGH_LOW,1 ;Safeguard against power to any detectors
; BCF WAIT_HIGH_LOW,2 ;Flag for when waiting for complete
second (as
;each clock period is 0.5s duration)
; BCF WAIT_HIGH_LOW,3 ;1st sensor release to start timer1
; BCF WAIT_HIGH_LOW,4 ;1st sensor set to engage timer on 1st
;sensor release
BSF TEL_MUTE ;Set TEL_MUTE to No Mute state
BCF D_PWR ;Set D_PWR to Power Off state
MOVLW .05 ;Set up sensor timer
MOVWF TIMER1
MOVLW .10 ;Set up telephone mute timer
MOVWF TIMER2
MOVLW .05 ;Set up sensor switching counter
MOVWF COUNT

;Time monitoring

T_MONITORING
CLRWDT
BTFSC TMR0,7 ;Check for 0.5s rising edge clock pulse,
skip
;next cmd if 0
CALL TIME_CHECK
BTFSC WAIT_HIGH_LOW,2 ;Check if we're waiting for the falling edge

;0.5s pulse
CALL TIME_CHECK


;Alert monitoring

A_MONITORING
CLRWDT
BTFSC ALERT
GOTO ACT_TEL_MUTE
BTFSS TEL_MUTE
GOTO ACT_TEL_MUTE

;INPUT1 monitoring

B_MONITORING
CLRWDT
BTFSS INPUT1
GOTO PWR_DOWN
BTFSC WAIT_HIGH_LOW,0
GOTO PWR_DOWN
BTFSC WAIT_HIGH_LOW,1
GOTO PWR_DOWN
GOTO PWR_UP
GOTO T_MONITORING

TIME_CHECK
CLRWDT
BSF WAIT_HIGH_LOW,2 ;We are now waiting for falling edge

;0.5s pulse
BTFSC TMR0,7 ;Check for other 0.5s falling edge
pulse
RETURN ;No? Go back to where we left off
BCF WAIT_HIGH_LOW,2 ;No longer waiting for falling edge
BTFSC D_PWR ;Don't touch timer if power
supplied
GOTO TIMER2_CHK_SET
BTFSS WAIT_HIGH_LOW,3 ;Don't touch timer if we're still
GOTO TIMER2_CHK_SET ;waiting for 1st sensor release
MOVF TIMER1 ;Check that timer1 > 0
BTFSS STATUS,Z
DECF TIMER1 ;Knock timer1 down by 1 second if >
0

TIMER2_CHK_SET
CLRWDT
BTFSC TEL_MUTE ;Don't touch timer if
TEL_MUTE is not 0
GOTO A_MONITORING
MOVF TIMER2 ;Check that timer2 > 0
BTFSS STATUS,Z
DECF TIMER2 ;Knock timer2 down by 1 second if >
0
BCF WAIT_HIGH_LOW,2 ;No longer waiting for falling edge
;transition
RETURN


Adios,
Tony

--------------- TAMA - The Strongest Name in Drums ---------------

-----Original Message-----
From: piclist...@mit.edu [mailto:piclist...@mit.edu] On Behalf Of
michael brown
Sent: Friday, 9 March 2007 11:14
To: Microcontroller discussion list - Public.

Subject: Re: [PIC] [EE] making a quartz clock

Here's how to do it: [ Note how TMR1H is only modified right after
TMR1L increments, this is important for perfect timekeeping ]

--

Brooke Clarke

unread,
Mar 9, 2007, 1:16:03 PM3/9/07
to pic...@mit.edu
Hi:

Another way to generate clock interrupts is to use Timer2. You set the
prescaler and program PR2 with the 8 bit count value. When the counter
rolls over it sets the interrupt and loads the PR2 value and keeps
going. You may want to have the interrupt more frequently than once per
second to allow doing different things in different parts of the second.

Have Fun,

Brooke Clarke

--
w/Java http://www.PRC68.com
w/o Java http://www.pacificsites.com/~brooke/PRC68COM.shtml
http://www.precisionclock.com

Jinx

unread,
Mar 9, 2007, 4:41:03 PM3/9/07
to Microcontroller discussion list - Public.
Just a thought Rikard - I forget whether you have 4 or 6 digits. A
2Hz IRQ would let you flash the colon to indicate seconds, or 4Hz
and flash the colon 1 in 4 IRQs (looks more like a 'tick')

alan smith

unread,
Mar 9, 2007, 5:08:18 PM3/9/07
to Microcontroller discussion list - Public.
Somewhere in the back of my mind.....I recall reading/hearing/seeing about sending data over DC power lines. One example was for ham rotor controls, to eliminate the multiconductor cables, it just sent a serial data stream over the DC power.

So, this application is to send a 65mS clock over the DC power lines (24VDC). Anyone done this before, or have a link to a circuit?


---------------------------------
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.

peter green

unread,
Mar 9, 2007, 5:15:31 PM3/9/07
to Microcontroller discussion list - Public.

> So, this application is to send a 65mS clock over the DC power
> lines (24VDC). Anyone done this before, or have a link to a circuit?
not done it before but i belive the basic principle would be to modulate it onto a carrier and then couple that carrier to the line.

much easier if you control the other stuff on the line than if you don't.

stef mientki

unread,
Mar 9, 2007, 6:00:48 PM3/9/07
to Microcontroller discussion list - Public.
peter green wrote:
>> So, this application is to send a 65mS clock over the DC power
>> lines (24VDC). Anyone done this before, or have a link to a circuit?
>>
> not done it before but i belive the basic principle would be to modulate it onto a carrier and then couple that carrier to the line.
>
> much easier if you control the other stuff on the line than if you don't.
>
>
>
I did it once to control about 20 trains on 1 rail system.
The trick I used was very simple, but worked great:
put a few diodes in series with the power supply
and put an mosfet across those diodes, now you modulate te DC power.
Maybe you also take a look at Power over Ethernet.

--
cheers,
Stef Mientki
http://pic.flappie.nl

peter green

unread,
Mar 9, 2007, 6:23:44 PM3/9/07
to Microcontroller discussion list - Public.

> -----Original Message-----
> From: piclist...@mit.edu [mailto:piclist...@mit.edu]On Behalf
> Of stef mientki
> Sent: 09 March 2007 23:01
> To: Microcontroller discussion list - Public.

> Subject: Re: [EE] sending a clock over DC power lines
>
>
> peter green wrote:
> >> So, this application is to send a 65mS clock over the DC power
> >> lines (24VDC). Anyone done this before, or have a link to a circuit?
> >>
> > not done it before but i belive the basic principle would be to
> modulate it onto a carrier and then couple that carrier to the line.
> >
> > much easier if you control the other stuff on the line than if
> you don't.
> >
> >
> >
> I did it once to control about 20 trains on 1 rail system.
> The trick I used was very simple, but worked great:
> put a few diodes in series with the power supply
> and put an mosfet across those diodes, now you modulate te DC power.

sounds rather lossy.

the way i'd do it is put an inductor in series with the power and load (remember for noise sensitive loads to put a paralell cap after the inductor)

then couple your signals and and out of the line with capacitors.

Jinx

unread,
Mar 9, 2007, 6:27:49 PM3/9/07
to Microcontroller discussion list - Public.
> So, this application is to send a 65mS clock over the DC power
> lines (24VDC). Anyone done this before, or have a link to a circuit?

Alan, this works well between two PICs. The original circuit was
from a Plessey handbook describing a 100 ohm twisted pair link
between an MV500 IR encoder/transmitter and MV601 receiver/
decoder. I used this at the time for wired 5-bit commands to motor
controllers, as well as wireless IR

Basically the Vcc for each chip is held stable by the electrolytics
and the bit in between carries the data. The receiving pin can be
a comparator or ADC input, or add another transistor to boost
the pulse to logic level. Components and practicality will depend
on what current is passing through the wire to supply the transmitter.
For short clicks though, the transmitter's reservoir cap should cope

dc-wire-data.gif

Herbert Graf

unread,
Mar 9, 2007, 9:04:42 PM3/9/07
to Microcontroller discussion list - Public.
On Fri, 2007-03-09 at 14:08 -0800, alan smith wrote:
> Somewhere in the back of my mind.....I recall reading/hearing/seeing about sending data over DC power lines. One example was for ham rotor controls, to eliminate the multiconductor cables, it just sent a serial data stream over the DC power.
>
> So, this application is to send a 65mS clock over the DC power lines (24VDC). Anyone done this before, or have a link to a circuit?

Haven't done it, but have a look at DCC control used in model
railroading (i.e. http://www.dccwiki.com/Main_Page), might give you some
ideas. TTYL

Vasile Surducan

unread,
Mar 10, 2007, 2:41:37 AM3/10/07
to Microcontroller discussion list - Public.
On 3/9/07, stef mientki <s.mi...@mailbox.kun.nl> wrote:
> peter green wrote:
> >> So, this application is to send a 65mS clock over the DC power
> >> lines (24VDC). Anyone done this before, or have a link to a circuit?
> >>
> > not done it before but i belive the basic principle would be to modulate it onto a carrier and then couple that carrier to the line.
> >
> > much easier if you control the other stuff on the line than if you don't.
> >
> >
> >
> I did it once to control about 20 trains on 1 rail system.
> The trick I used was very simple, but worked great:
> put a few diodes in series with the power supply
> and put an mosfet across those diodes, now you modulate te DC power.
> Maybe you also take a look at Power over Ethernet.

Another way will be to supply the DC from a constant current generator
(high output impedance) and modulate either the current at the input
of the current generator or modulating directly in the line using a
series capacitor.
Depends how clean must be the 24V line at the load side, that should
be the starting point.

Vasile Surducan

unread,
Mar 10, 2007, 5:21:54 AM3/10/07
to Microcontroller discussion list - Public.
On 2/23/07, Rikard Bosnjakovic <rikard.bo...@gmail.com> wrote

> but what I cannot understand is why a 32768Hz-crystal is magical
> enough to use for clock-applications.

32768 is not magical enough. There are a few crystals really magical:

14.7456MHz or 7.3728MHz because it could be divided exactly to 10mS
and offers also zero error for USART at 115200bps.

Vasile

Rich

unread,
Mar 10, 2007, 7:34:39 AM3/10/07
to Microcontroller discussion list - Public.
It could be sent over an AC or a DC line. Many intercoms send voice over
the AC lines. The thing you might want think about is capacitive coupling
your pulse to the line on transmit and also receive end.

----- Original Message -----
From: "Vasile Surducan" <picl...@gmail.com>
To: "Microcontroller discussion list - Public." <pic...@mit.edu>
Sent: Saturday, March 10, 2007 2:41 AM
Subject: Re: [EE] sending a clock over DC power lines

Reply all
Reply to author
Forward
0 new messages