Page 1 of 1
Question for example TUT22 (clock)
Posted: Tue Nov 03, 2009 12:40 pm
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?
Re: Question for example TUT22 (clock)
Posted: Tue Nov 03, 2009 1:09 pm
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.
Re: Question for example TUT22 (clock)
Posted: Tue Nov 03, 2009 1:31 pm
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?
Re: Question for example TUT22 (clock)
Posted: Tue Nov 03, 2009 2:36 pm
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.
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 7:01 am
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.
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 10:33 am
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.
Let me know if you have any problems.
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 11:38 am
by alexander70
Thank you! Good example.
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 11:42 am
by alexander70
Why use clock frequency 19660800 Hz? Is there are quartz crystals with such frequency?
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 12:02 pm
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.
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 12:39 pm
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.
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 12:45 pm
by alexander70
Thank you, I will see this.
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 12:57 pm
by medelec35
If using 8MHz then: Prescaler = 256 timer offset = 4 then interrupt frequency = 31.002Hz
which is slightly better.
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 1:02 pm
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?
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 1:05 pm
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
Re: Question for example TUT22 (clock)
Posted: Wed Nov 04, 2009 1:08 pm
by alexander70
Thank you very much, it is now clear.