📜 ⬆️ ⬇️

Electronic necklace. Part 2


Last time we reviewed the hardware of our bling, it is time to write the firmware.

Work 1-Wire


In the first part, almost nothing was said about the specifics of the work of 1-wire. The hardware of the protocol is very simple: one signal conductor, pulled to the positive power supply through a resistor. All that devices can do is close the signal line to earth at one time or another. How is the data transfer organized?

Each interaction act on 1-wire begins with a reset.


The master produces a low-level pulse with a duration of 480 µs to infinity. Thus, power on is also considered a reset. After that, the master releases the line and after 200 µs checks the voltage on it. Any slave device, if it is on the bus, at this time should give a response impulse, called Presence. If Presence is accepted, we can assume that slaves are detected and ready to accept commands.
')
Data transmission is divided in time into time slots, with a duration of 67 ms. Within one time slot, one bit is transmitted, so the transmission rate can reach 14.9 kbps. Some chips support the Overdrive mode, in which the slots are shortened to 10 µs and the speed increases to 100 kbps, but we will not consider this mode, the standard speed is more than enough.

To transfer the unit, the master issues a short pulse (5 µs) and releases the line to the end of the time slot. To transmit a zero pulse is longer than 60 µs.


Receiving data from slave devices is also synchronized by the master. At the beginning of the time slot, it gives an impulse of 5 µs duration. If the slave transmits a unit, it does not interfere with the process. If it transmits zero, it keeps the line low for 20 µs. The master is required to check the voltage level some time after the pulse has been applied.


Note! Some of the time transfer parameters for the DS2413 chip are different from the standard ones; they are highlighted in yellow in the datasheet .

Addressing


Each slave must have a unique address. In the chips, designed to work with 1-wire addresses are stitched during the production process. The address consists of 64 bits (8 bytes), and the low byte is the chip family code (for DS2413 - 0x3A), and the high byte is the checksum. After selecting a device at the address, all other devices do not respond to commands until the next reset.


Teams


1-wire devices are controlled by single-byte commands. There are commands common to all, as well as specific to certain circuits.
Common commands:

The DS2413-specific commands are only two:

Chip Address Detection


As it was mentioned in the first part of the article, it’s not a bad thing to count the addresses sewn in the purchased chips before assembly, otherwise we will not be able to manage them. For this purpose, a special firmware reader. It allows you to read the DS2413 address and write to the controller's EEPROM, from where it can be obtained by the programmer.

Unfortunately, the reader was written a little under another controller (ATTiny12) and in assembler. In the archive at the end of the article will be the source of this firmware, those who wish can try to port it to Tiny13. You can also take advantage of ready-to-use 1-wire transmission and reception functions from the main firmware and write your reader.

Finally, you can take a 1-wire / COM adapter (for example, one ) and read the chips on the computer.

Elements of the program

Pseudo Random Number Generator


Monotonously flashing decoration very quickly get bored, so you need to reproduce random lighting effects at random intervals. As a pseudo-random number generator, a linear-shift shift register is selected as the easiest to implement on AVR. The algorithm does not contain multiplication operations, only XOR and shifts. The 15-bit bit register provides 32767 states, which is enough for an hour of non-repeating device operation.

But that's not all, for the initialization of the RNG you need a source of entropy, otherwise the same sequence will be generated each time you turn it on. As such a source can be:

In this design, the third method is applied: counting the number of clock pulses in one period of the watchdog timer. Watchdog timer in Tiny13 is clocked by its own oscillator, the frequency of which is rather unstable.

Every 2 seconds, 2 8-bit pseudo-random numbers are generated, the bits of which are used as follows:
4 bits of the first number - the choice of the current effect. It may be:

The high bit of the second number is the color (green or orange).
The remaining bits encode the numbers of the first, second, and third LEDs for single flashes (in groups of 3 bits). Since 3 bits encode numbers from 0 to 7, and a total of 10 links, the number increases by 1 for the second flash, by 2 for the third flash. Thus, the entire range is covered, and this is easier than taking four-bit numbers and controlling the boundaries.

Clocking


The Tiny13 controller has two built-in clock sources - 9.6 and 4.8 MHz, and you can also turn on the clock divider. The lower the frequency, the lower the power consumption of the controller. On the other hand, a too low frequency will not allow setting time intervals for the 1-wire protocol. When writing firmware in assembler, it was possible to achieve operation at a frequency of 1.2 MHz, with the shortest measured interval being only three cycles. For firmware on C, this speed was not achieved, the minimum clock frequency is 4.8 MHz.

Most of the time, the controller is in PowerDown mode, with only the watchdog timer running, which causes an interrupt every 2 seconds.

Fuse bits


To configure the controller is a set of so-called fuse-bits (fyuzov). Their values ​​need to be set once before flashing. The values ​​of fyuzov for this project (values ​​other than the factory, highlighted):

SELFPRGEN = 1 // self programming is not allowed
DWEN = 1 // debugWire disabled
BODLEVEL1: 0 = 10 // brown-out detector is set to 1.8 V.
RSTDISBL = 1 // RESET pin is not disabled
SPIEN = 0 // SPI allowed
EESAVE = 1 // EEPROM protection disabled
WDTON = 1 // disabling the watchdog timer enabled
CKDIV8 = 1 // 8 clock divider disabled
SUT1: 0 = 00 // start time 64 clk
CKSEL1: 0 = 01 // 4.8 MHz clock frequency


You need to flash the controller before installing it on the board, connecting it directly to the programmer. If there are no panels for SOIC packages, you can neatly connect them with wires. After flashing and testing the controller, you can unsolder it completely.

Full source code of the main firmware:
pastebin.com/CEKvhYVt

Firmware for 1-wire address reader:
pastebin.com/tQ7AqmfN

Video showing the operation of the firmware:


Conclusion


At the moment, a little more than half of the controller's memory is occupied, so there is a huge scope for fantasy. You can implement new effects, you can slightly modify the scheme of the device and add a temperature sensor, light, accelerometer, microphone, UFO detector or IR receiver. You can organize the download of new firmware, for example, through a serial interface (Tiny13 supports self-programming). Naturally, for all the possibilities at the same time, neither the legs nor the memory of the controller will be enough, but one or two features can be added.

Congratulations to the beautiful half of habraklovechestvu with the upcoming March 8!

Source: https://habr.com/ru/post/139342/


All Articles