NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Simple tone from a speaker, can anyone help

February 27, 2011
by amartinez
amartinez's Avatar

I've been hacking for hours now. I used the theremin code to try to extrapolate all the basic components needed to create a tone to an 8 ohm speaker. I even went outside NK to other sites to see if I could get such a code for the Atmega 168. I followed the NK tutorial for the music box but the code was for the Atiny26L chip, not very helpful for newbies like me to convert ports and frequencies from one chip to another. I tried.

I find the NK code works and the descriptions ok but the methodology is multiple steps ahead of any novice and not really geared for the beginner. The advanced coding methodology confuse me, I'm really trying to learn step by step. I'm finding myself tearing the code apart and sub-dividing it in to separate codes for separate functions. It's a killer!

I have not found any simple sound code and I don't really know what I'm doing with PWM (yet).

I don't need push button scans or LCD readouts, just the basic components of a tone. One tini tiny little tone to come out of an 8 ohm speaker. Maybe 320 hertz, maybe 500 hertz, It doesn't matter. I just want to prove to myself that I can create a tone from a speaker using this MCU. My eyes crossed with all the code I had to descypher and go through already.

Please help, anyone. I'm drowning in advanced concepts and code.

February 28, 2011
by amartinez
amartinez's Avatar

I got this code after exhaustive research. It looked like it would work but it's giving me a delay_us error. I thought the delay.h file supported delay_us. I'm at a loss. Please help. If anyone has good code for tone generation please share.

#include <avr/io.h> // #include <util/delay.h> #include "../libnerdkits/delay.h"

#define F_CPU 14745600

void delay_us(uint16_t t) { while (t--) _delay_us(1); }

int main (void) { uint32_t counter = 0; uint16_t period;

/ Setup PB1 as an output / DDRB = _BV(PB1);

/ Initial period / period = 1600;

/ Main loop / for(;;) {
PORTB |= _BV(DDB1); delay_us(period / 2);

PORTB &= ~_BV(DDB1);
delay_us(period / 2);

counter++;
if (counter == 200)
{
  /* Reset the counter */
  counter = 0;

  /* Update the toggling period */
  if (period == 1600)
    period = 2000;
  else
    period = 1600;
}

}

return(0); }

March 01, 2011
by Ralphxyz
Ralphxyz's Avatar

Here is your code formated using the "Indent Selection as Code Block" button, makes it so much easier to read:

#include <avr/io.h> // 
#include <util/delay.h> 
#include "../libnerdkits/delay.h"

#define F_CPU 14745600

void delay_us(uint16_t t) 
{ 
    while (t--) 
    _delay_us(1); 
}

int main (void) 
{ 
    uint32_t counter = 0; 
    uint16_t period;

/* Setup PB1 as an output */  
DDRB = _BV(PB1);

/* Initial period */ 
period = 1600;

/* Main loop */  
for(;;) 
{
    PORTB |= _BV(DDB1); 
    delay_us(period / 2);
    PORTB &= ~_BV(DDB1);
    delay_us(period / 2);

counter++;
if (counter == 200)
{
  /* Reset the counter */
  counter = 0;

  /* Update the toggling period */
  if (period == 1600)
    period = 2000;
  else
    period = 1600;
}
View Original
}

return(0); 
}

Shouldn't Line 10 be:

  delay_us(1);  instead of _delay_us(1);

What in the world do you except to get from a 1 micro second delay, but that's another question.

Don't really know programming, but thought it might help just to clean up your code.

Without actually loading your code I can not be of much more help.

Ralph

March 03, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi amartinez,

I think you are getting a delay_us error because the code you copied is declaring a delay_us() function, and there is already a delay_us() function defined in the nerdkits libraries. You cannot have two functions that are named the same thing in the same program.

Humberto

March 05, 2011
by amartinez
amartinez's Avatar

Thanks guys. I dug deeper and found some code that helped me wrap my head around the problem. It's the "happy birthday" code posted several months ago. Very helpful and works awesome.

Thanks for all your help. I will try to read as many posts as I can before I ask a question.

I am putting this 168 chip through its paces as still havc some goals to tackle, then I will graduate to a 328. Sorry about the code format, I just learned how to "indent as code block."

Thanks for the help. Got this project out of the way.

Al

Post a Reply

Please log in to post a reply.

Did you know that one NerdKits customer discovered that his hamster ran several miles in a night using his kit and a Hall-effect sensor? Learn more...