<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ntsdt.net &#187; atmel</title>
	<atom:link href="http://ntsdt.net/tag/atmel/feed/" rel="self" type="application/rss+xml" />
	<link>http://ntsdt.net</link>
	<description>never the same day twice</description>
	<lastBuildDate>Wed, 02 Jun 2010 16:35:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>first steps with atmel assembly</title>
		<link>http://ntsdt.net/2008/08/08/first-steps-with-atmel-assembly/</link>
		<comments>http://ntsdt.net/2008/08/08/first-steps-with-atmel-assembly/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 01:26:57 +0000</pubDate>
		<dc:creator>lukus</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[atmega]]></category>
		<category><![CDATA[atmel]]></category>
		<category><![CDATA[avr studio]]></category>
		<category><![CDATA[electronics]]></category>

		<guid isPermaLink="false">http://blog.ntsdt.net/?p=85</guid>
		<description><![CDATA[I&#8217;ve been experimenting with Arduino for quite a while and have had a lot of fun using it.  Arduino is an open-source hardware platform which has become very popular with digital-artists, hardware-hackers and people interested in finding ways to make computing more physical.
While the Arduino IDE is suitable for most tasks, there are times [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been experimenting with Arduino for quite a while and have had a lot of fun using it.  Arduino is an open-source hardware platform which has become very popular with digital-artists, hardware-hackers and people interested in finding ways to make computing more physical.</p>
<p>While the Arduino IDE is suitable for most tasks, there are times when more granular control over the way that the ATMega microprocessor executes instructions is useful.  This is where an understanding of Atmel&#8217;s assembly language comes in handy.</p>
<p><span id="more-85"></span>To be honest, I initially thought that assembly language would involve concepts that my brain would have a lot of difficulty comprehending.  Luckily (for me) the process turned out to be far simpler than I initially imagined, and I was able to start using Atmel Assembly last night.  I decided to post my experiences here in case it helps anyone else (and to cement the info in my own mind).  Thanks go to a guy called eadthem on #freenode irc for help and guidance.</p>
<div id="attachment_229" class="wp-caption alignleft" style="width: 778px"><img class="size-full wp-image-229" title="atmega8" src="http://ntsdt.net/wp-content/uploads/2008/08/atmega8.jpg" alt="ATmega8 Microprocessor" width="768" height="576" /><p class="wp-caption-text">ATmega8 Microprocessor</p></div>
<h3>AVR Studio</h3>
<p>One of the great things about the ATmega chip (which is the family of microprocessor chips at the heart of the Arduino platform) is the range of free development tools which it&#8217;s parent company Atmel provides. In this case, I used an IDE called AVR Studio 3.5 (<a href="http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2724" target="_blank">http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2724</a>)</p>
<p>As I run linux, I needed to use wine to run AVR Studio.  There wasn&#8217;t any problem doing so.  Wine decompresses to a folder in the fake &#8216;windows/temp&#8217; directory&#8230;. you simply need to navigate and open &#8216;~/.wine/drive_c/windows/temp/cdrom/SETUP.EXE&#8217; which runs and completes the installation process.  After installation, Atmel Studio was available in application menu (Applications -&gt; Wine -&gt; Programs-&gt;Atmel AVR Tools-&gt;AVR Studio 3.56).</p>
<h3>Obtaining Supporting Documentation</h3>
<p>There are various versions of the ATmega chip in production &#8211; and each has a different range of capabilities associated with it. I&#8217;m using an Atmega8 microprocessor, so it was important that I made sure I had the relevant datasheet at hand and the AVR 8-bit Instruction Set documents.  You can find these via the links below.</p>
<p>[*1] (<a href="http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf" target="_blank">http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf</a>)</p>
<p>[*2] (<a href="http://www.atmel.com/dyn/resources/prod_documents/doc0856.pdf" target="_blank">http://www.atmel.com/dyn/resources/prod_documents/doc0856.pdf</a>) &lt;- pages 10 to 14 are especially relevant.</p>
<h3>Setting up the Project</h3>
<p>Okay, first task was to set-up a new project in AVR Studio.</p>
<pre class="code">Project -&gt; new -&gt; [give it a name] / [pick AVR assembler].</pre>
<p>This creates a blank project.  Next we need a new text file to put our assembly code.</p>
<pre class="code">File -&gt; new text file -&gt; [give it the name 'main.asm']</pre>
<p>As far as I know, the name isn&#8217;t important &#8211; what <strong><em>is</em></strong> important is that when you right click this file, &#8216;assembler entry file&#8217; is ticked.  You also need to drag this file into the Assembler Files folder.</p>
<p>Next, right click the route node of the project, and select the &#8216;project settings&#8217;.  Change the output file format to &#8216;intel hex&#8217;.  This defines the format that will eventually be used by the programmer software / board to upload the code to the microprocessor.</p>
<h3>Writing the Code</h3>
<p>Okay &#8211; next step is to produce the required code.</p>
<p>Open up the text file created a few steps ago.</p>
<p>The very first stage involves setting the start point in your program memory, to hex address 3A.  You need to add the following line at the begining of this newly opened file.</p>
<pre class="code">.ORG $3A</pre>
<p>and then continue to add the following lines of code &#8230;</p>
<pre class="code">START:	LDI	r16,high(ramend)	;ramend=$025F p7-9
	OUT	sph,r16
	LDI	r16,low(ramend)
	OUT	spl,r16</pre>
<p>It&#8217;s worth running through some of the conventions here.</p>
<ul>
<li> The word preceding the colon, defines a point in the program code which can be called or jumped to later on. This is then used to enable us to call subroutines and create loops.</li>
<li> The semicolon defines a comment.</li>
<li> LDI and OUT are instructions &#8230; (a full list of the AVR instruction set can be found in the guide linked above [*2], pages 10-14)</li>
<li> r16 is a register.  Registers can be though of as internal variables, which are able to be accessed very quickly by the processor; some are specific and can only be used in certain circumstances, while others are general purpose.</li>
<li> sph and spl refer to the AVR stack pointer (which is implemented as two 8-bit registers). The stack is a stack of data &#8211; you can add (push) data to the stack, or take (pop) data from the stack.  Find out more about the stack on page 13 of [*1].</li>
<li> As far as I can tell ramend, is a device specific constant, which refers to the end of the available data RAM.</li>
</ul>
<p>For this first program, I simply wanted to make an LED flash.  To do this, I need to output a HIGH signal followed by a LOW signal to one of the ports on the ATmega.</p>
<p>I was a bit confused here, thinking that port is another word for pin&#8230; it isn&#8217;t.  The Atmega8 has three ports labelled BCD which are responsible for looking after 23 IO general purpose IO lines/pins.</p>
<dl>
<dt> Port B </dt>
<dd> looks after 8 I/O lines (PB7-0) </dd>
<dt> Port C </dt>
<dd> looks after 7 I/O lines (PC6-0) &#8230; but note that PC6 is special case and is different to others, it&#8217;s used as an IO pin or reset see page 5 of [*1] for more info. </dd>
<dt> Port D </dt>
<dd> looks after 8 I/O lines (PD7-0) </dd>
</dl>
<p>Each port pin consists of three register bits ( DDxn, PORTxn, PINxn).  By using a bit of cleverness, all the required physical ports can be accessed through address lookups.</p>
<ul>
<li> DDxn bits are accessed at the DDRx I/O address,</li>
<li> PORTxn bits are accessed at the PORTx I/O address</li>
<li> PINxn bits are accessed at the PINx I/O address.</li>
</ul>
<p>(N/B Page 65 of [*1] is a useful overview of how data is output (PORTxn), how pin direction is defined (DDRxn) and where input data is stored/accessed (PINxn), in relation to the available ports and pins.)</p>
<p>Because the ports and IO lines/pins are general purpose, we need to be able to define which are used for input and which are used for output.  This is done by setting the port direction.</p>
<pre class="code">	LDI r18,0b10000000
	OUT ddrb,r18</pre>
<p>LDI is the instruction for &#8216;load immediate&#8217;, OUT is the instruction for &#8216;out to i/o location&#8217;.</p>
<p>Next, we preload registers r18 and r19 with the data that we&#8217;ll use to make our LED flash&#8230; because we use these values many times, the assignment happens before the main loop.</p>
<pre class="code">	LDI r18,0b10000000; set register to pin 7 high
	LDI r19,0b00000000; set register to all pins low</pre>
<p>Then we can add the main loop &#8230;</p>
<pre class="code">MAINLOOP:
	out portb,r18
	out portb,r19
	jmp MAINLOOP</pre>
<div id="attachment_203" class="wp-caption alignright" style="width: 310px"><a href="http://ntsdt.net/wp-content/uploads/2008/08/avrstudio_screenshot1.gif"><img class="size-medium wp-image-203" title="avrstudio_screenshot1" src="http://ntsdt.net/wp-content/uploads/2008/08/avrstudio_screenshot1-300x220.gif" alt="AVR Studio Screenshot" width="300" height="220" /></a><p class="wp-caption-text">AVR Studio Screenshot</p></div>
<p>Now, the great thing about AVR Studio &#8211; is that you can use it to run your code in a simulator, which allows you to debug the code, while monitoring the status of registers, i/o ports etc..</p>
<p>Before we can simulate the code we need to include a file in our AVR Studio project (which gives device dependant info) at the beginning of the code. (a range of inc files can be found in the &#8216;Appnotes&#8217; subdirectory of yr AVR Studio install)</p>
<pre class="code">.include "m8def.inc"</pre>
<p>Then select; Project -&gt; Build and run&#8230; You&#8217;ll need to select the chip you&#8217;re working with.  Select View -&gt; &#8216;registers&#8217; &amp; &#8216;new io view&#8217;&#8230; then you can step through yr code with F10 and examine it&#8217;s effect.</p>
]]></content:encoded>
			<wfw:commentRss>http://ntsdt.net/2008/08/08/first-steps-with-atmel-assembly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
