Sensirion CRC


Closed Thread
Results 1 to 4 of 4

Thread: Sensirion CRC

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default

    This is the code I use for calculating the CRC from the Dallas 18B20 temperature sensor. The only difference I can notice is the locations of the << and >> in the two code. Can you give me an example of how your code is used cause I'm still a little fuzzy on what data does where. I know the CRC value byte is the third byte but not sure what bytes are used to calculate the CRC to compare to this value.

    Code:
    ------------------------ Calculate the CRC from Byte 9 ------------------------
    
    GetCRC:
        for x = 0 to 7
        databyte = dq[x]
        gosub CalcCRC
        next x
        if dq[8] <> crccalc then
            sensorerror = 1
            else
            sensorerror = 0
        endif
        crccalc = 0                
        return
    
    '--------------------- CRC Bit Calcuation Method -------------------------------
    
    CalcCRC:
        for i = 0 to 7
        databit = crccalc.0 ^ databyte.0
        databyte = databyte >> 1
        if databit = 0 then Shift
        crccalc = crccalc ^ $18
    
    shift:
        crccalc = crccalc >> 1
        crccalc.7 = databit
        next i
        return
    Code:
    '------------------ Read SHT71 Temperature / Humidity Sensor -------------------
    
    ReadSHT71:
        gosub Initialize
        Sensorcmd = ReadTemp
        gosub readsensor
        if result => 4000 then
            tempC = result - 4000
            SignC = 160
            else
            tempC = 4000 - result
            SignC = "-"
        endif
        gosub ConvertToKelvin    
        gosub ConvertToFahrenheit
        gosub Initialize
        Sensorcmd = ReadHumidity
        gosub readsensor
    
    '--------------------- Calculate Relative Humidity -----------------------------
    
        w = result * 405
        w = div32 100
        x = result * result
        x = div32 1000
        x = x * 28
        x = div32 100
        RHLinear = w - x - 400
    
    '---------- Calculate Relative Humidity with Temperature Compensation ----------
    
        w = (8 * result + 1000) / 10
        if SignC = 160 then
            if tempC > 2500 then
                x = (tempC - 2500) * w
                x = div32 100
                RHTempComp = RHLinear + x / 100
                else
                x = (2500 - temp) * w
                x = div32 100
                RHTempComp = RHLinear - x / 100
            endif
            else
            x = (2500 + tempC) * w
            x = div32 10000
            RHTempComp = RHLinear - x
        endif
        sleep 1
        return
    
    '---------------------------- Initialize the Sensor ----------------------------
    
    Initialize:
        high DataPin
        low clk
        for i = 1 to 10
            high clk
            pause 1
            low clk
            pause 1
        next i
        call TransferStart
        return
    
    '---------------------------- Get Data from Sensor -----------------------------
    
    ReadSensor:
        gosub TransferStart
        gosub WaitSensor
        shiftout DataPin,clk,1,[Sensorcmd\8]        ' Send command byte
        input DataPin                               ' Wait for acknowledge
        low clk
        while DataPin = 1
        wend
        pulsout clk,10                              ' Send acknowledge
        while DataPin = 0
        wend
        while DataPin = 1                           ' Wait for conversion to complete
        wend
        low clk
        shiftin DataPin,clk,0,[result.byte1\8]      ' Get the first byte, 8 bits
        low DataPin
        pulsout clk,10                              ' Send acknowledge
        shiftin DataPin,clk,0,[result.byte0\8]      ' Get the second byte, 8 bits
        low DataPin
        pulsout clk,10                              ' Send acknowledge
        shiftin DataPin,clk,0,[crc\8]               ' Get third byte, 8 bits, CRC
        high DataPin
        pulsout clk,10                              ' Send acknowledge
        input DataPin                               ' End of Transmission
        input clk
        return
    
    '---------------------------- Start Transfer -----------------------------------
    
    TransferStart:
        high clk
        pause 1
        low DataPin
        pause 1
        low clk
        pause 1
        high clk
        pause 1
        high DataPin
        pause 1
        low clk
        return
    
    '---------------------------- Wait for Sensor ----------------------------------
    
    WaitSensor:
        result = 4096
    
    Loop:
        result = result - 1
        if DataPin && result.bit11 then Loop
        return
        
    '-------------------------- Convert to Kelvin ----------------------------------
    
    ConvertToKelvin:
    	if signC = "-" then
        	tempk = 27315 - temp
            else
        	tempk = temp + 27315
      	endif
      	return
      	
    ConvertToFahrenheit:
        if SignC = 160 then
            SignF = 160
            dummy = TempC * 18
            TempF = div32 10
            TempF = TempF + 3200
            else
            TempF = (tempc + 5000) * 900
            TempF = DIV32 500
            if TempC => 1778 and SignC = "-" then
                TempF = TempF - 12200
                SignF = "-"
                else				
                TempF = 12200 - TempF
                SignF = 160
        	endif    
        endif
    	return

  2. #2
    Join Date
    Jan 2004
    Location
    Grand Lake O' Cherokees USA
    Posts
    15


    Did you find this post helpful? Yes | No

    Default

    There isn't much difference between the Sensirion and 1wire CRC calculations. The Sensirion tests Bit 7 of the data and does a left shift for the next test bit whereas the 1wire version tests Bit 0 and does a right shift. They both use the same polynomial.

    If you haven't downloaded Sensirion's CRC application note, you need to do that. In there it states the CRC covers the whole transmission (command and response bytes) without the acknowledge bits.

    The CRC is initialized to the low 4 bits of the STATUS register. If you have not changed the STATUS register it defaults to zero meaning you should initialize SHTCRC to zero. If you have changed the status register then you need to read the application note carefully to see how to initialize SHTCRC (it has to be reversed).

    Then send your command byte and the two received bytes to the CRC routine via variable SHTRxCRC. After receiving the CRC byte from the device it must be reversed. In PBP you can do this: Reversed_CRC = Received_CRC REV 8. Now compare Reversed_CRC and SHTCRC. They should be equal if the transmission was OK.

    That's it....
    Tom

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Thank you very much for all your help Tom. Without your assistance I would still be out in the dark. Getting more familiar with this CRC checking thing as I go. This is my final code to do the CRC check. I simply compare CRC to CRCCalc and look for the match.

    Code:
    '--------- Calculate the CRC from Command Byte and Received Bytes --------------
    
    GetSHTCRC:
        CRCCalc = 0                
        DataByte = SensorCmd
        gosub CalcSHTCRC
        DataByte = SensorData.byte1
        gosub CalcSHTCRC
        databyte = SensorData.Byte0
        gosub CalcSHTCRC
        CRC = CRC REV 8
        return
    
    '--------------------- CRC Bit Calcuation Method -------------------------------
    
    CalcSHTCRC:
        for i = 0 to 7
        DataBit = CRCCcalc.7 ^ DataByte.7
        DataByte = DataByte << 1
        if DataBit = 0 then ShiftSHT
        CRCCcalc = CRCCcalc ^ $18
    
    ShiftSHT:
        CRCCcalc = CRCCcalc << 1
        CRCCcalc.0 = DataBit
        next i
        return

Similar Threads

  1. Dallas CRC8 Routines
    By Tom Estes in forum Code Examples
    Replies: 23
    Last Post: - 8th May 2018, 18:07
  2. Calculating CRC for Modbus RTU
    By tekart in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 20th January 2010, 22:42
  3. CRC Calculations
    By timmers in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th June 2009, 17:10
  4. Problems with CRC8 Calc in 1Wire
    By JohnB in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 16th March 2007, 22:01
  5. crc16 help
    By beto in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 4th January 2006, 21:21

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts