Is there a simple way to create an hex file to be sent to the chip for programming via serial, from a simple few lines of code ?
for example, how you create an hex from this basic thing :
Is there a simple algorithm to do this ?
Well, I assume that you know that "Verify" or "Upload" in your Arduino IDE will compile and/or compile and upload (send to your Arduino the finished, compiled program) as binary machine code in the form of an Intel IHEX file that the bootloader turns back into pure bytes and writes them to the flash memory of your board...
For example, one line of an IHEX file that the Arduino IDE generates looks something like this:
It contains information:
** **:[color=red]10[/color] [color=green]0000[/color] [color=blue]00[/color] [color=purple]64C0A9C072C071C070C06FC06EC06DC0[/color] 46** **
The colon ":" means this is the start of a line of hex data
Red is how many bytes the line contains (0x10 == 16 decimal)
Green is the memory address that the code will go into (here 0x0000 to 0x000F)
Blue is the "record type" and 00 means "Data Record" (see ** below)
Purple is the 16 bytes of actual data
Black is the checksum of the line derived from "0xFF - 8 bit rolling sum of the data"
When this code is uploaded into your Arduino board, the memory will be written like this:
The next line will start at address 0x0010 and contain 16 more bytes up to 0x001F.
This goes on until all of your code is uploaded and written to flash memory. Then when you reset (uploading automatically does this, or you can press the button manually), the Arduino goes to address 0x0000 and start executing code from there (i.e. runs your sketch).
** Data type codes:
If this isn't what you're asking about, then you must want to do something else. Please elaborate what you want to do so maybe I can help you.