MPL3115 altitude

fgnash

New Member
Read altitude in meters and temperature in Celcius using a Freescale MPL3115
mounted on a Sparkfun breakout board.

Code:
#picaxe 20M2
#no_data
; The MPL3115 is on a six pin breakout board from Sparkfun
; pin 1 - +3.0V
; pin 2 - Serial In
; pin 3 - C.7, nc
; pin 4 - C.6, nc
; pin 5 - C.5, nc
; pin 6 - C.4, nc
; pin 7 - C.3, nc
; pin 8 - C.2, Int2 data  MPL3115  1k0 pullup needed
; pin 9 - C.1, Int1 n/a   MPL3115
; pin10 - C.0, nc
; pin11 - B.7, HI2C SDL - MPL3115  1k0 pullup on breakout board   
; pin12 - B.6, LCD12
; pin13 - B.5, HI2C SDA - MPL3115  1k0 pullup on breakout board 
; pin14 - B.4, LCD11
; pin15 - B.3, LCD RS
; pin16 - B.2, LCD E
; pin17 - B.1, LCD13
; pin18 - B.0, LCD14
; pin19 - Serial Out
; pin20 - 0V
;
  symbol RS = B.2                 
  symbol E = B.3
  symbol LCD11 = pinB.4
  symbol LCD12 = pinB.6
  symbol LCD13 = pinB.1
  symbol LCD14 = pinB.0
;
;
;  INITIALIZATION
;
      setfreq m8                   ;arbitrary
;
      output RS
      output E
      output B.0
      output B.1
      output B.4
      output B.6
;
      gosub InitLCD               

      gosub InitMPL3115

      setint %00000000,%00000010   ;interrupt low on pulled up pin C.2
;
;
;  MAIN
;
Main:
      pause 1000
      goto Main
;
;
;  MPL3115 sample complete
;
interrupt:
 
      gosub ReadAltitude           ; read 3 bytes of altitude, 2 bytes of temperature

      gosub UpdateLCD              ; display altitude and temperature on LCD
                                   ; raw data without correction 

      pause 10000                  ; don't-flash-the-LCD delay

      setint %00000000,%00000010   ; reset interrupt

      return
;
;
; read the altitude and temperature from the MPL3115
;
ReadAltitude:
; I2C command values and sequence copied from Freescale MPL3115 datasheet's flowchart 
      hi2cin $12,(b0)               ;read INT_SOURCE
      if bit7 = 0 then goto RAexit  ;exit if SRC_DRDY is not set

; read OUT_P and OUT_T, which also clears the DRDY interrupt
      hi2cin $01,(b7,b6,b5,b9,b8)
; --OR--
;      hi2cin $01,(b7)  ;bits 12-19 of 20-bit real time altitude sample
;      hi2cin $02,(b6)  ;bits 4-11 of 20-bit real time altitude sample  
;      hi2cin $03,(b5)  ;bits 0-3 of 20-bit real time altitude sample
;      hi2cin $04,(b9)  ;bits 4-11 of real time temperature sample
;      hi2cin $05,(b8)  ;bits 0-3 of real time temperature sample
RAexit:        
       return
;
;
InitMPL3115:
      hi2csetup I2CMASTER,%11000000,i2cslow_8,i2cbyte
; I2C command values and sequence copied from Freescale MPL3115 datasheet's flowchart
      hi2cout $26,($B8)      ;set to Altimeter with an OSR = 128
      hi2cout $13,($07)      ;enable data flags in PT_DATA_CFG register
      hi2cout $28,($11)      ;set INT to Active Low Open Drain
      hi2cout $29,($80)      ;enable DRDY Interrupt
      hi2cout $26,($B9)      ;set Active
      return
;
;
UpdateLCD:
      b0 = $01               ;clear LCD display
      gosub SendCmdByte
; round up the 4 LSB of both values
      if b5 > $70 then 
        inc w3
      endif
      if b8 > $70 then
        inc b9
      endif
; reformat and display 5 digit altitude in meters, 2 digit temp in C
      bintoascii w3,b10,b11,b12,b13,b14
      bintoascii b9,b15,b16,b17
      b0 = b10
      gosub SendDataByte
      b0 = b11
      gosub SendDataByte
      b0 = b12
      gosub SendDataByte
      b0 = b13
      gosub SendDataByte
      b0 = b14
      gosub SendDataByte
      b0 = " "
      gosub SendDataByte
      b0 = b16
      gosub SendDataByte
      b0 = b17
      gosub SendDataByte
      return
;
;      LCD OUTPUT
; random pin version after Hippy's 18X LCD tutorial
; b0 contains the 8 bit command or data
;
SendInitialCmdByte:
      pause 40                ; LCD datasheet
SendCmdByte:
      low RS                  ; send a command this time
SendDataByte:
      LCD11 = bit4            ; high order nib
      LCD12 = bit5
      LCD13 = bit6
      LCD14 = bit7
      pulsout E,24
      LCD11 = bit0            ; low order nib
      LCD12 = bit1
      LCD13 = bit2
      LCD14 = bit3
      pulsout E,24
      high RS                 ; default to data byte as next char
      return
;
;
InitLCD:
      b0 = $33               ; 8bit to set 4 bit
      gosub SendInitialCmdByte
      b0 = $32               ; 4 bit from now on
      gosub SendCmdByte
      b0 = $20               ; 1 line
      gosub SendCmdByte
      b0 = $0C               ; turn on display with simple cursor
      gosub SendCmdByte
      b0 = $01               ; clear the screen
      gosub SendCmdByte
      return
;
      end
 
Last edited by a moderator:

john2051

New Member
Hi, out of curiosity I got a couple of these boards, and was wondering how best to go about testing them.
Thn i came across your code, problem solved!
I really appreciate you sharing your code, thanks and regards john
 
Top