From Tim's website
Jump to: navigation, search

Rugby Clock - Circuit

Rugby Clock circuit.png

Power Supply

The power supply is simply a 7805 regulator supplied from a 9v battery or mains adapter. The power is applied to the input pin and a common ground, and a constant 5 volts is supplied from the output pin. The current drawn by the circuit is small (about 20mA) so the power dissipated with a 9v input is less than 100mW. The regulator hardly gets warm and a heat sink is not required. I connected a green LED to the 5v output to indicate when the supply was on, which was particularly useful when trying to get signs of life from the PIC micro-controller! The current through the LED is limited by R1 to 3mA, so it only glows dimly and does not draw too much power. The supply decoupling capacitor, C1, was mounted across pins 19 and 20 of the PIC, as this is most susceptible to noise in the supply voltage.

LM78M05 Datasheet LM7805 pinout.png

Rugby MSF Receiver

The EM2 MSF receiver connects directly to a suitable tuned ferrite rod antenna and provides a logic output of the demodulated pulses transmitted from Rugby. The receiver can use the 5v supply directly, and the output is 4.9 or 0.1 volts when the 60kHz signal is present or not present respectively. The signal appears to be cleaned, as the logic output is fairly stable. However, interference (such as a mobile phone or computer) will cause problems and the output will switch between 0.1v and 4.9v randomly. This should be detected by the software and ignored, at which point the software should use an internal clock until a stable signal returns.

MSF Format

The Rugby Clock Photos show the receiver and the tuned ferrite rod antenna. The antenna has a built in capacitor to give a narrow 60kHz reception band. The official MSF format is described in the MSF Format document from the National Physics Laboratory website. The time and date information is sent by 60 pulses each minute, with one pulse on every second. The first 16 pulses contain the difference from Universal Time, using double or single pulses. The next 35 pulses give the year, month, day, date, hour and minute in binary coded decimal, using short and long pulses. A very long pulse is used for the start pulse of each minute. The four types of pulse are shown below.

MSF pulse types.png

The falling edge of the pulse is the start of a new second, corresponding to a break in the 60kHz transmission. The EM2 MSF Receiver output will fall from 4.9 to 0.1 volts at this point. A single short pulse (100ms) denotes a binary 0, a single long pulse (200ms) denotes a binary 1. Double pulses are used during seconds 1 to 16 in order to indicate the difference between Greenwich Mean Time and Universal Time (see the MSF Format sheet). The first second of the minute, second 0, is identified by a very long pulse (500ms).

The filters in the EM2 module lengthen the pulses slightly. The short pulse is between 110ms and 130ms long. The long pulse is between 210ms and 230ms long and the double pulse ends between 310ms and 330ms. The pulses can be identified by sampling 70ms, 170ms, 270ms and 370ms after the initial falling edge. A final sample should be made after 600ms to validate the very long pulse. This gives 5 values of High or Low, and the 4 types of pulse can be identified as:

  • L H H H H = Short pulse
  • L L H H H = Long pulse
  • L H L H H = Double pulse
  • L L L L H = Very long pulse

Any other combination of High and Low samples should be regarded as an error. The seconds should be triggered from the start of the pulse, so on the falling edge the seconds should be updated and then the five samples taken. The remaining 400ms can be used to identify the pulse and update any displays. The complete algorithm I used is described on the software page.

PIC Microcontroller

The PIC 16F873 was used for this project, which has the advantage of in-circuit debugging using an ICD module, such as the one from Microchip. The device has 5 Analog inputs AN0 to AN4 on pins 2, 3, 4, 5 and 7. There are two 8 bit ports, B (using pins 21 to 28) and C (using pins 11 to 18). When the device is debugged in-circuit pins 27 and 28 are required, and so bits 6 and 7 from port B are unavailable. The connections to the programming/debugging header socket are shown in the circuit at the top of the page.

PIC 16F873 Datasheet PIC16F873 pinout.png

The PIC used was a 16F873-04 meaning that it could be clocked up to 4MHz. This was done with a 4MHz crystal and two 68pF capacitors (see circuit). Pins 1, 24, 27 and 28 were connected to the programming/debugging socket, and pins 8, 19 and 20 were connected to the supply. The Analog port AN0 on pin 2 was connected to the output of the EM2 MSF Receiver module, and bit B0 from port B on pin 21 was used as an output to enable the receiver module. The 8 bits from port C were connected to the data pins of the LCD module and bits B1 to B3 on pins 22 to 24 were used for the LCD control lines. Finally, bits B4 and B5 on pins 25 and 26 were used to drive two small 5v indicator LEDs by driving low to turn them on. The PIC is able to sink a greater current than it can source (25mA as opposed to 20mA), but it would be able to source the current required by the LEDs if necessary. Port B can sink 150mA in total, but source only 100mA, so there is a limit of 12mA per LED if Port B is used to source 8 LEDs.

LCD Display

PC 1602F Datasheet
HD 44780 Datasheet

The PowerTip PC 1602F LCD display was connected to Port C on the PIC, with some control lines from Port B. The Powertip display uses an HD44780 compliant controller, which is surprisingly simple and versatile once setup. I used a preset resistor to set the voltage at the contrast pin of the LCD module to 0.45v (for room temperature). Without this the LCD contrast is so low the display appears blank.

Peter Ouwehand has an extensive page on How to control a HD44780-based Character-LCD, with PIC examples. The interface has two registers - the Instruction (or Control) register and the Data register. These are both 8 bits long, but it is possible to use a 4 bit interface by sending two 4 bit words at a time. The Commands sent to the Instruction register are as follows:

  • Set interface data length (8 bit or 4 bit)
  • Set the number of lines to use on the display
  • Set the size of the characters to display (7 or 10 pixels high)
  • Turn Display On or Off
  • Clear display and return cursor to home
  • Set default cursor movement (e.g. move right after each character)
  • Set default screen movement (e.g. scroll left after each character)
  • Set the cursor type (Hidden, Underscore or Blinking)
  • Move cursor or shift display by one, left or right
  • Set Character Generator RAM (CGRAM) address (select custom character)
  • Set Display Data RAM (DDRAM) address (set cursor position)
  • Read busy-flag and CGRAM or DDRAM address
  • Write to or Read from CGRAM (set up custom characters)
  • Write to or Read from DDRAM (write to screen)

When sending data to the LCD display, the R/W input is set Low (Write) and the appropriate register is selected (Low for Instruction, High for Data). The word is set up on the data pins and the Enable input is sent from High to Low to latch the data across. The Enable pin is then returned to a High state and the next word is set up. The 'busy' flag should be checked before attempting to send data, and the appropriate address space (CGRAM or DDRAM) should be selected with the Instruction register before latching data into the Data register. For an example see the PIC Software page.

For a 16 character, 2 line display the DDRAM addresses are 0x00 to 0x0F for line one, and 0x40 to 0x4F for line two. The most significant 2 bits set the line number 1 or 2 and the remaining 6 bits set the character 1 to 16. Screens with more than two lines do not continue this rule.