Shop OBEX P1 Docs P2 Docs Learn Events
Decimal to binary translation help required — Parallax Forums

Decimal to binary translation help required

GogsGogs Posts: 19
edited 2008-07-16 03:28 in BASIC Stamp
With the compass I just calibrated I wish to output the values to a three 7 segment LEDS. If you look at the code you can tell the I2C data is a decimal. A 7- segment display requires this value to be split and then translated into a binary or Hexidecimal. eg if the value is 1 then the output for a 7 segment display would be
either 00000110 or $06.

Can anyone help?

'{$STAMP BS2p}
'***********************************************************
'**······························································································· ·**
'**··········· I2C Routines for the Basic Stamp····································· **
'**··········· using the CMPS01 Compass Module································ ·**
'***********************************************************


SDA··· CON· 8····· ' I2C data
SCL····· CON· 9····· ' I2C clock
SDAin··· VAR· IN8
SDAout· VAR· OUT8
SDAdir· VAR· DIR8

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


Main:
· I2cAddr = $c0····· ' CMPS01 Compass module address
· I2cReg = 1······· ' Bearing as 0-255 (BRAD)
· GOSUB· I2cByteRead
· DEBUG· 2,0,0, "Compass Bearing (0-255 BRAD)·· ", DEC3 I2cData
· I2cReg = 2······· ' Bearing as 0-359.9 degrees
· GOSUB· I2cWordRead
· DEBUG· 2,0,1, "Compass Bearing (0-359 Degrees ", DEC3 I2cData/10

· GOTO· main

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, [noparse][[/noparse]I2cBuf]
· INPUT· SDA
· HIGH· SCL········· ' clock in the ack' bit
· LOW· SCL
· RETURN


I2cInByte:
· SHIFTIN SDA, SCL, MSBPRE, [noparse][[/noparse]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


·

Comments

  • FranklinFranklin Posts: 4,747
    edited 2008-07-16 03:26
    First you will probably need a seven segment driver chip and then code for that chip to translate what comes out of the compass to what goes into the chip. You need to get the hardware first then comes the code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • GogsGogs Posts: 19
    edited 2008-07-16 03:28
    It is ok I have already figured it out and have all the hardware already. Now I have my compas module outputing on the 7 segment LED and is very accurate.

    Cheers.
Sign In or Register to comment.