Binary clock. Binary clock on ATMega32

And continuing the topic, we publish the following material. This watch is a real radio geek ( radiogeek- radio fan, English) and display the time in binary code at the moment the button is pressed. The device displays hours and minutes by flashing two LEDs in sequence and represents two 4-bit binary numbers. Here is a description of how to read binary numbers.

The first number represents the hour and the second number represents the number of minutes. For example, if the clock flashes 0010 - 0110, this corresponds to 2 - 6, which means "2" hours and "6" minutes. That is 2:30. The device does not have an “am” or “pm” indication, but we think everyone understands whether it is 2:30 am or pm. The project also assumes that you have experience working with surface mount electronic components, and that you are familiar with the skills of soldering SMD parts.

Clock diagram

How to tell time

Many versions and copies of this watch were made - you can see them in the photographs. The archive contains versions 2.5 and 3.1, which uses an ATtiny controller for surface mounting and a mini-USB port for programming. The ATtiny controller has 8 pins. This chip can be programmed to perform various functions. The ATtiny has an internal clock and the connected LEDs are programmed to flash to indicate the time.

Electronic circuit components

  • Atmel microcontroller ATtiny85
  • 2 SMD surface mount LEDs
  • 2 SMD resistors 50 Ohm
  • Small SMD button
  • CR2032 3 volt coin cell battery
  • Mini battery holder

Soldering watches

For this process, a toaster oven was used to reflow the solder on small SMD components such as LEDs and resistors, and then used regular soldering iron to solder larger components such as the socket, button, and battery holder.

So, the clock is assembled, but ATtiny does not yet know how to control the LEDs. So we have to program it. There are several options when it comes to programming the ATtiny. You can make a quick circuit layout and use special ATtiny programming devices, or if you can make a great Arduino programmer, so you can easily program any controllers in the future.

Since the chip is very small, in order to program it, we had to add a mini-USB port that connects to necessary contacts. We attach a regular USB cable to the other end, so all you need to do is connect the watch to the programmer. Well, choose the strap yourself - either leather or silicone. The case is not needed here at all - let everyone see that this is the watch of a real radio maniac!

Discuss the article CLOCKS RADIOGICS

These wrist electronic watch show the time in the binary system, since it requires fewer LEDs to display the time than conventional digital indicator clocks, and the originality will be at the same level. The common 32C417 crystal at 32.786 kHz was used to run the PIC16F527 in low power mode and help improve timing accuracy. A suitable fabric belt was used to wear on the wrist.

The PIC16F527 microcontroller was used here because it has the smallest package available. The watch is powered by CR2032 batteries in a special holder. This battery has a decent capacity even though it is only the size of a coin. To reduce PCB area and design cost, a single quenching resistor was chosen for the entire SMD LED block.

You can enlarge the image by saving it on your computer. The design of the circuit here is based on a standard two-layer printed circuit board.

And a standard 20 mm strap fits just inside the cutouts.

Clock operation algorithm

The main loop keeps track of what mode the clock is currently running in. The first state is idle, where the clock is polled by the switch and waits for user input. Once the button is pressed, the system moves to a second state that calculates how long the clock will be displayed. Then there is a transition to state three, which does most of the branching work based on the fact that the user is in at the moment does. The watch turns on the LEDs in this state. If the user holds the button for more than 3 seconds, the clock goes into state four. This is a state of time correction. The longer the button is pressed, the faster time is changing.

Wanting to collect binary clock, I never found an acceptable ready-made design on the Internet. Most watches had a serious drawback - when the power was turned off, the time settings were lost. By luck, shortly before this, I began to master the C language and AVR microcontrollers. So, it was decided to reinforce the knowledge gained practical experience, and at the same time reinvent the wheel. I also really love the green flashing LEDs.



Scheme



RTC

The problem of saving current settings is perfectly solved by the real time clock (RTC). My choice fell on the DS1307 chip.

According to the manufacturer, if the power is turned off, it can save the time and date for 10 years, consuming the energy of only a CR2032 lithium battery. That is, the clock continues to tick, maintaining acceptable accuracy. The time does not go astray, turning on the clock again, we get real time on the dial, and not the time at the moment of shutdown. The microcircuit communicates with the microcontroller via the “square bus” I 2 C, reporting exact time and accepting its new meanings.

The heart of the device

The choice of the Mega32a microcontroller was dictated by the following factors:
Sufficient quantity ports, so as not to use dynamic display, which I do not like, primarily because it irritates the eyes (blinking at a high frequency is unnatural in any case). I became familiar with it while playing with PIC microcontrollers in the Proton PICBasic language, and if it is possible not to use a dynamic display, I would prefer to do so.
Relatively low cost of 130 rubles (Mega16a, for example, costs the same), and with a discount it’s generally 104 rubles.
Clear QPF-44 package, with convenient pinout


Port “A” displays seconds, port “B” displays minutes, and port “C” displays hours. It is very convenient that you can assign time values ​​received from the DS1307 to the ports without any changes. Buttons are connected to port “D” (pins 3 – 7), pins 0 and 1 work as a clock line (SCL) and a serial data line (SDA), respectively. The RTC chip is configured so that it produces pulses at a frequency of 1 hertz on its seventh leg. This leg is connected to the 3rd pin of port “D”. This port itself is configured as an input, and just in case, internal pull-ups to the power supply plus are included, duplicated by SMD resistors on the outside. Such actions in to the fullest protect against any surprises.

LEDs


I chose LEDs in a matte housing with low luminosity. First, bright diodes in a transparent case were tested, but even with a current of 3 mA they shone too brightly and unevenly, which again caused discomfort. With a voltage drop across the diode of 2 volts, a supply voltage of 5 volts and a resistor of 1 kOhm, the value of the current flowing through the diode will be equal to (5 – 2)/1000 = 3 mA. This value was selected empirically, and the brightness of the glow is perfect for a dim room. If you plan to set the clock directly sunlight, then the resistor value should be reduced, down to 200 ohms, for a brighter glow (thanks cap).

Buttons

On a separate board with buttons, there is a “fuse” (it will protect us from an accidental shot in the head), in the form of another Bt6 button. The time can be edited by first holding it down.

Software

The code is written in the CodeVisionAvr environment.
The program begins with us setting up the microcontroller peripherals.
Configure the ports (A,B,C – output, D – input)
Just in case, a pause of 300 ms is provided so that the DS1307 has time to “come to its senses”
Initializing the “square bus”
We configure the RTC chip so that it produces rectangular pulses every second on the SQW/OUT pin
We check whether the CLR button is pressed. If yes, then reset all values ​​to 0
Enable global interrupts
Yes, a few words about them. We use external interrupts INT0 on PD2 on falling edge, i.e. every second the program will go to the interrupt handler, in which we read the time values ​​from the DS1307 and display them on the LED indicators.
We go into an endless loop, where we poll the buttons
If the button is pressed, add (subtract) an hour (minute) and send the new value via I2C
At the same time, we check whether the new time values ​​fit into the 24-hour and 60-minute ranges.

PCB

The board is made using the Great Cosmic Laser-Ironing Technology on a one-sided PCB. When making the top board, plain paper was used (unsuccessful experiment).


There are many variations of this technology. In my opinion, this one is the best:
1. Cutting it out the right size piece of textolite.
2. We sand the ends, getting rid of harmful burrs.
3. Lubricate the future board with cleaning powder or toothpaste and scrub it with the hard side of the sponge until it shines.
4. Dip our piece for a couple of tens of seconds in a weak solution of warm ferric chloride until a uniform, matte, burgundy-brown surface appears. When pulled out of the solution, the liquid should completely wet the surface.
5. Wash off the poop and dry it carefully, without touching the surface with your fingers or anything else greasy. Immediately place it on clean paper with the copper side down to avoid dust or hair.
6. Print the mirrored drawing on thin(!) glossy paper, you can cut it from a magazine, for example. We do not touch the drawing with our hands. Carefully cut out and place pattern down.
7. Apply it to the prepared piece of PCB, iron it through 1-2 layers of clean paper, setting the iron to maximum temperature. 10 seconds should be enough, because if you overdo it, the tracks will flatten and flow on top of each other. The toner should completely adhere to the copper.
8. Soak under running warm water, you can leave it in the water for 10 minutes. Carefully tear off and scrape off the paper. The old one helps me with this toothbrush. Remove the remaining pieces of paper with a needle. The toner remains on the PCB.
9. Heat in a water bath strong solution ferric chloride, throw our board there and flounder for several minutes (according to Van’t Hoff’s rule, with an increase in temperature by 10 degrees, the reaction rate increases 2 times. Copper disappears right before our eyes. You don’t have to heat it, but you’ll have to wait longer.
10. As soon as all unnecessary copper has disappeared, turn off the gas, pull out (for example, with tweezers) the board, try to wash the plate and fingers from ferric chloride. We wash it off the board with running water.
11. Take acetone (nail polish remover) and wipe off the toner. You can try scraping it off with sandpaper or a sponge.
12. Drill holes.
13. Let's cheat. I use LTI as a flux, and I advise you, however, after tinning and soldering, this flux must be washed off (with the same acetone, and better with a mixture alcohol-gasoline 1:1), because LTIshka has some conductivity.
All work must be carried out in a ventilated area, during
a lot of harmful fumes are released.


The boards are connected to each other using PBS and PLD connectors. The first ones are connected to the top board using a thin mounting wire; it can be pulled out, for example, from an old LPT cable or adapter.


The second ones are soldered to the bottom board, and the pins leading to the keyboard are bent (see photo).

Printed circuit boards in SprintLayout5.0 format are included. There are a couple of mistakes in the photos, but they have already been corrected in the attached files.

Microcontroller firmware

For this purpose, a USBasp programmer was assembled, which can be seen in the photo above. It’s a pretty nice thing, easy to use and you can carry it with you in your pocket all year round (I hope no one will do that). To flash mega32 firmware you will have to install the “Slow SCK” jumper.
Fuses:
Low fuse = 0xC4
High fuse = 0xD9
Our microcontroller is clocked from an internal RC oscillator with a frequency of 8 MHz. I had to disable the JTAG interface on PortC, otherwise some LEDs would not light up.
The board has an ISP10 connector for fast flashing/debugging.

Front panel

Made of aluminum plate, 40 mm wide and 1.5 mm thick. It has 18 holes drilled with a diameter of 5 mm, and 4 holes with a diameter of 3 mm for attaching racks.


First, the template was printed and pasted onto the plate. Next, pilot holes were drilled with a 1.5 mm drill, after which the main holes were drilled with drills of the required diameters.


Finally, the plate was bent, sanded with fine sandpaper and polished with GOI paste.
The template is included with the attached files as a layout5.0 file

Red LED in the upper left corner

Repeats the pulses generated by the DS1307 on the 7th leg, i.e. flashes every second. A small p-channel MOSFET transistor operates in switching mode, opening and closing in time with pulses. At first I wanted to make a backlight (like Ambilight), for which I built a CMOS inverter on a complementary pair of transistors (to be sure). But I didn't like it. For one LED, one transistor is enough; you can even use pnp type bc857. I used irlml6402 or irlml6302 open-frame mosfet.

Files

The sources, hex file, printed circuit boards, circuits, circuit in proteus and fuses are enclosed in this picture in the form of an archive. I don’t trust file storage, I don’t have my own server yet, so, in my amateurish opinion, the most reliable place for storage would be Habr. Windows users can get to the files by opening a saved image using WinRar.
Yes, this is the picture.

Video

Conclusion

You can use any power source capable of delivering 5 volts at a current of 70 mA. A USB port is quite suitable for this. The main thing is that the power is “clean” and does not exceed 5 volts. Powering the clock from a DC-DC converter from the mc34063 chip with a noise level of ~50 mV, I noticed glitches when setting the time. Now the device is powered by a switch hanging nearby. It outputs strictly 5 volts. On the good side, you also need to make foolproof protection in the form of a diode, and some kind of linear stabilizer for 3.3 - 5 volts.
The absence of alarm clock and date display functions in the watch is quite justified: both are present in the phone, which means that no one will most likely use them in a binary watch (thanks to Uncle Occam for this conclusion).

However, before that, learn, because, you see, it will be awkward to admit ignorance when you are asked to tell what time it is. You will tell me, of course, but your information can be compared with the readings of a traditional watch. Agree, it will be awkward. So follow simple instructions and you will learn determine time using binary clocks. There are two ways to do this.

BCD mode

Decoding

First, take a close look at binary clock. The clock display has three columns, each of which has two rows of lights.

The first column shows the hours, the second, respectively, the minutes, and the third will tell you how many seconds have elapsed. All columns have the same structure.

The first row of lights in the column reflecting the hours shows the first digit, tens, and the second, second digit - units. Each row contains from two to four lights, each of which implies a power of two. So the lowest light represents 2 to the power of “0”. This value is taken as one.

The second light represents 2 to the power of 1, which is taken as two.

The third light symbolizes 2 to the power of “2”, i.e. stands for four. Well, the fourth light displays 2 to the third power, which stands for eight (see image).

Now that you have an idea of ​​how a binary clock works, try to determine how many hours the binary clock shows in an image. You see that in the first row of the column reflecting the hours, one light is on. We remember that the first light displays 2 to the power of “0” and is read as one. Further in the second column, not a single light is lit, so it is read as zero. Based on the data obtained, we can conclude that the clock in the image shows ten o'clock. Just remember that the clock displays the time in a day mode, so if you are uncomfortable, subtract twelve when the clock shows thirteen hours or more. For example, if the clock shows fifteen, then you can safely assume that it is three o’clock.

In the same way, try to determine how many minutes the binary clock shows in the presented image. So in the first row of the column reflecting minutes, you see that two lights are on. Remembering the rules for determining time using binary clocks, we can conclude that the first column displays a triple. In the second row of this column you can see that three lights are on. Remembering the powers of two, we add up all the indicators and get the number seven (see image). Comparing the first and second digits, we can conclude that the minute column shows the value 37. Let's remember the clock indicator and we get that the clock shows the time 10:37.

Decoding the indicators of seconds sometimes seems quite difficult, because seconds are constantly changing their values. You will learn to determine the value of the column with seconds when the determination of binary clock indicators reaches automaticity. So, in the presented image you see that in the first row of the column reflecting the second values, the upper third light is on. Remembering the powers of two, we can conclude that this light indicates the number 4. In the second row of the same column, the fourth light and the first one are lit, i.e. the lowest one. Following the rules for determining time using binary clocks, we can conclude that the top light means the number 8, and the bottom one, respectively, one. We add both numbers and get nine.

Result

Now we compare the value of the first and second rows of the column and get the value 49. Thus, the clock in the image shows the time 10:37:49.

True binary (binary) mode.

Decoding indicators

The method of deciphering binary clocks, which have only two rows, is exactly the same as in binary decimal mode. However, there is one caveat: now there is only one line in each row. The lights in the top column correspond to the principle of powers of two: 1, 2, 4, 8. But in the bottom column you can notice not four lights, but six. Don’t be alarmed, because the principle is followed in the second column, just a few values ​​have been added: 1, 2, 4, 8, 16, 32. There is no need to add more than six lights, because the next value will be 59, which can be written so that it will be lit first, second, fourth, fifth and sixth lights.

Determine hours

Now try to determine how many hours the clock in the image shows. You see that the first and second lights are on. Knowing about the powers of two in binary clocks, we can determine that the first light means the number one, and the second, respectively, two. Let's add both indicators and get the number “3”.

In the image shown, the clock has two rows of lights, but you should remember that there can also be clocks with columns. The principle of determining time using such a clock will be the same as in this case. The main thing to remember is the binary number system, which implies only two digits for encryption, “0” and “1”. So the indicators you just defined will look like 0011 in binary, which will also be equal to the value “3”.

Determine minutes

Using the technique described, you can see from the clock in the image that the first, fourth and fifth lights are burning. In the binary system it will look like 011001. Remembering the powers of two, we can conclude that the first light shows “1”, the fourth “8”, and the fifth, respectively, “16”. Let's add up all the indicators and get the value “25”.

Determine seconds

All that remains is to determine the seconds. Do it yourself according to the described principle if your watch has a column or row displaying seconds. It is absent from the presented image, so it makes no sense to describe its definition.

Remember to practice and train your brain. Over time, you will learn to quickly and easily determine time using a binary clock, even if at first glance it seemed very difficult to you. Don't get hung up on the math, just remember the meaning and location of each light. To quickly learn how to tell time using a binary clock, buy your first watch with a column displaying seconds. The values ​​​​in it will be the largest, therefore, having learned to quickly determine the seconds, reading minutes and hours will become very simple for you.

And various radio components to get acquainted with microcontrollers, the author decided to do something interesting and at the same time useful. Having in stock large number LEDs, the idea came to create a binary clock.

From the electronics side, binary clocks are not particularly complex, but the author complicated the task and decided not to save buttons and LEDs. Initially, the project was supposed to use 22 LEDs, 6 buttons, and one buzzer. There was also an idea to assemble a clock on Arduino Mega because more quantity pins, but the 74HC595 shift registers turned out to be a salvation.

Materials:
- Arduino Uno
- 2 full-size development boards
- Red LEDs 7 pcs
- Green LEDs 7 pcs
- Blue LEDs 6 pcs
- LEDs yellow and white 2 pcs.
- Resistors 220 ohm 25 pcs
- Piezo tweeter 1 piece
- Tact buttons 6 pcs
- Output shift registers 74HC595 in DIP-16 package 3 pcs
- Connecting wires 90 pcs
- Real time clock module based on DS1307 RTC chip

How everything will work.
There are about 10 types of binary clocks. Some show the time in binary coded decimal (BCD) format, others in the form binary numbers. Since the author doesn't particularly like BCD clocks, he decided to make his own purely binary. They may be harder to read for some, but they make little difference because converting numbers from binary to decimal is easy. Also prerequisite The creator of the watch was the indication of seconds on the watch.

In addition, the watch has 6 buttons:
Set - is responsible for setting the clock/alarm clock and saving the parameter in the setting mode.
Mode - is responsible for switching between clock, alarm and timer modes.
Up - in setting the clock/alarm/timer, increases the parameter by one. In an alarm clock and timer, it is responsible for activating and turning off the selected mode. When a signal is triggered, it will turn off the alarm/timer signal.
Down - in the clock/alarm/timer settings, will decrease the parameter by one. The timer will pause it without resetting the countdown. When the alarm goes off, it will postpone the alarm for 5 minutes.
24/12 - change the time format.
Dim - is responsible for turning the LEDs on and off (when the LEDs are turned off, the other buttons stop working).
LED position diagram:

Connecting components
The author will connect all LEDs in series and with a resistor. The resistor is soldered to one of the LED terminals, it doesn’t matter which one. The LEDs will be connected via shift registers; this chip has 16 pins. This number of pins allows you to use a large number of pins, taking up only 3 pins on the Arduino.


74HC595 shift register pinout:
Q0-Q7 are the register pins to which the LEDs will be connected.
Vcc - the power pin will supply 5V.
GND - ground connected to GND on Arduino.
OE - pin is responsible for inverted activation of the pins, but it will not be used, it is simply shorted to ground.
MR is an inverted register clear, it does not need to be controlled, so it will be connected to a 5V power supply.
ST_CP - pin is responsible for updating the register state. When writing the state, you need to apply LOW to it, after writing - HIGH, to update the state of the pins. It needs to be connected to a pin on the Arduino. You can connect this pin to three registers in parallel.
SH_CP - pin, responsible for shifting the register by 1 bit. It needs to be connected to a pin on the Arduino. They are also connected on microcircuits in parallel.
DS - data is supplied to this pin; it is connected to a pin on the Arduino.
Q7" - this pin is used for cascade connection with the rest of the 74HC595 registers.

Connection diagram:

The piezo tweeter will be connected to the third pin of the Arduino in series with a resistor. Before including the tweeter in the circuit, the author looked at which pins support PWM, since this is mandatory for it. On Arduino Uno, PWM is supported by pins 3, 5, 6, 9, 10 and 11.

Connecting the buttons uses resistors built into the Arduino, with one side of the buttons connected to ground and the other to the Arduino pins.

This is what the final design looks like:

Assembly on Breadboard
After purchasing additional parts, the author began assembling the project on a breadboard according to the diagrams. The appearance was roughly expected, because the Breadboard limits the freedom in placing components, and the protruding wires did not create aesthetic pleasure. But the development board is intended for prototypes, and not for finished devices.

Program code.
Having experience in programming, the author decided to write the code himself, without using the work of others. The first step was to write a subroutine; it is responsible for blinking all the diodes and sending a signal from the piezo tweeter when turned on. This function helps to verify the integrity of the circuit; this is implemented on many devices.

LED operation.
Since the LEDs are accessed through a shift register, first of all it was necessary to implement more subroutines for the LEDs. To make it easier to work with diodes, a number of additional functions. Implemented various effects diode animations. When the clock is not set, the diodes responsible for hours and minutes will begin to blink (as a regular clock blinks when not set). The LEDs responsible for seconds also have their own animation; the diode can move left and right in alarm mode, or in clock setting mode.

Main cycle.
The program is configured to work as follows: the clock displays information depending on current state, and change their state depending on the use of buttons and events. It all looks like a considerable number of nested conditions. The state of the diodes is updated every time after checking the state of timers and buttons and calling their handler.

Launching a Layout
After turning on the project, at first glance, the device worked correctly and stably. But the author discovered a flaw: the clock was behind by one second per hour, for long time this would be a big mistake.

After studying this problem, it was found that the original Arduino Uno uses a ceramic resonator, and it lacks the accuracy to measure time in long term. Most rational decision I purchased a real-time clock, plus because of this module, the time on the clock will not be lost when disconnected. The author purchased the Grove RTC module from Seeed Studio. It is a ready-made board with a clock chip. The author connected the pins of the SDA and SCL module to the Arduino on pins A4 and A5, GND to ground. Since the 5V power supply is occupied by the clock board, there was nowhere to connect the module. The author decided to power the module from one of the digital pins, which will be constantly energized. The author also needed to refine source code and add a real time clock library.

Watch assembly
Having completed long work above the code, it's time to give the device a completed look and move it from the breadboard to the printed circuit board. First of all, it was necessary to make the wiring for the board. Fritzing was used for this, since the author already had an idea of appearance hours, and he built a circuit diagram of the device. The author also traced the board manually, which took a lot of time.
PCB production project:

The production of the printed circuit board was ordered in China. Seeed Studio has a service for producing Fusion PCB boards. Through Fritzing, the file was exported to the Extended Gerber format; many board manufacturers work with it. Two weeks later, the author received the long-awaited payment in the mail.

All that remained was to solder the slightly dusty parts onto the board. The finished result after soldering it looked much better than the layout on Breadboard.

The author of the project worked for a long time and got what he wanted - a unique binary clock with a timer and alarm. Using the battery compartment, the watch can be placed anywhere. Arduino lived up to expectations and completely coped with the task.