Home ==> AVR-EN ==> Applications ==> Binary watch   Diese Seite in Deutsch:

# An alternative watch design: Binary watch ATmega8

## 1 BCD display as alternative to 7-segment displays

Already the first computers in the world, that where constructed with mechanical relais or electronic valves had lamps, with which the programmer could inspect in single steps, which bits were clear or set. So he could easily monitor, what the computer does and he held an overview on each of the bits bouncing.

The concept of this watch goes back in those times. The numbers displayed have to be added together and are not readily served as numbers on a 7 segment display. The adding-up of binary 1s, 2s, 4s and 8s keeps your brain running.

That is what the watch looks like at 24:00 hours:

All lamps of the watch are off. It is dark, but only for one short second.

Now lets see a different time. It is 23 hours, 59 minutes and 47 seconds.

Now the numbers to the left, which are all powers of two, come into play. The left-most decimal is a two, so the 2-lamp is on. The second decimal is five, so the 4-lamp and the 1-lamp are on, adding up to five. And so on for the 10-minutes, the minutes, the 10-seconds and the seconds.

Missing lamps, such as the 4- and 8-lamp of the 10s, are caused by our crazy date and time system. The time system is based on the ancient dozen (12) and the five-fold of it (60, in German: the Schock, I can't find an english term for that, seems that this has timed out in the meantime). These ancient number systems have survived in our time system (and in the british coin system), even though completely unnecessary. That is what makes computer programming nowadays so complicated.

Now let us compare this display system with the usual Seven-segment-display system. To display the time with the above resolution we need six of those 7-segment displays, which incorporate 42 LEDs (in fact 48 with the decimal point that we do not need here). The binary watch needs only 20 LEDs, half of the usual display system. Resources and current consumption is halved. A very strong argument in those days.

Computer scientists call our six digits BCD. That stands for Binary Coded Digit. We will see in the chapter Software that this is excellent food for microcontrollers, and that he is used to handle this food excellently.

## 2 The controller

### Why a controller?

First we look at the classic CMOS type of Xtal controlled 7-segment watches:

That means a set of 12 integrated circuits with 14 or 16 pins each (roughly 160 pins in total), 42 carbon resistors and six 7-segment displays with 42 LEDs. These components fill a complete standard PCB (160-by-100 mm). Plus approximately 350 conductive paths. A real CMOS grave, high costs and complicated.

This all (except for the LEDs and the resistors) fits into one single microcontroller with 28 pins (less than one tenth of the 12 ICs) and just needs a small piece of software (to activate the internal timer) and some fuse settings inside the controller (to activate the built-in Xtal oscillator).

### Controller selection

The modern microcontroller nerd now says: "Oh yes, use an Arduino for that!" Just because he is familiar with only this, knows nothing else than this and feels weird if you offer another microcontroller than an ATmega324P. "Isn't that ATmega8 too old for new designs?" is one of the most relevant arguments to join his own addictedness. He starts immediately his C-coding and realizes that his mighty library does not support BCD bit nibbling. Until he realizes that the controller internally knows BCD very well, he has performed many hours of intensive research and blog activities. From now on he is doing some inline assembler to fit his mighty powerful library to simple and straight-forward BCD math.

The other, non-addicted persons, now run the Windows64- or Linux64- AVR selector.

In the section "Clock src" we choose a Xtal oscillator and in the section "I/O" 20 I/O pins for the LEDs.

That's what we get: several controllers can handle that (and: yes, the ATmega324 is among those).

We select the second smallest in the list, readily available in every good electronics store. It does not matter which letters occur behind that type (none, A, or whatever), they all are usable for that purpose.

### Schematic

That is the schematic for the binary watch. Rather simple and deliverable, and fits all our needs.

Instead of the Xtal with 2.4576 MHz nearly any other type can be used that is divisable by 1024 without any remainder and that can be divided by an integer of up to 65536 to yield one. The Xtal is attached to the respective input pins of the ATmega8, with two ceramic capacitors of 22 pF added to ease nice swinging.

The six BCD digits are attached to ports D, B and C. In a manner that eases internal controller math. Only portbit PD7 overlaps slightly, which can be handled with some left- and rigth-shifting of a bit.

To program the flash of the ATmega8 within the system, an ISP6 plug has been added.

In this scheme the LEDs are driven in sink mode. With an operating voltage of 5 V the LED current is
ILED = (5 - 2 - 0.2) / 270 * 1000 = 10.4 mA

If all 20 LEDs would be on, the current would be 207 mA. The ATmega8 is specified with a max of 300 mA via the GND pin, so anything is fine.

To ease mounting a 20-pin plug has been added that connects the LEDs to the controller PCB. The pin-out of this plug eases LED soldering of the flat cable.

The cathodes of the 8- and 4-LEDs are connected to pin 1 to 8 of the plug, the 2- and 1-LEDs with pins 9 to 20. All anodes are connected and are equipped with an extra cable that leads directly to the power supply.

## 3 Power supply

### Dimensioning

As has been described above we need slightly more than 200 mA for all LEDs. To get real average values, the whole day was simulated in a spread sheet for all 86,399 seconds of a day and the number of LEDs was calculated. At maximum 14 LEDs are on (e.g. on 17:37:37 and on 17:57:37). The following distribution occurs:

On average 7.3 LEDs are on, the maximum over one minute is 11.7. This corresponds to 76 mA resp. 122 mA over one minute. This is much less than the unrealistic 20-LEDs-on case.

In the samalgundi box of previously failed electronic projects I found a 2*7.5V transformer with 3.6VA. To test if this would be sufficient, the software provided here was used to simulate the voltages.

At the maximum consumption of 200 mA the voltages are fine to drive a 7805 regulator. The more than 10 V DC that the power supply delivers, with a ripple of 0.5 V, are fine. The heat power that the regulator produces is at maximum 1.68 W, so a small heat sink is sufficient.

### Scheme power supply

This is the schematic of the power supply.

Those components fit to a small bread board and can be fitted into the final box.

### Power supply load test

To make sure that the power supply survives even dramatic conditions (e.g. heat periods) I tested it for several hours with 300 mA load (two resistors with 33 Ω-2 W parallel). The transformer and the heat sink get rather hot under these conditions, but not too hot.

Above 400 mA the ripple on the regulated 5 V gets unbearable, the 480 mA that the transformer delivers, are unusable with a 7805 regulator. Instead a 9 V transformer would do it. But that is all beyond the needs here.

## 4 Mounting the components

The components are mounted on bread boards that fit exactly into the plastic box.

The advantage of the 20 pin plug is that the two parts, controller and LEDs, can be placed separately.

The LED part on the top of the box looks like this:

Soldering the LEDs to the flat cable is simplified by the plug placement of the pins. My first version had two wiring errors.

## 5 The Software

The software is in asm format here, in HTML format here.

### Program structure

The software performs the following steps:
1. Init of the hardware,
2. Timer, interrupts and sleep mode,
3. Second interrupt and time, conversion to display format
4. Switch the Xtal oscillator on with a fuse.

### Init

Because the program uses interrupt mode, the stack has to be initiated. Because the ATmega8 has more than 256 bytes SRAM, the high- and low-byte of the stackpointer have to be set.

All portpins have to be defined as outputs, for which their direction bit has to be set. All ports are set to one to avoid the 20-LEDs-on case.

### Timer, interrupts and sleep mode

The timer works with a prescaler of 1,024 and in CTC mode (clearing the timer on Compare-Match A). The compare match values A are derived from the Xtal frequency, so automatically are accomodated to changes of the clock frequency.

The Xtal can be of a different frequency, as listed in the following table. Please note that Xtals with more than 10 MHz are unsuitable for the V/L versions of the ATmega8 or for operating voltages below 5 V.
< 5 MHz5 – 10 MHz> 10 MHz
2.0485.068810.24
2.0971525.1211.0592
2.45766.14412.288
3.0726.414.7456
3.27686.553616
3.932167.372818.432
4.0967.6819.6608
4.1943048.388608
4.91529.216
9.8304

If a Xtal other than 2.4576 MHz shall be used, change the constant clock accordingly.

Whatever Xtal used, the fuse has to be changed in any case (see below).

After starting the timer with writing port TCCR1B the compare match interrupt has to be enabled. To stop all controller activities during the period of timer counting, the sleep mode idle is enabled. In this mode, the interrupt wakes up the CPU, executes the interrupt service routine and is sent back to sleep again. Of course, the execution of interrupts has to be enabled by setting the I flag in the status port.

### Second interrupt and time

Because no other interrupt has to be executed and because nothing else than jump and sleep instructions have to be executed outside the interrupt service routine, the whole action is focussed on this routine.

Two versions to do this are incorporated in the source code:
1. The time is encoded as packed BCD, with the lower nibble being the ones, the higher nibble the tens.
2. The time is encoded as a binary, with values between 0 and 59 (minutes and seconds) or 0 and 23 (hours).
If the switch pbcd is one, version 1 is assembled. If not, the binary version 2 is assembled.

The time is held in the registers rSec, rMin and rHour. In every interrupt the seconds are increased.

In case of version 1 (packed BCD) six has to be added to the register to recognize if the lower nibble is larger than nine. In that case, the H (half overflow) flag is set and the upper nibble is correct after adding. If not, the six has to be subtracted again, because the lower nibble did not exceed nine and is correct without adding six.

To check, if 60 or 24 has been reached, is done by comparing with the packed BCD representation of those numbers (which are 0x60 resp. 0x24 in hex format. If the register reaches those values (carry is not set after comparison) the register is cleared and the next register (rMin, rHour) is increased.

Conversion to the output format is rather simple in that case because the numbers in the registers are directly BCD format and fit, after inverting all bits, to the LED port outputs.

In binary mode the registers are increased until their content reaches binary 60 or 24. In that case they are cleared and the next higher register (rMin, rHour) is increased. So increasing the time in binary is a little bit simpler, but the conversion of the binary to the BCD output format to drive the LEDs is a little bit more complicated.

In that case we have to convert the binary to a packed BCD. This is done by first dividing the binary by ten (by simply subtracting 10 until a carry occurs). The number of subtractions is the upper nibble of the packed BCD, the remainder of the division is the lower nibble. In an example:

```   Binary: 49d or 0x31 hex
Division by 10: 4 times (49 - 10 - 10 - 10 - 10, remainder 1)
Upper nibble = 4, SWAP moves 4 in the lower nibble to the upper nibble
Lower nibble = 1
OR Upper nibble and lower nibble combines the BCD, result = 0x41
```
In both cases the result registers have to be inverted because the LED is on if the portbit is low. This is done with the instruction COM.

### Switch the Xtal oscillator on

To switch the xtal oscillator on, the respective fuse in the ATmega8 has to be altered, as shown in the picture.

Either choose a low (less than 1 MHz), a medium (1 to 10 MHz) or a high frequency (larger than 10 MHz).

## 6 The user handbook

If you decide to give away the binary watch as a gift to someone else, you need a user manual. I designed such a manual in OpenOffice format here and converted it to a PDF here.