alex [a] avtanski.com
What do you do if you have a spare LCD module with backlight, a weird 16 button keyboard, and a PIC16F877A microcontroller gathering dust? A monster Martian Clock immediately springs to mind.
You are probably thinking "There are hundreds of PIC clocks on the Net - do we need yet another one?!" Well, this one is a bit different:
If you would like to build this clock, of if you are just curious about it, check the links below. The same information is accessible using the tabs at the top and bottom of the page.
If you have questions or comments, or if you have an error on this site, please let me know. My e-mail is at the bottom of the page. I would be also interested if you have built the clock - if so, I will appreciate any pictures from your project and would be glad to post them on this site, or to put up a link to your site.
Finally, if you want to build a Mars clock but you don't have the tools to program a PIC microcontroller, check Mars Base dot Net for another project based on a 4059 programmable divide-by-n counter and a MM5387/LM8361 alarm clock IC.
Finding the right enclosure for a project is often surprisingly difficult. You can make a nice box yourself if you have the tools, or you can buy a ready-made enclosure from places like RadioShack. My favorite approach, however, is to give new life to some old device - a modem, a computer mouse, whatever, - then to take out what I don't need and to keep anything I can re-use. In this case, a friend found for me this beautiful dialup modem from a long-gone era:
The junk electronic shop sold it for just $2.00 - way better than hobby shop enclosures. As a bonus, it had all the connectors I might need, and significant part of the electronics of the modem can be reused.
Time to take out the 10x magnifying glass and reverse engineer the schematics for the power supply on the board, and the amplifier circuits for the internal speaker. Another potentially useful thing are the LEDs in front, along with their driver chip (I haven't used the LEDs in this project yet, but decided to keep them, just in case). The rest should be taken out.
This is the "lightweight" version, after I'm finished with the soldering iron:
Time to decide what to do about the ports at the back. The main connector can be used to re-program the PIC without taking it out of the box. To do so, I attached some pin connectors to the internal side of the DB-25 leads so the microcontroller board can be plugged in there later:
Next comes putting connectors to tap the stabilized 5V of the modem's power supply electronics. Another connector attaches to the modem's internal speaker amplifier (an easy to find LM386). Finally, I added a 9V connector for the rechargeable battery, along with a simple trickle-charger composed of a 2.2k resistor and a diode glued to the modem board:
To plug it to the PICKit2 programmer, I took apart a printer cable and did some very mean things to it:
The board where the PIC and the rest of the electronics will sit is quite simple. Since we already have the speaker amplifier and the power supply, what's left is mostly connecting wires. Here is the board, in all its red-and-black glory:
Time for some drilling and filing to attach the keyboard and the LCD module with its backlight switch. Attaching the keyboard and the backlight switch was easy. Making sure that the opening for the LCD module is the exactly right size and accurately positioned was not. But with some care, the end result was quite satisfactory.
It starts to look like Monster Martian Clock already. After some hard programming, a few sleepless nights, and just a bit of magic, it is finally complete:
If I try to take it with me on a trip they will evacuate the airport, for sure!
The clock display is a 2x16 LCD module. The first row always shows the currently selected timer. The second row is used to show either the Julian time, or the current menu selection:
The sixteen timers are named TmA, TmB and so on, through TmP. Here, TmA is the name of the timer, followed by the current time. The second row shows the same time in Julian format (useful for astronomy and other things). The keyboard looks like this:
Not the best layout, but this is what I had on hand when I was making the clock. If you decide to build this project and have a keyboard with different layout, check Using Different Keyboard in the Code section.
Main Menu - Timer Selection
When the clock is just powered up, you are in the "Timer Selection" menu (the screenshot above). In this mode, you can browse through the 16 timers - TmA through TmP (as with any menu, use the and buttons to select timers). If you are not sure which menu you are in, pressing the button several times will bring you to the main menu.
If you want to change something about a timer - to set up the time, adjust the timer rate, set up an alarm, etc. select the given timer and press the button. This will bring the next menu, with two options:
When the Adjust Timer option is visible, you can use the button to pause and restart the timer, and the button to switch timer direction (forward or backward). This will affect only the currently selected timer.
Select a timer, and press the button twice (once to get to the Adjust menu, and the second time to choose the Adjust Timer option). This will show you the next menu with the following options:
Selecting the Set Timer option and pressing will show you a screen where you can enter the new time. Enter the time using the number keys. The new time will be set when you enter all six digits (HH:MM:SS) or when you press the button. Pressing at any time will abort the operation and the time will not be set.
Each timer has an alarm associated with it. This menu will let you choose between the following options:
To set the alarm time, choose the Alarm Time option and press . Enter the time. As usual, the will confirm and the will cancel the operation at any time.
The microcontroller program is written in assembly language. I tried to make it as extensible as possible - even at the price of increasing the code size sometimes. It has room for improvement, for sure, but it works and is not overly messy - that's enough for me. I might be updating the program from time to time - providing bugfixes and new features. If you decide to build this clock, make sure you check this site for the latest version.
Adjusting the Clock Rate I spent some time tuning the clock rate and got the accuracy is to a pretty good level - less than a second for a week. If your clock is doing worse than this, you can easily adjust the rate by changing the TMR_STD_LO, TMR_STD_HI, TMR_XTR_LO, and TMR_XTR_HI constants defined in the beginning of astro_clock.asm.
First, how it all works. Every time the microcontroller's T1 timer overflows, it generates an interrupt. TMR_STD_LO and TMR_STD_HI set the starting point of the timer, so increasing or decreasing those values will speed up or slow down the timer respecively. For nineteen cycles of the timer the TMR_STD_LO and TMR_STD_HI values are used, but for the twentieth the timer is initialized using TMR_XTR_LO and TMR_XTR_HI. This allows TMR_XTR_LO and TMR_XTR_HI to be used for precision adjustments.
Use the TMR_STD_LO and TMR_STD_HI for rough adjustments. The current value is $0x66C5 - increase this a bit to speed-up the clock, redice it to slow it down. I expect you will have to modify only the TMR_STD_LO byte, but if you are using a resonator running at different frequency than mine, you might have to adjust both.
When you have found the best value for this, use the TMR_XTR_LO and TMR_XTR_HI to fine-tune the clock rate. Again, to speed-up the clock, increase the two-byte number; to slow it down, decrease it. This adjustment is about 20 times more accurate than the first.
Using a Different Keyboard The keyboard that I've found for the clock (see the image in the Operation section) might not be the same as yours. If your keyboard layout does not match mine, edit the button definitions in KBD.h. In this file, all the keys are listed in order, starting from the top left, and going row by row. Reorder these definitions according the layout of your keyboard, then update the hex numbers for each key so they are sequential.
If you plan to use alternative keys for the menu navigation, stopping, starting and reversing the timers, change the definitions of the special buttons in that same file. They appear toward the end of KBD.h.
This section has some examples for setting up the clock, as well as links to various useful tools and sites. And, because I called this a Mars clock, we start with...
Setting the Timer Cycle Length, or how to answer the "What time is it on Mars?" questionBy default each of the 16 timers runs on a standard 24-hour cycle. If you wish to have a timer tracking a different time period (for example, have it run on Mars time), you have to change the timer's cycle length. The timer will still show numbers from 00:00:00 to 23:59:59, but it will run faster or slower, depending on the cycle length setting. (If you wish to have a timer running at regular speed, but resetting before it reaches 23:59:59, this is another issue - use alarm setting for this timer, along with a Reset action. See the Alarm Settings section on how to do that.)
Below you will find a table of commonly used values for the cycle length. If you need to set up a timer to something that is not shown in the table below, calculate the value to use by multiplying the number of seconds in the cycle by 4 and rounding the result to the nearest integer. For example, take the Mars day in the table below - a day on Mars is 24 hours 39 minutes and 35.24 seconds long. This gives us
4 * ( 24*60*60 + 39*60 + 35.24 ) = 4 * 88 775.24 = 355 100.96 ≈ 355 101
To set a timer cycle length, select the timer, choose Adjust Timer → Set Cycle Length and enter the new value.
Once you configure the correct cycle length, you can set the timer to Mars time. You can get the current Mars time from NASA's Mars24 Sunclock. An excellent resource for all things Martian is MarsBase dot net.
Sky and Telescope's website has information on Jupiter's Great Red Spot transit times. If this link has expired, go to www.skyandtelescope.com and search for "GRS".
Julian time is used in astronomy. There, the number does not roll back from 1.0 to 0.0 each day, but keep increasing. The result is a unique number for each moment of time. Because this clock does not have date, we always show zero-point-something.
What is Julian Time?
The second row on the clock's LCD is a decimal number that represents the time of the day in Julian format. The number is 0.0 at 12:00 at noon, and then starts increasing toward 1.0 until it 12:00 noon on the next day. Midnight is 0.5. Representing the time this way makes it very convenient to calculate time differences and is generally easier to work with than time expressed in hours, minutes, and seconds.
PCB Pricing: https://jlcpcb.com/quote