music player help

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Hello,
I am new here. I am using ATMEL STUDIO 7.0 and using an ATMEGA 1284. I am following the tutorial from http://aquaticus.info/pwm-music. When I load their code onto the ATMEGA 1284 
the buzzer just make a lot of 

fast tapping noises. I have tried fixing the code by trying out the recommendations from one of the people that commented which is pasted below. It was still a bunch of tapping with some delay. What can I do to fix this?

.

void PlayMusic( const int* pMusicNotes /** Pointer to table containing music data */,
				uint8_t tempo /** paying tempo from 0 to 100. Higher value = slower playback*/ )
{
	int duration;
	int note;
	int i;
	uint16_t delay = tempo * 10000;

	while( *pMusicNotes )
	{
		note = *pMusicNotes;
		pMusicNotes++;

		duration = *pMusicNotes;
		pMusicNotes++;

		if( p == note )
		{
			//pause, do not generate any sound
			OCR1B = 0;
		}
		else
		{
			//not a pause, generate tone
			OCR1B = DEFAULT_VOLUME;

			//set frequency
			ICR1H = (note >> 8);
			ICR1L = note;
		}

		//wait duration
		for(i=0;i<32/duration;i++)
		{
			_delay_loop_2( delay );
			_delay_loop_2( delay );
			_delay_loop_2( delay );
			_delay_loop_2( delay );
			_delay_loop_2( delay );

		}

	}

	//turn off any sound
	OCR1B = 0;
}

 

This topic has a solution.
Last Edited: Thu. Aug 31, 2017 - 07:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jborja wrote:
What can I do to fix this?
Show more of the code. You are clearly using PWM but have not shown the setup of that. You haven't even shown how the note/duration data is defined and passed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jborja wrote:

 I am following the tutorial from http://aquaticus.info/pwm-music.

Why did you use the code button to insert your non-code text?!

 

Anyhow, that page starts by saying,

 

Aquaticus wrote:
 Playing music is a bit more complicated then generating simple “beep”

 

So, before trying the (more complicated) music project, did you get the simple "beep" to work?

 

http://aquaticus.info/pwm-beep

 

If not, you should probably do so.

 

even for that, using PWM seems over-complicated - so have you successfully done a simple LED blink, or a really simple beep?

 

What hardware are you using?

 

jborja wrote:
fast tapping noises.

Are you sure that your CPU is running at the frequency you think it is ?

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
did you get the simple "beep" to work?
 

Yes, I did. 

 

awneil wrote:
have you successfully done a simple LED blink, or a really simple beep?

Yes, I have.

 

awneil wrote:
What hardware are you using?

I am using a Piezzo buzzer/speaker connected to PD4/OC1B (pin 18), the ATMEGA 1284

 

awneil wrote:
Are you sure that your CPU is running at the frequency you think it is ?

When I go to |tools > device programming > interface settings| the ISP clock is at 125 kHz. In the comments of the code it says the clock is 8MHz. Idk if this is what you meant in your question.  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

/*
	Music demo.

	Microcontroller: ATmega16 or ATmega32
	Clock: 8 MHz
	External hardware: Piezzo buzzer/speaker connected to PD4/OCR1B (PDIP pin 18)
	Compiler: AVR GCC

	http://aquaticus.info/pwm-music

	$Revision: 142 $
*/

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

// The output file from notegen.pl script
#include "notes.h"

/// Default volume, value assigned to OCR1B
#define DEFAULT_VOLUME 100

/**
	Initialize timer1

	PWM output on OCR1B
*/

void InitMusic()
{
	// Configure OC1B pin as output

	DDRD |= _BV(DDD4); //OC1B as output

	// timer1 configuration (for PWM)
    TCCR1A |= _BV(COM1B1);  // Clear OC1A/OC1B on compare match

	
	TCCR1B |= _BV(WGM13) 	//mode 8, PWM, Phase and Frequency Correct (TOP value is ICR1)
		   |_BV(CS11); 		//prescaler(8)
}


/**
	Plays music.
*/

void PlayMusic( const int* pMusicNotes /** Pointer to table containing music data */,
				uint8_t tempo /** paying tempo from 0 to 100. Higher value = slower playback*/ )
{
	int duration;
	int note;
	int i;
	uint16_t delay = tempo * 10000;

	while( *pMusicNotes )
	{
		note = *pMusicNotes;
		pMusicNotes++;

		duration = *pMusicNotes;
		pMusicNotes++;

		if( p == note )
		{
			//pause, do not generate any sound
			OCR1B = 0;
		}
		else
		{
			//not a pause, generate tone
			OCR1B = DEFAULT_VOLUME;

			//set frequency
			ICR1H = (note >> 8);
			ICR1L = note;
		}

		//wait duration
		for(i=0;i<32/duration;i++)
		{
			_delay_loop_2( delay );
			_delay_loop_2( delay );
			_delay_loop_2( delay );
			_delay_loop_2( delay );
			_delay_loop_2( delay );
			
		}

	}
	

	//turn off any sound
	OCR1B = 0;
}

const int octave[] = {c4, 8, d4, 8, e4, 8, f4, 8, g4, 8, a4, 8, h4, 8, c5, 8, MUSIC_END};


// Star Wars
const int starwars[] = 
{
	Ais2,8, Ais2,8, P,16, F3,8, F3,8, P,16, Dis3,16, P,16, D3,16, P,16, C3,16, P,16, Ais3,8,
	Ais3,8, P,16, F3,8, P,16, Dis3,16, P,16, D3,16, P,16, C3,16, P,16, Ais3,8, Ais3,8, P,16,
	F3,8, P,16, Dis3,16, P,16, D3,16, P,16, Dis3,16, P,16, C3,8, C3,8, 
	MUSIC_END
};

// Fur Elise
const int furelise[] = 
{
    e4, 8, d4x, 8, e4, 8, d4x, 8, e4, 8, b3, 8, d4, 8, c4, 8, a3,8, p, 8,
    c3, 8, e3, 8, a3, 8,  b3, 4, p, 8, e3, 8, g3x, 8, b3, 8, c4, 4, p, 8, e3, 8,
    e3, 8, d4x, 8, e4, 8, d4x, 8, e4, 8, b3, 8, d4, 8, c4, 8, a3, 8, p, 8, c3, 8,
    e3, 8, a3, 8, b3, 4, p, 8, e3, 8, c4, 8, b3, 8, a3, 4,
    MUSIC_END
};

// Beatles, Hey Jude
const int Jude[] = 
{
	G2,8, E2,8, P,16, E2,16, E2,16, G2,16, A2,16, D2,8, P,16, D2,16, E2,16, F2,8, 
	C3,8, C3,16, C3,16, H2,16, G2,16, A2,16, G2,16, F2,16, E2,8, P,16, G2,16, 
	A2,16, A2,8, A2,16, D3,16, C3,16, H2,16, H2,16, C3,16, A2,16, G2,8, P,16, 
	C2,16, D2,16, E2,16, A2,16, A2,16, G2,8,
	MUSIC_END
};


int main()
{
	PORTB = 0x00; 
	InitMusic();

	while(1)
	{

		//PlayMusic( octave, 40 );
		//_delay_ms(1000);

		PlayMusic( furelise, 20 );
		_delay_ms(1000);


		//PlayMusic( starwars, 17 );
		//_delay_ms(1000);
//
		//PlayMusic( Jude, 35 );
		//_delay_ms(1000);
	}

}

This is the code I have been using. I isolated to only one song in the main so that I could tell if it was working. I have included the header file as an attachment. 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jborja wrote:
I am using a Piezzo buzzer/speaker

Please clarify: the term "buzzer" would normally be applied to something where you just apply a power supply, and it generates the tone all by itself - whereas a "speaker" has to be supplied with the appropriate drive signal.

 

See also: http://www.avrfreaks.net/comment...

 

connected to PD4/OC1B (pin 18), the ATMEGA 1284

What I was really asking was whether that's on a commercial board, or your own PCB, or a bread board, or ... ?

 

In the comments of the code it says the clock is 8MHz.

But what is the actual clock frequency?

 

If the code is expecting it to be 8MHz - as the comment says - but it is actaully 1MHz, then the sounds are obviously not going to be quit right ...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have attached pictures.

 

 

awneil wrote:
what is the actual clock frequency?
 

How would I check for that?

Attachment(s): 

Last Edited: Wed. Aug 30, 2017 - 04:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can't necessarily tell if it's a "buzzer" or a "speaker" just by looking at it.

Where did you get it? How was it described? give a link.

 

You could test it by just applying 3V (observing any polarity indication): if it beeps, it's a "buzzer" - not a "speaker" 

 

See http://www.avrfreaks.net/forum/tuthardsoft-avr-clocking regarding clocking

 

 

EDIT

 

polarity

Last Edited: Wed. Aug 30, 2017 - 04:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
	if( p == note )

That line bothers me. I must be going blind. I don't see this "p" variable anywhere. Something in notes.h perhaps?

 

EDIT: OK just realised notes.h was an attachment. I can't believe what I am seeing in that!!! ....

#define P 1
#define PAUSE P
#define p P

Seriously?

Last Edited: Wed. Aug 30, 2017 - 04:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is a buzzer. I applied 3V to it and it buzzed. 

As for the clocking I have included info about as an attachment. From what I see "the device is shipped with internal RC oscillator at 8.0MHz" according the to ATmega 1284 datasheet. The PDF tutorial was informative thank you. 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jborja wrote:
This is a buzzer. I applied 3V to it and it buzzed. 

So that's not going to work for this application - is it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jborja wrote:
"the device is shipped with internal RC oscillator at 8.0MHz" 

But the sentence doesn't end there, does it?

 

You need to read the full sentence:

 

Note how much easier it is when you embed the image in the post - instructions here: http://www.avrfreaks.net/comment...

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got it working now that I changed that the clock. http://www.avrfreaks.net/forum/solved-atmel-studio-not-compiling-properly

There is not problem using a buzzer or a speaker. (speaker sounds a lot better) The delay between each note is not that bad. The only problem I have now is that each note does not hit the frequency that it needs to hit. It sounds like buzzing with a delay but with the pattern of the melody.

Last Edited: Thu. Aug 31, 2017 - 06:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jborja wrote:
I got it working now that I changed that the clock

So please mark that post as the solution - to help others who may have the same problem.

 

There is not problem using a buzzer or a speaker

Yes, there is:

The only problem I have now is that each note does not hit the frequency that it needs to hit

Think about it: this is bound to be the case - isn't it?