bhattaroshan
Member level 1
Hello friends i have made simple codes for generating sound from avr microcontroller.
#include<avr\io.h>
#include<avr\delay.h>
#include<avr\interrupt.h>
uint16_t FREQ_CNT=0;
uint16_t FF_CNT =0;
static volatile uint16_t freq_pb0=20; // initial value of frequency (any value..)
uint16_t note[]={300,337,378,401,451,506,569,603}; // array of frequency that we are
// going to generate
uint16_t j=0;
int main()
{
DDRB=0xff;
PORTB=0x00;
TCCR0=(1<<CS00); //prescalar 1 i.e. F_CPU (8MHZ) in our case
TIMSK=(1<<TOIE0); //enable timer0 overflow interrupt
sei(); //enable global interrupt
while(1); // never ending loop
}
ISR(TIMER0_OVF_vect)
{
FREQ_CNT++;
if(FREQ_CNT>=31250) // FREQ_CNT count 31250 in one second
{
FREQ_CNT=0; // reset FREQ_CNT to 0
FF_CNT=1;
freq_pb0=note[j]; //changing the value of frequency in each second
// that means each note will be played for 1 second
j++; // increase the counter value by one
if(j>7) j=0; // if last note is being played then again start from begining
}
if(FREQ_CNT>=(FF_CNT*(15625/freq_pb0))) // for generating frequency
{ // just flipflop every time we
// enter inside this condition
FF_CNT++;
if(PORTB&(1<<PB0))
{
PORTB&=~(1<<PB0);
}
else
{
PORTB|=(1<<PB0); // so we are getting output from PB0
}
}
}
Everything is working fine except when i first give power supply to microcontroller the sound coming from the microcontroller is 20hz i.e.
the initial value assigned to freq_pb0 for 1 second.
I couldnot understand why this is happening.. friends please help me with
this??????
MICROCONTROLLER i am using is ATMEGA8 with 8Mhz clock frequency for above code.
#include<avr\io.h>
#include<avr\delay.h>
#include<avr\interrupt.h>
uint16_t FREQ_CNT=0;
uint16_t FF_CNT =0;
static volatile uint16_t freq_pb0=20; // initial value of frequency (any value..)
uint16_t note[]={300,337,378,401,451,506,569,603}; // array of frequency that we are
// going to generate
uint16_t j=0;
int main()
{
DDRB=0xff;
PORTB=0x00;
TCCR0=(1<<CS00); //prescalar 1 i.e. F_CPU (8MHZ) in our case
TIMSK=(1<<TOIE0); //enable timer0 overflow interrupt
sei(); //enable global interrupt
while(1); // never ending loop
}
ISR(TIMER0_OVF_vect)
{
FREQ_CNT++;
if(FREQ_CNT>=31250) // FREQ_CNT count 31250 in one second
{
FREQ_CNT=0; // reset FREQ_CNT to 0
FF_CNT=1;
freq_pb0=note[j]; //changing the value of frequency in each second
// that means each note will be played for 1 second
j++; // increase the counter value by one
if(j>7) j=0; // if last note is being played then again start from begining
}
if(FREQ_CNT>=(FF_CNT*(15625/freq_pb0))) // for generating frequency
{ // just flipflop every time we
// enter inside this condition
FF_CNT++;
if(PORTB&(1<<PB0))
{
PORTB&=~(1<<PB0);
}
else
{
PORTB|=(1<<PB0); // so we are getting output from PB0
}
}
}
Everything is working fine except when i first give power supply to microcontroller the sound coming from the microcontroller is 20hz i.e.
the initial value assigned to freq_pb0 for 1 second.
I couldnot understand why this is happening.. friends please help me with
this??????
MICROCONTROLLER i am using is ATMEGA8 with 8Mhz clock frequency for above code.
Last edited: