Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

testing if an SDPT switch was pressed

Status
Not open for further replies.
testing if an push to make switch was pressed

Hi
I read the .

I connected a switch to my avr in exactly the same way but with my internal pull ups activated. In the tutorial it said that when the switch wasn't pressed it's a 1 and when it's pressed it's a 0.

if(PIND & 0b00000010)
{
//Switch is not pressed
...
}

Would that not be 0b0000000 for pressed? The idea is my code only runs when it's pressed. Or are you saying I must use an else
 
Last edited:
Hi
I read the .

I connected a switch to my avr in exactly the same way but with my internal pull ups activated. In the tutorial it said that when the switch wasn't pressed it's a 1 and when it's pressed it's a 0.

if(PIND & 0b00000010)
{
//Switch is not pressed
...
}

Would that not be 0b0000000 for pressed? The idea is my code only runs when it's pressed. Or are you saying I must use an else

If the above code works as described you can use:

if(!(PIND & 0b00000010))
{
//Switch is pressed
...
}
 
Forgive me if this is too simplistic.

bits are numbered 7 down to 0

bit 0 is at position 0b00000001
bit 1 is at position 0b00000010
...
bit 7 is at position 0b10000000

The code
(PIND & 0b00000010)
will return 1 if bit 1 of PIND is 1.

But because you switch is active low you need to invert it
!(PIND & 0b00000010)
as suggested in a earlier post.

I do not know what you are using for debugging but I would check to see if the switch was effecting the port. Declare a char var and set it to the value of PIND. Check to see if it is different with the switch open or closed.

char switchVal;
...
switchVal=PIND;
// look at the value of switchVal here
 
yeah that what I thought too. I gathered that info from the tutorial over at extreme electronics.

According to the tutorial I have a 4.7 pull up resistor connected to positive terminal which is then connected to the switch.
The second pin on the switch is connected to the appropriate port which is PD1 in my case. The switch am using is push to make.
It's doesn't seem to be detecting the press. Trouble is the avrisp mk2 doesn't have debugger functionality.

What do you mean by actively low?
 
Last edited:
You should have the pull-up resistor connected between positive terminal (the microcontroller supply voltage) and the PD1 pin. The push button should be connected between ground and the PD1 pin.

The pull-up resistor keeps the PD1 pin high when the button does not make connection. When the button is pressed, it connects the PD1 directly to ground.
 
Last edited:
Thanks alot guys it seems to be working now.

For some reason that didn't work !(PIND & 0b00000010) but I
used the bit_is_clear(PIND,PD1) then it just started working??????
STRANGE!!
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top