Electronic lock with iButton key (DS1990A) on the microcontroller. Electronic key lock iButton on AVR Lock with key control ibutton tablets

The device is assembled on an ATtiny2313 microcontroller, to which an external non-volatile memory EEPROM - 24C16 is connected. Source codes are available, work "on iron" is checked up. The access controller works with DS1990 type keys, supports up to 500 pcs. Programming the access controller through the master key or through the jumpers on the board.
The access controller, in addition to the button for opening the lock, has a limit switch that tracks the time the door is open. The signal response time is programmed by jumper JP2 (from 10 sec to 127 sec).

The controller on electronic keys DS1990A.

Supports up to 500 keys. The device is made on the AVR microcontroller: ATtiny2313.

Schematic diagram of the i-Button DS1990 key reader (click on the diagram to enlarge):

Programming instructions

Operating mode - when 12V is applied to the controller, the LED flashes at 1Hz.
Programming mode - LED flashes 2 Hz.
When you press the "OPEN" button, a series of short beeps for the duration of the lock opening.
The door limit switch (NC) monitors unauthorized entry without a key.

The controller beeps if:
1 - After opening the door, the door remained open for more than the programmed passage time.
2 - Opening the door without a programmed key or using the "OPEN" button, the controller starts emitting short double beeps until the door is closed.

Attention!!! Programming the waiting time for a closed door CAN ONLY BE USED WITH JP2 JUMPER.

Sound signals

1 short beep - the key is stored in the controller's memory.
2 short beeps - the key is already stored in the controller's memory.
5 short beeps - exit from the programming mode.
1 long beep - the key memory has been erased from the controller.
short beeps 20sec. - the key memory is full.

Record MASTER Key and lock opening time

1 - Power off the controller.
2 - Press the "OPEN" button
3 - While holding the button, connect the power to the controller, after 5 seconds. controller "Squeak",
the LED will flash at a frequency of 2 Hz.
4 - Release the button.
5 - Bring the key to the reader, a single beep will sound,
The master key is WRITTEN, and the lock opening time of 1 sec will be recorded.

6 - Holding the key on the reader - we count the sound signals,
number determines the required time to open the lock, increment 1 sec. But no more than 32 seconds.
7 - Turn off the power to the controller or pause for 20 seconds.

Erase all key memory.

1 - Operating mode.
2 - Press the "OPEN" button and while holding it, bring the MASTER key to the reader and hold it, after 10 seconds. a long beep will sound - the key memory has been erased.
3 - Release the button and remove the key.

Adding new keys

1 - We bring the Master key to the reader and hold it for 5 seconds, 2 short beeps will be heard, the LED will flash at a frequency of 2 Hz.
2 - Bringing additional keys one by one to the reader - a short beep will sound, - the key is RECORDED, if 2 short beeps are heard - ATTEMPT TO RECORD A DUPLICATE KEY.
3 - We write down the required number (up to 500) of keys. The pause between the recorded keys is no more than 20 seconds.
4 - Turn off the power to the controller or pause for 20 seconds.

Access controller programming via JUMPERS JP1,JP2,JP3,JP4

JP1 - RELAY operating time, increment 1 sec.
JP2 - Waiting time for closing the door, increment 1 sec.
JP3 - Adding new keys (maximum - 500).
JP4 - Erase all keys from the controller's memory.

The jumper performs its function with a constant short circuit for the duration of use.

Printed circuit board:

Appearance of the assembled device:

"RELAY work time"- JP1 the red LED flashes with a frequency of 1 Hz throughout the duration of the jumper. and a short beep sounds. With each sound and light signal (green LED) 1 second is added to the variable "RELAY operation time". In total, it can be from 1 second to 32. After counting the required number of seconds, remove the jumper.

"Waiting time closed door"
- JP2 the red LED flashes at a frequency of 1 Hz throughout the duration of the jumper. and a short beep sounds. With each sound and light green signal, the "Closed door waiting time" variable is added by 1 second, in total it can be from 5 seconds to 126. After counting the required number of seconds, remove the jumper.

"Adding New Keys"- JP3 throughout the duration of the jumper closing, the red LED flashes at a frequency of 2 Hz. Bringing the key to the reader, upon successful reading, a short beep will sound and the LED will blink green - the key is written. If the key is already stored in the memory, then when you try to write it again, 2 short beeps are heard. When trying to write more than 500 keys to memory, the red LED flashes constantly at a frequency of 2 Hz. and a short beep sounds periodically. Remove the jumper.

"Erasing all keys from the controller's memory"- JP4 3 seconds after closing, 5 short beeper signals are emitted, a pause, then the beeper sounds continuously for the erasing time (approximately 10 seconds) - the key memory is erased. Remove the jumper.

Presented to your attention is a diagram of an electronic lock with key iButton tablet DS1990A (Touch Memory) models. The key is a device that has a unique serial number in its memory. The uniqueness lies in the fact that the serial number consists of 48 bits and as a result, the number of possible options is 281474976710656.

Description of the electronic lock

The lock is assembled on a Microchip PIC16F628A (627A, 648A) microcontroller. After power-up, the microcontroller sends a reset pulse with a duration of 500 µs and after 70 µs checks the response from the DS1990A. If there is no answer, then the microcontroller waits for about 80 ms more and transmits a reset pulse again. According to this algorithm, the connection of the key to the lock is checked.

If there is a response, then the DS1990A is connected to the lock. Then the “read ROM” command (33h) is sent, after which the microcontroller switches to receive and writes the number transmitted by the DS1990A tablet to the RAM, where it compares it with the number written in the EEPROM. If it matches one of them, then a beep sounds and the RA1 pin is set high for 1.5 seconds. Optocoupler DA1 (AOT122A) will open VT1 (KT972, BD677, BD679, BD681), which controls the 12 volt electromagnetic.

The process of writing the key number in the EEPROM: attach the key to the lock and after a beep, press the SA1 button. This button must be located in a hidden place inaccessible to unauthorized people.

To clear all numbers from the EEPROM, press the SA1 button with the power off, turn on the power and hold the button for 5 seconds. After clearing the EEPROM memory, the lock will beep. The total number of serial numbers that can be memorized is up to 21.

So that the microcontroller does not suffer from a static discharge, the VD1 zener diode (KS156A, 1N4733A, BZX55C5V1) at 5V is used in the circuit. Any of PIC16F627A, PIC16F628A, PIC16F648A microcontrollers can be used in this lock. The PIC12F629/PIC12F675 microcontroller has its own firmware. You can request the scheme from .

When programming , the following bits should be set.

general description

Application idea I-Button keys arose for me as the weight and size characteristics of the common bundle in my pocket increased. When you live in a private house, you want all the locks to open with one key. Unfortunately, this is still difficult to achieve. But you have to start somewhere.

Naturally, I decided to start with a little responsible object - the back door of the entrance to the yard. Although it is used quite often, but in case of any failure, you can always use the main entrance.

When choosing a locking part, I decided to abandon the holding electromagnet due to low reliability for lack of power (broken wires), since, unlike the use of such devices indoors, it is difficult to hide the cable (unless buried deep). The solution to the problem was an ordinary latch from a standard apartment lock, with the only difference being that the opening mechanism would be driven not from a handle or key, but from an electromagnet controlled by the device being developed (I took it from a car lock). Thus, in the event of a power failure, for example, in the event of a break, the device remains de-energized, and the lock remains closed.

Main characteristics I-Button key lock:

  • automatic locking of the door mechanically (slamming), which makes it independent of the presence of power supply,
  • unlocking the lock both from the outside and from the inside with a key touch memory(intercom "tablet"), to exclude the opening of the lock after a banal climbing over the fence,
  • informing about the open door with an intermittent sound signal,
  • saving up to ten I-Button keys in non-volatile memory using a master key.

Device diagram

Rice. 1. Schematic diagram

The basis of the device is the controller PIC16F628A.

A voltage stabilizer is made on the KREN5A chip.

Two pads for reading the key code XS1 and XS2 are connected to different pins of port A (A2 and A3). This is necessary to determine from which side the key is attached (since memorization of new keys is possible only from the internal contact pad). Zener diodes, resistors and diodes 1N4148 are protected from inputs.

The position of the door is determined by two reed switches - 1 and 2. In the diagram of fig. 1 shows the closed state. The magnet installed in the door is located opposite the reed switch 1. The reed switches are arranged in such a way that when the door is opened, the magnet sequentially passes through positions 1=>2=>3=>4, shown in fig. 2.

Reed switch 2 is used to "release" the latch after opening the door, as well as to "retract" the latch when the door is closed.

Speaker D1 (I used an electromagnetic one from a Chinese toy) is turned on through the transistor VT1.

The lock solenoid amplifier L1 is based on the TIP122 Darlington. To protect against switching voltage surges, a reversed KD202 diode is used.

The optocoupler PC817 has a circuit for monitoring the integrity of the solenoid circuit. It works as follows. In the absence of a control signal to open the lock, transistor VT2 is closed. Current flows through coil L1 and the optocoupler LED. The input B5 of the controller is low, which is controlled by the program. When the solenoid circuit is open, the optocoupler transistor closes and a high level appears at input B5, which is fixed to transmit the corresponding signal.

It should be noted that when a command is given to open the lock, the input B5 will also have a high level, however, it is not fixed by the program, since the time the lock is in the open state is significantly less than the time it is in the closed state.

The resistor Rk in the optocoupler circuit is selected, depending on the selected solenoid, so that the current through the LED is 10-20 mA.

Device circuit board format Sprint-Layout 5.0 downloading .

Firmware

Project files in the environment MPLab IDE can be downloaded.

Source code with description algorithms work is shown below.

The PIC16F628A microcontroller uses an external 20MHz crystal. The watchdog timer is set to the maximum prescaler.

Timer 2 interrupts are used to turn on the buzzer.

The main loop of the program is based on timer 1 and is located in the TIME_UP subroutine, which is executed with a period of about 0.1 sec.

The TIME_UP subroutine, using nested procedures, performs all the necessary actions described below.

In the INIT subroutine, readers are interrogated in turn (external and internal, depending on the IN_FLAG, 0 flag). When the key response flag (ANSWER_FLAG,0) is set, the READ_CODE procedure for reading the key code is executed.

In the READ_CODE procedure, eight bytes of data are read in turn from the key (via the WR_BYTE and R_BYTE subroutines, data exchange is organized on the 1-WIRE bus), after which the CHECK_CRC subroutine counts the CRC (cyclic redundancy code) of the first seven received bytes and compares the CRC with the eighth received byte.

If the CRC does not match, the READ_CODE procedure exits, otherwise the read data is considered valid and processed further.

When executing the CHECK_CODE procedure, the compliance of the read code with that stored in EEPROM is checked.

The storage of key codes in EEPROM is organized as follows. The eight bytes of the first key are stored at addresses 00h to 07h starting from the first byte. Address 7Fh stores the value of the next EEPROM address free for writing the next key (in this case, 08h). The key, the code of which is located at addresses 00h to 07h, is the master key and is used to activate the code saving mode for subsequent keys. The code of the next key (when saving mode is activated) is written to addresses from 08h to 0Fh with 10h entered in the cell with address 7Fh. Thus, it is possible to record up to ten keys. It should be noted that to activate the mode of writing a new key, you must have a master key. Therefore, the master key code must be entered in cells from 00h to 07h (with the entry 08h in the cell with the address 7Fh) at the controller firmware stage.

The CHECK_CODE procedure sequentially checks the compliance of the read code and the data in the EEPROM, starting from a larger address to zero.

Upon exiting CHECK_CODE, the state of the EQUAL_FLAG, 0 flag is checked. If it is set (the code matched), then the lock is opened (the OPEN_LOCK procedure), and if the code of the master key read from the internal contact pad matches, then the MASTER_FL flag is set, 0. The MASTER_FL, 0 flag is used in the LOCK_DRIVER subroutine, where, after 5 seconds, in the case of an unopened door, the new key writing mode is activated (the SAVE_FLAG, 0 flag is set).

If the EQUAL_FLAG, 0 flag is not set upon exiting CHECK_CODE (there is no code match), then the new key writing mode is checked (SAVE_FLAG, 0 flag). When the mode is on and if a new key is read from the internal contact pad, the SAVE_TO_EE procedure is called, where the code of the new key is written (if the limit is not exceeded - 10 keys).

This completes the execution of the READ_CODE subroutine and the transition to the outer loop takes place.

From the foregoing, it follows that after applying the flashed key (including the master key) to any contact pad, the lock opens. The procedure for writing a new key looks like this. While holding the door from opening, we apply the master key to the internal contact pad, while the lock opens, there is an intermittent sound signaling that the door is open. At the same time, if the door is held closed for more than 5 seconds, the lock closes, the intermittent sound alarm changes to continuous, informing that the new key recording mode is enabled. After that, it is necessary to attach a new key to the internal contact pad, after which the code will be saved and the lock will be opened with the already newly recorded key (the continuous beep is replaced by an intermittent one).

Now let's go over the rest of the procedures performed in TIME_UP (with a period of 0.1 seconds).

CHECK_N_CLR_SAVE checks whether the new key writing mode is enabled (SAVE_FLAG flag, 0). When the write mode is enabled, using the SAVE_COUNT counter, it counts down the time (about 5 seconds) until the mode is turned off if the new key code is not read (the initial value is entered into SAVE_COUNT simultaneously with setting the SAVE_FLAG, 0 flag in the LOCK_DRIVER procedure).

CHECK_HERCONS checks the state of the door position control reed switches with fixation of the corresponding modes (flags DOOR_OPEN, 0 and DOOR_H_OPEN, 0). The block diagram of the subroutine algorithm is shown in Fig. 1. 3.

CHECK_LOCK_CONT checks the state of the PORTB, 5 (solenoid circuit control) input when the lock is not open and sets the error flag LOCK_ERR, 0 if necessary.

The CHECK_BUTTON subroutine for processing the state of the SB1 button is not used in this firmware version (initially I wanted to open the door from the inside, then turn on the new key recording mode - in the end I refused everything, since the button can be found by jumping over the fence).

In LOCK_DRIVER, using the counter LOCK_COUNT, the time of the open state of the lock is counted if the door is not opened. After 5 seconds, the lock closes to avoid overloading the solenoid and control circuit. In addition, the flag SAVE_FLAG, 0 is set here after reading the master key.

In the CHECK_TONE subroutine, using the TONE_COUNT counter, the time is counted in pulses and tweeter intervals with the flag FLAG_TONE, 0 set at the time of the pulse. Depending on the state of the lock, the following options for turning on the sound alarm are used: pulses and intervals are equal - the door is open or the lock is open; pulse with a long interval - solenoid circuit malfunction; pulse without interval - new key recording mode is enabled.

In the timer 2 interrupt handler, the state of the FLAG_TONE, 0 flag is checked, and when it is set at the output of PORTB, 4, pulses with a period of 2 timer overflows are generated.

The practice of using the lock in question showed its fairly stable operation throughout the calendar year. Of the identified shortcomings, a significant one is the impossibility of opening the door during a power outage, which is solvable by organizing a battery reserve.

Also, during operation at temperatures below -20 degrees Celsius, freezing of reed switches was observed. However, this did not affect the quality of the door opening - it simply turned off the tweeter after a certain period of time. I put a more powerful magnet: up to - 40 degrees it works fine.

A photo

Here the device is shown in a home environment, so to speak, on a test bench. (Please do not scold too much for the mess - the atmosphere is creative.)

Completion of the lock - connection of thrust with a spring.

The design of the contact pads: the body is made of sanitary brass plugs (15 mm thread), the central electrode is insulated with a rubber insert.

Fixing the device in place. The lock is built into a piece of board.

The location of the reed switches at the end of the board. (A magnet is installed in the door - not shown in the photo.)

View of the door from the street.

Installation completed.

The internal contact area is located below, from the overflowing rain.

This electronic lock is an immobilizer; it uses iButton (Dallas Touch Memory) keys of the DS1990 type as keys for authorization. Each such key has its own unique 64-bit serial number, which is an access code that allows the key holder, for example, to open the door.

The main parameters of the electronic lock:

  • memory up to 15 keys;
  • three operating modes: monostable, bistable and with adjustable relay on time;
  • light indication of lock operation (reader indicator);
  • power supply from 9 to 15 V / 100mA;
  • current consumption in standby mode: 6mA;
  • board dimensions: 34×46 mm;
  • the operation of the scheme was tested in Proteus (link at the end of the article)

Schematic diagram of the lock is shown in the figure below. Its main element is the PlC12F675 microcontroller with an internal ADC. In this case, the ADC of the microcontroller is used to configure the lock parameters.

The microcontroller is clocked from an internal RC oscillator with a frequency of 4 MHz. The circuit is powered by a constant voltage of 12 V. It can be any battery or power supply with a load current of at least 100mA.

Diode VD2 protects the system from the wrong polarity of the input voltage connection. The input voltage is supplied to the DAl stabilizer type 78L05, capacitors C1 ... C4 act as a power filter. A miniature relay of the JQC3FF type (coil 12 DC, contacts 10 A/230 V) was used as an actuating element.

The output signal from the microcontroller (pin 2) goes to the base transistor VT1, in the collector of which the relay coil is connected. Resistor R7 limits the current flowing through the base, while diode VD1 protects the transistor from self-induction EMF that occurs when the relay is turned off.

Resistors R2 and R3 together with R1, depending on the position of jumper JP2, determine the mode of operation of the lock:

  1. If jumper JP2 is not installed, then a high level voltage is applied to input G4 through resistor R3. In this case, the microcontroller will switch the lock to monostable mode. In this mode, touching the key to the reader will turn on the relay. This state will be permanent until the power is turned off, regardless of whether the key is reapplied. This mode of operation can be used to work as an immobilizer in a car.
  2. Installing a jumper on the pins will put the lock into bistable mode. In this mode, each touch of the key will switch the relay.
  3. Installing a jumper on the outputs sets the temporary mode of operation. In this mode, touching the registered key to the reader will turn on the relay for a certain time. This time depends on the voltage applied to the G4 input of the microcontroller. To set this value, is used, assembled from the elements R2, R3 and R1. The relay activation time is in the range from l…30 seconds.

As a key reader, you can use a ready-made head containing a two-color diode signaling the working state of the lock. The PROG jumper is used to switch the microcontroller to the key registration mode.

This scheme has been tested in Proteus:

Key Registration

In order for the lock to respond to our key, you must first register it in the microcontroller's memory. Up to 15 keys can be recorded. To switch the lock to registration mode, install the PROG jumper with the power off and turn on the power.

After that, the red LED will flash 10 times and the microcontroller will enter the key registration mode. In this case, all previously saved keys will be deleted from the memory. From now on, the keys to be registered should be attached to the reader in turn.

If an error occurs during registration, this key will not be saved and you must reattach it. An erroneous read from the DS1990 will be indicated by the red LED for one second, while a correct read will be indicated by the green LED.

Errors during transmission can be caused by interference when the key is applied to the reader, so to be sure that the key is read correctly, it should be applied for about two seconds.

After writing the fifteenth key, the microcontroller will exit the key writing procedure and further saving will be impossible. This state is signaled by simultaneous flashing of both LEDs. Now you need to turn off the power and remove the PROG jumper. After the power is turned on again, the lock will be ready for operation.

In a situation where it is not necessary to register all 15 keys, after entering the required number of keys into the microcontroller's memory, turn off the power from the lock and remove the PROG jumper. Only the specified keys will be stored in memory, and only the microcontroller will respond to them.

In normal operation, the microcontroller constantly checks to see if a registered key has been attached to the reader. If an unregistered key is detected, the red diode will light up for one second, and when you touch the registered key reader, it will turn on the lock relay, depending on the position of jumper J2.

The whole circuit fit on a single-sided printed circuit board measuring 34 × 46 mm.

Please note that all changes to the lock configuration, such as changing the relay on time or selecting the operating mode (J2), will only be active after turning the lock supply voltage off and on again.

(30.6 Kb, downloaded: 1 349)

The lock has a simple design and is intended mainly for individual use. The lock works with any type of iButton keys, so you can use existing keys for other purposes. In total, up to 9 keys can be stored in memory, although this number can be easily increased. To authorize the programming process, a master key is used, the code of which is stored in the ROM and cannot be erased or changed by the normal lock programming procedure.

Recently, locks have become widespread, the key to which is an iButton (or touch memory) electronic tablet from Dallas Semiconductor. Such locks are often used on the doors of entrances, as well as inside many institutions. In addition, iButton keys are often used for payments at gas stations and other places. So many people already have iButton keys for something. Therefore, when designing a homemade lock, it is rational to use the keys that the user already has in it. This is exactly what is done in the proposed lock: any type of keys can work with it, since only the serial number stored in the iButton ROM is used, which is in any of their types. In addition, the command to read this number is the same for all types of keys (33H). The family code, which differs for different types, can be anything. It is perceived as another digit of the serial number. It should be noted that the cheapest type of keys is DS1990A.

The castle was designed for individual use and has an extremely simple design. On the front door from the outside there is only a socket for iButton and a door opening LED. Opening the doors from the inside is carried out using a button. As an actuator, a standard latch with an electromagnet is used, which is designed for a voltage of 12V. Key codes are stored in non-volatile memory and can be deleted and added by the user. A master key is used to protect against unauthorized reprogramming of the lock. In total, 9 keys can be stored in memory. This number is dictated by the capabilities of the 1-digit indicator of the number of the programmable key. If you also use letters, you can increase the total number of keys to 15. This is done by changing the value of the MAXK constant in the program. In the same way, you can reduce the maximum number of keys.


Rice. 1. Schematic diagram of the lock

Schematic diagram of the lock is shown in Figure 1. The design is based on the U1 microcontroller type AT89C2051 from Atmel. A 7-segment indicator is connected to port P1, which is used when programming keys. The SB1 button connected to the P3.7 port is also intended for the same purposes. Key serial numbers are stored in EEPROM U3 type 24C02, connected to ports P3.4 (SDA) and P3.5 (SCL). The external iButton socket is connected to the P3.3 port via the XP2 connector and VD4, R3, VD5 and VD6 protection elements. The pull-up resistor R4 is selected according to the single-wire bus specification. Parallel to the external socket, an internal XS1 socket is also connected, which is used for key programming. The door open button is connected to the P3.2 port via the XP1 connector and the same protection elements as for the iButton. The actuator of the lock is an electromagnet connected via terminal XT1. The electromagnet is controlled by the VT3 key, which is used as a powerful MOS transistor of the IRF540 type. Diode VD7 protects against self-induction emissions. The VT3 switch is controlled by the VT2 transistor, which inverts the signal from the P3.0 port and provides 0/12V control levels at the VT3 gate. The inversion is needed so that the actuator does not work during the reset of the microcontroller, when there is a logical unit level on the port. The 12-volt control levels made it possible to use a conventional MOSFET instead of the more scarce low-threshold (logick level). To indicate the opening of the lock, an LED is used, which is controlled by the same port as the electromagnet, but through a transistor key VT1. The LED is connected through the same connector as the iButton. Since the device must operate around the clock without maintenance, an ADM1232 type U2 supervisor is installed to improve reliability. It has a built-in watchdog and power monitor. On port P3.1, the microcontroller generates periodic pulses to reset the watchdog timer.

The device is powered by a built-in power supply unit containing a transformer T1, a rectifier bridge VD9-VD12 and an integral stabilizer U4. As a backup power source, a BT1-BT10 battery of 10 AA size NiMH batteries with a capacity of 800mAh is used. When the device is powered from the mains, the battery is charged through the resistor R10 with a current of approximately 20mA, which is 0.025C. The mode of charging with a small current is called drip (trickle charge). In this mode, the batteries can be indefinitely long, control of the end of the charging process is not required. When the batteries are fully charged, the energy they take from the power source is converted into heat. But since the charging current is very small, the generated heat is dissipated into the surrounding space without any noticeable increase in the temperature of the batteries.

Structurally, the device is made in a housing measuring 150x100x60mm. Most of the elements, including the power transformer, are mounted on a printed circuit board. Batteries are placed in standard plastic holders, which are fixed inside the case next to the board. In principle, other types of batteries can also be used, such as the 12-volt maintenance-free acid battery used in security systems. To connect an actuator, the board has TB-2 type terminals, all other external circuits are connected through small-sized connectors with a pin pitch of 2.54 mm. The connectors are located on the printed circuit board and are not accessible from the outside of the housing. The wires exit the housing through rubber seals. Since the HG1 indicator, the SB1 button and the iButton XS1 socket are used only during programming, they are placed on the board inside the device. This simplifies the design of the housing and makes it more protected from external influences. On the side panel of the housing there is only a VD13 power-on indication LED. The scheme of external connections is shown in fig. 2.


Rice. 2. Diagram of external connections

When the door is opened, a pulse lasting 3 seconds is applied to the electromagnet. The logic of the device is such that if the door opening button is held down, then all this time the electromagnet will be energized and, accordingly, the door will be open.

The lock can have a maximum of 9 keys, plus one master key. The key codes are stored in non-volatile memory under numbers from 1 to 9. The master key code is stored in the ROM of the microcontroller and cannot be changed. Programming new keys or erasing old ones can only be done with a master key. Like other keys, the master key can be used to open the lock.

To program a new key, do the following:

  1. Press the programming button.
  2. The letter “P” will appear on the indicator, which means entering the programming mode.
  3. Touch the panel with the master key.
  4. The indicator will show the number "1", which indicates the number of the programmable key.
  5. Use the button to select the desired number.
  6. Touch the panel with any key.
  7. The number on the indicator will start flashing, indicating that it is ready for programming.
  8. Touch the panel with the key whose code you want to memorize.
  9. In case of successful programming, the figure on the indicator will stop blinking and will start to burn constantly.
  10. To exit the programming mode, you just need to wait 5 seconds, after which the indicator will turn off.

Schematically, the process of programming a new key is shown in fig. 3.


Rice. 3. Programming a new key

If you need to program several keys, you can immediately go from step 9 to step 5 and repeat steps 5-9 as many times as necessary.

If, after step 7, it turns out that the wrong number has been selected, then to avoid losing the key code under this number, you can press the button or simply wait 5 seconds. In the first case, the current number will increase by one, and the contents of the memory will remain unchanged. In the second case, a complete exit from the programming mode will occur without changing the codes. In general, you can exit programming at any time if you pause for more than 5 seconds.

To erase an extra key from the memory, the sequence of actions remains the same as during programming, only all actions are performed by the master key. Those. the erasing process is actually writing the master key code to unused numbers.
Schematically, the process of erasing an extra key is shown in Fig. 4.


Rice. 4. Erase extra key

During programming, you can open the door with the button, but opening with iButton is blocked. Since the inner and outer sockets are connected in parallel, care must be taken that during programming no one touches the outer socket with any keys.

The master key code is written to the ROM of the microcontroller programs, starting from the address 2FDH. The code length is 8 bytes. The sequence of numbers should be the same as on the touch-memory case, you need to read from left to right. Those. at address 2FDH, the checksum value is entered, then at addresses 2FEH - 303H, six bytes of the serial number, starting from the high byte, and, finally, at address 304H, the family code. For example, the code as a whole might look like this: 67 00 00 02 D6 85 26 01.

The electronic lock program has a main loop, the block diagram of which is shown in fig. 5. In the main loop, the socket is polled, and if a key is found there, then its code is read. Then this code is checked, and if it matches the code of the master key or any other key (user key) stored in memory, the lock opens. The state of the door opening button is also checked, and if a press is detected, the lock also opens.


Rice. 5. Block diagram of the main cycle of the program

There are two subroutines for handling events related to programming: PROGT and PROGS, the block diagrams of which are shown in fig. 6. The first one is called when the key code is read in the programming mode, the second one is called when the programming button (NUMBER) is pressed. The programming process is divided into 3 phases. When the NUMBER button is pressed, programming is entered, i.e. transition to phase 1. In this case, the letter “P” is displayed on the indicator. The key codes that are read after that are checked for a match with the master key code, since only it can allow programming to continue. If such a match occurs, then the transition to phase 2 is carried out. The indicator displays the number of the current key, which the NUMBER button can change. If a key touch is registered again, a transition to phase 3 will occur. Another key touch will lead to storing its code and returning to phase 2. By pressing the NUMBER button, you can also return to phase 2, but without changing the contents of the memory. Any action in programming mode causes the reset of the return timer, which has an interval of 5 seconds and is checked in the main loop. If a reset of this timer is detected, the programming mode is exited.


Rice. 6. Block diagrams of subroutines used when programming the key code

Shown in fig. Flowcharts 5 and 6 are greatly simplified, but they allow you to understand the general logic of building a program.

The described castle, of course, does not have a wide range of possibilities. However, it is very simple, which makes it easy to repeat. The open source code of the program allows you to independently improve the design or adapt it to specific requirements.

Download files:
lock.asm - source code for the lock program.
lock.bin - compiled program.