Path: Home => AVR-EN => Applications => Stop watches with AVRs => Stopwatch ATtiny24
Stopwatch_tn24 AVR Applications

Stopwatch with ATtiny24 in Assembler
Logo

Stopwatch with an ATtiny24

Display and design layout for ATtiny24 stopwatch Here a stopwatch with the following properties is described: These pages as PDF file (12 pages, 500 kB).

1 Hardware

Schematic Stopwatch tn24 As the hardware is based on the existing standard PCB tn24_lcd the additional four keys are simply attached to the six-pin bread connector.

Power supply for that all comes either from three 1.5 V batteries or from four rechargeable batteries of 1.2 V. That allows to work with a 5 V standard LCD.

2 Software structure

The software can be programmed in two versions:
  1. a 10 ms version: Time measurements are based on a 10 ms cycle, with the time in four registers,
  2. a 1 ms version: Measurement in a 1 ms cycle, with the centiseconds in one register (from 0 to 99) and the milliseconds (from 0 to 9) in an additional register.
The version described here is the second version with milliseconds, even though the resolution of 1 ms is a little bit inappropriate, given the large inaccuracy of the internal RC oscillator.

The following properties determine the software design:

2.1 Clearing the SRAM storage place

Clearing the SRAM storage space At start-up and when resetting the stopwatch the whole storage area in SRAM has to be cleared, which means that zeros have to be written there. As this part is executed two times, it is formulated as a subroutine, so it can be called from different addresses.

The routine uses the register pair ZH:ZL as pointer to SRAM. It is set to the beginning of the storage space.

Clearing is done in a loop that writes, with the AVR instruction st Z+,R16 the content of the register (zero) to the storage byte addressed in register pair ZH:ZL and automically increases the address in Z by one. If the LSB of the Z pointer reaches the end of the SRAM area to be cleared, the routine stops.

2.2 Shifting values in the storage space

In case the Store key has been pressed the
  1. current time, located in five registers, has to be copied into the SRAM,
  2. all stored times have to be shifted by five bytes to upper addresses, by that overwriting the last five bytes (done with the subroutine Shift), and
  3. those shifted times have to be displayed in the lines 2 to 4 on the LCD

2.2.1 Shifting of time information

Shifting of time information Here is the storage space in SRAM with all addresses in hexadecimal format.

Shifting has to start from the end of the storage space (by overwriting the last time stored) and has to move backwards to lower addresses. The byte transfer is from a source address (at the five byte lower address) to a target address. So two pointers are required that differ by five.

Flow and source code in assembler for shifting The two pointers are XH:XL or X for the source address and ZH:ZL or Z for the target address.

The flow utilizes a specialty of the AVR instruction set: with LD R16,-X the pointer X is first decremented and after that copies the content of the SRAM at this already decremented address to the register. Similarly ST -Z,R16 first decrements the address in Z and after that writes the register's content to the already decremented location address in SRAM. This makes it easy to read and write backwards, but you'll have to be aware that both pointers, at the beginning, have to point one position higher than were they first transfer from/to.

This operation has been programmed as a subroutine, too, even though it is performed only once (when the Store key is pressed). This eases simulation and debugging.

2.2.2 Displaying the shifted values

Display lines on LCD When displaying the three lines on LCD the following goes on:
  1. The line counter ZH starts with 1 (on line 2) and is increased for each displayed line (if it reaches four, the display is done), as this register is altered by other called subroutines it is pushed to the stack and later restored (not shown here). The column address ZL of the LCD points to the tens of the hour to be displayed. With that positioning is done for each line to be displayed.
  2. The SRAM address of the time to be displayed is held in X. Reading one byte of information increases this address (LD rmp,X+ with subsequent auto-incrementation) and therefore, after five bytes have been read, is already on the address of the next line's time information.
  3. Displaying the time advances from the hour to the minutes, then to seconds, centiseconds and milliseconds. Except for the last, a routine named Bin2Dec2 is used to convert the byte to a 2-digit decimal and display those on the LCD. In between hours and minutes and between minutes and seconds a colon is added. Following the seconds a decimal point is added.

3 Software

The software has been written by Jochen Girschik (translation by me) and is completely in assembler. The source code can be viewed in browser format here and downloaded from here in asm format. Assembling requires that the modified LCD include file is stored within the same path.

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

To the top of that page

©2018 by http://www.avr-asm-tutorial.net