Ping Ultrasonic Range Finder
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;
· ·}
· ·
}
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
There's nothing like a new idea and a warm soldering iron.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support