Shop OBEX P1 Docs P2 Docs Learn Events
Ping Ultrasonic Range Finder — Parallax Forums

Ping Ultrasonic Range Finder

speakstrangespeakstrange Posts: 1
edited 2007-08-21 14:05 in General Discussion
Hi ,
I am trying to run a series of ping ultrasonic rangefinders off timer 0 on an Atmel·ATMEGA128 chip. My problem is I need the other timers for other hardware and was wondering what is the best way to do this? Also im not sure if I should be using interrupts or not as I am running so many different things off this micro.

I attempted to write some code but have a feeling it is very wrong! I can see the inital pulse send to the rangefinder but never get a value back. Any ideas would be great! Thanks

Code:
#define Range_Pin 0x10 //Port B.4 OC0 pin
unsigned int Range = 1;

void init_rangefinder()
{
· ·int var = 0;
· ·TCCR0=0; //Restart Timer
· ·PORTB &= Range_Pin; //Clear Pin
· ·DDRB· |= Range_Pin; // Set Pin as output
· ·PORTB |= Range_Pin; //Set output pin to high
· ·_delay_us(5); //Delay
· ·PORTB &= ~ Range_Pin; //Set Pin to low
· ·DDRB &= ~ Range_Pin; //Set Pin to input
· ·
· ·while (var == 0)
· ·{
· ·· ·/*********************************************************/
· ·· ·/* Start Timer set prescaler to 1024· ·· ·· ·· ·· · */
· ·· ·/* 16Mhz/1024 = 15625Hz· ·· ·· ·· ·· ·· ·· ·· ·· · */
· ·· ·/* 256/15625(8bit counter)= 16.284ms to overflow counter */
· ·· ·/*********************************************************/
· ·· ·TCCR0 = 0x05;
· ·· ·while ((PORTB &= Range_Pin) !=1)
· ·· ·{
· ·· ·· ·//If timer has no feedback
· ·· ·· ·if(TCNT0 == 0xFF)
· ·· ·· ·{
· ·· ·· ·· ·Range= 0;
· ·· ·· ·· ·var=1;
· ·· ·· ·}
· ·· ·}
· ·· ·
· ·· ·if ((PORTB &= Range_Pin) == 0x10)
· ·· ·· ·{
· ·· ·· ·· ·TCCR0 = 0x05;//restart timer
· ·· ·· ·· ·var=1;
· ·· ·· ·}
· ·}

· ·if ((PORTB &= Range_Pin) == 0)
· ·{
· ·· ·TCCR0=0;
· ·· ·Range= TCNT0;
· ·}
· ·
}

Comments

  • pwillardpwillard Posts: 321
    edited 2007-08-21 12:30
    Hmm, you probably won't find a lot people that can provide ATMEGA guidance in the Basic Stamp forum since your coding is in AVR dialect of Gnu C Compiler. Have you asked your question in the AVRFREAKS GCC forum at http://www.avrfreaks.net ?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

    There's nothing like a new idea and a warm soldering iron.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-08-21 14:05
    This thread is being moved from the BASIC Stamp Forum to the Sandbox Forum.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.