Question for example TUT22 (clock)

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

Moderators: Benj, Mods

Post Reply
alexander70
Posts: 79
Joined: Mon Aug 03, 2009 12:14 pm

Question for example TUT22 (clock)

Post by alexander70 »

Hi! I have a question for example TUT22 (clock). It would be interesting to set up 2 buttons to set the time, one button to set hours, the other - to set minutes. Which way is better to do this?
Respectfully, Alexander

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: Question for example TUT22 (clock)

Post by Benj »

Hello Alexander

In the main program loop you could add an input icon to read your switches. If they are pressed then increment the hour or minute variable approproiatly. Then include a while loop to hold the program while the switch is pressed. This should allow you to increment a single digit at a time. Looking at the example there is a much better way of processing the time then has been shown. You simply need a timer count and variables to hold the Hours, Mins and Seconds. Then when printing out the variables you need to check if they are under 10. If they are then first print out a 0 to allow the spacing to stay constant. If I get time today I will make a better clock example file.

alexander70
Posts: 79
Joined: Mon Aug 03, 2009 12:14 pm

Re: Question for example TUT22 (clock)

Post by alexander70 »

I did so (attached), but when I click on the button B0 to set the hours, the program stops. Where I made a mistake?
Attachments
TUT_22_16F887_clock_new.fcf
(16.07 KiB) Downloaded 408 times
Respectfully, Alexander

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: Question for example TUT22 (clock)

Post by Benj »

Hello Alexander

The reason your program seems to be stopping is because the connection point is just below the read switches icon meaning the switch variable does not get updated and the program therefore becomes trapped in a loop. Moveing the connection point label to above the read switches should solve this for you.

alexander70
Posts: 79
Joined: Mon Aug 03, 2009 12:14 pm

Re: Question for example TUT22 (clock)

Post by alexander70 »

Thank you, I moved the connection point above the switches reading, but still does not work. When pressed, the program stops, and when I release the button, the program is working again. But the hours is not set as I had planned.
Attachments
TUT_22_16F887_clock_new2.fcf
(16.07 KiB) Downloaded 331 times
Respectfully, Alexander

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: Question for example TUT22 (clock)

Post by Benj »

Hello Alexander

Here is the better clock example I mentioned. It should be much easier to follow then the old example and also allows for the user to set the time using switches for hours and minutes.
Better_Clock_Example.fcf
(10 KiB) Downloaded 426 times
Let me know if you have any problems.

alexander70
Posts: 79
Joined: Mon Aug 03, 2009 12:14 pm

Re: Question for example TUT22 (clock)

Post by alexander70 »

Thank you! Good example.
Respectfully, Alexander

alexander70
Posts: 79
Joined: Mon Aug 03, 2009 12:14 pm

Re: Question for example TUT22 (clock)

Post by alexander70 »

Why use clock frequency 19660800 Hz? Is there are quartz crystals with such frequency?
Respectfully, Alexander

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

Re: Question for example TUT22 (clock)

Post by medelec35 »

alexander70 wrote:Why use clock frequency 19660800 Hz? Is there are quartz crystals with such frequency?
Yes, EB0006 programer which you can use with Eblocks has a crystal which runs at that frequency.
I agree Good example.
Martin

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: Question for example TUT22 (clock)

Post by Benj »

Hello Alexander

Yes there certainly is a crystal with this frequency. Crystals like this are designed for use with 8-bit micros for generating baud rates etc. eg the 19.6608MHz crystal gives a timer interrupt of exactly 75hz. Our EB006 and HP488 boards all come with a 19.6608MHz crystal pre fitted.

19660800 / internal clock divider 4 = 4915200
4915200 / timer count register 256 = 19200
19200 / interrupt prescaler 256 = 75

If you using a different crystal eg the 8MHz internal crystal then you will need to check the interrupt frequency in the interrupt enable options, You will then need to update the interrupt handler macro so that it uses the updated count value to time a second interval. At 8MHz there is a slight error in the interrupt timing so instead of the dead on 75hz you get the closest value of 61.035. This is an error of 0.035 that will accumilate every second. Eg in 10 seconds the error will have grown to 0.35. 100 seconds and the error is now 3.5 seconds.

I have attached an updated example for an 8MHz clock so you can see the differences.
Attachments
Better_Clock_Example.fcf
(15 KiB) Downloaded 417 times

alexander70
Posts: 79
Joined: Mon Aug 03, 2009 12:14 pm

Re: Question for example TUT22 (clock)

Post by alexander70 »

Thank you, I will see this.
Respectfully, Alexander

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

Re: Question for example TUT22 (clock)

Post by medelec35 »

If using 8MHz then: Prescaler = 256 timer offset = 4 then interrupt frequency = 31.002Hz
which is slightly better.
Martin

alexander70
Posts: 79
Joined: Mon Aug 03, 2009 12:14 pm

Re: Question for example TUT22 (clock)

Post by alexander70 »

I want to ask about the internal clock. If 19660800 divided by 256 is equal to 76800. How is the frequency of interruption of 75 Hz?
Respectfully, Alexander

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: Question for example TUT22 (clock)

Post by Benj »

Hello Alexander

The answer is shown above.
19660800 / internal clock divider 4 = 4915200
4915200 / timer count register 256 = 19200
19200 / interrupt prescaler 256 = 75

alexander70
Posts: 79
Joined: Mon Aug 03, 2009 12:14 pm

Re: Question for example TUT22 (clock)

Post by alexander70 »

Thank you very much, it is now clear.
Respectfully, Alexander

Post Reply