PDA

View Full Version : [Question(s)] Programing AtMega



sam
10-02-2010, 02:48 PM
Hi again everybody,

I have a problem that I can't seem to resolve...

While doing a simple check list of functions when I built my robot, I got to the point where I needed to check that my AVR could detect changes with the floor (darkness/whiteness), but it couldn't.

I have some QDR1114 Floor sensors (the ones that almost everyone uses). I tested every physical problems I could have : With a camera I checked and the IR lights work. With my multimeter I checked that the pin on my AtMega 328 (newer version of 168) went high (4.68V) when the sensor was over black and low (0.19 V) when on white (or vice versa... I can't remember).

So I come to the conclusion that my code is wrong... but I can't see what is wrong. It's really simple.

-B0 connected to LED. LED blinks well, I already checked that.
-10 qdr1114 sensors connected to 8 bits of C and bit 0 and 1 of D.



#include <avr/io.h>
#include <util/delay.h>


#define B0 0x01

int main (void)
{

DDRB = 0xff; //output
DDRC = 0xfb; //input
DDRD = 0xFB; //input


while (1)
{
if ( PINC & 0x08 )
{
PORTB |= 0x01;
}
else
{
PORTB |= B0;
_delay_ms(50);
PORTB &= ~B0;
_delay_ms(50);
}

}
return 0;

}


Really simple, but it doesn't work. The light blinks always (even if it's on white or black). The voltage changes from white to black thought.

Thanks,

Sam

EDIT : Indeed, lnxferg.

lnxfergy
10-02-2010, 03:06 PM
-B0 connected to LED. LED blinks well, I already checked that.
-10 qdr1114 sensors connected to 8 bits of B and bit 0 and 1 of D.

I assume you meant to say the 8 bits of port "C", and then pins 0 and 1 of port D. Correct? In which case, DDRC should be set to 0x00 (all pins input). If you aren't using anything else on Port D, I'd leave DDRD set to 0x00 as well.

Also, realistically speaking, the default is already 0x00 -- so you really only need to set DDRx bits to 1 for pins when you want them to act as outputs, everything defaults to an floating input.

-Fergs

Upgrayd
10-02-2010, 03:09 PM
if ( PINC & 0x08 )
Its hard to tell from your description what is connected to what.

The code above is looking at the port C pin PC3. Make sure this is the pin your sensor output is connected to.

sam
10-02-2010, 04:24 PM
I assume you meant to say the 8 bits of port "C", and then pins 0 and 1 of port D. Correct? In which case, DDRC should be set to 0x00 (all pins input). If you aren't using anything else on Port D, I'd leave DDRD set to 0x00 as well.

Also, realistically speaking, the default is already 0x00 -- so you really only need to set DDRx bits to 1 for pins when you want them to act as outputs, everything defaults to an floating input.

-Fergs

Yes, it's 8 bits of port C.

Thanks! Now it works... I thought 0xfb would set it as input? seems I was wrong. 0x00 works fine now.

Sam

lnxfergy
10-02-2010, 04:57 PM
Yes, it's 8 bits of port C.

Thanks! Now it works... I thought 0xfb would set it as input? seems I was wrong. 0x00 works fine now.

Sam

See the datasheet for more detail, but basically, each bit of the DDRx registers relate to a single pin on port X. Setting that bit to 1 makes the pin an output, setting it to 0 makes it an input. Thus, for a port x of all 8 bits as inputs, the DDRx = 0.

-Fergs