Path: Home => AVR-EN => Applications => RGB BCD watch m16 => Software
RGB display of time 15:39:13 AVR applications

RGB BCD watch with ATmega16
Software for the BCD watch

The software of the RGB BCD watch with ATmega16

This describes the software fot the BCD watch as provided as source code here and listed for browsers here.

Page top Diagnosis Time ADC Multiplexing Conversion AD complete

1 Hardware diagnosis

The software as provided has built-in functions that can assist hardware debugging. Those are switched on in the header lines of the assembler source code named Debugging switches. You can invoke those functions by defining those switches with setting the constants to Yes. Note that it does not make much sense to set more than one switch Yes as those, in most cases, exclude each other. Following any changes, re-assemble and transfer the hex code to the flash memory of the controller.
  1. "cAccel": This factor accelerates the watch by the given factor, so the functioning of the watch can be viewed in high speed. With N = 8 the eight-fold speed, with N = 100 one hundred-fold speed can be tested, with N = 144 the whole day lasts 10 minutes only.
  2. "dbg_leds": With this function the LEDs are, one by one, switched on in the following row: for one second each. With that you can identify errors in wiring the LEDs.
  3. "dbg_adc": The measured ADC value is displayed as hour (between 0 and 23). This shows if the potentiometer works fine and advances in the right direction.
  4. "dbg_key": If keys are pressed, the red LEDs of the hour tens (key 1) resp. the hour twenties (key 2) are on.

    Key 1 is pressed Key 2 is pressed If none of the keys are pressed, the LEDs remain dark.
  5. "dbg_moni_xxx": The content of the register xxx is displayed on the second's LEDs.
  6. "dbg_colbal": In a very fast sequence all LEDs, one by one, are switched to white (blue+green+red on). This can be used to check the color balance of the LEDs.

    All LEDs white The LEDs shall all be white. If not: wiring error or lousy quality.
For the final version all debug switches have to be set to No and cAccel=1. Re-assemble and transfer to flash.

Page top Diagnosis Time ADC Multiplexing Conversion AD complete

2 Time control

Timing works as follows. The crystal-controlled clock signal of 4 MHz is divided in Timer/Counter 1 by 16,000 (prescaler = 1, Compare A = 15,999). This results in a frequency of 250 Hz for initiating the Compare-Match-A-Interrupt. The register rDivSec counts from 250 down. If it reaches Zero, the flag bSec is set. Its further handling is outside the interrupt service routine.

With each Compare-Match-A-Interrupt the hours- minutes- and seconds bits of the LED cathodes are written to the multiplex ports, and the respective anode driver is switched on. If the watch is in time adjustment mode, the current adjust is displayed instead of the current time, and the currently active display blinks in second rhythm with an adjustable duration (constant BlinkPeriod).

Outside the service routine, the flag bSec advances the time by one second. The time is handled in binary format in the registers rHour, rMinute und rSecond. More theoretical stuff on handling date and time on AVRs can be found here.

The values for the three cathode ports and for the active anode port are stored in an SRAM buffer from sRgb on. The display is done by the TC1-OCRA-Int by using the register pair YH:YL as pointer. If the end of the buffer is reached, it restarts again from the beginning. Conversion of the binary time to LED cathode bits is shown here.

Page top Diagnosis Time ADC Multiplexing Conversion AD complete

3 AD conversion as additional clock source

Independant from this timer operation the measuring of the potentiometer value is constantly performed by the ADC. The conversion is clocked by 4 MHz / 32 = 125 kHz. Each conversion needs 13 ADC clock cycles. 64 of the measured values are summed in the register pair rAdcH:rAdcL. Then the MSB of the sum is copied to the register rAdcRes and the flag bAdc is set. Further handling of the flag is outside the ADC-Complete-Interrupt. The clocking of the ADC leads to a flag frequency of 4,000,000 / 32 / 13 / 64 = 150.24 Hz or 6.656 ms.

This clock is less accurate the the TC1-CTC because in between the ADC-Complete-Int and the restart of the AD converter some delay is introduced by instructions.

The flag bAdc adjusts the display brightness or, in case that time adjustment is active, the display of the adjusted digit. More on this is in the chapter on key adjustment here.

Page top Diagnosis Time ADC Multiplexing Conversion AD complete

4 Multiplexing

Multiplex buffer in SRAM In order to ensure that the multiplex clock always finds the current RGB combinations to be displayed, those are stored in SRAM in a 12 byte long buffer. In the series hours - minutes - seconds the three cathode bytes (Zero: cathode active, One: cathode off) for the ports A, C and D are stored. The LEDs 0 to 3 refer to the ones (BCD encoded), the 4 to 6 (hours: 5) refer to the tens. Color association of the bits is in the order blue - green - red, just as the port bits are connected to the RGB LEDs.

The three cathode bytes are followed by an anode byte for port B. The three anode combinations for hours, minutes and seconds are inverted, too, so that an active anode needs to be zero. Those anode bytes are once generated during init and remain as they are during operation.

Page top Diagnosis Time ADC Multiplexing Conversion AD complete

5 Conversion of the binary time to multiplex bits

Color encoding This is the color encoding in the register rCol. The three lowest bits are one if the LED color is off (during display of a binary zero as digit, if the BCDE bit is zero or if the blink period is off).

The colors are associated to the binary representation of hours, minutes and seconds. In case of the hours, the levels 6, 12 and 18 change colors, in case of minutes andd seconds the levels 15, 30 and 45 change colors. All associated LEDs are of the same color: all six LEDs of the hours, all seven of the minutes and all seven of the seconds have the same color.

The necessary color is identified by comparing the time byte with the value in register rCmp. This is either set to 6 in case of hours or to 15 in case of minutes or seconds. Base color in rCol is blue (0b0000.0110 = 6). If the time byte is smaller than rCmp, rCol is used as color. If not, rCol is set to green and rCmp is doubled (12 resp. 30). By comparing the time byte with rCmp is identified if the green is the resulting color. If not, then rCol is set to the third color (red) and half of the doubled rCmp is added to rCmp (18 resp. 45). Again: if smaller this color is used, if not the fourth color is written to rCol (violet = red and blue).

After that rCmp is in any case set to 15 for further minute and second comparisons.

Conversion binary to BCD This shows the algorithm that is used to convert the time binaries in register rmp (either hours, minutes or seconds) to BCD encoded values in rBcd: continued subtraction of ten from the binary and adding the rest of the binary with OR. The following tricks are used:
  1. The result is set to minus 0x10, so that the first addition of ten leads to zero (in case the binary is smaller than ten.
  2. Addition of ten to rBcd by using SUBI rBcd,-0x10 as replacement for the missing ADDI instruction in AVR assembler.
  3. Then subtraction of ten from the binary in rmp. If following that instruction the carry bit is clear, then further adding 10 to rBcd and subtracting 10 from rmp is performed. If carry is set, the loop is terminated.
  4. By adding 10 to rmp (SUBI rmp,-10) the last subtraction is cancelled.
  5. By OR-ing the upper nibble in rBcd and the lower nibble in rmp, the result is generated and the conversion ends.

Conversion of the color bits to multiplex bits That demonstrates how the conversion of the color bits into multiplex bits works. A copy of the color bits in rCol is three times shifted right, bit by bit, to the carry flag and then rotated left into the three (previously cleared) bytes R0:R1:R2.

After having so shifted seven color triples into the registers R0:R1:R2 the three bytes have the correct color bits for the cathodes and can be copied to SRAM.

Conversion of the binary time to mux bits This is the complete algorithm of the conversion in Assembler. To be able to convert the time as well as the input time (under time adjustment mode) with the same routine, the address in Z either points to the time (in the three registers) or to the input time (in SRAM).

  1. Init starts with setting the pointers Z (to the time) and X (to the multiplex buffer) and setting rCmp to 6 (hours).
  2. The first step of the loop is reading a time byte from Z.
  3. By comparing it with the value in rCmp, 2*rCmp and 3*rCmp/4 the color value is determined and rCol is either set to cColor1, cColor2, cColor3 or cColor4.
  4. The time byte is re-read from Z and converted to BCD in register rBcd.
  5. Then the seven color triples are converted to R0:R1:R2 and those are written to SRAM. The anode byte is jumped over by adding 1 to X.
  6. If all time bytes have been converted, the routine ends.
The whole conversion routine requires 150 µs at 4 MHz clock and is shorter than one multiplex cycle or an ADC conversion cycle.

Page top Diagnosis Time ADC Multiplexing Conversion AD complete

6 Adjusting the time

If the flag bAdc is set, ca. 6.6 ms have elapsed.

Algorithm of the AD conversion handling This is the whole algorithm of the ADC flag routine. Caused by the various functions to be performed here, it is rather sophisticated.

Rather simple is the part if time adjustment is inactive: the MSB of the AD conversion sum (0..255) is multiplied with 250, the 16 bit result is divided by four (0..15,937) and is simply written to the Compare-B-Port of TC1. The compare match B switches the anode driver off before the mux cycle ends. The earlier that happens the darker are the LEDs (e.g. at zero: 1 / 16,000 = 0.006% brightness, 15,937 / 16,000 = 99.6% brightness).

Finally it is checked whether key 1 is pressed. If yes (input pin is low) time input mode is initiated. The flag bTSet is activated and the flags bTSetM, bTSetS and bTSetO are cleared. The MSB of the AD sum is multiplied by 24 (hours) and written to the time input byte for hours. Following that, the bounce protection register rBounce is set to its start value. This prevents from any further key events as long as this register is not counted down to zero.

Whenever another bAdc event happens, it finds bTSet is activated. In this case it is checked whether rBounce is at zero. If not, the keys 1 and 2 are consulted. If both are inactive (11), rBounce is counted down. If not, the debounce period restarts.

If the debouncing period has ended, the bit rTSetO is checked. This is set, if the last digit (seconds) has been finalized or if the back key 2 had been pressed during the hour input mode. In both cases it had to be ensured that the keys were not pressed any longer and had absolved their debounce period. After that the input mode is skipped by clearing the rTSet bit and by displaying the current time.

If in a bAdc event the bit bTSet was set, the debounce counter rBounce was at zero and bTSetO was cleared, key 1 is checked. If that is pressed (input pin low) a backward command has to be executed. Depending from the current input state, the following actions are executed: In any case the debounce counter rBounce is restarted.

If key 1 was not pressed, key 2 is checked. If pressed, a forward command is executed. Depending from the current input state the following happens: In all cases the rBounce counter is restarted.

If neither key 1 nor key 2 are pressed, the current input position (hours, minutes, seconds) is pointed to in X and, depending from that, the MSB of the ADC sum is either multiplied by 24 or 60. If the current and the previous value differ, it is re-written, the cathode bits are caclulated and written to the display mux.

Not shown here is the algorithm for the time-out of the time adjustment. This uses the register pair rInpToH:rInpToL. This is decremented each time that no action occurred and is restarted by each key event. If it reaches zero, the input mode is switched off without setting the time. The time-out can be at maximum set to seven minutes (constant InputTimeOutMinutes).

Page top Diagnosis Time ADC Multiplexing Conversion AD complete

Praise, error reports, scolding and spam please via the comment page to me.

©2019 by