Digital Clock Project 2011

Design Specification

    1. Objective

    To provide a non-volatile digital clock, with basic display & alarm capability.

Time must be kept for up to 2 weeks without power, but the display need not operate.

Display brightness to track ambient light.

12 & 24-hour time selectable.

2. Hardware

    RTC chip: Microchip MCP7940, providing all needed RTC functions.

CPU: Atmel ATmega08: rather more features than required, but no learning curve.

Power: External plug-pack supplying 6.5V, with an internal LDO reg. to 5V.

Display: LED, multiplexed 4 digits, each 7-segment & 2 decimal points.

Controls: 3 rear-mounted buttons (A, B, C).

Light: LDR to sense ambient light & adjust display.

2.1 CPU Clock

The AVR's internal oscillator is used, with the internal system bus running at 8MHz (nominal).

2.2 Display

The display comprises 4, common-anode 7-segment LED arrays, each having 2 decimal points (at left & right). The digits are numbered 1..4, from left to right. The decimal points on digit 3 (for example) are referenced as 3L & 3R (left & right).

The display digits are multiplexed in the conventional fashion.

2.3 Pin Assignments


Port B

Port C

Port D










BEL (output tone)


SGI0 (seg A)


MOSI (button A)

ALM (signal from RTC)

SGI1 (seg B)


MISO (button B)

SDA (I2C to RTC)

SGI2 (seg C)


SCK (button C)

SCL (ditto)

SGI5 (seg F)


SGI3 (seg D)


SGI6 (seg G)


SGI4 (seg E)

PHT (ADC: ambient light)


3. Button Actions (Running)

If the alarm is sounding, any button or combination will cancel the alarm, & do no more.

Otherwise, any of the buttons may be pressed singly, to enter a new mode:

A. Enters Alarm Set mode

B. Toggles alarm in/active

C. Toggles 12/24 hour display

4. Button Actions (Power-up)

Additional modes are entered if buttons are held down as power is applied, thus:

A. Enters main Time Set mode

B. Enters High Brightness Set mode

C. Enters Low Brightness Set mode

B&C Enters Calibration Mode

5. Software Architecture

The bulk of the processing is done by a base-level loop. The following are handled by interrupts:

  1. Display multiplexing

  2. Display brightness

  3. Button debounce & auto-repeat

5.1 Display Multiplexing

The 8-bit Counter/Timer 2 drives the display, with 1 full count (ie prescaler*256) per digit. The Output Compare interrupt can prematurely disable the segment output, so reducing the duty cycle, & so the effective brightness.

The timing is 8MHz(osc) / 128(prescaler) / 256(counter) = 244Hz / 4(digits) = 61Hz(panel rate). A counter from panel rate generates a 2Hz (nominal) timer for base-level actions.

5.2 Display Brightness

The T2 timer generates 2 interrupts: on compare-match, and on overflow at count=256. The overflow interrupt selects the next digit to display, & enables its segments. The compare interrupt turns off the current digit. So, by setting the compare register to a value between 0 and 255, the duty cycle of the LEDs can be controlled. Values close to the limits should be avoided, to ensure the interrupts are seen in the proper order.

Note that the comparator has a higher priority than the overflow interrupt.

5.3 I2C Bus

I2C transactions are initiated by the base-level loop, which uses a polled access mode, rather than interrupts. I2C is used only to access the RTC module, to set or read the time. Only hours & minutes (main & alarm) are maintained by the software. The RTC time is always 24-hour, the conversion to 12-hour format is done by the display software.

User settings (alarm, 12/24 mode, etc.) are stored in the RTC's RAM area.

5.4 Alarm Waveform Generator

The 16-bit Counter/Timer 1B generates the alarm tones, using the 50:50 output square-wave mode. The volume is fixed by the circuit design. The frequency may be controlled by programming the timer (within the range of the transducer).

The alarm is controlled by the base-level software.

6 Base-Level Loop

Most functionality is embodied in the base-level software loop.

6.1 Initialisation

At power-up, the hardware is first initialised. This includes a check of the RTC status, and a read of the panel switches. If necessary, the RTC oscillator & standby power features are activated.

Control then passes to the appropriate mode, as follows:

6.2 Start-Up Defaults

At power-up, the mode settings (12/24 hour, and brightness) are read from the RTC's RAM area. If the RTC has not been initialised, modes default to 12-hour, and 100% brightness for light & dark.

6.3 Main Time-Set Mode

When the time is being set, the central DPs are extinguished, & the displayed time does not advance with the RTC. The B & C rear buttons advance hours & minutes respectively. When the button is pressed, the selected display increments then, if the button is held for more than 4 seconds, the display advances at 2Hz. The display wraps at maximum. There is no carry from minutes to hours. The mode (12 or 24-hour) is the same as the main display.

Pressing the A button cancels Set Mode, returning to Normal Mode.

6.4 Alarm Set Mode

This is the same as the main set-mode, with DP 4R lit steadily.

6.5 RTC Calibration Mode

All DPs are off. Digit 1 is either blank or a '-' symbol. The remaining 3 digits show the adjustment value, which can range through -127..+127, dead-ending at the ends. The B & C buttons raise & lower the value. Pressing the A button exits to Normal Mode.

6.6 High Brightness Set Mode

The brightness value is displayed in digits 1 & 2 (0-99%). The B & C buttons raise & lower the value, which wraps at 0 & 99. The brightness used at any given ambient light is a linear interpolation of the 2 limits, as full daylight & full dark. Pressing the A button exits to Normal Mode.

6.7 Low Brightness Set Mode

The brightness value is displayed in digits 3 & 4 (0-99%). The B & C buttons raise & lower the value, which wraps at 0 & 99. The brightness used at any given ambient light is a linear interpolation of the 2 limits, as full daylight & full dark. Pressing the A button exits to Normal Mode.

6.8 Main Display Mode

The display tracks current time, HH.MM, with the central DP cycling at 1 Hz between 3L & 2R, so it appears to move back & forth.

If 12-hour time is selected, the DP at 1L (ie MS position) will light in PM.

If the alarm is active, the DP at 4R (LS position) will light steadily.

If the value stored is invalid, the display is EEEE.

6.9 Alarm Mode

Alarm is displayed in the same mode (12 or 24-hour) as the main display. In alarm mode, the central DPs do not light, instead the rightmost DP (4-right) lights steadily.

Alarm setting is as main setting.