Writing AVR Programs

Write the program in C, then translate to AVR.

Example: sum of first 4 integers.

sum = 0;
for (i = 1; i <= 4; ++i)
   sum += i*i;
can be translated to
sum = 0;
i = 1;
loop:
   if (i == 5) goto end;;
   sum = sum + i;
   i = i + 1;
goto loop;
end:
can be translated to
; sum in R16
; i in R17
; R18,R19 used for temporary values
    LDI R16, 0
    LDI R17, 1
    LDI R18, 5
    SUB R18, R17
    BRBS 1, 22
    ADD R16, R17
    LDI R19, 1
    ADD R17, R19
    JMP 2
    SLEEP

Index