MD-CLOCK or A cheap clock for the rev. 2 MDs by Mike Allen 400J Summer Wind Way Glen Burnie, MD 21061 (301) 768-1721 For a long time I had been wondering to what good use I could put the I/O expansion header (that strange looking 2 rows of 20 pins each) in my rev. 2 MD3. Then in the March 1986 issue of BYTE, Ciarcia's Circuit Cellar took a look at real-time clocks. "Aha," sez I, "just what I need." So I took a look at the schematic for the clock that used a 8255 Programmable Peripheral Interface and the MSM5832 clock. It looked to me that all I had to do to make it work with the I/O expansion connector was to invert the RESET- (reset not) signal. So off I ran to my junque box (I have high class junk!) and my catalogs to see where I could get what the cheapest. It seemed that JDR Microdevices (1224 S. Bascom Ave., San Jose, CA 95128) had the best prices on all the critical parts. Off went an order to them. Had I purchased the parts that I dug out of my junk box from local sources I would have spent a total of $18.69. I spent a little less. The circuit is based on the MSM5832 clock chip from OKI. The thing is a very old chip and is very slow but has the advantage of being cheap. It will run on as little as 2.2 volts and draws very little power in the standby mode. Ideal for battery back-up. The MSM5832 is organized about a 4 bit bus structure. (Remember the 4004?) It has 4 address and 4 data lines. There are also READ, WRITE, HOLD and Chip Select lines. The functions of READ, WRITE and Chip Select are obvious. The HOLD line is a little strange. It stops the clock! I suppose there are uses for it when reading, but I couldn't find any. While setting the clock, it is used to stop the clock until you want it to start. That's useful. The addresses to the MSM5832 from 00H through 0CH are used to address 13 four bit registers within the MSM5832. These registers have 4 bit values as follows: (NOTE: These differ from the table in Ciarcia's article. Through hard experience I have found that he is wrong and these are correct.) ADDRESS FUNCTION RANGE 00 1s of seconds 0->9 01 10s of seconds 0->5 02 1s of minutes 0->9 03 10s of minutes 0->5 04 1s of hours 0->9 05 bits 0,1 10s of hours 0->2 bit 2 AM(0)/PM(1) 0->1 bit 3 12(0)/024(1) hr format 0->1 06 Day of the week 0->6 07 1s of day of month 0->9 08 bits 0,1 10s of day of month 0->3 bit 2 regular(0)/leap(1) year 0->1 09 1s of months 0->9 0A 10s of months 0->1 0B 1s of years 0->9 0C 10s of years 0->9 To read the MSM5832 you set the READ line high and send out an address. Then you wait 6 usec and read the data. Writing is a little more difficult. You send out the HOLD line, wait 150 usec then send out the address. Wait 6 usec and then send out the data. Set the write line high for 1 usec and then back low. Drop the hold line when you want the clock to start. Since it is so slow, it cannot interface directly with the Z80 bus. To give you an example, it takes 6 usec. for the data to be ready after the address is sent. This is much too slow for the bus that is available in the MD. The workaround is to use a 8255 to buffer the address, data and control lines. I stole the circuit directly from the Ciarcia article, using the bottom 4 lines of the 8255 A port as bi-directional data lines to the MSM5832, the bottom 4 lines of the B port as the address lines to the MSM5832 and the top 4 lines of the C port as the control lines to the MSM5832. This way all the timing can be established in software. For instance, I can send out the address on the B port, waste a little time and then read the result on the A port. Inefficient use of the 8255, but who cares. I decided to use the IO30- signal for the 8255 chip select. This means that port A is at I/O location 30H, port B at 31H, port C at 32H and the control for the 8255 at 33H. The only critical part in the circuit is the 1N34A diode. If you must substitute, make sure you use a germanium diode with a forward voltage drop of 0.5 volts or less. I used a piece of perf board that I had lying around to build the clock. The 3x5 perf board that I call out in the parts list is a little large, but it can be cut down. I used a little 5 minute epoxy to glue down the sockets and the battery holder. I stuffed the various components where it looked like they fit the best and then point-to-point wired everything using #26 solid wire. It got a little tedious, especially soldering the 40 conductor ribbon cable to the board. Then I installed the 40 pin connector on the end of the ribbon cable. I found the best way to do this was to put a couple blocks of wood in a vise and then squeeze the connector onto the cable using the vise. The wood helps to distribute the force evenly across the connector and also protects the connector itself. Probably the easiest way to mount the board is with some foam tape that is stick on both sides. Radio Control modelers use it alot. It's called 'servo tape' and can be found at most any hobby shop. Well, after a couple of days of trying to induce blindness in my already weak eyes, it was time to try it. I plugged it in, being very careful to make sure that I had the connector oriented correctly, and started checking voltages. Being a chicken at heart I had not installed the chips in the board yet. Everything seemed like it was what it should be, so I turned off the MD, plugged in the chips and turned the power back on. There it sat. The MD worked fine, but I wasn't sure what or if the clock was doing. Back to Ciarcia's article. He had a BASIC program that was supposed to allow you to read and set the clock. I pulled out my pristine BASIC disk (I program mostly in 'C' or Pascal) and entered the program. Well, things looked ok. I could set and read the clock, although in a rather unuseful format. Then I noticed that the seconds weren't setting properly. Well, really the were. What Ciarcia didn't tell us is that you can't set the seconds! Writing to the seconds and 10s of seconds registers resets them to zero. OK, now I understood that. The next thing I noticed was that, according to Ciarcia, the days of the week were supposed to be a number between 1 and 7. I was checking out things like leap-years and 12 and 24 hour time formats and I had set the day-of-the-week to 7. It ticked over and went to 0! Checking some other articles that I have on the MSM5832 clock I found out that the day-of-the-week really goes between 0 and 6. So, I started writing programs to make nice pretty time and date displays on my MDT60. Things were going swimmingly until I noticed that every time I pressed the reset button, I lost about 30 seconds. Investigating further I found out that I was losing the time from when I pressed the reset button and when I called up the program to read the time. Looking at the schematic a little harder and with my trusty Intel data book at my side I discovered that when the 8255 was reset all the I/O lines go to the input mode. This meant that the hold line which is normally an output from the 8255 to the MSM5832 was being pulled high and was staying high until the 8255 was programmed by the read time program. It made me wonder if Ciarcia had ever even built the circuit. After much head-scratching I decided that the easiest approach was to force the Chip Select to the MSM5832 to be low until the 8255 could be programmed. This was done by using another part of the 74LS04 that I was using to invert the RESET- signal. I used it to invert another I/O line from the 8255. This line would be pulled high (and inverted low to the MSM5832) until the 8255 could be programmed and that particular line commanded low. As long as the MSM5832 is not selected, it keeps on keeping time but ignores all the command lines. Just what I needed. The clock ignores all commands until the 8255 is programmed properly! Aligning the clock is easy and tedious. Just see how much you are off in a given period of time (say a day) and then adjust the trimmer capacitor slightly; less capacity if you are running slow, more it you are fast. If you have access to a frequency counter there is a faster way. Send the clock an address of 0FH. Connect the counter to pin 9 of the MSM5832 and adjust the capacitor until you read exactly 1024 hz. As an exercise, I decided to see if I could write programs in all the different languages that I had to read and set the clock. I was able to read and set the clock in 8080 assembly language, MicroSoft BASIC, MIX 'C' and Turbo Pascal 3.0. I was unable to write routines in PILOT since I could find no instruction to read an I/O port although there is one to output to an I/O port. The programs are rather brute force, but they work. I also wrote an insert for use with BYE5. About this time a buddy of mine with a Radio Shack Color Computer (at which I had been scoffing greatly) showed me a public domain schematic drawing program that he had. It was really a nice effort and worked well, if a little slowly at times. It ended up creating 9 disk files which were screen dumps of different parts of the schematic. We took a look at the program that took those files and sent them to the printer and decided that it would be easy to write an 8080 program to do the same thing. So, I sat down in front of the CoCo and drew the schematic. We then transferred the files to my MD by modem and I sat down to write the CADPRINT program. Many hours of confusion later, I had a program that could take the screen dumps generated by COCOCAD on a CoCo and dump them to my Epson MX-80 to print the schematic. The bottom line is that I how have a file called MDCLCK12.LBR that contains all the programs I have written for using the clock, a parts list, this article and the CADPRINT program for an EPSON or OKI ML92/93 printer and the 9 files that contain the schematic. There are also a couple of neat programs to set and read the clock by John Osnes who also made the mods to CADPRINT for the OKIs. Hopefully some people out there will look at the CADPRINT program and make modifications so that it can be used with other printers. I do hope this happens, 'cause I plan on using CADPRINT with other hardware articles in the future. MDCLCK12.LBR is available on the MOR BBS or on a FLOB. -30-