Society of Robots - Robot Forum

Software => Software => Topic started by: Commanderbob on July 15, 2008, 02:27:13 PM

Title: Ping Problems
Post by: Commanderbob on July 15, 2008, 02:27:13 PM
OK I can't figure it out. I have the PING sensor from Parallax and I am trying to use it with the ATmega128. Here is my code:
Code: [Select]
#define ULTRA_SONIC_MASK (1<<4)

#define ULTRA_SONIC_OUT (DDRD|=ULTRA_SONIC_MASK)
#define ULTRA_SONIC_IN (DDRD&=~ULTRA_SONIC_MASK)
#define ULTRA_SONIC_SET (PORTD|=ULTRA_SONIC_MASK)
#define ULTRA_SONIC_RESET (PORTD&=~ULTRA_SONIC_MASK)
#define ULTRA_SONIC_IS_LOW (!(PORTD&ULTRA_SONIC_MASK))
#define ULTRA_SONIC_IS_HIGH ((PORTD&ULTRA_SONIC_MASK)==ULTRA_SONIC_MASK)
#define ULTRA_SONIC_CAP_RISING (TCCR1B|=(1<<ICES1))
#define ULTRA_SONIC_CAP_FALLING (TCCR1B&=~(1<<ICES1))

uint16_t ultra_sonic (void)
{
    uint16_t fcap=0,scap=0;
    ULTRA_SONIC_OUT;
    ULTRA_SONIC_SET;
    _delay_ms (1);
    ULTRA_SONIC_RESET;
    ULTRA_SONIC_IN;
    TCNT1=0;
    ULTRA_SONIC_CAP_RISING;
    while(ULTRA_SONIC_IS_LOW);
    fcap=ICR1;
    ULTRA_SONIC_CAP_FALLING;
    while(ULTRA_SONIC_IS_HIGH);
    scap=ICR1;
    return scap-fcap;
}

For some unknown reason it gets stuck at while(ULTRA_SONIC_IS_LOW); even if I disconnect the PING sensor and force it high. Any ideas?
Thanks,
Justin
Title: Re: Ping Problems
Post by: pomprocker on July 15, 2008, 03:26:27 PM
Justin, I'm glad to see someone else working on the same thing as me. Nobody seems to be replying on my post about this. Maybe we could work together here:

http://www.societyofrobots.com/robotforum/index.php?topic=4656.0 (http://www.societyofrobots.com/robotforum/index.php?topic=4656.0)
Title: Re: Ping Problems
Post by: Commanderbob on July 15, 2008, 05:09:30 PM
Fixed it!
EDIT: I guess I should add that I fixed it by using PIND not PORTD to read the inputs. PIC use PORTx to read inputs while AVR use PINx. See the link above of my code!