Egg-timer ATtiny24 Version 2 Hardware, Mounting, Application and Software for a 10 minute egg-timer
Properties of this egg-timer
This timer has the following properties:
Simple design, costs below 10 Euro.
Blinks on current and on next LED (shows more than minutes only).
Optimized for very low current and for battery operation.
Displays the full minute and the next running minute.
Melody play on each full minute via speaker, 10 different melodies.
The schematic and other graphs are available in the LibreOffice Draw file
here, the calculation sheets are
here for download.
Hardware
This is the complete schematic of the egg-timer: one micro-controller, two
resistors, ten LEDs, a capacitor, an electrolytical capacitor, two batteries
with a switch, ohne small speaker and an angled 6-pin box connector for
programming the device.
As the software switches only one LED on, it is not necessary to connect
ten resistors: one controls them all.
The device works with an operating voltage from 2.4 V (with low-current
LEDs) on and with all types of LEDs with higher voltages. The displayed
picture shows a calculation sheet to estimate the resistor for a desired
LED current. It stems from the sheet "resistor" in the
LibreOffice Calc file here. The
sheet allows to calculate the E12 resistor for all given circumstances.
The sheet estimates the LED's forward voltage (for standard-LEDs and
for low-current-LEDs) as well as the voltage drop on the active output
pins. Both are estimates, those who want to know more details have to
measure those.
With the given dimensions and with 2 AA batteries the current per LED
is between zero and 5 mA, two alternating LEDs need roughly 5 mA,
and an active sound on the speaker consumes up to 19 mA (with an
8Ω speaker).
Mounting
This is the drill plan for the cover in Original size (40-by-50 mm).
The holes for the LEDs are 3.2 mm in diameter, all others are 2.5 mm.
The squared hole for the switch needs 1 mm holes on the four edges and
is sawn with a jigsaw. Because of the small space to the edge saw with care.
These are all the parts that are needed. With roughly 10 Euro you are
with it.
Those who want to place the speaker in between the oval round of the LEDs
need a speaker that is less than 30 mm in diameter. I have used an
BL 28 type, which has 8Ω only. It works fine.
Software
Download
The software is written in assembler. The source code can be downloaded
here. It is not necessary to change
any fuses.
Due to the large space needed for the melodies, the source code uses
nearly the complete flash memory space of the ATtiny24. Those who want
to expand the melodies have to change to an ATtiny44 or 84 with larger
flash space.
Controlling the LEDs
The control of the LEDs have been optimized a lot, the previous versions
were less elegant.
Switching the minute LEDs is done with the register pairs rBitB:rBitA
and rNxtB:rNxtA, which are written to the two ports B and A. Those
alternate between the LED of the current minute (rBitA/B) and of
next following minute (rNxtA/B). On reaching a full minute the
bits in rNxtA/B are copied to rBitA/B, after that rNxtA/B
is shifted by one bit to the left.
The timing of the LED is performed with the 16-Bit-TC1. It works in
CTC mode, with the compare value in the compare registers A.
Each minute is divided into 256 PWM stages in register rPwm.
Those are repeated 256 times and those PWM rounds are counted in register
rPwmCnt. This requires a delay, so that 256 * 256 reaches
60 seconds:
tdly = 60 * 1,000,000 / 256 / 256 = 915.5 µs
This 916 µs delay is the task of TC1 in CTC mode.
The complete LED control is within the Interrupt-Service-Routine of the
OCIE1A of TC1. On each full minute, when rPwm as well as rPwmCnt
reach zero, a new minute cycle starts. The LED, that represents this
minute, is switched on. She remains on over the whole following PWM cycle
(256 PWM cycles.
When rPwmCnt is larger than zero, the PWM cycle starts with the
next LED on. When rPwm reaches rPwmCnt, the previous LED
is switched to. When rPwnCnt = 1, this results in a next cycle
duration of 0.92 ms, when it is 2 a 1.83 ms long on-cycle,
etc. The duration of the next LED increaeses, while the duration of the
actual LED decreases.
The program uses the following algorithm:
On each full minute both registers, rNxtA and rNxtB,
are shifted by one bit to the left. Both registers have a single
one, which is shifted to the left, and which, on OUT instructions
switch this single LED on.
Prior to shifting, the content of rNxtA/B is copied to the
register pair rBitA/B. This shifts the current LED one position
to the left.
On full minutes the cycle starts with the bit in rBitA/B,
which remains on over the complete 255 PWM cycles.
Also, on full minutes, the next melody is started (see below for
details on this).
If the next PWM cycle starts with zero, the LED in rNxtA/B
is switched on. This remains on until the PWM counter rPwm
reaches the value of the PWM counter rPwmCnt. If that is
the case, the bit in rBitA/B is written to the ports.
If the LED L10 is fully on, the control enters the overflow stage.
In this phase the speaker pin is toggled with a frequency of
1,000,000 / 916 / 2 = 546 Hz.
Additionally the LED L10is toggled with a frequency of
1,000,000 / 916 / 256 / 2 =
2.13 Hz.
After start-up, only LED L1 is active: by rNxtA/B at one, because
rLedA/B still is at zero. Within the first PWM cycle, all LEDs are
off.
After 1,000,000 / 916 / 256 = 0.234 seconds
the first PWM cycle is finished and rPwm gets zero. That increases
rPwmCnt to one and the cycle starts by setting the LED-outputs with
rNxtA/B, which switches the LED L1 on.
On all further PWM stages rPwm is compared with rPwmCnt.
When equal, rBitA/B is written to the output pins, which turns the
LED L1 off (as rBitA/B still is zero).
With each additional PWM cycle rPwmCnt increases, which prolongs
the aktive Phase of the next LED and decreases the off-time.
After 60 seconds rPwmCnt reaches zero again and the next minute
starts.
In the left picture the LED control after the first minute can be seen, the
right picture shows the algorithm of the ISR.
With the first PWM pulse, when rPwm is zero while rPwmCnt is
not zero, the LED output is set to rNxtA/B. That switches L2 on and
L1 off. After rPwm reaches rPwmCnt, the LED output of
rBitA/B switches L1 on and L2 off, which continues until the end of
the PWM cycle.
In the flow diagram to the right can be seen the initial check, whether the
time has reached the 10-minute-limit yet. If that is the case, the speaker
output is toggled with a frequency of 1,000,000 / 916 / 2 = 545.9 Hz,
somewhere in between C'' and D''. If the increased
register rPwm also reaches zero, the output L10 is also toggled
with a frequency of 1,000,000 / 916 / 256 / 2 = 2.13 Hz. That should
be signal enough to urge the user to switch off the operating voltage.
Ablauf der Musiksteuerung
At start-up and on every full minute a melody is played. The melodies are tables
in the flash memory, from Melody0: to Melody10:. The addresses,
where those melodies start, are doubled (for LPM) and are listed in the table
Melodies:.
Playing the notes is performed with the 8-Bit-Timer/Counter TC0 on its OC0A
output pin. This pin is set to toggle on compare match (to clear when pauses
are made).
The melodies are composed with music notes, which are calculated in the
spread sheet notes in the LibreOffice-Calc file
eggtimer_tn24_v2.ods. All notes are listed
with their frequencies in Hz, their names are given in column 1 (notes below
C have an attached m). Notes above H and below C have their octave as
digit attached.
The note ps is a still pause: the speaker output is cleared for a certain
selected time.
For each of the 70 music notes are given:
the divider value for TC0,
the optimal prescaler value,
the resulting CTC divider value minus 1 (CTC compare match value), and
the duration of the note for one second as OC0A toggle events.
From that the spread sheet generates two tables for export to the assembler
source code:
the note names get a number between 0 and 70, all note names start with an
n, the followed by the tone C to H, then the octave (if not zero) and,
for notes below C, an additional m,
the NoteTable consist for each note of
in the first byte the CTC compare match value for TC0,
in the second byte the prescaler value for the TC0,
in the third and fourth byte the number of cycles for a duration of
one second (which is twice the frequency of the note).
The table can do additional things:
With another clock frequency of the controller the notes for this frequency
can be calculated. Clock frequencies up to 8 MHz are tested and function
well.
If the music shall use the 16-bit-TC1, just change the TC number. The
16-bit-TC plays all notes with a prescaler of one. In that case, the first
two bytes of all notes in the table are the 16-bit-CTC values.
Some AVRs have an additional TC2, which is an 8-bit TC, but with additional
precaler values. A 2 in the TC field uses these addional precaler values.
The melodies look like this:
; Voel- ker hoert die Sig- na- le!
.db ne1,6,nd1,4,nps,2,nc1,8,nps,2,ng,6,nps,2,ne,2,nps,2,na,8,nps,2,nf,4,nps,4
First, the note E' (329 Hz) is played for a duration of 6/16 of a
second (= 0.375 s), then the D' (293 Hz) is played for a
duration of 4/16 seconds. It follows a pause with a duration of
2/16 seconds. After that a G with 195 Hz, an E with 166 Hz, an A
with 217 Hz and an F with 174 Hz is played, each for different lengthes
and separated with pauses of different lengthes.
Each melody ends with nEnd as note (0xFF). This switches the speaker output
pin low and disables the TC0-Timer-Interrupts.
In the program
the register pair X (XH:XL) points to the melodies table, each play increases
this address by two,
the register pair Y (YH:YL) points to the next note to be played as part of
the melody, each note played increases this by four.
A special method has been used to calculate the duration:
The duration of the note (1..255) from the melody table is read into register
rmp2. This is the duration as 1/16th of a second.
The duration for one second length is read from the notes table to the register
pair rLenH:rLenL.
The 16-bit down counter in rDurH:rDurL is cleared. This counter finally
determines the number of CTC events and the duration of the tone.
On start the register rmp is set to 16 (= 16/16th = 1 second), which is
the duration of the value in rDurH:rDurL.
If rmp2 is larger or equal rmp, rLenH:rLenL is added to
(the empty) rDurH:rDurL, and rmp2 is subtracted by rmp.
If that results in a zero, the value in rDurH:rDurL is correct. If the
subtraction does not lead to a carry the step is repeated.
If the subtraction did not lead to zero or if the comparison lead to a carry,
the value in rLenH:rLenL is shifted to the right. If after shifting a
carry rolls out the halfed value is increased by one (rounding up). The value
in rmp also is shifted right, so that the values 8, 4, 2 and 1 are
resulting. If zero results, the value is correct. If not, the whole steps are
repeated.
The duration byte can, with values above 16, prolong a tone or a pause to many#
seconds. For notes up to H''' the length byte can be up to 255, which
results in 16 seconds duration. With higher tones this does not work any
more, the length byte has to be limited. With very high frequencies the limit
is at 16 (for one second). For maximum values of the length byte consult the
note table in the spread sheet.