Page 1 of 1
Digital Clock
Posted: Wed Apr 04, 2012 2:36 pm
by ben
Hello everyone,
I have recently been trying to get a very simple flowcode working! Benj kindly provided me with a file enclosed, but although the simulation works fine, I can't actualy get it working on the hardware. The clock ticks fine, but the external triggers have no affect. Also the timing seems to lag the actual time. I'll be very grateful for any advice. Many thanks in advance.
Re: Digital Clock
Posted: Wed Apr 04, 2012 4:02 pm
by Benj
Hello,
I have just tested the file here without any changes and it's working great on the hardware. By default the clock will not be running so I wonder about the setup of your switches. Have you used a pull up and pull down method like we use for the EB007 board? You can refer to the schematic at the end of the datasheet to check if your switches are wired correctly. Maybe also worth checking with a multimeter if you have one handy so you can see the voltage being applied to the input pins.
I have also noticed that in the device config settings you have the watchdog timer enabled. I would suggest you disable this. All the other settings look fine unless you need the external MCLR reset functionality.
Regarding the timing what speed crystal are you using? 20MHz as defined in the project options will not give you a very accurate clock. If you change to say a 19.6608MHz crystal then you can divide the clocks down to get a bang on count of 75 ticks per second. This will then have a max error of around 2 seconds a day using a 20PPM crystal.
Re: Digital Clock
Posted: Thu Apr 05, 2012 9:24 am
by ben
Hi Benj,
Once again thank you very much for your assitance. The circuit is working prefectly now and the problem was my switch setting !!

Re: Digital Clock
Posted: Thu Apr 05, 2012 9:33 am
by medelec35
Hi
Looking at flowchart you have posted, I can see there could be an issue regarding accuracy?
If you look at the properties of timer0 interrupt, at 20MHz the interrupt frequency is 152.588Hz
So instead of setting SECOND_COUNTER >= 153 on your Flowchart is is set for >=150
So Clock is going to run a bit too fast.
If it was me and I was not using built in PWM then easiest way would be to use Timer2 instead.
Note: Timer2 only simulates with Flowcode V5. So for simulation testing purposes use a call INTERRUPT_TMR2
(I have changed the name in the Flowchart I have modified)
If you look at timer2 properties you will see I have set the following values:
Prescaler Rate: 1:16
Postcaler Rate 1:16
Rollover Value 217
This gave an interrupt frequency of 90.006Hz which will be more accurate than 152.588Hz
To find the best Rollover Value I went to
http://eng-serve.com/pic/pic_timer.html
Change Oscillator frequency to 20000000 and moved sliders to the right to give the lowest interrupt frequency possible.
Then I moved the lowest slider (PR2 register) until interrupt is rounded to the nearest whole value.
If I had more time I may have been able to get a better interrupt frequency, so I’m not stating the chosen one is the optimum value.
If you would like to use timer0 instead, then you may get a more accurate time by placing
Some code at the beginning of timer interrupt to preload timer0 with a different value other than 0 (e.g tmr0=0x20; )so rollover occurs more quickly. With trial and error or using a scope you can get interrupt frequency to be more accurate.
See:
http://www.matrixmultimedia.com/mmforum ... =26&t=8660
Hope this helps,
Martin
Re: Digital Clock
Posted: Thu Apr 05, 2012 11:12 am
by ben
Hi Martin,
Thank you for your input, very informative. I'll try your suggestion in the very near future.
Re: Digital Clock
Posted: Sun Apr 22, 2012 2:55 pm
by Carlos Viveiros
I normally use a 4 Mhz (4000000 Hz) Crystal
Here is an example of a very acurate time for digital clock. In this case, 50 Hz.

Re: Digital Clock
Posted: Sun Apr 22, 2012 4:06 pm
by medelec35
You can't get much more accurate than that!

Re: Digital Clock
Posted: Sun Apr 22, 2012 8:08 pm
by Carlos Viveiros
Yes, i guess not ...
