24 hrs Clock

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

24 hrs Clock

Post by Ondra »

Hi All. I would like to know how, if it's posible to make a 24 hr clock using flowcode only. I have a slight understanding when reading C but can't as yet write in C.

Ondra

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Yes, it is quite easy in Flowcode.

Use the Timer0 interrupt set to say 75Hz. In the interrupt routine, increment a variable. If this variable reaches 75 (or whatever the interrupt frequemcy is), then you know that 1 second has elapsed and you will be able to increment the variables that represent the seconds, minutes and hours.

In your main routine, simply display the clock reading on an LCD or 7-seg display.

Remember, try to keep your interrupt routine as simple (and quick) as possible.

Xsusakamei
Posts: 12
Joined: Sat Sep 15, 2007 11:40 pm
Contact:

Clock runs to fast ...

Post by Xsusakamei »

Helllo!

Yes, so i have done it. I increase 3 Variables (h,m,s) an put it on an LCD. This works fine, but after a few hours the Clock runs 5 Seconds (and much more!!!) to fast! Thats not good. So i can't use Flowcode for my Projekt. Whats wrong?

I bought Flowcode 2 Weeks ago and i am programmed befor in Assembly. Here i can load the 16 Bit Timer Register of Timer 0 so i want. So i can use anything XTAL. In Flowcode i did not find this Function :(. The divider of Timer 0 is set fixed in Flowcode :-( Please tell me the calculation of the Timer 0 IRQ frequency.

Can you help me?

If you are interested in source, i can send you the Flowcode File. I use the PIC 16F877A with the Multiprogrammer of Matrix Multimedia and PPP.

Thanks!

---------------------
Xsusakamei

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:

Post by Benj »

Hello

You could maybe try and see if you can get a more accurate crystal to use in your application. If not then you can use a C code icon at the top of your interrupt routine that does the following

Code: Select all

tmr0 = 250;
255 would be the normal amount of time that the tmr0 takes to interrupt. However it will take a few program cycles to execute the c code block so it is best to reduce this value slightly to compensate for the execution time and the innacuracy you are seeing.

Xsusakamei
Posts: 12
Joined: Sat Sep 15, 2007 11:40 pm
Contact:

Post by Xsusakamei »

Hello!

Thanks for the answer, but with this C-Code the Clock runs super fast! The Minutes runs so fast like the Seconds ... .

You mean the 19.660800 MHz Crystal is inaccurate? I don't think so, because the frequency would be divide and the Error(s) also! 5 Seconds (and a lot more proportionally to the runtime) is not an error of the Crystal!

I think, the reason of this problem is the position of reload of the tmr0 Registers. At this moment Flocode reload the registers at the end of the IRQ. Thats for many Applications ok, but bad for time critical Applications. The time to run the IRQ routine must be added to the IRQ-Time, so this would be inexact.

In the Future the IRQ-Properties Dialog the user must be determine if Flocode reload the timer-registers at the beginning or at the end of the IRQ-Routine.

If the user use the reload at the beginning of the IRQ, he must watch the IRQ Length! If this would be too long, the routine runs not to the end and it will start always new again.

Also in the Futur, the user must be able to set the Value of the tmr0 Registers, so in example "normaly" Crystals can be used.

Do you share this statement?

By the way, can you explain me the creating of an User-IRQ by example of tmr0? Perhaps, we can minimize so the drift of the clock yet?


Greetings

----------------------
Xsusakamei

(sorry about my bad english ....)

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Xsusakamei wrote:I think, the reason of this problem is the position of reload of the tmr0 Registers. At this moment Flocode reload the registers at the end of the IRQ. Thats for many Applications ok, but bad for time critical Applications. The time to run the IRQ routine must be added to the IRQ-Time, so this would be inexact.
I don't think Flowcode reloads the timer registers. All it does is reset the interrupt flag (which should not affect the timer registers).
Xsusakamei wrote:Also in the Futur, the user must be able to set the Value of the tmr0 Registers, so in example "normaly" Crystals can be used.
This is something I want to implement in v4 of Flowcode. It would allow us to give a much better range of values for the timer interrupt.

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:

Post by Benj »

Thanks for the answer, but with this C-Code the Clock runs super fast! The Minutes runs so fast like the Seconds ... .
You are quite correct. Sorry I made a big mistake.

Intead of using

tmr0 = 250;

you need to use

tmr0 = 5; //Note that this value may need to be edited slightly to trim the time.

The timer counts up to an interrupt. so before it only had to count to 5 before interrupting again. Sorry about the confusion.

Xsusakamei
Posts: 12
Joined: Sat Sep 15, 2007 11:40 pm
Contact:

Post by Xsusakamei »

@ Ben Rowland:
I have tried a lot of values for tmr0, but no one gives a better result. On the contrary, the clock runs faster or slower! A better result was not to reach :-(

@ Steve Tandy:
Ok, Flowcode disables (tmr0) IRQs at beginning of the IRQ-Makro to prevent a new IRQ and enabled it after the routine - so i think. This duration of the Makro added to a big delay after a while.

You are right with the registers of timer 0. I have mistake it with the 16 Bit Timer 1 - here the 2 Registers must be set with the count Value!

Ok, so the Timer 0 was not exact enough for a clock.

Now i take the Timer 1 for IRQ.
I don't have reached it to generate an signal of one second. I don't understand the calculation of the IRQ Frequency of 2400 Hz by a clock of 19660800Hz and a divider of 1:8. With this Values and an counter which i increment the seconds after 2400 calls, the clock runs to slow! Why?

Please explain me the calculation of the IRQ Frequencies. The german help is in this thing very bad!
Greetings
J. Schol

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:

Post by Benj »

Hello

as long as your interrupt routine is farily short then you can get rid of the disable tmr0 interrupt. This will allow the timer 0 to interrupt at the correct frequency and give you a very accurate time period. Eg run for a year and only loose or gain a fraction of a second depending on the accuracy of your crystal.

This will also remove the need to set a value into the tmr0 register.

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

The timer interrupt frequencies are calculated like this:

Code: Select all

int freq = ((osc / 4) / 256) / prescaler
So for your example:

Code: Select all

osc = 19660800
prescaler = 8

((19660800 / 4) / 256) / 8 = 2400
How slow does your clock run - I've just created this program and it seems to work fine (I cannot see any problems with the timing after 5 minutes).

Post Reply