Full Version : Reading GPIOs on ATmega128
avr >>C PROGRAMMING (AVR) >>Reading GPIOs on ATmega128


kaushal98- 10-09-2007
Hello All,

I am trying to read PD6 on ATmega128, but somehow it is always read as HIGH. Even if I don't connect anything to the pin PD6, it still is high internally.

I measured the voltage on the pin with multimeter, and it would show LOW, but the code reads high. And I've no idea what's going on. Basically I am trying to detect presence of iPod on one of the pins. When I connect iPod to the board I see the pin going high and when I remove it it would go low, but internally the code always thinks it's high.

I tried to disable the pull up resistor and everything else that I could think of but nothing would work.

Here's my sample code:
**************************
#define SetBit(x,y) (x |= (1<<y))
#define ClrBit(x,y) (x &= ~(1<<y))
#define TestBit(x,y) (x & (1<<y))

VOID main (VOID)
{
//Set PD5 as an output to control LED
SetBit(DDRD, PD5);

//iPod sense enable, no internal pull up on PD6
ClrBit(PORTD, PD6);
ClrBit(DDRD, PD6);

while (1)
{
ClrBit(PORTD, PD5);
//See if there is a HIGH level on PD6
if (TestBit(PIND, PD6))
//toggle LED
SetBit(PORTD, PD5);
}

**************************

It should go in the if () statement only when iPod is present. I couldn't make my code any simpler than this, just to see if it can detect LOW on PD6.

I would highly appreciate any kind of insight to resolve this.

Thanks,
Kaushal


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.