BS2 Sonar SRF08

From RoboWiki
Jump to: navigation, search

Tento program meria vzdialenosť senzorom SRF08 a vypisuje hodnoty na sériový LCD displej a zároveň posiela cez USB/RS-232 do PC, kde sa dá zobraziť v terminálovom okne.

Pripojenie

SRF08: 
     SCL      CON  9         ' I2C clock
     SDA      CON  8         ' I2C data
LCD Backpack:
     LCD      CON  0         ' LCD Serial Backapck Data Line


'{$STAMP BS2}

SCL      CON  9         ' I2C clock
SDA      CON  8         ' I2C data
LCD      CON  0         ' LCD Serial Backapck Data Line
SDAin    VAR  IN8
SDAout   VAR  OUT8      ' To change the pins used, alter these 5 lines
SDAdir   VAR  DIR8      ' The 4 SDA numbers must be the same, of course

loop     VAR  Byte      ' just a looping counter
I2cBuf   VAR  Byte      ' I2c read/write buffer
I2cAddr  VAR  Byte      ' Address of I2C device
I2cReg   VAR  Byte      ' Register number within I2C device
I2cData  VAR  Word      ' Data to read/write
I2cAck   VAR  Bit       ' Acknowledge bit
                        ' LCD: Defining some useful constants for the Backpack.
N9600    CON  $4054     ' Baudmode-9600 bps inverted.
    I    CON  254       ' Instruction prefix value.
  CLR    CON  1         ' LCD clear-screen instruction.
LINE2    CON  192       ' Address of 1st char of 2nd line.
L1_C7    CON  135       ' Address of line 1, character 7.
L2_C7    CON  199       ' Address of line 1, character 7.

  SEROUT 0,N9600,[I,CLR] ' Clear the LCD screen.
  SEROUT 0,N9600,["Range:"] ' Print the label.

Main:

  I2cAddr = $e0
  I2cData = 81          ' Rangeing command - 80 for inches, 81 for cm, 82 for uS

  I2cReg  = 0
  GOSUB  I2cByteWrite
  PAUSE  70             ' Wait for ranging to complete

  I2cReg = 1            ' Address of light sensor register
  GOSUB  I2cByteRead
  DEBUG  2,0,0, "Light Sensor    ", DEC3 I2cData, " u"


  I2cReg = 2        ' address of first ranging result
  GOSUB  I2cWordRead
  DEBUG  2,0,1, "Range           ", DEC4  I2cData, " cm"
  SEROUT 0,N9600,[I,L1_C7]                                   ' Move to line 1, character 7.
  SEROUT 0,N9600,[DEC4 I2cData, " cm"]                       ' Print message.

' --------------------------------------------------------------------------------------------

  I2cData = 82          ' Rangeing command - 80 for inches, 81 for cm, 82 for uS

  I2cReg  = 0
  GOSUB  I2cByteWrite
  PAUSE  70             ' Wait for ranging to complete

  I2cReg = 2            ' address of first ranging result
  GOSUB  I2cWordRead
  DEBUG  2,0,2, "Range           ", DEC4  I2cData, " us"
  SEROUT 0,N9600,[I,L2_C7]                                   ' Move to line 1, character 7.
  SEROUT 0,N9600,[DEC4 I2cData, " us"]                       ' Print message.



  GOTO  main


'--------------------------------------------------------------------------------------------
' I2C subroutines follow
'--------------------------------------------------------------------------------------------

I2cByteWrite:            ' writes I2cData.lowbyte to I2cReg at I2cAddr

  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  I2cBuf = I2cData.LOWBYTE
  GOSUB  I2cOutByte      ' send the data
  GOSUB  I2cStop
  RETURN


I2cWordWrite:            ' writes I2cData to I2cReg at I2cAddr

  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  I2cBuf = I2cData.HIGHBYTE
  GOSUB  I2cOutByte      ' send the data - high byte
  I2cBuf = I2cData.LOWBYTE
  GOSUB  I2cOutByte      ' send the data - low byte
  GOSUB  I2cStop
  RETURN


I2CByteRead:
  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  GOSUB  I2cStart      ' repeated start
  I2cBuf = I2cAddr | 1
  GOSUB  I2cOutByte      ' send device address (with read set)
  I2cAck = 0        ' send Nak
  GOSUB  I2cInByte
  I2cData.LOWBYTE = I2cBuf  ' read the data
  I2cData.HIGHBYTE = 0
  GOSUB  I2cStop
  RETURN


I2CWordRead:
  GOSUB  I2cStart
  I2cBuf = I2cAddr
  GOSUB  I2cOutByte      ' send device address
  I2cBuf = I2cReg
  GOSUB  I2cOutByte      ' send register number
  GOSUB  I2cStart      ' repeated start
  I2cBuf = I2cAddr | 1
  I2cAck = 1        ' send Ack
  GOSUB  I2cOutByte      ' send device address (with read set)
  GOSUB  I2cInByte
  I2cData.HIGHBYTE = I2cBuf  ' read the data
  I2cAck = 0        ' send Nak
  GOSUB  I2cInByte
  I2cData.LOWBYTE = I2cBuf
  GOSUB  I2cStop
  RETURN


I2cOutByte:
  SHIFTOUT SDA, SCL, MSBFIRST, [I2cBuf]
  INPUT  SDA
  HIGH  SCL          ' clock in the ack' bit
  LOW  SCL
  RETURN


I2cInByte:
  SHIFTIN SDA, SCL, MSBPRE, [I2cBuf]
  SDAout = 0
  SDAdir = I2cAck
  HIGH  SCL          ' clock out the ack' bit
  LOW   SCL
  INPUT  SDA
  RETURN


I2cStart            ' I2C start bit sequence
  HIGH  SDA
  HIGH  SCL
  LOW  SDA
  LOW  SCL
  RETURN


I2cStop:            ' I2C stop bit sequence
  LOW  SDA
  HIGH  SCL
  HIGH  SDA
  RETURN