Author Topic: Ping Problems  (Read 3314 times)

0 Members and 1 Guest are viewing this topic.

Offline CommanderbobTopic starter

  • Robot Overlord
  • ****
  • Posts: 146
  • Helpful? 2
  • Embeddedmicro.com
    • Embedded Micro
Ping Problems
« 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

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: Ping Problems
« Reply #1 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

Offline CommanderbobTopic starter

  • Robot Overlord
  • ****
  • Posts: 146
  • Helpful? 2
  • Embeddedmicro.com
    • Embedded Micro
Re: Ping Problems
« Reply #2 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!
« Last Edit: September 25, 2008, 04:20:30 PM by Commanderbob »

 

SMF spam blocked by CleanTalk