Applications => Longtimer tn13
Diese Seite in Deutsch:
Long timer with ATtiny13
Hardware, Mounting and Software for a long timer
Long timer with ATtiny13
You are a smoker and you want to control your smoking habit a little further?
Here's the application for you. Without any jumpers this device starts with a
red LED fully on, then within 25 minutes decreases its brightness, then turns
to green and increases its brightness within five minutes. Then the interval
restarts again. If you want to reduce your consumption from 32 down to 16
zigarettes per day: mount jumper 1 and the time increases to one hour. Or
jumper 2 for one and a half hour. Or jumper 3 ...
Of course this can be used by non-smokers as well. If you need to do something
every hour and you do not want to restart an alarm clock for that. This here
restarts automatically, so you only have to look at the LED from time to time.
This works as follows:
- First mount none, one, two or three jumpers (or use a dip switch device)
to select the time-base: without jumper these are 30 minutes, with jumper 1
an hour, with jumper 2 on one-and-a-half-hour, with jumpers 1 and 2 on two
hours, with jumper 3 on two-and-a-half-hour, with jumpers 3 and 1 three hours,
with jumpers 3 and 2 on three-and-a-half or with all three jumpers on four
- By switching the power supply of the device on, the jumper's are read
and the LED turns to fully red. Within the selected time the LED's red
intensity decreases down to zero.
- For the last five minutes of the selected time interval the LED turns green
with increasing intensity.
- Then the LED switches from fully green to fully red, re-reads the jumpers
and re-starts the interval.
1 The schematic of the timer hardware
This is the simple schematic of the timer. Here
the schematic can be downloaded as LibreOffice Graph file. It consists of the
The whole timing and switching is done by the ATtiny13.
- an ATtiny13 (oder A, egal), programmed,
- a two-pin-duo-LED with a resistor R,
- the triple jumpers Sw,
- a reset resistor of 10 kΩ,
- the ISP interface with a 6-pin-standard-plug for programming, and
- a supply voltage source between 2.4 and 5.0 Volt.
The value of the operating voltage determines the resistor value of R. At
low voltages the resistor is critical, at higher it is not. The formula is
in given the LibreOffice-Calc-Sheet here.
The voltage drop of the LED is around 2.0 V. Further 0.22 V are
resulting from the output drivers of the ATtiny13. When supplying with two
1.2V-rechargeable batteries the resistor gets very small. You can instead
use three rechargeable (or even four). If you want to supply the device with
AA or AAA batteries use either two or three of those. Also, a thick Lithium
rechargeable battery with 3.7 V or a power supply of 3.3 or 5.0 V
can be used.
If you later on change the supply voltage to the next higher category, only
the 2.4V version delivers too much current. Add an additional resistor before
Altogether fits onto a very small PCB of a size of e.g. 20-by-25 mm.
Those who want to use SMDs instead can even solder that on a smaller PCB.
3 The software of the long timer
The software for the ATtiny13 is written in assembly language, the source code
in assembly format is here and for
the Internet browser in HTML format is here.
For those who want to change the properties of the device can change the source
code from line 64 downwards:
Please keep cTimeBase always larger than cTimeGreen. Make slso sure
that with long times the prescaler is at 64, on shorter times a smaller prescaler
can be used.
.equ clock = 1200000 ; Clock rate default in Hz
.equ cTimeBase = 30 ; Time slot in minutes
.equ cTimeGrn = 5 ; Time for green phase in minutes
.equ cPresc = 64 ; Prescaler, can be 1, 8 or 64
After assembling the machine code can be transferred to the Flash Memory of the
ATtiny13 via the 6-pin interface. When programming remove jumper 1 as it blocks
programming pulses. Fuses of the ATtiny13 must not be changed, the device runs
with its default clock rate.
3.1 The timing of the interval timer
The timer works with the 8-bit-timer TC0 in the ATtiny13. This is operated in
8-bit-PWM-mode to control the intensity of the LED.
With selecting the prescaler of TC0 with the constant cPresc in the header
of the source code the clocking of the PWM is adjusted as follows:
|Clock (Hz)||Prescaler||PWM-Stages||PWM-Frequency (Hz)||PWM-Time (ms)||Intensity steps||Run time (s)|
To achieve longer times, the PWM cycles are repeated with a 16 bit value. This
is held in rDelayH:rDelayL and counted down in rCntH:rCntL. This
repeats the PWM cycle between one and 65,636 times. The following maximum
times can be achieved:
|Prescaler||PWM-freq. (Hz)||256 Intensities||Maximum time|
|64||73.24||3.494 s||63.6 hours|
|8||585.94||0.437 s||7.96 hours|
|1||4,687.5||0.054,5 s||59.53 minutes|
Even higher prescaler values are not recommendable, because this also changes the
PWM frequency and PWM switching gets visible. 256 would be possible with some
flickering, but at 1,024 the blinking is visible. But if you like that, you can
simply change the source code.
The interval consists of two different run cycles:
The duration of the red and green phase can be converted with the formula
- The red phase: this is the longer one and determines the whole duration.
Depending from the three jumpers this can either last 25, 55, 85, etc.
- The green phase: this is always the same, by default 5 minutes.
rDelayH:rDelayL = t (min) * 60 (sec/min) * Clock (1/s) / Prescaler / 65,536
to delay counts. At five minutes for green rDelayH:rDelayL is 85.8
or rounded up 86. For 25 minutes the five-fold results, 429.
By default (unchanged software) the following values result.
3.2 The LED control
Controlling the LED works as follows:
- Red phase: The green anode and the red cathode is low, the red LED is
switched on when the red anode pin OCR0B is high. To always increase
the compare value, no matter if the phase is red or green, the red
output pin is running in inverted mode: it switches low at the beginning
of the PWM cycle and high at compare match.
- Green phase: The red anode and the green cathode is low, the green
anode is switched high at the beginning of the PWM cycle and switched
low at compare match (non-inverted).
3.3 Main program init
In the initial phase of the program the followig is done:
- the stack pointer is initiated (the stack is needed for interrupts),
- the direction of the LED pins are switched to be output, the jumper pins
are input and their pull-up-resistors are switched on,
- the timer TC0 is brought to the mode Fast-PWM, its overflow
interrupts is enabled,
- the counter rCntH:rCntL is set to one, the PWM compare value in
rCmp to 255 and the T flag to zero. This provokes the
initiatialization of the device with the first interrupt of TC0 and starts
with the red phase,
- finally the sleep mode is set to idle and interrupts are enabled.
The complete handling is done in the TC0's ISR. The flow diagram is available
as LibreOffice Draw file here.
The ISR does the following:
The red phase is more complicated because at the beginning of the red phase the
three jumpers are read and converted to the longer phase duration (three 16-bit
addings). Please note that the jumpers are only read when the red phase starts.
Changes in between have no effect.
- the 16-bit-counter is counted down,
- if zero the next PWM value is written to both compare matches and the
counter is restarted with the delay value,
- if the PWM reaches zero, depending from the T flag either
- the red phase re-starts (T=0), or
- the green phase re-starts (T=1).
Most of the entries of the flow diagram are AVR assembler mnemonics, such as
SBIW (SuBtract Immediate Word) or INC (INCrease). Execution times
of all operations have been added in red, so that the number of consumed clock
cycles can be seen for all flow combinations. The maximum of consumed cycles
are 49, which is well below the 256 available cycles for a complete PWM cycle
(if the prescaler is at one). So, no interrupt request can get lost due to the
Because the complete handling is inside the ISR and the program loop only
re-starts the sleep, the status register SREG must not be saved and restored
within the ISR.
©2021 by http://www.avr-asm-tutorial.net