is this fcf efficient in keeping time

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

is this fcf efficient in keeping time

Post by brandonb »

what this project does is starts a generator by sending a signal to another controller for the duration of engine run, once a week on tuesday 10-11am will run engine for one hour... then if engine is serviced there is an engine 5 minute run mode which is enabled by holding switch #3 for 3 seconds to initiate 5 min mode....at any time it can be stopped by pressing switch #4..... i added the newest file (1/1/12) in attachements.... the thing i was wondering is can you guys look at the flowcode and see if it will be displaying the clock time correctly a year from now..... and in my calculations i set prescalers to 75hz then made decsison on tick--> if tick=75= tick=0 second=second+1 if second=60 second =0..... is there a difference in time keeping if i did it --> if tick=75 tick=tick-75 second=second+1 if second =60 second-60? if so whats the reasoning behind this......thanks Image
Attachments
WEEKLY PRODUCTION MODEL 19.66Mhz XTAL.fcf
(57.25 KiB) Downloaded 316 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: is this fcf efficient in keeping time

Post by medelec35 »

Hi Brandon,
Here are my thoughts:
If I want the time to be accurate and I use a very accurate crystal, then I would only use 1 timer interrupt not 2 or
more interrupts.
This is because while an interrupt macro (E.g tm1) being accessed then although timers are still running in the background,
all interrupts are disabled until tm1 macro is exited.
This could mean a tm0 tick is missed. I know that don't sound much, but could add up over time.
brandonb wrote:in my calculations i set prescalers to 75hz then made decsison on tick--> if tick=75= tick=0

second=second+1 if second=60 second =0..... is there a difference in time keeping if i did it --> if tick=75

tick=tick-75 second=second+1 if second =60 second-60? if so whats the reasoning behind this......thanks
There is no difference except the calculations use more processing power and RAM(that's just my therory but you can
compile with both methods and look at ram usage) , so thats why I would stick to if tick=75= tick=0 second=second+1.
So long as you know that if timer is set for 75Hz then interrupt is accessed 75times every second or once every 1/75

= 13.33ms

As for displaying the time. It would be a good idea to show 0 if less than 10 minutes, hours or seconds.
You can do this like:
Show Preceding 0.png
(16.73 KiB) Downloaded 5679 times
If you look at :
http://www.matrixmultimedia.com/mmforum ... 98&#p24998

I have also did flwochart so the semicolon toggles on an off every second, so you can see all is running.

I will look at your flrowchart in a bit more detail when I get the chance

If you want to have correct time all year round what about adjusting for daylight savings? Then it get more complex since adjustment for leap year would be required.

Martin
Martin

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: is this fcf efficient in keeping time

Post by brandonb »

then I would only use 1 timer interrupt not 2 or
more interrupts.
This is because while an interrupt macro (E.g tm1) being accessed then although timers are still running in the background,
all interrupts are disabled until tm1 macro is exited.
This could mean a tm0 tick is missed. I know that don't sound much, but could add up over time.
this is the kind of stuff i've been wondering about... i noticed that when i put all three timer interrupts at the top of the program after the osccon block then the program corrupts, i have to move the tmr1 to another place in the program to be able to run all three at once.... lately when i use internal osc and i have a couple timer interrupts using the 690 chips when i activate a exteranal mclr clear it corrupts on restart or if i use connection points to restart the program it does the same thing... now if i use an external crystal with the same set up mclr and jump points to beginning of program work correctly(whats up with that?) can you explain to me exactly how the timer0-1-2 interrupts work, like how they effect the main program... for example using a external interrupt on pin when the interrupt is triggered it leaves the main program to execute the interrupt macro.... on timer interrupts does it leave the loop everytime it ticks? what is the priority of all kinds of interrupts? it seems tmr1 for somereason has top priority, then timer 0 then timer 2... interrupt on pin and last port, that is a guess, i would like to understand this as its been bugging me for the last month and a half how all the interrupts effect the loop and the priority, can i get this information by looking at the datasheet, if so could you point me to that so i know what im looking for in future, since you showed me interrupts i've been using them like crazy in all programs...........thanks for clearing up the tick=60 tick=0 part... stupid question about this but if the tmr0 rolls over at 75 why do i put a if tick=75 tick=0 in there, becuase it should already be done, i modify the tick sometimes to count in rough values to displays and such by reseting the tick before its close to be reset essentually speeding up the frequency of the tmr interrupt for a shorter timer period, i could see how tick=0 is useful hear but not if it is already set for 75 ticks..........about the fcf file, john wanted it to only count 7 days and reset so i wont need to worry about leap years and month(good thing what a releif that was).... i'll have to go back through the fcf and figure out how to make the numbers not blink but the dots allowed to blink..... thanks for checking out the fcf......

Gary Freegard
Posts: 45
Joined: Mon Nov 07, 2011 6:36 pm
Has thanked: 1 time
Been thanked: 30 times
Contact:

Re: is this fcf efficient in keeping time

Post by Gary Freegard »

Hi Brandon

I agree with Martin about multiple interrupts, plus you should keep the interrupts workload to a minimum, though I havent always adhered this this, but can cause timing problems for the reason that Martin pointed out i.e. the interrupts are disabled on entry to interrupt routine and are only enabled on exit.

The attached program does the following.
  • Main
    • Initialise all variables
      Initialise one timer at 75Hz (timer 0)
      Indefinite loop
      Call updatetime macro
      Call updatedisplay macro
    timer0 interrupt
    • Increment variable count by 1
    updatetime macro
    • This will increment and check the time variables.
      It also has a check for the state of a variable called calibrate against variable target, these are both integers. calibrate is incremented every second in this example, but you could change that to minutes or hours. This check would then be used to change the value of seconds and/or count.
      The reason that you would change count is so that you could compensate for sub second inaccuracies over the time period you have chosen (13.33ms). So to add 133ms onto the time you would start count at 245 ( 255-10), to reduce by the same amount you would set count to 10 (0+10).
      You could have multiple calibration points i.e. one for seconds, one for minutes , one for hours depending on how accurate your crystal is.
    updatedisplay
    • Only call the display macro when updatedisplay variable is 1, i.e. every second.
      I haven’t included any LCD display macros
Just a bit of information for you, accuracy required to be within an hour after a year , 0.0114%
for a minute is 0.00019% and for a second is 0.00000317%. I have been using a 877a and ECIO40 as millisecond timers and without the use of variable capacitors, I have been able to get an accuracy of 0.017% by changing the preset value of the timer on nearly every interrupt.

Gary
Attachments
timer_example.fcf
(8 KiB) Downloaded 317 times

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: is this fcf efficient in keeping time

Post by jgu1 »

Hi Gary! :D

I understand it is a kind of software adjustment or calibration if the crystal which I used is not accurate.
I've even tried making a clock and a timing using internal interrupt and where there is great inaccuracy.
It's a nice long explanation you give, but I can not really see where you initial adjustment. (I'm a little heavy and slow)
Can I get you to elaborate a little more about where the adjust is going + or -. :?:
Thanks in advance.

Best regard


jgu1

Gary Freegard
Posts: 45
Joined: Mon Nov 07, 2011 6:36 pm
Has thanked: 1 time
Been thanked: 30 times
Contact:

Re: is this fcf efficient in keeping time

Post by Gary Freegard »

Hi jgu1
Sorry forgot to mention that in my example in the UpdateTime macro there is decision check to see if calibrate = target, if this is true then the seconds = new seconds value and count = new count value. And just notice that I didn’t reset calibrate.

Found another mistake and had to alter the calibrate routine so have attached a correct program, I hope :D . This program will calibrate at the first seconds = 30 after the target value, so obviously if you have a display showing seconds you may notice a change depending on what method you use

I used seconds = seconds etc as I didn’t want strange things to happen if someone tried using the program, see examples

If the target value is greater than 32768 then ‘calibrate = calibrate + 1’ must be moved to after the next decision i.e calibration point is 170 hours, 170 * 3600 = 612000 seconds too large, so move the calibrate increment to after the minutes decision the new target will be 170*60 = 10200 minutes.


Now some examples
  • After 9 hours (9 * 3600 = 32400sec) your time is out by +1.078 sec . You would have seconds = 29 and count = 6 . Its 6 because 78/13.33 = 6 approx.
    If its out by -1.280 then seconds = 31 and count = 234 (0.280/0.013 = 21, 255-21=234)

    After 340 hours the time is out by +2.133 sec. The calibrate increment would need to be moved to after the minute decision. The target value would be 340*60 = 20400, seconds = 28 and count = 10.
    If the time is out by -2.300, seconds = 32 and count = 232 (255-23, 23= 0.300/0.013)

    Another way to do it based on the first example.
    After 9 hours (9 * 3600 = 32400sec) your time is out by +1.078 sec. Each second is out by +0.000033271 sec, after 10000sec the error is 0.33271 to compensate set count = 25 (25 = 0.33271/0.01333) and target value = 10000 (seconds).
Even after using the above methods you still have errors, then I would use another calibration point (calibrate2 and target2) i.e. use the last method for first level and then find the next point where the remaining error is sub 1 sec over a longer period of time (mins, hours, days) and then adjust count for this. So in the last example the remaining error is -0.00054 sec (0.33271-(25*0.01333)) every 10000sec, the next point would be at 10000000 sec where the error should be -0.54sec , the target2 value would have to be 2777 (hours) and count = 40 (0.54/0.0133).

Hope this is helpful.

Gary
Attachments
timer_example.fcf
(8.5 KiB) Downloaded 315 times

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: is this fcf efficient in keeping time

Post by Steve001 »

Hi Brandon,

I have made a project that uses a clock to test things, I am using a Xtal of 2.4576 MHz
So far it's not too bad at keeping time :D

but then again clock accuracy is not that much of a issue in my project

have a look here

http://users.tkk.fi/~jalapaav/Electroni ... .html#xtal

and here found this one today

http://www.microchip.com/stellent/idcpl ... e=en546206

Steve
Last edited by Steve001 on Mon Feb 06, 2012 1:58 pm, edited 2 times in total.
Success always occurs in private and failure in full view.

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: is this fcf efficient in keeping time

Post by brandonb »

thanks guys for the responses, all gonna be very helpfull, although i havent had time to go over them yet with out outside distractions, i did hear from john today he said it seems to be off 2 seconds in 24 hours, thats about what i was guessing for the losses, i could correct this with calculations before weekly engine start occurs but, we still have to test the interaction of timer 1 and timer 2 occuring in the same period of time, may end up omiting timer 1 as were many comments stating possible issues happening using this, would then convert it back to switch combinations to activate 5 minute sevice start instead of holding a button down which allow timer1 operation, will check out posts in detail this weekend

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: is this fcf efficient in keeping time

Post by brandonb »

this may be a stupid question but when i buy a crystal from digikey and it gives me a LOAD CAPACITANCE = 20PF ....does that mean that i should use 2 of the 20uf or 2 of the 10uf to make combined total of 20uf, and would this effect accuracy if i set this part up incorrectly? here is supposively the best crystal for 19.66Mhz that digikey offers, what do you guys think http://search.digikey.com/us/en/product ... -ND/827599 i used 2 of the 20pf

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: is this fcf efficient in keeping time

Post by Benj »

Hello,

This Microchip app note details crystals and things like load capacitance.
http://ww1.microchip.com/downloads/en/A ... 00826a.pdf

I have run into problems before with smaller SMD type crystals due to load capacitance so it is something to watch out for.

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: is this fcf efficient in keeping time

Post by brandonb »

ben that document is hard to read, could you summerize it? to note the load capacitance of a 20.48 is 20pf, i gave it 2 of the 20pf and it ran great, when i put 2 of the 10pf caps on it didnt allow the crystal to work, what does that mean? on the other side of the spectrum if i dont put any caps on it at all it appears to work fine on observation only

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: is this fcf efficient in keeping time

Post by Benj »

Hello Brandon,

I think load capacitance is different from the crystals inherent shunt capacitance. For load capacitance i.e. the caps from each side of the crystal down to ground I always use 22pF ceramic caps as these seem to work well. Regarding using no caps at all, this may work for some situations but is probably not very reliable when exposed to varying humidity or temperature.

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: is this fcf efficient in keeping time

Post by brandonb »

thanks ben, i guess in this case it depends on the load of the crystal in comparison to how the pic is driving it, which is different what they consider a normal load is

Post Reply