|★ APPLICATIONS ★ DIVERS ★ Real Time Clock: Clock on with your Amstrad ★|
|Real Time Clock|Computing with the Amstrad)||Applications Divers|
HAVE you ever been so engrossed in your programming that you've lost all track of time? I bet every programmer's had the annoying experience of missing an appointment in this way. Help's at hand though, with this machine code real time clock for the CPC series.
Most people know of the Amstrad's internal clock system, constantly updating itself every 1 /300th of a second. It is very easy to access it from Basic by typing:
This will just give you the number of 1/300ths of a second which have passed since the computer was last reset. Although simple to use it wouldn't be very useful as a real time clock.
A better method would be to use Basic's EVERY routine to update our own clock every second. This would take the form:
where the clock routine is at line 1000 onwards. The 50 tells the computer to go to the routine every second and the 1 tells it to use timer 1. This works fine if you stay within a program which is being executed, but when you break out the clock stops updating itself.
The solution is to use a machine code routine which is called every second whatever the computer is doing at the time, be it running a program or waiting for a command to be input directly.
This program will display the clock in the top left hand corner of window #7 in the form:
Once the code is poked into memory via Program I, or Program II if you have an assembler, the clock can be started with:
This initialises a ticker event and sets it calling the clock routine every second. Once this has been set going it can be stopped with:
which takes the event off the ticker list.
Once the ticker event has been initialised you will see. after a second, the time 12:00:01. This will continue to update itself every second whatever you do.
This isn't a lot of good as it is, unless you started the clock at 12 o'clock, as it will not be displaying the correct time. However any clock worth its salt can be adjusted, and to change this one you must type:
The clock should now be displaying the correct time.
The routine works by loading the E register with the hour, obtained from location &80AA, L with the minutes stored at &80AB and H with the seconds from &80AC.
H, the seconds, is incremented first and tested to see if it's 60 and if it is then L, the minutes, is also incremented and H set to zero. If L is now 60 the hours in E are also incremented and the minutes in L reset to zero. Finally if the hours reach 13, E is set to 1 and the time can be printed.
The display routine first stores the current position of the text cursor, switches to window #7 and moves the cursor to the top left corner. The hours, minutes and seconds in the E, L and H registers are printed and the cursor replaced.
The machine code routine is called every second so there is a slight slowing down of the Amstrad when it is doing other tasks. The clock itself does not lose much time, except when the computer is doing certain tasks like accessing the disc drive or more noticeably the tape recorder, since the Amstrad switches interrupts off. It is therefore necessary to set the clock again if it does lose a lot of time.
Problems can also occur if you are not working in the default stream #0, as the routine switches to this stream after it has displayed the time.
The machine code can be saved on its own by typing:
will load the code and call it. You could also have it change the clock to the correct time and set up window #7 so that the top left of the window is where you want the clock to be. For instance:
would place the clock in the centre of the screen in Mode 1. Window #0 could similarly be defined so that is out of the way of the clock.
Now there will be no excuse for missing meals or your favourite TV program because you didn't realise what the time was - because it will be there in front of your eyes!