12 June 2017

Super Easy AVR Assembly Programming

From this May on, I somehow got interested again in assembly language. It's been more than 25 years since my last assembly programming, during and a bit after my university years. I've been playing around with the Atmel’s microcontrollers for some time now, so I thought I would better try something with them. A simple Attiny13 blinker for instance ... My previous experience on assembly language, apart from being ages old, also concerned (as might expected) an ancient chip ... the 6502 microprocessor. Who really knows about it know? ...

Honestly, I didn't really believe that writing and testing even a small assembly program would be an easy task for me, but surprisingly I found out the opposite. It's very very easy and you don't even need any expensive programmer to do it...
It is so easy that I decided to write this article about it, as to encourage others, who might think of it as something unachievable. I don't imply that this "project" presented here is for everyone, but if you already are into Arduino, with stand alone chips and AVR C programming, it definitely won't be hard...

You will just need AVR Studio 7 and the later version of PonyProg software (Version 2.08d Beta Apr 2 2017), plus a pony serial programmer clone.
Obviously you will need some basic reading and understanding about the AVR architecture and assembly language. I recommend this book: "Programming and Interfacing ATMEL's AVRs" 1st Edition by Thomas Grace. Examples from this book used for the following code. Atmel's website and other web resources are also helpful. Be aware that, from my own small experience, the following instructions work only with the software mentioned and the Atmel Studio assembly syntax!

So if you don't already have it, download and install AVR Studio 7. Do the same for PonyProg.

A. Write and debug your assembly program.
Start ATMEL studio -> New file -> Assembler.
Name it as "at13blink", select Attiny13 as the device.
Type this "simple" assembly code on Atmel Studio. (Or just copy the AVR Studio project from here)
The code below is almost self explanatory, if you have studied basic assembly syntax and keywords.

;
; at13blink - test
;
; Created: 6/5/2017 2:56:37 μμ
; Author : A. Pappas
; Default  1.2Mhz Internal Clock assumed (9.6MHz/8). 
; MechineCycle period 0,8333us


.org 0x0000
start:
    ldi r16, 0x0f   ; Set the first four bits of r16 to 1 
    out ddrb, r16   ; Set PB0 to PB3 of PortB to output
    ldi r17, 0x00   ; Load all bits r17 with 0
    ldi r18, 0b11   ; First and second (LSB) of r18 set to 1
top:
    out portb, r18  ; Set PB0, PB1 ON for 1s (4x250ms)
    rcall delay_100ms   ; Call the 100ms delay routine
    rcall delay_100ms
    rcall delay_100ms
    rcall delay_100ms
    out portb, r17       ; 1s (4x250ms) OFF
    rcall delay_100ms
    rcall delay_100ms
    rcall delay_100ms
    rcall delay_100ms
    rjmp top

delay_1ms:                ; Roughly 1200 machine cycles 
    ldi r19, 120
delay_loop1:
    nop                    ; 1st cycle
    nop
    nop
    nop
    nop
    nop
    nop
    dec r19                ; 8th cycles
    brne delay_loop1    ; 10th machine cycles, loop 120 times
    ret

delay_100ms: ; Actually 1 to 255ms. Change the "ldi r16, 250" to the number of ms you want
    push r16
    ldi r16, 250
delay_loop2:
    rcall delay_1ms
    dec r16
    brne delay_loop2
    pop r16
    ret


Save the file, build the program.
In the directory of the project, subdirectory "Debug" you can now find the "at13blink.hex" file. (Or whatever name you used).
This is the file that must be uploaded to the microcontroller for your project to become alive.

B. Load the program to the Chip.
To upload the file you either need a programmer supported by Atmel Studio or another much simpler and super cheap, the Pony Serial programmer. This article used Pony Serial. The Web is full of Pony programmer implementations. I just made my own on a breadboard. It took me more to solder the DB9 serial plug than to put together the few components needed. (You can find a nice article at http://www.circuitvalley.com/2011/04/avr-serial-port-programmer.html )
On my breadboard I provided the necessary voltage regulator (78L05) to power the chip and programmer. An Led should be conected to either PB0 or PB1 (or both). Once the programmer is finished connect it to the serial port.



Start PonyProg, calibrate it, and load the "at13blink.hex" file.
Select  Device -> AVR micro -> ATtiny13.
Click Command -> Write Program (Flash) and you should be all set.

If things doesn't go as expected check the cable connections, especially the ones from serial to ICSP.
This small "project" has been actually constructed, double-checked and working fine. It actually serves as a proof of concept.











No comments:

Post a Comment