bobdxcool
Member level 5
THIS A PROGRAM FOR MUSIC GENERATION..
ACCORDING TO MY PROGRAM whenever i press a switch 8051 uc must play a certain music and when not pressed it should play a different one.
but the problem is after continuous pressing of switch the program under switch pressed is playing but after i release it it is not returning to non pressed part of the program.
i have connected a tactile switch between pin and gnd n 1k resistor b/w pin and 5v/
what could be the problem??
is there any error in my if else loop??
ACCORDING TO MY PROGRAM whenever i press a switch 8051 uc must play a certain music and when not pressed it should play a different one.
but the problem is after continuous pressing of switch the program under switch pressed is playing but after i release it it is not returning to non pressed part of the program.
i have connected a tactile switch between pin and gnd n 1k resistor b/w pin and 5v/
what could be the problem??
is there any error in my if else loop??
Code:
#include<reg51.h>
sbit out=P1^0; //output pin 1
sbit button= P1^3;
void delay(unsigned char p,unsigned char q)
{
TMOD=0x01; //timer0 mode1(16 bit)
TL0=q; //load TL0
TH0=p; //load TH0
TR0=1; //turn on T0
while(TF0==0); // wait for flag generation
TR0=0; // turn off T0
TF0=0; // clear TF0
}
void play(unsigned char j,unsigned char k)
{
int i;
for(i=0;i<1000;i++) //to play sound
{
out=1;
delay(j,k);
out=0;
delay(j,k);
}
}
void main()
{
int l;
while(1)
{
if (button==0) // if switch pressed execute this part
{
play(0xff,0x03); //playing sound of frequency of about 1975.33 hertz with a delay between two frequencies
for(l=0;l<10;l++)
{
delay(0x00,0x00);
}
play(0xfe,0xe4); //playing sound of frequency of about 1760 hertz with a delay between two frequencies
for(l=0;l<10;l++)
{
delay(0x00,0x00);
}
play(0xff,0xe7); //playing sound of frequency of about 20000 hertz with a delay between two frequencies
for(l=0;l<17;l++)
{
delay(0x00,0x00);
}
play(0xfc,0x44); //playing sound of frequency of about 523.25 hertz with a delay between two frequencies
for(l=0;l<5;l++)
{
delay(0x00,0x00);
}
play(0xfd,0x82); //playing sound of frequency of about 783.99 hertz with a delay between two frequencies
for(l=0;l<13;l++)
{
delay(0x00,0x00);
}
play(0xfe,0xc1); //playing sound of frequency of about 1567.98 hertz with a delay between two frequencies
for(l=0;l<7;l++)
{
delay(0x00,0x00);
}
play(0xff,0xde); //playing sound of frequency of about 15000 hertz with a delay between two frequencies
for(l=0;l<10;l++)
{
delay(0x00,0x00);
}
play(0xfe,0xae); //playing sound of frequency of about 1479.98 hertz with a delay between two frequencies
for(l=0;l<5;l++)
{
delay(0x00,0x00);
}
play(0xfc,0xdc); //playing sound of frequency of about 622.25 hertz with a delay between two frequencies
for(l=0;l<12;l++)
{
delay(0x00,0x00);
}
}
else //if not pressed execute this part
{
play(0xff,0xe8);
for(l=0;l<20;l++)
{
delay(0x00,0x00);
}
play(0xff,0xdc);
for(l=0;l<20;l++)
{
delay(0x00,0x00);
}
play(0xff,0xcc);
for(l=0;l<5;l++)
{
delay(0x00,0x00);
}
play(0xff,0xd1);
for(l=0;l<20;l++)
{
delay(0x00,0x00);
}
play(0xfe,0x33);
for(l=0;l<20;l++)
{
delay(0x00,0x00);
}
play(0xff,0xa3);
for(l=0;l<20;l++)
{
delay(0x00,0x00);
}
play(0xfc,0x66);
for(l=0;l<20;l++)
{
delay(0x00,0x00); //delay of 71 miliseconds
}
}
}
}