Those. the task is to be able to program it with the help of buttons connected to the microcontroller without flashing and without writing an interpreter (the first thing that came to mind was fort). Those. save and call a function in which a limited set of commands is used, one numeric parameter is accepted and one numeric parameter is returned. Thank!

Explanation

It is necessary:

  • Boot
  • Generate bytecode directly in the program
  • Pass it execution
  • After the end of his work, return to the place of the call and get the result.

An example of the simplest subroutine would be: var + 1.

    4 answers 4

    Answer YES :) I have not tried it myself. Fragment of an article from Habr

    The most common and convenient interface for AVR firmware is the SPI (Serial Peripheral Interface). To connect via SPI you need only four wires, not including ground:
    SCK - clock signal, synchronizes all data exchange operations;
    MOSI (Master Out Slave In) - data line from master to slave;
    MISO (Master In Slave Out) - data line, on the contrary, from the slave to the master;
    RESET - to enable SPI firmware, you must apply a logical "0" to this output.

    Scheme: buttons from VCC to pins Reset, Mosi, Sck, after pressing pulled to the ground resistors. Pin GND to ground, with pin Miso LED with ground resistor.

    Programming mode is activated by applying “0” to RESET leg. But there are some subtleties. Atmel recommends that you first put a low level on the RESET and SCK outputs, and only then supply power to the controller. If this is not possible, after powering up, apply “0” to SCK, and then a positive impulse to RESET

    Next, you need to send a command to actually enable the programming mode: 10101100 01010011 xxxxxxxx xxxxxxxx
    Bits labeled x can be any. During the transmission of the third byte, the controller must send back the second byte (01010011). If this happens, then all is well, the command is accepted, the controller is waiting for further instructions. If the answer is different, you need to restart the MC and try it all over again.

    First you need to load data into the page buffer, for this you use the command “Load Program Memory Page”
    01000000 000xxxxx xxxxbbbb iiiiiiii - to load the low byte of the word, and 01001000 000xxxxx xxxxbbbb iiiiiiii - to load the high byte.
    The 4 low bits of the 3rd byte of the bbbb command are the address of the word on the page, iiiiiiii is the download byte. First, the low byte of the word must always be loaded, and then the high byte of the same word.

    After the page buffer is loaded, you need to execute the "Write Program Memory Page" command
    01001100 0000000a bbbbxxxx xxxxxxxx to write the page directly to the controller's memory. The low bit of the second byte and the high 4 bits of the third a: bbbb is the five-bit page number to write.

    It is better to look at the link in details. In case of an uninhabited island, print the datasheet, laminate, keep with you.

    • Thank you, but unfortunately it is not that. Here about the programming of all microns. And I need only one procedure to write to the memory and the means of the controller itself. - Aleksander K.
    • Roughly speaking, I want to change the logic of behavior by changing a pair of switches? You can store data in the EEPROM, which saves information after shutdown. You can just make a block with branching "if switch 1, then relay 2, if switch 2, then relay 1" and so on. It seems to me, then easyelectronics.ru/example-virtualnoj-mashiny.html will fit - Alexey Prisyazhny
    • I'm afraid the interpreter will take a lot of CPU time, you need to execute the subroutine 8 thousand times per second. - Aleksander K.
    • 8 kilohertz on a controller capable of 8 megahertz? About a thousand cycles per action ??? If speed is really needed, easyelectronics.ru has a good series of lessons, specifically in assembler - Alex Prisyazhny

    Can. See the datasheet section Self-Programming the Flash. But, if there are no restrictions, it will be more convenient to take a micron with a large amount of memory. Your "interpreter" will be easier to accommodate :) Although the task is certainly interesting and useful

      It is possible, but very difficult. It is necessary to enter the program manually with zeros and ones. Easier to take arduino nano or another microcontroller.

      • This is understandable) There will be two buttons, there will be an alphabet of binary codes (or the compiler forth) that will be translated into opcodes. I am interested in the very moment of programming myself: a procedure that stores binary data of a subroutine in a memory area and transfers control to this area. - Aleksander K.

      ATTiny13 has very little memory for such feats - it is better to use MK with a large memory size or an external eeprom. Then, as a matter of fact, you will only need to read the program on SPI or I2C from the EEPROM. The translator will receive, for example, 2 bytes each, where the 1st byte is the function code, the 2nd byte is the argument. In this case, it will look like a set of if-else or case-switch, or an array of pointers to functions. In the first and second cases, check the function code in the if-else or switch headers and execute pieces of code specified in the bodies of conditional operators. In the case of an array of pointers to functions, simply call the function pointed to by the array element with the number equal to the function code. Something like this) SPI and the samopisny protocol of type number_function-argument can be used to enter a program.