twi with ultrasonic ranger

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi everyone, wondering if someone can help me, i have an ultrasonic ranger that communicates via i2c and have a test program i have been fiddling with. however, it out puts to the terminal via uart0 fine but it says slave address error and inches = ?. i dont know much about twi really so i was hoping someone here could help. its using the mega128 and winavr. there is quite a few header files so i wont include them unless u want me too. here is the main.c

/*
 * $Id: main.c,v 1.10 2005/03/22 19:12:15 bsd Exp $
 *
 */

/*
 * SRF08 - a complete test program using the SRF08 on the BDMICRO
 * MAVRIC and MAVRIC-II microcontroller boards.  This program is
 * intended to demonstrate how to use the BDMICRO MAVRIC or MAVRIC-II
 * microcontroller boards in conjunction with SRF08 Ultrasonic Ranger
 * Module.  Feel free to copy the relevent function definitions into
 * your own applications and to customize them for your needs.
 *
 * See: http://www.bdmicro.com/
 *
 * Configuration:
 *
 *   Simply attach your SRF08 Ultrasonic Ranger Module to your board's
 *   I2C bus, power, and ground.  It's OK to use one of the supplied
 *   VCC pins on your microcontroller board to supply power to the
 *   SRF08.
 *
 *   If desired, attach a terminal to UART0 for 9600, 8N1; the SRF08
 *   will be read once per second and the result in inches printed to
 *   UART0.
 *
 */

#include 
#include 
#include 
#include 
#include 

#include 

#include "i2c.h"
#include "srf08.h"

#define BAUD_RR ((CPU_FREQ/(16L*9600L) - 1))


#define CPU_FREQ 16000000L  /* set to clock frequency in Hz */

#if CPU_FREQ == 16000000L
#define OCR_1MS 125
#elif CPU_FREQ == 14745600L
#define OCR_1MS 115
#endif

volatile uint16_t ms_count;


/*
 * ms_sleep() - delay for specified number of milliseconds
 */
void ms_sleep(uint16_t ms)
{
  TCNT0  = 0;
  ms_count = 0;
  while (ms_count != ms)
    ;
}


/* 
 * millisecond counter interrupt vector 
 */
SIGNAL(SIG_OUTPUT_COMPARE0)
{
  ms_count++;
}


/*
 * Initialize timer0 to use the main crystal clock and the output
 * compare interrupt feature to generate an interrupt approximately
 * once per millisecond to use as a general purpose time base.
 */
void init_timer0(void)
{
  TCCR0 = 0;
  TIFR  |= _BV(OCIE0)|_BV(TOIE0);
  TIMSK |= _BV(TOIE0)|_BV(OCIE0);         /* enable output compare interrupt */
  TCCR0  = _BV(WGM01)|_BV(CS02)|_BV(CS00); /* CTC, prescale = 128 */
  TCNT0  = 0;
  OCR0   = OCR_1MS;                     /* match in aprox 1 ms */
}




int def_putc(char ch, FILE* unusedparam)
{
  /* output character to UART0 */
  while ((UCSR0A & _BV(UDRE)) == 0)
    ;
  UDR0 = ch;
  return ch;
}





int main (void)
{
  uint16_t distance;

  init_timer0();

  /* enable UART0 */
  UBRR0H = 0x00;
  UBRR0L = 0x67;


   UCSR0C = (1<<UCSZ1) | (1<<UCSZ0);
   
   UCSR0B = (1<<RXEN) | (1<<TXEN);
  /* enable interrupts */
  sei();

  /* initialize stdio */
  fdevopen(def_putc, NULL);

  /* set the I2C bit rate generator to 100 kb/s */
  TWSR &= ~0x03;
  TWBR  = 28;
  TWCR |= _BV(TWEN);

  while (1) {
    ms_sleep(1000);                  /* sleep for 1 second */
    srf08_ping(0x70, RANGE_CM);      /* initiate a ping, distance in cm */
    ms_sleep(70);                    /* wait for 70 ms */
    srf08_range(0x70, 0, &distance); /* read first echo */
    printf("range = %0.1f inches\n", ((double)distance) / 2.54);
  }
}

let me know if the rest is required. cheers

:arrow: Dan :!:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you using the SRF08 Ultrasonic Ranger Module connected to either the MAVRIC or MAVRIC-II microcontroller board or are you using a different ATmega board?

BTW, link to all of the sources:
http://www.bdmicro.com/code/srf0...

link to SFR08 Ultrasonic Ranging Module
http://www.robot-electronics.co....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

well to be honest its the srf04 and just an atmega128 devboard

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think it's time to read over the SRF04 datasheet. If you want to adapt the SFR08 code to the SFR04 then you'll need to read its datasheet also. Look for differences between the two. I haven't done this myself, so I can't tell you how close they are. Also, the sources you are using are written for the MARVIC or MARVIC-II microcontroller boards and may initialize the clocks differently than required for your board. Take a look at the MARVIC user manual and/or schematic to see what the clock frequency is compared to your board and make the appropriate changes.

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

well actually i already changed the uart settings for the xtal im using. but yeah i will do that. thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just for curiosity, since I had developed my own ultrasonic 'range finder'. Can anyone that uses SRF0X tell me something about they'r real specs? Real range, death band, real directivity, stability/repeatability with the same target, and so on?

Thanks,

Guillem.

Guillem.
"Common sense is the least common of the senses" Anonymous.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
well actually i already changed the uart settings for the xtal im using.

You may also have to modify the TWI Bit Rate Register, TWBR.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another question. Does your ATmega128 development board have pull-up resistors on the TWI SDL and SCK lines?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CAN ANY HELP I NEED vELOCITY CALUCLATION THROUGH DISTANCE CLAC USING ULTRA SONIC SENSOR
I AM USING atmega16 AND avrstudio4 SEND TO MY MAIL pavan_chekuri2003@yahoo.co.in