Sunday, March 27, 2011

Atmega168 Hello World.

Hey all,Below is some very simple Assembly code for the Atmel Atmega168

First of all is the circult:



Here is the code.


.NOLIST
.INCLUDE "m168def.inc"
.LIST

.DEF mp=r16

rjmp main

main:
ldi mp,0xff ; This added all 8 bits to true in MP reg
out DDRC,mp; ;Turns DDRC into output pins by loading 8 true bits from MP reg into DDRC
loop:
ldi mp,0xff
out PORTC,mp


rjmp loop;




I am running a linux box and compiling using avra but you can use gavrasm aswell which you can get from
http://www.avr-asm-tutorial.net/gavrasm/index_en.html

sudo apt-get install avra


and to burn to the chip you will need to use avrdude.

sudo apt-get install avrdude


To compile using this, we simply run the following command.

avra main.S


which returns
Pass 1...
Pass 2...
done

Used memory blocks:
Code : Start = 0x0000, End = 0x01C7, Length = 0x01C8

Assembly complete with no errors.
Segment usage:
Code : 456 words (912 bytes)
Data : 0 bytes
EEPROM : 0 bytes


This will produce main.S.hex
This hex file is what we use to burn our program into our chip.

This part will depend on your programmer.

I am using a Home Made Direct Serial Programmer(BSD), Find more information on this programmer here:
http://avrprogrammers.com/parallel.php

So for me, once i have plugged in some BSD Programmer and have plugged in the Mosi,Rst,Miso and Sck cables i am ready to program..

I use avrdude and run

sudo avrdude -p atmega168 -P /dev/parport0 -c bsd -b 19200 -F -u -U flash:w:main.S.hex


Which returns to me

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: Expected signature for ATMEGA168 is 1E 94 06
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "main.S.hex"
avrdude: input file main.S.hex auto detected as Intel Hex
avrdude: writing flash (912 bytes):

Writing | ################################################## | 100% 0.34s

avrdude: 912 bytes of flash written
avrdude: verifying flash memory against main.S.hex:
avrdude: load data flash data from input file main.S.hex:
avrdude: input file main.S.hex auto detected as Intel Hex
avrdude: input file main.S.hex contains 912 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.30s

avrdude: verifying ...
avrdude: 912 bytes of flash verified

avrdude done. Thank you.


Now i am Producing a soild Green glowing LED.


Notes about code:
First of all i am using the register R16 to store my buffer which i will use.
I give it a variable name of mp instead of having to type out R16 everything i need to use that register.

First thing i do is turn on the DDRC Register to be an output.
i do this by making mp = 0xFF OR 0b11111111( all bits true)
and then load mp into DDRC register, turning DDRC on as an ouput.

Now i can refer to its physical pins via the register PORTC.
So to turn on all the pins to Logic high(true(< 3.5volts))) we first want to move 0xff into MP (all bits true) and then load MP into PORTC.

Simpe enough....

If you don't understand this i would highly recommend that you read these AVR asm tutes at
http://www.avr-asm-tutorial.net/avr_en/beginner/index.html
This is a great site to learn how to program AVR's in Assembly

No comments:

Post a Comment