Results 1 to 5 of 5

Thread: Programing AtMega

  1. #1
    Join Date
    Jan 2008
    Location
    Montreal, Canada
    Posts
    477
    Images
    63
    Rep Power
    56

    Programing AtMega

    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.
    Last edited by sam; 10-02-2010 at 04:22 PM.

  2. #2
    Join Date
    May 2008
    Posts
    2,228
    Images
    155
    Rep Power
    128

    Re: Programing AtMega

    Quote Originally Posted by sam View Post
    -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

  3. #3
    Join Date
    Jun 2009
    Location
    Wisconsin
    Posts
    369
    Images
    192
    Rep Power
    58

    Re: Programing AtMega

    Code:
    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.
    Last edited by Upgrayd; 10-02-2010 at 03:11 PM.

  4. #4
    Join Date
    Jan 2008
    Location
    Montreal, Canada
    Posts
    477
    Images
    63
    Rep Power
    56

    Re: Programing AtMega

    Quote Originally Posted by lnxfergy View Post
    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

  5. #5
    Join Date
    May 2008
    Posts
    2,228
    Images
    155
    Rep Power
    128

    Re: Programing AtMega

    Quote Originally Posted by sam View Post
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Question(s) Controlling SSC-32 with Atmega 168
    By sam in forum Software and Programming
    Replies: 0
    Last Post: 03-15-2010, 06:25 PM
  2. Question(s) ER1 Programing Help
    By Tymtravler in forum Robotics General Discussion
    Replies: 6
    Last Post: 02-02-2009, 06:31 PM
  3. Genetic Programing and Evolutionary Robotics w/the Khepera II
    By mr-sk in forum Software and Programming
    Replies: 8
    Last Post: 08-08-2008, 10:01 PM
  4. Programing the Parallax Servo Controller (USB)
    By MinyFly92 in forum Software and Programming
    Replies: 7
    Last Post: 03-10-2008, 08:02 AM
  5. Easy Programing of servos and play back
    By hotrodled in forum DYNAMIXEL & Robot Actuators
    Replies: 1
    Last Post: 01-30-2008, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •