How do I modify my PIC microcontroller program to deliver 150 hz also with the existing 1hz output

MrChips

Joined Oct 2, 2009
35,022
I have this clock/stop watch which is NIST traceable.
The last time I think I set it was two years ago. It is now ahead by 20 seconds.
That works out to an error of about 0.3 ppm

Stop Watch1.jpg

Stop Watch2.jpg
 

Futurist

Joined Apr 8, 2025
933
@Arjune There are radio based clocks synchronized to US atomic clock sources, 25 bucks at wallmart. If one wants a battery powered time reference this is the way to do it.

1768925023180.png
 
Last edited:

LesJones

Joined Jan 8, 2017
4,522
I think that your expectations are unrealistic to get about 0.2 PPM accuracy using an unknown 32768 hz crystal with no manufacturers specification and not even trimming the crystal frequency. It would have saved a lot of time if you had told us at the start of the required accuracy.
I have a modified version on the code for a PIC16F628 that uses the 32768 Hz oscillator on timer1 oscillator and a 4Mhz crystal for the CPU clock. The 32768 hz crystal is the source for the 1Hz output the 150 ish hz is derived from the 4 Mhz crystal just counting 1 Mhz instruction cycles. This is no help as I don't think you will get a 32768 hz crystal to meet your accuracy requirment.
Les.
 

drjohsmith

Joined Dec 13, 2021
1,630
I think that your expectations are unrealistic to get about 0.2 PPM accuracy using an unknown 32768 hz crystal with no manufacturers specification and not even trimming the crystal frequency. It would have saved a lot of time if you had told us at the start of the required accuracy.
I have a modified version on the code for a PIC16F628 that uses the 32768 Hz oscillator on timer1 oscillator and a 4Mhz crystal for the CPU clock. The 32768 hz crystal is the source for the 1Hz output the 150 ish hz is derived from the 4 Mhz crystal just counting 1 Mhz instruction cycles. This is no help as I don't think you will get a 32768 hz crystal to meet your accuracy requirment.
Les.
Even if you trimmed the setup , I doubt it would stay trimmed I've voltage , temperature and time.
My car clock is good , but my cheap watch on my wrist is better , by my wrist is at a fairly constant temperature .
@Arjune just what do you require ?
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
Unfortunately this world is fixed physically with knowledge. I think you all are ignoring the spirit and not allowing it to make a decision in your lives for you. My clairvoyance indicates dishonesty. The spirit is part of my life and allows its adjustments in the circuitry to make favorable corrections and compensations for accuracy according to ones feelings. The conspiracy hides the spirit from reality This is not all about science and reasoning--electronics is about human nature also. I don't expect any of you to identify this phenomenon. I'm being asked questions without a full deck of cards. I am left with only my schizophrenia to explain this and this is not viable in this world
 

Ya’akov

Joined Jan 27, 2019
10,278
@Arjune, I haven't read through this entire thread but generally speaking when seeking to create a highly accurate clock for a reasonable price the usual approach is to discipline the clock with an external standard.

Any practical clock that is allowed to free run will drift for a number of reasons what are increasingly expensive to eliminate. There are two things that I would try here. The simplest and least costly would be a crystal oven. Quartz crystals are sensitive to temperature and the ambient temperature of an ordinary dwelling will easily drift enough to make the accuracy you seek impossible.

A TCXO (Temperature Controlled Crystal Oscillator) like the 16MHz version here with a rating of 0.1ppm might work for your application.

Alternatively, a GPS module with a 1pps output (for example, this one) could be used to step the clock. If you don't need sub-second timing this might be sufficient. If you do, the 1pps could discipline a local clock that would only need to run for one second so the drift would have no real effect on overall time keeping.

This information may be redundant and if it is, please accept my apology. I have no idea if this arrangement would be vulnerable—according to your understanding—to the bad intentions that could, or does(?) affect the current arrangement, but perhaps by taking control of one of the parameters of drift (temperature) or isolating the source of timing in space as in the GPS case, this would give you more confidence the clock is free of malicious influence.
 

LesJones

Joined Jan 8, 2017
4,522
I think Ya’akov's link to the TCXO will solve your problem. They are also available for 32768 hz. It also meets your requirement to not have to calibrate the oscillator against an external standard. I did not know about the existance of these devices.
Les.
 

Futurist

Joined Apr 8, 2025
933
Unfortunately this world is fixed physically with knowledge. I think you all are ignoring the spirit and not allowing it to make a decision in your lives for you. My clairvoyance indicates dishonesty. The spirit is part of my life and allows its adjustments in the circuitry to make favorable corrections and compensations for accuracy according to ones feelings. The conspiracy hides the spirit from reality This is not all about science and reasoning--electronics is about human nature also. I don't expect any of you to identify this phenomenon. I'm being asked questions without a full deck of cards. I am left with only my schizophrenia to explain this and this is not viable in this world
FYI, nobody has a full deck of cards, do not assume you are the only person here with troubles.
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
@Arjune, I haven't read through this entire thread but generally speaking when seeking to create a highly accurate clock for a reasonable price the usual approach is to discipline the clock with an external standard.

Any practical clock that is allowed to free run will drift for a number of reasons what are increasingly expensive to eliminate. There are two things that I would try here. The simplest and least costly would be a crystal oven. Quartz crystals are sensitive to temperature and the ambient temperature of an ordinary dwelling will easily drift enough to make the accuracy you seek impossible.

A TCXO (Temperature Controlled Crystal Oscillator) like the 16MHz version here with a rating of 0.1ppm might work for your application.

Alternatively, a GPS module with a 1pps output (for example, this one) could be used to step the clock. If you don't need sub-second timing this might be sufficient. If you do, the 1pps could discipline a local clock that would only need to run for one second so the drift would have no real effect on overall time keeping.

This information may be redundant and if it is, please accept my apology. I have no idea if this arrangement would be vulnerable—according to your understanding—to the bad intentions that could, or does(?) affect the current arrangement, but perhaps by taking control of one of the parameters of drift (temperature) or isolating the source of timing in space as in the GPS case, this would give you more confidence the clock is free of malicious influence.
How about some kind of module that produces 1hz; do you know one and where I can get it? Ya' Akov, low cost ($10 or less). Otherwise I might just keep using the wall clock timing unit that uses the AA battery and apply the associated circuitry which is simple. Anyhow when I get a chance I'll try the trimmer capacitor on the 32 khz Crystal with the same pic microcontroller. By the way, would decreasing the values of the loading capacitors of the crystal increase the frequency or lower the frequency 0f oscillation?
 
My go-to source of cheap 1PPS (one pulse per second) is hobbyist GPS modules. I started buying these ten years ago, when the price dropped below $30; today they are under $10. Power it up near a window or whatever, and the 1PPS pin and its LED get going in under a minute.

Search "GPS Arduino" on ebay (that's where I get them). A quick check shows them on amazon as well. Probably plenty of other places too. Anyway, sort by price, and they are there at the beginning, around 10 bucks.

VERY IMPORTANT: They come in 4- and 5-pin versions. Get the 5-pin version. That fifth pin is the 1PPS. Also, you want the large antenna. Antennas come either with a small cable or soldered directly. I personally prefer the latter, but I've bought and been happy with plenty of both kinds. The image below from an ebay seller named "worldchips" is a quick example of what to look for; five pins and the integrated full-size antenna.

View attachment 362532
[MODS: I do not know your policy regarding others' images. I did not create a hyperlink to https://www.ebay.com/itm/225032313351 because it will (may?) go away when the seller stops selling this product. Please edit my post as needed in order to conform to policy. thanks.]
 

Ya’akov

Joined Jan 27, 2019
10,278
How about some kind of module that produces 1hz; do you know one and where I can get it? Ya' Akov, low cost ($10 or less). Otherwise I might just keep using the wall clock timing unit that uses the AA battery and apply the associated circuitry which is simple. Anyhow when I get a chance I'll try the trimmer capacitor on the 32 khz Crystal with the same pic microcontroller. By the way, would decreasing the values of the loading capacitors of the crystal increase the frequency or lower the frequency 0f oscillation?
Aside from the GPS module I mentioned above, no. Unfortunately any module that produces any timing is subject to the same problems of drift in its timebase no matter what its pulse rate. If you need an independently stable oscillator with very high accuracy you can adapt a surplus Rubidium-based 10MHz frequency standard.

This is much more expensive than using a GPS solution to discipline a clock with a much less accurate local timebase, but it is not dependent on anything external to keep time once you set it.
 

Ya’akov

Joined Jan 27, 2019
10,278
My go-to source of cheap 1PPS (one pulse per second) is hobbyist GPS modules.
This was mentioned in my answer to which he is replying.
Alternatively, a GPS module with a 1pps output (for example, this one) could be used to step the clock. If you don't need sub-second timing this might be sufficient. If you do, the 1pps could discipline a local clock that would only need to run for one second so the drift would have no real effect on overall time keeping.
 

drjohsmith

Joined Dec 13, 2021
1,630
How about some kind of module that produces 1hz; do you know one and where I can get it? Ya' Akov, low cost ($10 or less). Otherwise I might just keep using the wall clock timing unit that uses the AA battery and apply the associated circuitry which is simple. Anyhow when I get a chance I'll try the trimmer capacitor on the 32 khz Crystal with the same pic microcontroller. By the way, would decreasing the values of the loading capacitors of the crystal increase the frequency or lower the frequency 0f oscillation?
The fundamental question your asking is about stability and accuracy.
Any timming circuit will have numberes for this .
Things like mains power around a lot of the world , the average frequency is traceable back to a national time standard ,
Things like gps are also tracable back to a national time standard
The time standards are traceable back to the fundamental oscilation frequency of a gas . I think it's Cesium gas .
The earth notabley is less accurate than the modern standards , which is why we add leep seconds at various times .
Things like mobile phones , the transmitters are all locked and traceable back to gps timming.
Some countries have transmitted time standards , such as the UK msf, Europe one fro. Frankfurt , USA one on each coast .

It all depends what accuracy and stability you require @Arjune
 
Last edited:

LesJones

Joined Jan 8, 2017
4,522
I don't think the wall clock will meet your requirement of 0.2 PPM. A radio controlled clock will meet your requirements.
Reducing the capacity will increase the frequency.
How accurate do require the 150 hz clock to be ?
Les.
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
I don't think the wall clock will meet your requirement of 0.2 PPM. A radio controlled clock will meet your requirements.
Reducing the capacity will increase the frequency.
How accurate do require the 150 hz clock to be ?
Les.
140-160HZ for the 150hz to set the clock. With 10pf instead of 22pf caps on the 32khz crystal, the frequency was higher because seconds counted faster-about twice as fast--about 4 seconds in 8 hours. Unfortunately a device ID error was produced when I tried to program the pic 16f628A again. Is there a way to fix this error instead of using a new chip? I am confident that all connections on the breadboard are OK and the connections from the pickit3 to the breadboard are OK. I couldn't fit the pins of my trimmer caps on the breadboard--I think the trimmer caps are 30pf. Maybe I'll put the 22pf caps in parallel with the 10pf caps to slow down the count of the crystal. I am using my cellphone for reference time. I think the intelligence is switching because I believe at one time the cellphone is ahead and a moment later the clock is ahead that is driven by the pic that I am testing for accuracy.
 
Last edited:

drjohsmith

Joined Dec 13, 2021
1,630
140-160HZ for the 150hz to set the clock. With 10pf instead of 22pf caps on the 32khz crystal, the frequency was higher because seconds counted faster-about twice as fast--about 4 seconds in 8 hours. Unfortunately a device ID error was produced when I tried to program the pic 16f628A again. Is there a way to fix this error instead of using a new chip? I am confident that all connections on the breadboard are OK and the connections from the pickit3 to the breadboard are OK. I couldn't fit the pins of my trimmer caps on the breadboard--I think the trimmer caps are 30pf. Maybe I'll put the 22pf caps in parallel with the 10pf caps to slow down the count of the crystal. I am using my cellphone for reference time. I think the intelligence is switching because I believe at one time the cellphone is ahead and a moment later the clock is ahead that is driven by the pic that I am testing for accuracy.
using your phone is a good idea .
assuming its logged into a network, its using the network time, so long term should be accurate . interesting, ive never looked at two or three phones together, see if they are all same ,

be aware, you say your using bread board , did not remember that, sorry.
can you send picture of the clock circuit please .
22pf , is the sort of number you need ,
but these bread boards, though great for general circuits, have high inductance and capacitance , if you also have long wires , that could be "swamping" the capacitances your changing.

for referance, look up some app notes on pcb layout for these pic oscilators ,

on bread board, its easiest and safer to use an oscilator chip as opposed to a cystal / resonator .
 

LesJones

Joined Jan 8, 2017
4,522
I do not think that you have some faulty PICs. (I have never had a fauily pic even after pluging them into the programming socket the wrong way round.) If the test you are referring to in post #116 is using the 32768 hz crystal in pins 12 and 13 then the device ID error may be due to a difference betwen the PIC16F627/8 and the A versions. did you get the error when compiling the code or when programming the .HEX code into the PIC.
When using 15 pf capacitors with my 32768 hz I found that it's frquency was jumping 32768 hz to about 32800 hz from time to time What software are you using to compile the code and to program the PIC ?
Les.
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
using your phone is a good idea .
assuming its logged into a network, its using the network time, so long term should be accurate . interesting, ive never looked at two or three phones together, see if they are all same ,

be aware, you say your using bread board , did not remember that, sorry.
can you send picture of the clock circuit please .
22pf , is the sort of number you need ,
but these bread boards, though great for general circuits, have high inductance and capacitance , if you also have long wires , that could be "swamping" the capacitances your changing.

for referance, look up some app notes on pcb layout for these pic oscilators ,

on bread board, its easiest and safer to use an oscilator chip as opposed to a cystal / resonator .
In about 12 hours (I'm guessing) the pic circuit is about 1 second fast with 32pf (10pf in parallel with 22pf) loading capacitors on the 32khz crystal.
pic clock picture.jpg
 

Thread Starter

Arjune

Joined Jan 6, 2018
354
I do not think that you have some faulty PICs. (I have never had a fauily pic even after pluging them into the programming socket the wrong way round.) If the test you are referring to in post #116 is using the 32768 hz crystal in pins 12 and 13 then the device ID error may be due to a difference betwen the PIC16F627/8 and the A versions. did you get the error when compiling the code or when programming the .HEX code into the PIC.
When using 15 pf capacitors with my 32768 hz I found that it's frquency was jumping 32768 hz to about 32800 hz from time to time What software are you using to compile the code and to program the PIC ?
Les.
I am using mplabx ide and ipe v.6.05. I get the error when programming the hex code into the pic. Maybe I should say that I am using the pic16f628 instead on the pic16f628A in the IDE and IPE and there may not be a device ID error although I am using the A version?
 
Top