Today’s project is a rather unusual one: a word clock made implemented using a graphical LCD (GLCD), a PIC18F45K22 microcontroller, all put together on a EasyPIC v7 development board from MikroElektronika.
I have to admit that the idea of making my own word clock bothers me for some time, and I always was too lazy to go to to all that hassle of building it, especially because of the issues related to the mechanical construction. Until one day inspiration come: I can emulate the feeling of a word clock using a GLCD. No mechanical work, and all I needed was already in my inventory. So, all you need is:
- EasyPIC V7 from MikroElektronika, with 8MHz crystal and PIC18F45k22 microcontroller (standard configuration).
- 128×64 GLCD, mine has part no. MIKROE-4. Note that this is only a GLCD, without touch capabilities. As such, programming of the clock is made using the buttons on EasyPIC v7.
- mikroC PRO for PIC full license. A full license is required as the code exceeds the limit of 2kb. I do prefer this compiler as it comes with many libraries, including one for GLCD.
- Timer Calculator from MikroElektronika software. As I found that I don’t have any RTC click boards, I will use TIMER1 to generate an interrupt every 250ms, and a counter to increment every minute (note to self: order some RTC’s).
In the above picture, you see the EasyPIC v7 with the GLCD installed. Besides this, several jumpers must be set for the software to run correctly. The settings are
- J17 is set to ground (required for button operation).
- On the switch from PORTE, pull-ups are activated for pins RE2, RE1 and RE0.
- J21 is set to disable the piezo buzzer.
- All switches on SW1 and SW2 are set to disabled (left position).
- On SW3 the switch corresponding to LEDs connected to ports A/E is set to enabled (right position). All other switches are on the left (disabled).
- On SW4 the BCK is connected to 5V (switch 6 on the right position). All other switches are on left. This will enable the backlight of the GLCD, on full intensity.
That is all for the hardware!
Well, now comes the difficult part: writing a word clock code. The code that I propose is written in MikroC, as I take full benefit from having a GLCD library, completed with some nice system fonts. Starting with a picture of a LED-based word clock in my hand, and after some trial and errors I came with the following clock layout:
As I don’t have a RTC, the clock setting is lost when the reset button is pressed or when the power is lost. I turned this into an advantage by writing the code so the clock will enter programming mode when the reset button is pressed or when the power is applied. After we exit the programming mode by pressing RE0 all the buttons are ignored and the clock runs until power is turned off or the reset button is pressed. And here are some pictures of the display when the clock is in programming mode:
As you observe, I used a small 3×5 system font is programming mode to maximize the information on the screen. After that, I will change to a larger 5×8 system font that is easier to read from the distance.
To generate the timing I used the main clock, running from an external crystal of 8MHz. Obviously, with this clock frequency, with all the prescaling and the 16 bits resolution of the timer, the period of the generated pulsed cannot he higher than 262ms. I set the period to 250ms, at it makes further computation easier. An extra software counter is incremented every time timer overflows, so we can generate the minutes and the hours.
Oh, the mighty code. Here’s the full listing:
All the code, including the compiled .hex file can be downloaded from here.
By now you have spent several minutes trying to understand how the code works. Here are some extra considerations:
In the programming part, I used the Button function, which performs button debouncing. You might wish to read the mikroC help file for information about the way this function works.
The timer generates an interrupt every 250ms. At every interrupt the variable count is incremented. For a minute we have 60 seconds, and we have four timer overflows every second, thus we have to increment the count for 240 times to reach a full minute. When a minute is reached, the count is reset, and then the minute variable is incremented. Next we check to see if we went over 59. If so, we reset the minutes and we increment the hour, again checking for overflow (which happens when we go over twelve, as the clock is 12h only).
The internal clock works in minutes and hours, but the display is updated every five minutes. This is why I use the variable disp_minute, which is determined as disp_minute = 5 * (unsigned) ( minute / 5 ); The result if then passed to the Glcd_Word_Clock routine.
The Glcd_Word_Clock routine takes care of emulating the word clock feel. First we clear the GLCD and we show the text IT’S. Then, the disp_minute is evaluated via a series of switch-case statements to determine which text to show on the display. If the disp_minute is below 30, the text PAST is shown. If disp_minute is over 35 the text TO is shown, then the hour is incremented, obviously performing an extra check so the hour cannot go past twelve. Once again a series of switch-case statements is used to display the corresponding text for the hour. Finally, we show the text O’CLOCK.