#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
}
}
}
}
void main(){
[INDENT]int l;
while(1){
[INDENT]
[COLOR="#FF0000"]read_button(); // read the button to know if is pressed or not[/COLOR]
if (button==0) // if switch pressed execute this part{
...
}else{ //if not pressed execute this part
...
}[/INDENT]
}[/INDENT]
}
I think you forgot to read the button position in the main loop.
Code:void main(){ [INDENT]int l; while(1){ [INDENT] [COLOR="#FF0000"]read_button(); // read the button to know if is pressed or not[/COLOR] if (button==0) // if switch pressed execute this part{ ... }else{ //if not pressed execute this part ... }[/INDENT] }[/INDENT] }
You need to read the button each time the while() loop starts over.
Best Regards.
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??
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 #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; button=1; //<-------------------------------- ADD THIS LINE 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 } } } }
so now i should add the line u suggested and connect p1.3 to switch n other end of switch to 5 v(which i had connected to gnd before) n use 1k pull up resistor.
thats all right??
yes add the line to configure the pin.
Now if you want to express a closed switch as 0. Use a pull-up resistance like R1. one end of switch to micro and other end of the switch to ground [sw1 in picture]
if (button==0) will be true when button is closed.
thanks a lot!
will let u know the results!
is 1kohm resistor enough?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?