newbee in c - my button isn't working

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I made this program to turn on some lights on PORTC on mY Atmega8535.There's a button on PORTD, pin4 (counting from 0), but it has no effect when I press it.Can you help me please? thank you.
(i'm using programmers notepad)

#include
#include

int main(void)
{

DDRC = 0xFF;
PORTC = 0x00;
DDRD = 0x00;
PORTD = 0xFF;
while(1)
{
if ((PIND & 0x0F))
{
PORTC = ~PORTC;
}

}
return(0);
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"PIND & 0x0F" means read the 8 bit value currently presented at PIND and then set the top 4 bits to 0. as the bit you are trying to "watch" is in this region you are just throwing away it's current value by doing the test this way. Try:

if (PIND & (1<<PD4)) {
  PORTC = ~PORTC;
}

and then mosey on down to the Tutorial section here and read the thread that has "101" in the title!

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you might want to add some delay in order to note the blinking LEDs...

#include 
.....
.....
.....
.....
if (PIND & (1<<PD4)) { 
  PORTC = ~PORTC; 
  _delay_ms(20);
} 

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

worked fine :), thank you very much