Monday, June 17, 2013

Producing Different Sound through Speaker

In this we will learn how to interface speaker with our Atmega 16 .

Using this you can generate different sounds through your speaker. You can make your own ringtone.

Lets start with our practical.

For this we need a speaker and ATmega 16 microcontroller.

Hardware configuration :
Step 1:
Please connect the speaker to the PB3 pin of Your microcontroller.
This is because we will using the internal timer of our microcontroller to generate the different frequecy.
And the output of the timer is configured to the PB3 pin.

Step 2:
Code:
The code here is very easy again



#define F_CPU 1000000UL
#include<avr/io.h>
#include<util/delay.h>


void init_timer()
{
    TCCR0|=(1<<WGM01)|(1<<COM00)|(1<<CS01);    // configuring the timer in fast PWM mode
    DDRB|=(1<<PB3);


}
int main()
{
   
    init_timer();
        _dela_ms(1000);

    while(1)
    {
   
    OCR0=59;                  
// OCR is the register where i am assigning the value 59 so that it count upto 59 and that will generate //different frequency.
        _dela_ms(500);
   
    OCR0=52;
        _dela_ms(500);
    
      OCR0=44;
        _dela_ms(500);
   
    OCR0=38;
        _dela_ms(500);
   
    }
    return 0;
}

Now you can see it that, speaker will be producing different sounds.

If you the like the post please join us the member to this blog.
thanks....

No comments:

Post a Comment