<?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>webstersprodigy.net &#187; avr</title>
	<atom:link href="http://webstersprodigy.net/tag/avr/feed/" rel="self" type="application/rss+xml" />
	<link>http://webstersprodigy.net</link>
	<description>Me trying to learn how to use a computer</description>
	<lastBuildDate>Sat, 04 Feb 2012 01:17:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>playing a scale with the atmega16</title>
		<link>http://webstersprodigy.net/2009/05/playing-a-scale-with-the-atmega16/</link>
		<comments>http://webstersprodigy.net/2009/05/playing-a-scale-with-the-atmega16/#comments</comments>
		<pubDate>Mon, 04 May 2009 03:51:22 +0000</pubDate>
		<dc:creator>webstersprodigy</dc:creator>
				<category><![CDATA[Bits and Bytes]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[atmega]]></category>
		<category><![CDATA[avr]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://webstersprodigy.net/?p=526</guid>
		<description><![CDATA[A musical “note” is a waveform that has a certain fundamental frequency. A perfect sine wave consists only of the fundamental frequency, and sounds very “pure” or mellow, like a flute. For this project, you are to use interrupts to generate the notes of a musical scale, while at the same time using another interrupt to update a count. We will generate square waves by toggling a bit within a port on and off. Specifically, your program should play the notes from middle C to the next higher C, each of approximately one second in duration. These notes should be playing at the same time that an 8-bit binary count is being displayed in the LED’s, updating at approximately 1/4 second. ]]></description>
			<content:encoded><![CDATA[<p>The Specification:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>The purpose of this assignment is to get further practice using interrupts, particularly concurrent interrupts. A musical “note” is a waveform that has a certain fundamental frequency. For example, the “A” above middle C on a piano has a frequency of 440 Hz, and middle C itself is approximately 261 Hz. On an equally tempered scale, each of the twelve notes is related to each other by a factor of 2 12 . A note that is an octave above another note is double the frequency of the lower note. A perfect sine wave consists only of the fundamental frequency, and sounds very “pure” or mellow, like a flute. Most waveforms consist of a fundamental and harmonics or overtones, which are additional frequencies above the fundamental. For instance, a square wave consists of the fundamental and multiples of the fundamental in decreasing amplitude. The distinctive sound of a violin is mostly a sawtooth wave (created as the rosin on the bow catches the string, then releases it), with other overtones (called dissonant overtones, due to the fact that they are not multiples of the fundamental) created by resonances in the wood.</p>
<p>For this assignment, you are to use interrupts to generate the notes of a musical scale, while at the same time using another interrupt to update a count. We will generate square waves by toggling a bit within a port on and off. Specifically, your program should play the notes from middle C to the next higher C, each of approximately one second in duration. These notes should be playing at the same time that an 8-bit binary count is being displayed in the LED’s, updating at approximately 1/4 second. As before, determine the code sizes, and estimate the percentages of time your ISR’s and “main” program takes of the total execution time. Also, for each note, estimate its accuracy &#8211; that is, how close is the note your program plays to the calculated frequency for the note.</p>
<p><a href="http://webstersprodigy.net/wp-content/uploads/2009/05/sound_avrtar.gz">Download the source here</a></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>This code requires avrdude, avr-gcc, and objcopy are installed</p>
<p>To compile, type:</p>
<blockquote><p>make</p></blockquote>
<p>After it&#8217;s compiled, to install to a connected and powered on atmega16 board, type:</p>
<blockquote><p>make install</p></blockquote>
<p>Note Makefile may require some modification depending on your environment.</p>
<p>This program includes a demo called homework3_demo.avi, which should play on vlc.</p>
<p>Program Size<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
The ATmega16 provides 16K bytes of Programmable Flash Program Memory, 512 bytes eeprom, and 1K byte SRAM</p>
<p>make gives a 3982 byte executable. It contains the following headers:</p>
<blockquote><p>Section Headers:<br />
[Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al<br />
[ 0]                   NULL            00000000 000000 000000 00      0   0  0<br />
[ 1] .text             PROGBITS        00000000 000074 000248 00  AX  0   0  2<br />
[ 2] .data             PROGBITS        00800060 0002bc 000020 00  WA  0   0  1<br />
[ 3] .bss              NOBITS          00800080 0002dc 000002 00  WA  0   0  1<br />
[ 4] .stab             PROGBITS        00000000 0002dc 000378 0c      5   0  4<br />
[ 5] .stabstr          STRTAB          00000000 000654 000054 00      0   0  1<br />
[ 6] .shstrtab         STRTAB          00000000 0006a8 00003b 00      0   0  1<br />
[ 7] .symtab           SYMTAB          00000000 00084c 000450 10      8  17  4<br />
[ 8] .strtab           STRTAB          00000000 000c9c 0002f2 00      0   0  1</p></blockquote>
<p>#without space optimization<br />
Program: .text + .data + .bootloader == 618 bytes (3.8% Full)<br />
Data: .data + .bss + .noinit == 34 bytes (3.3% Full)</p>
<p>Here is most the code</p>
<pre class="brush: cpp; title: ; notranslate">
/* http://www.avrfreaks.net/index.php?name=PNphpBB2&amp;amp;file=viewtopic&amp;amp;t=50106
 * was a big help understanding the timer syntax
 *
 * also relied on the atmega16 manual and the #defines located in /usr/avr/include/avr */

#include &lt;avr/io.h&gt;
#include &lt;avr/interrupt.h&gt;

/*counter keeps track of how many overflows in timer0 = about a second */
int counter = 0;

/*number of ticks to make a note at 1 MHz*/

/*
At 1MHz, these are the number of ticks it takes to equal a given note

const int C_l = 3817/2;
const int D   = 3401/2;
const int E   = 3030/2;
const int F   = 2865/2;
const int G   = 2551/2;
const int A   = 2272/2;
const int B   = 2024/2;
const int C_h = 1912/2; */

//const long int notes[8] = {3817, 3401, 3030, 2865, 2551, 2272, 2024, 1912};
const long int notes[8] = {1908, 1700, 1515, 1432, 1275, 1136, 1012, 956};

void port_init() {
  /*PORTC is connected to the amplifier */
  /*PORTB is connected to the LEDs */
  DDRB  = 0xff;  /*11111111 for all output */
  DDRC  = 0xff;
  PORTC = 0xff;  /*all lights turned off   */
  PORTB = 0xff;  /*all lights turned off   */
}

void init_timer1() {
  /*setup timer1, this will measure the notes */
  TCCR1B |= (1 &lt;&lt; WGM12);
  /*Enable CTC interrupt */
  TIMSK |= (1 &lt;&lt; OCIE1A);
  /* OCR1A = 15625;*/
  OCR1A = notes[0]; /* Set CTC value to middle C to begin with */
  /*timer runs with a 1MHz resolution */
  TCCR1B |= (1 &lt;&lt; CS10);

}

void init_timer0() {
  /*prescaler/64*/
  TCCR0 |= ((1 &lt;&lt; CS01) | (1 &lt;&lt; CS00));
  /*overflow mode - about every 1/61 seconds */
  TIMSK |= (1 &lt;&lt; TOIE0);
}

void main (void) {
  port_init();
  init_timer0();
  init_timer1();

  /*Enable global interrupts */
  sei();

  while(1) {
    /*most logic is handled by the interrupts */
  }
}

/* toggle the LED for now, this is the musical note */
ISR(TIMER1_COMPA_vect) {
  /*toggle the output */
  PORTC = ~PORTC;
}

ISR(TIMER0_OVF_vect) {
  counter += 1;
  if (counter == 61) {
    /*count up to 8 for all 8 notes*/
    if (PORTB == 0xf8) {
      PORTB = 0xff;
    }
    else {
      PORTB--;
    }
    //OCR1A = notes[~PORTB];
    OCR1A = notes[~PORTB];
    counter = 0;
  }
}
</pre>
<p>Here is a short video demonstration:</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/H6ew1qtbq9M&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/H6ew1qtbq9M&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://webstersprodigy.net/2009/05/playing-a-scale-with-the-atmega16/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>stk500 avr atmega16 linux gcc hello, world</title>
		<link>http://webstersprodigy.net/2009/04/stk500-avr-atmega16-linux-gcc-hello-world/</link>
		<comments>http://webstersprodigy.net/2009/04/stk500-avr-atmega16-linux-gcc-hello-world/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 06:12:48 +0000</pubDate>
		<dc:creator>webstersprodigy</dc:creator>
				<category><![CDATA[Bits and Bytes]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[avr]]></category>

		<guid isPermaLink="false">http://webstersprodigy.net/?p=467</guid>
		<description><![CDATA[Does my title sound like buz-word central? You bet it does. That's because it was a bit difficult to find any good introductory material on this. Maybe that's because there's so much information out there...]]></description>
			<content:encoded><![CDATA[<p>Does my title sound like buz-word central? You bet it does. That&#8217;s because it was a bit difficult to find any good introductory material on this. Maybe that&#8217;s because there&#8217;s so much information out there&#8230;</p>
<p>Anyway, here is some. The README:</p>
<blockquote><p>Rich Lundeen<br />
lundrich@isu.edu</p>
<p>The Specification:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>The purpose of this assignment is to introduce you to the software tools we will use for the<br />
AVR microprocessor, and to give you a better understanding of the run-time environment that<br />
a C program operates under.<br />
For this assignment, you will write a C main program and two subroutines &#8211; one in AVR<br />
assembly, and the other in C. The subroutines should each do the same thing:<br />
1. Monitor a port (your choice) that is connected to a switch. Wait until the switch is<br />
pressed.<br />
2. Once pressed, the routine should keep track of how long the switch is held down.<br />
3. Wait for approximately one second.<br />
4. Using another port (your choice) connected to a LED, turn on the LED for the same<br />
amount of time that the switch was pressed earlier.<br />
In addition to writing the code, determine the size of the code (main and both subroutines) in<br />
your program.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>This code requires avrdude, avr-gcc, and objcopy are installed.</p>
<p>To compile the C version, type:</p>
<p>make</p>
<p>To compile the asm version, type:</p>
<p>make asm</p>
<p>After it&#8217;s compiled, to install to a connected and powered on atmega16 board, type:</p>
<p>make install</p>
<p>homework1.avi contains a short demonstration of the C version &#8211; however, both do the same thing. It<br />
will play with vlc or mplayer.</p></blockquote>
<p>Here is a demo of the program.. oooh blinky lights. Yeah, and anyway, the hardest part is just to find a hello world. This includes one in both C and assembly, so good hunting!</p>
<p>
<object width="425" height="344" data="http://www.youtube.com/v/I654p4fcjww&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/I654p4fcjww&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /></object></p>
<p>Download the source<a href="http://webstersprodigy.net/uploads/blinky.tar.gz"> here</a>,</p>
<pre class="brush: cpp; title: ; notranslate">
/*main.c*/
#include &quot;portstuff.h&quot;

int main (void) {
  DDRB = 0xff;  /*11111111 means all output */
  DDRD = 0x00;  /*00000000 for all input */

  PORTB = 0xff;
  portmon();
  return(0);
}
</pre>
<pre class="brush: cpp; title: ; notranslate">
/*portstuff.h*/
#include &lt;inttypes.h&gt;
#include &lt;avr/pgmspace.h&gt;
#include &lt;avr/io.h&gt;

void portmon();
</pre>
<pre class="brush: cpp; title: ; notranslate">
/* portstuff.c */
#include &quot;portstuff.h&quot;

/*this is in clock cycles for my board.
* 30000 seems to be close to about a sec */
const DELAY = 30000;

void portmon() {
  while(1) {
    unsigned long timer = 0;
    unsigned long i;
    /*while a button is pressed */
    while (PIND != 0xff) {
      /*light up the button pressed */
      PORTB = PIND;
      timer += 1;
    }
    PORTB = 0xff;
    /*if the timer is not zero, we need to handle a button pressed */
    if (timer != 0) {
      /*delay for about a second */
      for (i = DELAY; i!=0; i--);
      PORTB = 0x00;
      /*delay for timer roughly equal to how long button was pressed */
      for (i = timer; i!=0; i--);
      timer = 0;
    }
  }
}
</pre>
<pre class="brush: cpp; title: ; notranslate">
#Makefile
#c specific - this is the default

homework1.hex: homework1.out
    objcopy -S -O ihex homework1.out homework1.hex
homework1.out: main.o portstuff.o
    avr-gcc -mmcu=atmega16 main.o portstuff.o -o homework1.out
main.o: portstuff.h main.c
    avr-gcc -c -mmcu=atmega16 main.c
portstuff.o: portstuff.h portstuff.c
    avr-gcc -c -mmcu=atmega16 portstuff.c

#assembly specific rules

asm: howework1asm.out
    objcopy -S -O ihex homework1.out homework1.hex
howework1asm.out: main.o portstuffasm.o
    avr-gcc -mmcu=atmega16 main.o portstuffasm.o -o homework1.out
portstuffasm.o: portstuff.h portstuff.s
    avr-gcc -c -mmcu=atmega16 portstuff.s -o portstuffasm.o

#clean and install

clean:
    rm ./*.o ./*.hex ./homework1.out

install:
    avrdude -y -C /etc/avrdude/avrdude.conf -p atmega16 -P /dev/ttyS0 -c stk500v2 -U flash:w:homework1.hex:i
</pre>
<pre class="brush: cpp; title: ; notranslate">
/*portstuff.s*/
#include &quot;portstuff.h&quot;

.file	&quot;portstuff.c&quot;
PORTB  = 56-0x20
PORTD  = 48-0x20
ALLON  = 31
WAIT   = 0xa
.text
.global	portmon
.type	portmon, @function
portmon:
/*initialize count to zero */
.BEGIN:
/*r18 is our counter*/
ldi     r18,0x00

/*initialize port values*/
in  r24,PORTD
out PORTB, r24

.LEDWAIT:
in  r24,PORTD
nop

/*see if the button is currently pressed - if it is then continue
if not jump to .LEDWAIT */
cpi r24, lo8(-1)
breq .LEDWAIT

.BUTTONPRESSED:

out PORTB, r24

/*measure the number of delays the button is pressed*/
rcall delay
inc r18

/*see if button is still currently pressed - if it is then
jump to Buttonpressed */

in   r24, PORTD
cpi  r24, lo8(-1)
brne .BUTTONPRESSED

out PORTB, r24
ldi r23, WAIT
.DELAYSECOND:
/*theoretically this delays a second */
rcall delay
dec r23
cpi r23,0
brne .DELAYSECOND

.LEDSON:
/*light up all leds for the amount of time the one was pressed*/
out PORTB, ALLON
rcall delay
dec r18
cpi r18,0
breq .BEGIN
rjmp .LEDSON

/*delay function makes time reasonable to deal with */
delay:
ldi r17,0x60
waitouterloop:
ldi r16,0xFF
waitinnerloop:
subi r16,0x01
brne waitinnerloop
subi r17,0x01
brne waitouterloop
ret

.size	portmon, .-portmon
</pre>
]]></content:encoded>
			<wfw:commentRss>http://webstersprodigy.net/2009/04/stk500-avr-atmega16-linux-gcc-hello-world/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

