SPI + MS5540C driver



  • Hello everyone,

    I am trying to write the driver for the MS5540C Miniature Barometer Module using SPI interface, I based my code in an existing arduino sketch. Nevertheless, I haven't managed to read from the sensor, which worked on the Arduino Uno without problem. I think the problem is in the phase and polarity value that have to be change during the reading sequence, in the Arduino SPI they just change SPI.setDataMode(SPI_MODE1) to SPI.setDataMode(SPI_MODE0) . I am not familiar with the SPI protocol, so I post my code looking for any insight in this matter.

    MS5540C.py:

    import ustruct
    import time
    from machine import SPI, PWM
    
    # SPI default pins for SCLK (CLK) , DIN(MOSI) and DOUT(MISO) (``P10``, ``P11`` and ``P14``)
    
    # MCLK signal with a frequency of 32.768KHz on P12
    pwm = PWM( 0, frequency=32768 )
    # 0 create pwm channel on pin P12 with a duty cycle of 50%
    pwm_c = pwm.channel ( 0, pin='P12', duty_cycle=0.5 )
    
    _RESET_SEQUENCE = bytearray ( [0x15, 0x55, 0x40] )
    _ADDRESS_WORD1 = bytearray ( [0x1D, 0x50] )
    _ADDRESS_WORD2 = bytearray ( [0x1D, 0x60] )
    _ADDRESS_WORD3 = bytearray ( [0x1D, 0x90] )
    _ADDRESS_WORD4 = bytearray ( [0x1D, 0xA0] )
    
    class MS5540C:
    
        def __init__( self ):
    
            #Calibration Word 1
            self.w1 = self.__read__calibration__word__( _ADDRESS_WORD1 )
            #Calibration Word 2
            self.w2 = self.__read__calibration__word__( _ADDRESS_WORD2 )
            #Calibration Word 3
            self.w3 = self.__read__calibration__word__( _ADDRESS_WORD3 )
            #Calibration Word 4
            self.w4 = self.__read__calibration__word__( _ADDRESS_WORD4 )
    
        def reset_sensor( self ):
            self.spi = SPI( 0, mode=SPI.MASTER, baudrate=500000, polarity=0, phase=0ze, firstbit=SPI.MSB )
            e = self.spi.write( _RESET_SEQUENCE ) # sent the byteds for _reset_sensor_
            if e != 3:
                print('Error en Reset \n')
    
        def __read__calibration__word__ ( self, adress ):
            self.reset_sensor() # phase = 0
            # Calibration word request
            e = self.spi.write( adress ) #  Get Calibration word
            if e != 2:
                print('Error en request Word \n')
            # Reading Calibration
            self.spi = SPI( 0, mode=SPI.MASTER, baudrate=500000, polarity=0, phase=1, firstbit=SPI.MSB )
            rbuf= self.spi.read(2)
            return  (rbuf[0] << 8) | rbuf[1]
    

    main.py:

    import time                   # Allows use of time.sleep() for delays
    import pycom                  # Base library for Pycom devices
    import ubinascii              # Needed to run any MicroPython code
    import machine                # Interfaces with hardware components
    import utime                  # Unix timestamp
    import micropython            # Needed to run any MicroPython code
    from machine import  Pin, Timer
    from MS5540C import MS5540C
    
    
    
    # Setup
    # Level sensor
    level_sensor = MS5540C()
    
    # FUNCTIONS
    
    # MAIN
    def main():
    
        while True:
            print( "Word 1 is {}".format( level_sensor.w1 ) )
            print( "Word 2 is {}".format( level_sensor.w2 ) )
            print( "Word 3 is {}".format( level_sensor.w3 ) )
            print( "Word 4 is {}".format( level_sensor.w4 ) )
    main()
    
    


  • hello there,

    Did you manage to solve this issue? :)
    Thanks


Log in to reply
 

Pycom on Twitter