Shop OBEX P1 Docs P2 Docs Learn Events
Sping Stamp and I2c / SRF02 problems — Parallax Forums

Sping Stamp and I2c / SRF02 problems

raptor313raptor313 Posts: 4
edited 2008-02-21 16:25 in Propeller 1
I'm using an SRF02 with a Spin Stamp. I've run into a problem. Until yesterday I had the thing working. After a very long time trying to debug it. Well yesterday I went to open up the file only to find an Optimal configuration within brackets and a list of Windows Drivers. I can't for the life of me get the SRF02 sensors to work. I know they're still working as I tested them with my Basic Stamp code and they work just fine. Could someone with some more experience take a look at this and tell me whats going on. I've downloaded this code from
SRF02.spin

con
  _clkfreq = 10_000_000
  _CLKMODE = xtal1 + pll8x


  i2cSCLLine        = 9        'SCL is on Proppeller pin A13
  i2cSDALine        = 8        'SDA is on Proppeller pin A12
  
  SRF08_addr        = $e0       'SRF08 default address
  SRF08_CMD_REG     = 0
  SRF08_VER_REG     = 0
  SRF08_LDR_REG     = 1
  SRF08_ECHO1H_REG  = 2
  SRF08_ECHO1L_REG  = 3
  PING_CM           = 81

OBJ
  i2cObject      : "i2cObject"    'include the I2C handler
  Num            : "numbers"     'number handler allows number (keypad states) to easily be formated to ascii text string
                                 'note numbers is an object file included in installation directory
var
  byte version,length,light
  word range
pub main

  Num.init                                              'required by parallax to be called before using Num.ToStr function                                                     
  i2cObject.init(i2cSDALine,i2cSCLLine)                 'passes the pin numbers of the sda and scl lines to i2cobject
  repeat
    i2cObject.Writelocation(SRF08_Addr,SRF08_CMD_REG,PING_CM)          'command to commence a range 'ping'
    waitcnt(6_000_000 + cnt)                                           'delay for ranging
    version := i2cObject.readLocation(SRF08_addr,SRF08_VER_REG)        'SRF08 version in register 0
    range := i2cObject.readLocation(SRF08_addr,SRF08_ECHO1L_REG)       'low byte of first echo at reg 3
    range += i2cObject.readLocation(SRF08_addr,SRF08_ECHO1H_REG)<<8    'High byte of first echo at reg 2    




this is the modified SRF02 code I got from http://www.robot-electronics.co.uk/files/i2cObject.spin

'' ******************************************************************************
'' * Simplified I2C for Devantech modules                                       *
'' * Chris Clarke Feb 2007                                                      *
'' ******************************************************************************
''
'' adapted from I2C SPIN Object by James Burrows
''
'' for reference look at: www.semiconductors.philips.com/ acrobat/literature/9398/39340011.pdf
''
'' this object provides the PUBLIC functions:
''  -> Init  - sets up the SCL and SDA pin referance 
''  -> readLocation - READ from a single specified register
''  -> writeLocation - WRITE to a single specified register
''  -> i2cStart - performs a bus start
''  -> i2cStop - performs a bus stop
''  -> i2cRead - performs a read of the i2c bus
''  -> i2cWrite - performs a write to the i2c bus

CON
  ' i2c bus contants
  _i2cNAK         = 1
  _i2cACK         = 0
VAR
  byte  i2cSDA, i2cSCL
PUB init(_i2cSDA, _i2cSCL)
    i2cSDA := _i2cSDA  'referances passed from main file for pin numbers
    i2cSCL := _i2cSCL
    outa[noparse][[/noparse]i2cSDA] := 0  'as inputs to start
    outa[noparse][[/noparse]i2cSCL] := 0

PUB readLocation(deviceAddress, deviceRegister) : i2cData | ackbit
  ' do a standard i2c address, then read
  ' read a device's register
  ackbit := _i2cACK

  i2cStart
  ackbit := (ackbit << 1) | i2cWrite(deviceAddress)   
  ackbit := (ackbit << 1) | i2cWrite(deviceRegister)   
    
  i2cStart
  ackbit := (ackbit << 1) | i2cWrite(deviceAddress | 1)   ' repeat with read bit now set
  i2cData := i2cRead(_i2cNAK)
  i2cStop

  ' return the data      
  return i2cData

    
PUB writeLocation(deviceAddress, deviceRegister, i2cDataValue) : ackbit
  ' do a standard i2c address, then write
  ' return the ACK bit from the device address
  ackbit := _i2cACK
  i2cstart
  ackbit := (ackbit << 1) | i2cWrite(deviceAddress)
  ackbit := (ackbit << 1) | i2cWrite(deviceRegister)
  ackbit := (ackbit << 1) | i2cWrite(i2cDataValue)
  i2cStop
  return ackbit

PUB i2cStop
  ' i2c stop sequence - the SDA goes LOW to HIGH while SCL is HIGH
  dira[noparse][[/noparse]i2cSCL] ~
  dira[noparse][[/noparse]i2cSDA] ~

    
PUB i2cStart

       dira[noparse][[/noparse]i2cSDA] ~  
       dira[noparse][[/noparse]i2cSCL] ~
       dira[noparse][[/noparse]i2cSDA] ~~       
       
       repeat until ina[noparse][[/noparse]i2cSCL] == 1  

PUB i2cWrite(i2cData) : ackbit
    
    i2cData <<=24  

    ' init the clock line                             
    dira[noparse][[/noparse]i2cSCL] := 1 
 
    repeat 8
      ' set the SDA while the SCL is LOW 
      dira[noparse][[/noparse]i2cSDA] := (!(i2cData <-= 1) & 1)
      ' toggle SCL HIGH
      dira[noparse][[/noparse]i2cSCL] := 0
      ' toogle SCL LOW
      dira[noparse][[/noparse]i2cSCL] := 1

   ' setup for ACK - pin to input'    
    dira[noparse][[/noparse]i2cSDA] := 0
                  
    ' read in the ACK
    dira[noparse][[/noparse]i2cSCL] := 0
    ackbit := ina[noparse][[/noparse]i2cSDA]
    dira[noparse][[/noparse]i2cSCL] := 1

    ' leave the SDA pin LOW 
    dira[noparse][[/noparse]i2cSDA] := 1    

   return ackbit  


PUB i2cRead(ackbit): i2cData

    ' set the SCL to output and the SDA to input
    dira[noparse][[/noparse]i2cSDA] := 0
    dira[noparse][[/noparse]i2cSCL] := 1
     
    ' clock in the byte
    i2cData := 0
    repeat 8
      dira[noparse][[/noparse]i2cSCL] := 0
      i2cData := (i2cData << 1) | ina[noparse][[/noparse]i2cSDA]
      dira[noparse][[/noparse]i2cSCL] := 1
      
    ' send the ACK or NAK
    dira[noparse][[/noparse]i2cSDA] ~~
    dira[noparse][[/noparse]i2cSCL] := 0
    dira[noparse][[/noparse]i2cSDA] := !ackbit
    dira[noparse][[/noparse]i2cSCL] := 1

    ' return the data
    return i2cData

Comments

  • JavalinJavalin Posts: 892
    edited 2008-02-21 09:05
    raptor313,

    Have you looked at my updated i2cObject (v2.1 currently) in the object exchange?··obex.parallax.com - its also available on the http://www.robot-electronics.co.uk/acatalog/examples.html examples page.

    It has working demo code for the SRF08 and many other i2c devices from Davantech.

    Couple of spots on a quick check:

    con
      _clkfreq = 10_000_000
      _CLKMODE = xtal1 + pll8x
    


    Shouldn't this be:

    CON
      _clkmode      = xtal1 + pll16x
      _xinfreq      = 5_000_000
    


    ??· What crystal are you running with?

    and;

      i2cSCLLine        = 9        'SCL is on Proppeller pin A13
      i2cSDALine        = 8        'SDA is on Proppeller pin A12
    


    the comments don't match the pins?

    Let us know how you get on.

    Cheers,

    James
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-21 09:28
    @Javalin, most things are just because of the SpinStamp - you are not so familiar with it??

    @raptor: You can also try my I2C routines, but tested with PCF chips only... Though they look very similar smile.gif
  • JavalinJavalin Posts: 892
    edited 2008-02-21 10:14
    Ah - No i've not had the pleasure. I assme then, that the I2C routines will work - just need pins changing etc.

    J
  • raptor313raptor313 Posts: 4
    edited 2008-02-21 16:18
    Thank you for the responses. The Spin stamp has a 10Mhz chip on it so that pll8x is correct. And it doesn't have designated SDA SCL pins on it. I just got it working again, found an old working copy,
    but ther estill seems to be something wrong. I'll post it up later, I'm trying to see if its an electrical issue right now, although it seems strange that it works just fine on the
    BASIC stamp on the same BOE-bot board.
  • JavalinJavalin Posts: 892
    edited 2008-02-21 16:25
    Have you got 4.7k resistor pullups to +3.3V on the SCL and SDA. As a point of note - the Propeller doesn't have dedicated pins bar the P28-P31 which have special function at startup.

    Good news

    james
Sign In or Register to comment.