# doubt in program of 8051 microcontroller...regarding switch

Status
Not open for further replies.

#### 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??

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)
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
}
}

}
}

#### diegobb

##### Member level 5
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.

#### bobdxcool

##### Member level 5
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.

I have included sbit p1.3 which is d pin where i have conencted my switch (button)...so there's no need of readbutton.

#### diegobb

##### Member level 5
What uC are you using?
Best regards,

#### papunblg

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)
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
}
}

}
}

To configure button as input pin write button=1; at the start of main. After that button will get 0 or 1 depending upon how the switch is connected

---------- Post added at 23:43 ---------- Previous post was at 23:37 ----------

Two common ways to interface switches to a microcontroller input. Input P0 uses R1 as a pull up. If SW1 is open, P0 will be high, and read as a logical 1. When SW1 is closed, pin P0 is shorted to ground, or 0V, and P0 will read as a logical 0.

---------- Post added at 23:45 ---------- Previous post was at 23:43 ----------

P1 has R2 as a pull down resistor. When SW2 is open, P1 is pulled low, and read as a logical 0. Closing SW2 causes current to flow through R2, raising the voltage at P1 to the Vcc level. At that point P1 will read as a logical 1.

---------- Post added at 23:46 ---------- Previous post was at 23:45 ----------

bobdxcool

### bobdxcool

Points: 2

#### bobdxcool

##### Member level 5
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 down resistor.

thats all right??

Last edited:

#### papunblg

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.

bobdxcool

### bobdxcool

Points: 2

#### bobdxcool

##### Member level 5
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.

sir i am going to use port 1..is 1k enough pull down resistor value..

so now i should modify the program to

button=1;

if (button==1)
{
----
--}

else
{

}

right sir?

#### papunblg

Yes you get the idea. Port name does not matter here. You can connect switch like this in any port. Switch with pull-up shows logic 0 or low when pressed. Switch with pull down shows logic 1 or high when pressed.

#### bobdxcool

##### Member level 5
thanks a lot!

will let u know the results!

is 1kohm resistor enough?

#### Qaisar Azeemi

##### Full Member level 5
i think any resistor from 100ohms through 10k ohms can be used as a pull up or pull down. it is only one resistor with one pin so its work is to drop full voltage supply to it and limit the current.... low value resistors will cause to flow more current than high values and it should be according to your design....

#### papunblg

thanks a lot!

will let u know the results!

is 1kohm resistor enough?

Pullup resistors are used to pull logic signals up (to logic 1). So when some inut signal needs to be set to logic 1, but might need to be changed for some reason to 0 at some other time, a pullup resistor can keep the signal at logic 1 until the signal is pulled down by something(in this case the switch).

Typical application for pullup resistors is to connect 4.7 kohm (or some other suitable resitor value usually between 1 kohm and 10 kohm) from the circuit operating voltage (+5V usually) to the input pin. This resistor keep the signal at logic 1.

When the signal needs to be set to 0 it is pulled down by conencting that input pin to ground (usually throuhgh a button, DIP switch or open collector output of some other part of circuit).

Current will flow from Vcc, through pull up resistance, and to ground. It isn't considered a short, however, because pull up resistance will limit the amount of current that can flow to a very small amount. In fact, you can compute this using Ohms law.

I = V / R
I = 5v / 10,000ohms
I = .0005A (.5mA)

The exact value doesn't actually matter, as long as it is high enough to prevent too much current from flowing. 10k seems to be the most common

Pulldown reistors work in the opposite way. They keep signal a logic 0 until somethign connect te signal input to +5V (or whatever the operating voltae of the logic circuit is).

http://www.interfacebus.com/IC_Output_Input_Pullup_Resistor_Values.html

Last edited:

Status
Not open for further replies.