<?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; atmega</title>
	<atom:link href="http://webstersprodigy.net/tag/atmega/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>
	</channel>
</rss>

