A custom approach to programming a microcontroller

This article will discuss a non-standard approach to creating a program for a microcontroller. For example, we will build a “code lock” project; the microcontroller program will be written in the Horizont Configurator visual environment.

A simplified schematic diagram of the project is presented in the following figure. In this figure, the power leads are not displayed, and an LED is also used as an actuator. The ATmega8 microcontroller is selected as the manager.

image


To implement the project, it is necessary to use 4 outputs of the microcontroller, namely data input from the “button field 0-9”, data input from the “save code” button, output for controlling the “lock” actuator, output for confirming the input of the digit “digit”.

To enter the code, a typesetting field 0-9 is used, made in the form of a voltage divider with a variable division coefficient. The coefficients are selected as follows, when you press 0, the output generates a voltage of 0 volts, 1–0.5, 2–1.0, 3–1.5, 4–2.0, 5–2.5, .., 9–4.5, and in the free state, 5 volts.

image


To measure the potential from the "button field 0-9" I used the output of the microcontroller associated with the ADC, this is "PINCO". In order to measure the voltage at the PINCO pin, ADC_ATmega8A and ADC PinCO blocks have been added to the canvas.

image


The ADC_ATmega8A block adjusts the parameters of the analog-to-digital converter built into the microcontroller; in the settings it indicated the value of the reference voltage equal to the supply voltage of the microcontroller. Thus, I configured the ADC to measure voltage in the range of 0-5 volts. To obtain the voltage value at the pin of the PINCO microcontroller, I used the ADC PinCO block. A discrete signal in the range 0-1023 proportional to the voltage at the PINCO pin will be received at the block’s output, with 0 volts corresponding to 0 and 5 volts to 1023. To bring the voltage to a value convenient for us, I used the “division” and “constant” block ".

image


By dividing the input range 0-1023 by 90, we get the range 0-11, which is convenient for us, from 0-11. Moreover, pressing the “button 0” will correspond to the value 0, 1-1, .., 9-9, and in the free state (no button has been pressed), the value will be 11. To fix the steady state event of the line, I use the “delay” block and block "equality".

image


At the output of the “delay” block, we get the value that was at the input of the “delay” block in the previous working cycle. If the values ​​in the previous cycle and in the current are equal, we fix the stable state of the line, while at the output of the “equality” block there will be a logical 1 (true) signal, otherwise 0 (false). To filter the value 11 of the "free state of the line" we use the blocks "less", the constant 11 and the logical unit "AND".

image


The logical unit will be present at the output of the “logical AND” block if the line is in a stable state and the value after the divider is less than 11. To limit the permissible speed of entering values ​​from the “key field 0-9”, this increases the noise immunity and limits the speed of “code selection”, I use the block "on delay" and the block "millisecond counter" necessary for it.

image


For the final generation of the “digit input” signal, we will track the rising edge and, using this signal, we will generate a single “digit input” pulse. To do this, use the "RTRIG" block.

image


In order to read the circuit and reduce additional connecting lines, I will add “transitions” of the connecting lines. The first transition is the “entered digit”, the second is the “input signal of the digit”. We use the output that controls the LED “the number is accepted”, in my circuit it is the output of the PinB6 microcontroller.

image


We will use the “entered digit” and “digit input signal” signals, as well as the “memory” blocks for storing discrete numbers, and organize the logic for storing the last entered numbers. In my case, I used four “memory” blocks, which will determine the security of the castle in four digits.

image


The logic works as follows when a “character input signal” is received, the value “entered digit” will be written to the block “memory 1”, in turn, the value previously contained in the block “memory 1” will be written to the block “memory 2”, etc. In the blocks “memory 1” - “memory 2” the last entered numbers will be stored. Moreover, in the block “memory 1” the last digit entered will be stored, in the block “memory 2” - the penultimate, etc. Add “transitions” for the values ​​of the input characters.

image


To store the lock code, we will use the non-volatile EEPROM memory of the microcontroller. Each digit of the code will be stored in a separate EEPROM cell. In our case, we need 4 memory cells. We’ll select the addresses of the EEPROM cells for storing the code. For my project, I selected cells 10, 11, 12, 13.

image


To read the value stored in the EEPROM cells, we will use the “read EEPROM byte” block.

image


As a result, we have the numbers entered from the “button field 0-9” and the numbers obtained from the EEPROM cells, it remains to make a comparison of the numbers and form a control signal for the actuators of the lock. For this task, I used the “equality” block and the “logical AND” block.

image


At this stage, the logic of the code lock is fully operational, the only thing left is to “teach” the microcontroller to independently save the lock code necessary for the user in the EEPROM memory. For this task, I use the external signal “save code” and use this signal to generate an EEPROM recording pulse, for this I will use the PinD6 logical input blocks and the RTRIG block, and for convenience I will create the corresponding transition.

image


We will build the logic for writing data to EEPROM, all signals and data have already been generated earlier, we will directly use the “write byte to EEPROM” block to write data to EEPROM.

image


The logic of the code lock is described, the general view of the project is as follows.

image


Putting together a project:

image


After building the project, we received an executable * .hex file of 1450 bytes in size, we conclude that we can use a microcontroller with a flash memory size of 2 kbytes.

Testing the project:


Also popular now: