Digital timer based on atmega8 circuit. Kitchen timer on ATmega8

Digital timer based on atmega8 circuit. Kitchen timer on ATmega8

27.10.2023


A simple kitchen timer that can protect your kitchen from excessive smoke. An ideal gift for March 8th.
The timer allows you to set the time from 1 to 99 minutes, the countdown starts automatically, 3 seconds after setting.

Device diagram:

The basis of the device is an ATMega8 microcontroller with an external 16 MHz quartz. The Zas connector is used to power the device; it is supplied with a constant voltage of 7-15V. The 5V supply voltage is provided by the L7805 linear stabilizer; if you use a stabilized source of 4.5 - 5V, such as a mobile charger, then the stabilizer, C3 and C4 can be excluded.
Buzzer B1 and display anodes W1 are controlled by transistors T1 - T5. Resistors R9 - R16 are needed to limit the current flowing through the display segments. A programmer is connected to the Prog and R(reset) connectors, and a keyboard or buttons are connected to the Sw connector. There are only 5 buttons, four of them are responsible for setting the time (+1, +5, +10 and +20 minutes, respectively) and one for resetting the timer.

Printed circuit board:


List of components:

U1 ATmega8A-AU 1pc
U2 Linear stabilizer LM7805CT 1pc
T1-T5 Bipolar transistor BC556 5pcs
C1, C2 Ceramic capacitor 22 pF 2 pcs
C3 Electrolytic capacitor 100 uF 1 pc.
C4 Electrolytic capacitor 47 uF 1 piece
R1-R8, R17, R18 Resistor 3.3 kOhm 10 pcs
R9-R16 Resistor 330 Ohm 8pcs
W1 seven-segment indicator AF-05643FG-B (or with similar pinout) 1 pc.
B1 Buzzer with generator 1pc
X1 Quartz crystal 16 MHz 1pc

Photo of the finished device:


Let's look at how to make a timer with your own hands on the ATmega8 microcontroller, although the code is quite easy to adapt for AVR MKs of other series. An electronic timer is a necessary device in all areas where it is necessary to perform certain actions after a specific period of time.

The timer control consists of only four buttons:

— increasing the value of the number;

— decreasing the value of the number;

— start timer;

— timer reset.

An audio frequency generator with a speaker is used as an indicator of the timer operation. The generator will be started using transistor switch Q5, which in turn is opened by a positive potential coming from the PC2 port of the microcontroller.

Simplified, the timer works as follows. Use the “+” and “-” buttons to set the required number of seconds; The “start” button starts the timer. When the timer counts down to zero, a high potential will appear on the PC2 pin of the ATmega8 microcontroller, which will open Q5. Next, the transistor switch will start the generator and a sound will be heard in the speaker. The timer is reset by pressing the “reset” button. The audio frequency generator is assembled on two transistors Q6 and Q7 of different semiconductor structures. The operating principle and description of the circuit of such generators can be found by clicking on.

Timer operation algorithm on a microcontroller

Our timer will count down exactly one second at a time, although you can set any other time, for example minutes, hours, hundredths of seconds, etc.

To form a time interval of one second, we will use the first timer-counter of the ATmega8 microcontroller. We will define all its settings in the function start. First, we divide the operating frequency of the microcontroller 1000000 Hz by 64 and get a new frequency of 15625 Hz. Bits CS10, CS11 and CS12 of the TCCR1B register are responsible for this. Next, we enable the coincidence interrupt and write a binary number equal to the decimal 15625 into the comparison register (high and low) comparison registers.

void start (void)

TCCR1B &= ~(1<

TCCR1B |= (1<

TIMSK |= (1<

OCR1AH ​​= 0b00111101;

OCR1AL = 0b000001001; // comparison register 15625

TCNT1 = 0;

TCCR1B |= (1<

When the timer counts down exactly one second, an interrupt will be called. In the body of the interrupt function, we will decrease the value of the variable by one. When zero is reached, a high potential will appear on the second output of port C of the microcontroller, which will open the transistor switch and start the generator, as a result of which we will hear sound in the speaker.

ISR (TIMER1_COMPA_vect)

Z—;

This timer is designed to set shutter speeds from 5 seconds to 100 minutes. At its output there is a fairly powerful electromagnetic relay that allows you to switch current up to 30A at a voltage of 12V and current up to 10A at a voltage of 220V. Thanks to the use of an electromagnetic relay, the timer can control not only heating or lighting devices, but also electronic devices that are critical to the form of the AC supply voltage. Transformer power supply, in combination with a relay, provides complete galvanic isolation of the timer electronic circuit from the network.

To communicate between the timer and the operator, there is a four-digit LED indicator; it contains four very old 7-segment AL304 matrices, connected into a matrix by connecting together segment pins of the same name. Of course, you can use more modern LED indicators, and even ready-made four-digit matrices for dynamic display.

The timer is controlled by buttons S1, S2, S3, S4. When you press the S1 button, the load is turned on and the timer starts. To set the time during which the load should operate, you need to press S4. The two most significant digits (minutes) will flash on the display. Now you can use buttons S2 and S3 to set the minutes. Then you need to press S4 again. Now the least significant digits will flash and you can use the S2 and S3 buttons to set the seconds. To save the settings, press S4 again. Now the indicator will show the set shutter speed. To start the timer you need to press S1. The load is turned on, and the indicator readings begin to decrease. As soon as the set time has expired, “OFF” appears on the indicator, and the load is turned off by the electromagnetic relay. To repeat, press the S1 button twice. The first time you press “OFF” it will change to show the set time, and the second time the timer starts. Relay control via pin 23 D1. Switching on is a logical unit. The key on VT5 and VT6 controls the electromagnetic relay K1. Such relays are used in car alarm circuits. They can switch both direct current (12V) and alternating current (220V), since they have good insulation.

The power source is made using a low-power transformer. Since the secondary winding of the transformer is tapped from the middle (12-0-12), the rectifier is made not using a bridge, but using a full-wave circuit using two diodes VD2 and VD3. If the transformer has a 12V winding without a tap, then a rectifier bridge is needed. The relay is powered directly from the output of the rectifier, and the rest of the circuit through the 5V voltage regulator A1.

When flashing the firmware, you need to set it to work with an internal 8 MHz oscillator.

The circuit is assembled on a purchased prototype printed circuit board, on one side there is a microcircuit and other parts, and buttons and indicators on the other side. Power transformer outside the board.

KT315 transistors can be replaced with KT3102 or any analogues. The KT815 transistor can be replaced with KT817, KT604. Diode KD521 - almost any analogue. Diodes in the KD209 rectifier are any rectifier diodes for direct current not lower than 150 mA. The 7805 integrated stabilizer can be replaced with any 5-volt one, for example, KR142EN5A. Or make a stabilizer using a parametric circuit using two transistors and a 5V zener diode. Regarding indicators, it was said above. These can be any seven-segment indicators with a common anode (cathode).

Archive for the article "Timer on Atmega8 and LED indicators"
Description: Firmware files
File size: 5.58 KB Number of downloads: 4 319

In the previous article, I wrote that this is the latest development of such a device using seven-segment LED indicators, but it turned out that I was in a hurry. The fact is that this design uses only 40% of the microcontroller’s memory, and there is still one unused pin of the microcontroller port (except for the RESET pin). Therefore, it was decided to correct this injustice in relation to the MK and add another load control channel. After the work has been done, the MK memory is used by 99% and all the MK pins are used. Full name of the modified design:
“A two-channel thermometer, a two-channel thermostat (thermostat) with the ability to operate on time, a single-channel real-time timer on an ATmega8 microcontroller and DS18B20 temperature sensors”

Description and characteristics of a two-channel thermometer, thermostat (thermostat), single-channel real-time timer
on ATmega8 and DS18B20

Since this design “emerged” from the previous one - and is described in detail (all characteristics of thermometers and thermostats, operating modes, reactions to errors - remained unchanged), I will only focus on the innovation - the real-time timer.

Real time timer

Introduced into the design real time timer, which allows you to manage your third load in real time for 24 hours and allows you to set two time intervals for load control during the day. The timer also allows you to set one time interval for load control during the day for each temperature control (thermal control) channel.
What do I mean by real time timer. Essentially, this is an internal clock with a resolution of 10 minutes. During the initial setup of the device, the real current time is set with an accuracy of 10 minutes, and then the timer counts 24 hour intervals in 10-minute increments like an ordinary clock.

The discreteness of counting time intervals of 10 minutes was adopted for two reasons:
— convenience of displaying information on a three-digit indicator, for example 22 hours 40 minutes — 22.4
- load control with an accuracy of 10 minutes is quite sufficient for most tasks (in reality the accuracy is 5 minutes - if you need to turn on the load at 7 hours 35 minutes, you can set either 7.4 or 7.3)

The introduction of a timer slightly changed the algorithm for working with the device (I will talk about the algorithm of operation below). Now by pressing the “Select” button you can get to two menus:
— menu for setting temperature limits for thermostats and time intervals for thermostat operation, time intervals for load control using a timer
— menu for correcting the clock rate and setting the current time.
Since the MK operates from an internal RC oscillator (8 MHz), which is not stable and depends on both the temperature of the MK and the supply voltage, the clock rate correction function allows you to adjust the clock rate accuracy for specific conditions. And the current time setting function allows you to set the current real time during the initial setup or clarify it if it differs greatly from real time.
The timer indications are not displayed when the device is operating; you can find out “what time it is” only when you enter the mode for setting the current time.

Timer control of loads is not carried out (disabled) if the on and off times are set to zero. In principle, loads are not controlled by a timer if the on and off times are equal.

When you enter the clock correction menu and set the current time, the timer stops. Therefore, when correcting the clock rate, it is necessary to set the current time before exiting the menu.

Scheme of a three-channel thermometer, thermostat, timer on ATmega8

The device circuit was created in the program and, in principle, does not differ from the circuit of a two-channel thermostat (a third load control channel was added and, for variety, the load control circuits were changed):


Since the circuit uses “output” parts, for the convenience of placing the structure in a suitable housing, the circuit is divided into two parts:
— Display block — indicators and buttons
— Control unit — everything else
It would be necessary to include LEDs in the display block that signal that the channels are on, but this can be done independently when laying out the board (add three pairs of contact pads for the LEDs and connect them to the wire control unit).

Device design

Device base- ATmega8 microcontroller with a clock frequency of 8 MHz from a built-in oscillator with an internal RC circuit.
To adjust the frequency of the internal oscillator, when programming the MK, it is necessary to write the value of the calibration cell for a clock frequency of 8 MHz into the EEPROM memory at address zero. The default HEX file of the EEPROM memory below contains the number $В1 (В1) - the average value of the calibration cells of 5 tested microcontrollers.
In addition, for the correct operation of the real-time timer, and it works by interruptions from the T1 timer/counter when the counting register and the OCR1A comparison register are equal, when programming the EEPROM memory, following the value of the calibration cell, the number 33050 (1A81) is written, which is programmed into comparison register OCR1A. When the timer progress is corrected, the value of this number also changes.

Indication current temperatures and values ​​in the installation mode are displayed on two three-digit seven-segment indicators with a “common cathode” switching circuit.

Sensors DS18B20 are connected to the device via 3-pin pin strips DS1 and DS2, the numbering of the pins of which corresponds to the numbering of the sensor pins.

Bit management carried out by low-power NPN-type bipolar transistors.

Entering the menu, setting values, the launch of single heating (cooling) modes is carried out by three DTS-type tact buttons:
— S1 — “Selection”
— S2 — » + »
— S3 — » — »


— for temperature control channels — through optosimistors MOS3063 and triacs VT139-800E according to the standard switching circuit, which allows you to control loads with a power of up to 3.5 kW (if the load power is more than 300-400 W, triacs must be installed on radiators)
- for the channel from the timer - through a miniature mechanical relay with a coil supply voltage of 5 Volts, which allows, depending on the relay used, to control a load of up to 2 - 2.5 kW

I draw your attention to connecting the mains voltage of 220 volts to the device and turning on the load - it must be connected as in the diagram, taking into account the “phase” and “zero” of the mains voltage.

Device power carried out from any direct current source with a voltage of 7-25 Volts. The circuit can also be powered from an unnecessary charger from a cell phone with an output voltage of 5 +-0.5 Volts. In this case, it is possible to exclude the 7805 stabilizer and capacitors C4, C5 from the circuit. The average current consumption of the device is 40 milliamps.

If you need to organize backup power (for uninterrupted operation of the timer), you can use, for example, the following scheme:

Details used in the design:

Control of a three-channel thermometer, thermostat, thermostat, timer

1. Enter the menu

The device has two menus.
When you “shortly” press the “Select” button, the inscription “ON—-OFF” is displayed on the indicators, we enter the menu:
— setting temperature limits for thermostat operation and time intervals for thermostat operation, time intervals for load control using a timer
When you “long” press the “Select” button, the inscription “ON—-OFF” is replaced by the inscription “Cor—-USt”; you must release the button and enter the menu:
— correction of the clock rate and setting the current time

Please note that when entering the menu (long or short press of the “Select” button), all load control channels are disabled.

2. Menu “Corrections of progress and setting the current time” (long press the “Select” button)

After entering the menu, we immediately find ourselves in the clock correction mode:
"Cor—-00"
By pressing the “Select” button again, we switch to the mode for setting the current time:
"USt—-00.0"
In the current time setting mode, we look at our most accurate watch and use the “+” and “-” buttons to set the nearest time with an accuracy of 10 minutes.
For example, the current time is 20 hours 37 minutes, set the indicator to “20.4” (20 hours 40 minutes) and exactly at 20.40, by pressing the “Select” button we exit the menu. That's it, the real time is set, the clock is started.
You can adjust the clock rate from +50 units to -50 units. The initial value is “00” (“00” always appears when entering this mode)
Changing the setting by one increases the clock rate (+1) or decreases (-1) by approximately 4 seconds per 24 hours.
The accuracy of the clock can be checked on the load control channel using a timer without a connected load by lighting the LED.
For example, at 21.00 we set the current time, set the load to turn on at 8.50, and turn off at 9.00. In the morning we measured the load switch-off time. Let's say the load turned off at 8 hours 59 minutes 20 seconds. This means the timer is 40 seconds behind in 12 hours. In 24 hours the lag will be 80 seconds. Divide 80 seconds by 4 = 20. In the correction mode, set the reading to 20, then go to the current time setting mode, set the nearest current time, for example 9.1, and at 9 hours 10 minutes, by pressing the “Select” button, we exit to the operating mode.

Please note that in the absence of a backup power source, in the event of a “loss” of mains voltage, the clock is reset to zero and the current time must be set again.

3. Menu for setting temperature and time intervals for thermostats

Let me remind you of the operating modes of the thermostatting (thermal control) channels:
— thermostatting mode — maintaining a certain temperature
— thermal control mode — maintaining the temperature within certain limits
— single heating (cooling) mode
All these modes are described in detail in the article, which also provides detailed instructions and capabilities of each mode.
With the introduction of a real-time timer into the design, it became possible for each channel to set one time interval for the channel to operate during the day. To do this, additional lines for turning on and off channels have been added to the menu.
For example, we need the 1st temperature control channel to work only at night from 23.00 to 6.30. To do this, in the 1st menu (short press the “Select” button):
— after setting the upper and lower temperature limits, two more lines will appear: “t.On——00.0” and “t.OF——00.0” (the same will happen for the second channel)
— use the “+” and “-” buttons to set: “t.On——23.0” and “t.OF——06.3”
Now, at 23.00 the 1st channel will start working in the specified mode, and at 6.30 the channel will be turned off, and so on every day.
Single heating/cooling mode. If the time interval is not selected (the on/off time is set to “0”), then these modes are launched manually by pressing the corresponding button. This mode can also work over time.
Let’s say we need to heat the water in the tank to 45 degrees on the 2nd temperature control channel in the morning, by 7.00, taking into account that the water in the tank heats up to this temperature in 25 minutes:
— set “2.On——00” and “2.OF——45”
— set “t.On——06.3” and “t.OF” leave the default “t.OF——00.0”
Now, channel 2 will automatically start at 6.30 minutes, and when the water temperature reaches 45 degrees it will turn off.
When using the single heating/cooling mode together with a timer, it is still possible to manually start the mode, but it should be taken into account that during the time period “t.OF—-t.On” (for the previous example - from 24.00 to 6.30) manual mode is not possible . Therefore, in order to start the mode manually at any time, it is necessary to set “t.OF” 10 minutes less than “t.On”.

4. Menu for setting time intervals for the timer

Real time timer allows you to set two time intervals during the day to control the load using a timer.
To do this, four additional lines have been added to the menu:
— t1.1 — switching time for the first time interval
— t1.0 — shutdown time for the first time interval
— t2.1 — switching time for the second time interval
— t2.0 — shutdown time for the second time interval
Time intervals must not overlap.
Let's say we need to turn on the lighting in the yard twice a day: from 21.00 to 0.30 and from 5.30 to 7.00
Install:
— t1.1 — 21.0
— t1.0 — 00.3
— t2.1 — 05.3
— t2.0 — 07.0
Now the timer load will be turned on at 21.00 and 5.30, and turned off at 0.30 and 7.00

Second PCB option:

Option to set FUSE bits:

(22.2 KiB, 2,016 hits)

In this tutorial we will talk about timers.

This topic is directly related to the topic of microcontroller clocking. Therefore, I recommend that you read the previous one before reading this lesson.

So why do we need a timer?

When building projects on microcontrollers, there is often a need to measure precise time intervals. For example, the desire to blink an LED at a certain frequency, or poll the state of a button at the required time intervals.

Timers help solve the tasks. But AVR microcontroller timers do not know what a second, minute, or hour is. However, they know very well what tact is! They work precisely due to the presence of controller clocking. That is, the timer counts the number of controller cycles, thereby measuring time intervals. Let's say the controller operates at a clock frequency of 8 MHz, that is, when the timer counts to 8,000,000, one second will pass, counting to 16,000,000, 2 seconds will pass, and so on.

However, here comes the first obstacle. Our registers are 8-bit, that is, we can count up to a maximum of 255, and taking a 16-bit timer, we can count up to a maximum of 65535. That is, in one second we must reset the timer a huge number of times! Of course, you can do this if you have nothing else to do. But simply measuring time using a powerful microcontroller is not at all interesting; I want to do something more. This is where the predivider comes to our aid. In general terms, this is an intermediate link between the timer and the controller clock frequency. The prescaler makes our task easier by allowing us to divide the clock frequency by a certain number before feeding it to the timer. That is, by setting the prescaler to 8, in 1 second our timer will count to 1,000,000, instead of 8,000,000 (Of course, with a controller clock frequency of 8 MHz). It’s already more interesting, isn’t it? And we can divide not only by 8, but also by 64 and even by 1024.

Now it's time to assemble the circuit, set up our timer, prescaler, and do at least something useful!

And today we will make “running lights” from LEDs. That is, we will light 3 LEDs one by one, with a period of 0.75 seconds (That is, the operating time of one LED is 0.25 seconds). Let's put together the following diagram:

Calculate the values ​​of resistors R 1-R 3 yourself.

Next, let's look at the registers responsible for the operation of timers. In total, AtMega 8 has 3 timers. Two 8-bit (Timer 0, Timer 2) and one 16-bit (Timer 1). We will consider the example of 16-bit timer 1.

A pair of registers, 8-bit registers TCNT 1H and TCNT 1L, together form the 16-bit register TCNT 1. This register is open for both writing and reading. When timer 1 is running, the value of this register changes by one with each count. That is, register TCNT 1 records the number of clock cycles that the timer has counted. We can also write here any number in the range from 0 to 2 to the 16th power. In this case, the clock cycles will be counted not from 0, but from the number we recorded.

The TIMSK register is responsible for interrupts generated when the microcontroller timers operate. An interrupt is a handler for a special signal received when something changes.. Any microcontroller interrupt can be enabled or disabled. When an enabled interrupt occurs, the course of the main program is interrupted and this signal is processed. When a disabled interrupt occurs, the program flow is not interrupted and the interrupt is ignored. The TOIE 1 bit (Timer 1 Overflow Interrupt Enable) is responsible for enabling the overflow interrupt of the counting register TCNT 1 of timer 1. When writing 1 to this bit, the interrupt is enabled, and when writing 0, it is disabled. This interrupt is generated by timer 1 when the maximum value of the TCNT 1 register is reached. We will talk more about interrupts in the next lesson.

Register TCCR 1B is responsible for the configuration of timer 1. In this case, with bits CS 10-CS 12 we set the value of the prescaler according to the following table.

The remaining bits are not of interest to us for now.

There is also a register TCCR 1A, which allows you to configure other timer operating modes, for example PWM, but about them in a separate article.

And now the code in C:

#define F_CPU 16000000UL #include #include uint8_t num=0; ISR(TIMER1_OVF_vect) ( PORTD=(1<2) ( num=0; ) TCNT1=61630;//Initial timer value ) int main(void) ( DDRD|=(1<

#define F_CPU 16000000UL

#include

#include

uint8_t num = ;

ISR(TIMER1_OVF_vect)

PORTD = (1<< num ) ;

num++ ;

if (num > 2)

num = ;

TCNT1 = 61630 ; //Initial timer value

int main(void)

DDRD |= (1<< PD0 ) | (1 << PD1 ) | (1 << PD2 ) ;

TCCR1B |= (1<< CS12 ) | (1 << CS10 ) ; //Prescaler = 1024

TIMSK |= (1<< TOIE1 ) ; //Enable timer 1 overflow interrupt

TCNT1 = 61630 ; //Initial timer value

sei(); //Enable interrupts

while(1)

//The main loop of the program, it is empty, since all the work is in the interrupt

ASM code:

Assembly (x86)

Include "m8def.inc" rjmp start .org OVF1addr rjmp TIM1_OVF start: ldi R16,LOW(RamEnd) out SPL,R16 ldi R16,HIGH(RamEnd) out SPH,R16 ldi R16,1 ldi R17,0b00000111 out DDRD,R17 ldi R17,0b00000101 out TCCR1B,R17 ldi R17,0b11110000 out TCNT1H,R17 ldi R17,0b10111110 out TCNT1l,R17 ldi R17,0b00000100 out TIMSK,R17 sei main_loop: nop rjmp main_loop TIM1_OV F: out PORTD,R16 lsl R16 cpi R16,8 brlo label_1 ldi R16,1 label_1: ldi R17,0b10111110 out TCNT1L,R17 ldi R17,0b11110000 out TCNT1H,R17 reti

Include "m8def.inc"

Rjmp start

Org OVF 1addr

Rjmp TIM 1_OVF

start :

Ldi R 16, LOW (RamEnd)

Out SPL, R 16

Ldi R 16, HIGH (RamEnd)

Out SPH, R 16

Ldi R 16, 1

Ldi R 17, 0b00000111

Out DDRD, R 17

Ldi R 17, 0b00000101

Out TCCR 1B, R 17

Ldi R 17, 0b11110000

Out TCNT 1H, R 17

Ldi R 17, 0b10111110

© 2023 hecc.ru - Computer technology news