playing a scale with the atmega16

Specification

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.

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 – that is, how close is the note your program plays to the calculated frequency for the note.

Download the source here

Compiling

This code requires avrdude, avr-gcc, and objcopy are installed

To compile, type:

make

After it’s compiled, to install to a connected and powered on atmega16 board, type:

make install

Note Makefile may require some modification depending on your environment.

This program includes a demo called homework3_demo.avi.

Program Size

The ATmega16 provides 16K bytes of Programmable Flash Program Memory, 512 bytes eeprom, and 1K byte SRAM

make gives a 3982 byte executable. It contains the following headers:

Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00000000 000074 000248 00 AX 0 0 2
[ 2] .data PROGBITS 00800060 0002bc 000020 00 WA 0 0 1
[ 3] .bss NOBITS 00800080 0002dc 000002 00 WA 0 0 1
[ 4] .stab PROGBITS 00000000 0002dc 000378 0c 5 0 4
[ 5] .stabstr STRTAB 00000000 000654 000054 00 0 0 1
[ 6] .shstrtab STRTAB 00000000 0006a8 00003b 00 0 0 1
[ 7] .symtab SYMTAB 00000000 00084c 000450 10 8 17 4
[ 8] .strtab STRTAB 00000000 000c9c 0002f2 00 0 0 1

#without space optimization
Program: .text + .data + .bootloader == 618 bytes (3.8% Full)
Data: .data + .bss + .noinit == 34 bytes (3.3% Full)

Here is most the code

/* http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&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 <avr/io.h>
#include <avr/interrupt.h>

/*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 << WGM12);
  /*Enable CTC interrupt */
  TIMSK |= (1 << OCIE1A);
  /* OCR1A = 15625;*/
  OCR1A = notes[0]; /* Set CTC value to middle C to begin with */
  /*timer runs with a 1MHz resolution */
  TCCR1B |= (1 << CS10);

}

void init_timer0() {
  /*prescaler/64*/
  TCCR0 |= ((1 << CS01) | (1 << CS00));
  /*overflow mode - about every 1/61 seconds */
  TIMSK |= (1 << 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;
  }
}

Here is a short video demonstration:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s