USB Clock Version 2

I wanted a clock on my computer that showed the date and time and was always visible even if the monitor was turned off. In addition I wanted the clock to beep on the hour. This clock is built on a panel that fits in a 5.25" drive bay. I had built an earlier version of this project in a separate case, but I kept knocking it to the floor, so I decided a built-in version would be better.

To drive a microcontroller project from a PC computer program was not rocket science but it had to use a USB port. My trials and tribulations are documented on my USB page, and I ended up using the FTDI UB232R, which is a tiny inexpensive module with 8 pins and a mini-B USB connector. 


The LCD is a ordinary parallel 2x16 backlit display. The backlight is controlled by the processor and can be commanded on and off from the PC. There are two pushbuttons that can be programmed to do anything you want; in my case S1 sends a command to the PC application program to close, and S2 switches the time and date display format from local to UTC and back.

front viewInside view 

I used an empty 5.25" drive bay and built the project on the plastic panel that normally fills that space. The LCD display fits nicely with room for the pushbuttons. Each pushbutton holds an aluminum bracket which in turn holds a small circuit board in place behind the panel. I used a spare 2-port USB header on the motherboard to attach the project. I built a short adapter to go from the 4 pin male header on the motherboard to a USB-A female connector. Then an ordinary USB-A to USB-miniB cable can be used.

The command protocol used is very simple, documented in file protocol.txt. There is a one-letter command, a length, some optional data, and a checksum. The checksum probably isn't even required in a project like this. On startup the host program queries the project, which responds with its name. This also causes the backlight to come on (if it was off before). Then every second the host sends the time in the form of a string (displayed on line 1), and every minute sends the date (for line 2), and a beep is commanded on the hour. When the host program closes, it sends a shutdown command to the project, which simply causes the backlight to turn off. The project will remain powered up as long as the USB port it is connected to has power.

I don't think many people want a USB clock like this, but the project illustrates just how easy it is to implement a USB attached project. You need some facility with PC programming (I use C/C++ but perhaps more prefer Visual Basic, and I don't see why applications could not be developed for Linux, which FTDI also supports).

The host program is called USBCLOCK2 and was developed under Visual Studio 2008. It is a relatively plain WIN32 application and the language in C. I used the D2XX interface to the FTDI driver because it is much cleaner than the Windows serial port API. In many ways it is simplistic but it works and was easy to develop. The program needs a command line parameter like COM4 to work, so you need to find out the COM number from the Windows device manager (under Ports). 

Open the Startup menu and create a shortcut to where USBCLOCK2 is on your hard drive. Add the COMx specification to the command line. Then select Minimized instead of Normal Window in the Run box.
Once running the program minimizes to a small icon (looks like a clock) on the taskbar. If you click on it the window will appear. All the window does anyway is show error and status messages in a listbox, and has Close and Test buttons.

2015-05/06 Update version 2.2:  This project had been working flawlessly for years on my old Intel i5 computer with MSI motherboard (no USB 3). I recently upgraded to a new i5 processor on an ASUS motherboard with USB 3 capability. Then the project failed intermittantly, with hex data on the LCD screen or blank LCD. First I suspected power problems and placed larger capacitor on the power line but this did not help. Finally I looked at the error handling for Rx data errors, and found some problems, in routine diagnostic() where it would halt the program with no hint of what was happpening. Since I never ran into problems before, I never tested the routine properly. My conclusion is that the new computer USB port was generating Rx errors fairly regularly at system start-up. So there is a code update for the AVR program, during which I took the opportunity to remove the 12MHz crystal in favor of using the 8MHz internal clock (I don't remember why I originally used an external crystal), and the host program. The real fix was to add a syncronization step at start-up which clears any communications errors.

Download WinAVR C source code for the usb clock
(2007/10 Updated to add day of week to the end of line 2.)
(2013/12 Updated to support change to button 2 (above).)
(2015/05 Updated to improve error handling and use internal 8MHz clock.)
(2015/06 Updated to support startup syncronization)
(2015/08 Fix problem of conflict of startup byte and checksum byte)

Download Visual C++ C source code for the usb clock
(2011/02 Updated to fix a too-early startup problem.)
(2013/12 Updated to support change to button 2 (above).)
(2015/06 Updated to support startup syncronization)
(2015/08 Fix problem of conflict of startup byte and checksum byte)

Back to VE3LNY's AVR Project Page