09/09/2023
Checksum calculation[edit]
A record's checksum byte is the two's complement of the least significant byte (LSB) of the sum of all decoded byte values in the record preceding the checksum. It is computed by summing the decoded byte values and extracting the LSB of the sum (i.e., the data checksum), and then calculating the two's complement of the LSB (e.g., by inverting its bits and adding one).
For example, in the case of the record :0300300002337A1E, the sum of the decoded byte values is 03 + 00 + 30 + 00 + 02 + 33 + 7A = E2, which has LSB value E2. The two's complement of E2 is 1E, which is the checksum byte appearing at the end of the record.
The validity of a record can be checked by computing its checksum and verifying that the computed checksum equals the checksum appearing in the record; an error is indicated if the checksums differ. Since the record's checksum byte is the two's complement — and therefore the additive inverse — of the data checksum, this process can be reduced to summing all decoded byte values, including the record's checksum, and verifying that the LSB of the sum is zero. When applied to the preceding example, this method produces the following result: 03 + 00 + 30 + 00 + 02 + 33 + 7A + 1E = 100, which has LSB value 00.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Conclusionat de mine! Cioe tutto in hex
So... for
:10010000214601360121470136007EFE09D2190140
End of file:
:00000001FF
Start code= ":" Byte count="10" Address=0100 Record type="00" (01 per end code) Data="214601360121470136007EFE09D21901" Checksum="40"
10+01+00+00+21+46+01+36+01+21+47+01+36+00+7e+fe+09+d2+19+01=3C0
Checsum :100-C0=40 EXACT!!! SAME!!!
-------------------------------------------------------------------------------------------------------------------------------------------------------
You know that you can write directly opcode and load to microcontroller?
I know , is a suicide mode ... but you understand better how processor works, is not a mode of programming!
So ... memory of arduino begin with interrupts... the "reset interrupt" ,for example when push reset button , have address 0... so at adress 0 need to write address where need to go for find your program !
So from 0 to 54 is reserved from interrupts ... mean that need to write program after address 54 ... so begin maybe with address 60.
Keep in mind that these address is in hexazecimal!!!
So ... open hexeditor... from example this online hexeditor https://hexed.it/
(For those who not know hexazecimal : the numerating decimal is 0,1,2,....9 but hex numerating is 0,1,2,....9,A,B,C,D,E,F
mean that numer 10 decimal have 10 sign and 10 hexa have 16 sign...
E hexa =14 decimal
F hexa =15 decimal
10 hexa =16 decimal
11 hexa =17 decimal and go on
Hexa is represent by $sign ... so $11=17 and from now on we will use only hex numbers)
So at address 0 need to write :
00 rjmp $60 >>>rjmp mean jmp at address
rjmp have menemonic $C0 .... mean that when processor receive number $C0 he knows that need to jump ... but where?...
The second number represent how many locations need to jump
So ... when processor receive $C02F ... he know that need to jump $2F locations
And now ... a little introducing about how processor read memory instructions:
The processor read 2 location , and execute ,after read another 2 location and execute...
But memory is organized as first bit is less important...that mean that processor read second number first and interpret as function
So ... if our command is $C02F that mean jump $2F locations (attention JUMP $2F LOCATIONS not JUMP AT $2F LOCATION!!!)
Need to write $2FC0 because microprocessor begin read second location that is $C0 (jump)
So finally you can write $2FC0 that mean JUMP $2F LOCATIONS ...That mean after $2F location is address $60
Now at $60 address you can write your code!
First need to set register at OUT.
8F EF LDI r24, 0xFF
84 B9 OUT 4, R24
Now a little theory about registers.
The register is locations of another little memory(not memory program)
and have a set of temporary register from 0 to 32 where can
be load data , the processor work only with values stored in these registers , not directly...
and another set of register where contain information about periferics ( for example ports ,that is pins where can connect a led)
This pin can be input or output in function of what value contain these registers.
Any register have a specific function ...
For example $24 register control 8 pin (Portb)
So ... I can control PB0 to PB7 pins with $4 register
Now a little theory about binary numerotation ... If for hexa i adeed few extrasign A,B,C,D,E,F
Now for binary i use only two sign ... 0 and 1
Binary is represent by B
So 0 decimal=0 binar,1 decimal=1 binar,2 decimal=10 binar ... so in another words:
0B=0 , 1B=1, 10B=2 , 11B=3
A register contain 8 little location where can write only 1 or 0
So ... my register $4 that control 8 out pin have 8 bit.
each bit controls a gate ... so if i write in $4 register B00000001 ... mean that i set PB0 as output
if i write B11111110 means that all PB1 to PB7 is as output but only PB0 is as input
0 is for input and 1 for output
So... if i want command a led need to set port as output ... but if i want read a senzor need to set port as input!!!
So... if i put a led on PB0 and need to command it , need to set bit0 from $4 register
Attention ... exist many type of register ... one that control peripheal , another can store data temporally ,ecc
So when want to write to register $20 for example , need to know wich register is , ecause $20 can be a location of memory, $20 can be temporary registers ,$20 can be input /output register
So same name ... but different registers!!!
Now return to own program!
sa continui cand am timp ... am ramas sa caut tabela cu opcode ... si nu o gasesc!
PB5 ( D13 ) Led
Register
$4 DDRB
$5 PORTB
0000:
2FC0
00-$54 vector intrerupere
incepem cu $60
:$60
DDRB = 0xFF; //Nakes PORTC as Output
8f ef ldi r24, 0xFF
84 b9 out 4, r24
PORTB = 0xFF; //Turns ON All LEDs
8f ef ldi r24, 0xFF
85 b9 out 5, r24
-----------------------------------------------
PORTB = 0; //Turns OFF All LEDs
80 e0 ldi r24, 0
85 b9 out 5, r24
Many people try to understand how avr microcontrollers working!
And many for them ... think to modify or create programs in low mode.
For this i create this page to explain that is not easy, is a complicated mode.
So...i have blinking led.c file.For title you know what make it.
This file is write in c avr asm... that is low language possible known by all programmers...
but yet is not very low!
The low language is opcode (click here for see table of opcodes), is impossible working in this mode! The microprocessor can read only this numbers... so ... for this in arduino is load a hex file, language that microprocessor understand!
First try to load a mini program in arduino with minimal components.
The popular program is avrdude .
You need download these files in your folder that use for your test.
When you upload a file to microcontroller (arduino nano has a atmega32 microcontroller) , you can use avrdude for example for upload, will begin write by adress 0.
But in microcontroller memory begin with interrupts, that mean is allocates adress for type of interrupts and after this begin the code.Any type of microcontrollers have divers interrupts.
for example the
-----------------------------------------------------
Arduino nano
0x03 PINB
0x04 DDRB
0x05 PORTB
0x06 PINC
0x07 DDRC
0x08 PORTC
0x09 PIND
0x0A DDRD
0x0B PORTD
PB5 led onboard
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
https://github.com/Protoneer/grbl-master
Grbl v0.8 v0.9
D8 PB0 stepper enable/disable
D7 PD7 direction Z
D6 PD6 direction Y
D5 PD5 direction X
D4 PD4 step Z
D3 PD3 step Y
D2 PD2 step X
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
test.bat create hex from asm
avra test.asm
test1.bat upload to microcontroller
mode com3 BAUD=9600 PARITY=n DATA=8
avrdude -p m328p -b 57600 -P com3 -c arduino -U flash:w:test.hex
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
test.asm
.device ATmega32
.equ PORTD = 0x0B
.equ DDRD = 0x0A
.equ PORTB = 0x05
.equ DDRB = 0x04
.org 0x0000
jmp main
.org 0x0060
main:
;PD5 grbl direction pin
;ldi r24, 1 ;PB0 enable x
;out DDRB,r24
;out PORTB,r24
ldi r24, 0b100100 ;PD5 direction x and PD2 step x
out DDRD,r24
loop:
ldi r24, 0b100000 ; STEP (PD2) HIGH
out PORTD,r24
ldi r18, 2
ldi r19, 160
ldi r20, 147
L1: dec r20
brne L1
dec r19
brne L1
dec r18
ldi r24, 0 ; STEP (PD2) LOW
out PORTD,r24
ldi r18, 2
ldi r19, 160
ldi r20, 147
L1x: dec r20
brne L1x
dec r19
brne L1x
dec r18
rjmp loop
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
http://www.bretmulvey.com/avrdelay.html
------------------------------------------------------
PB5 ( D13 ) Led
Register
$4 DDRB
$5 PORTB
0000:
2FC0
00-$54 vector intrerupere
incepem cu $60
:$60
DDRB = 0xFF; //Nakes PORTC as Output
8f ef ldi r24, 0xFF
84 b9 out 4, r24
PORTB = 0xFF; //Turns ON All LEDs
8f ef ldi r24, 0xFF
85 b9 out 5, r24
-----------------------------------------------
PORTB = 0; //Turns OFF All LEDs
80 e0 ldi r24, 0
85 b9 out 5, r24
---------------------------------------------
<__stop_program>:
ff cf rjmp .-2
avr assembler
http://avra.sourceforge.net/README.html
de terminat
link to google drive unde am salvat avrdude minimal
contiene avra.exe che compila asm:
https://drive.google.com/open?id=0BzDuTPB5XlKOMm1jM29wUk1EQWc
contiene avra.exe ma anche bin2hex e disavr
https://drive.google.com/open?id=0BzDuTPB5XlKOalVhMkI2VkQzWUE
http://www.atmel.com/images/doc1022.pdf
http://www.alberti-porro.gov.it/wordpress/wp-content/uploads/2014/01/ProgrammareArduino.pdf