Shop OBEX P1 Docs P2 Docs Learn Events
Altimeter Module MS5607 (29124) C driver using SPI — Parallax Forums

Altimeter Module MS5607 (29124) C driver using SPI

twm47099twm47099 Posts: 867
edited 2016-02-06 06:39 in Propeller 1
The parallax MS5607 pressure/temperature module documentation has an I2C demo in Spin. The C code provided is in an application note from the manufacturer and includes both SPI and I2C but is written for the Atmel Atmega 644P. I wanted to write a C program using the SPI interface for the Propeller. (also part of my learning effort.)

I first tried using the Simpletools library SPI functions shift_in and shift_out, but had problems reading the correct data from the 5607. After trying a number of things (e.g. limiting data transfers to 8 bit at a time as well as the full 16 and 24 bits put out by the 5607) I decided to try using the libspiasm library I had written here forums.parallax.com/discussion/comment/1315065/#Comment_1315065

Since there wasn't a set of library functions written for the 5607, I decided to use the generic libspiasm functions. Since I didn't have a generic example in the above post I felt that it would be worthwhile.

I used the MEAS application note as a base and made changes for the Propeller, for spiasm, and for my personal style (e.g. using bitwise operators to build commands rather than arithmetic operators)

I'm using the 5607 on an ActivityBoard, but any Prop board should work. the pins I, used are:

MS5607 Pin   Function               Prop Pin  
   CS          CS          <-  Prop P4  low starts transfer, high terminates
   SDO        MISO         ->  Prop P5  Prop in (MPRE)
   SDA        MOSI         <-  Prop P6  Prop out (MFIRST)
   SCL        SCLK         <-  Prop P7  rise then fall  State = 0 
   PS         Protocol     <-  Gnd selects SPI


The libspiasm SPI commands used are:
#include "simpletools.h"
#include "spiasm.h"

// ****** GLOBAL DECLARATION ****** 

#define cstate_alt 0         // for SPI 0 = start clock low, 1 = start clock high
#define clockd_ns_alt 500   // Actual delay in nanoseconds (1000ns = 1 usec), not less than 300ns

spia_t *spiaalt;      // the structure that holds the variables for the PASM

//  *** Start SPI Interface ***  *** In main()
  spiaalt = spi_start(clockd_ns_alt, cstate_alt);

// *** Send command to 5607 ****** ** in functions
  spi_out(spiaalt, MOSI_Pin, SCLK_Pin, MFIRST, 8, command); 

// *** Read data in Propeller   ****** ** in functions
  value = spi_in(spiaalt, MISO_Pin, SCLK_Pin, MPRE, number_of_bits); 


The instructions for using the library with SimpleIDE are in the post above. I found that it was necessary to open the Project View in SimpleIDE and select Add Simple Library, point to the libspiasm folder in:
\SimpleIDE\Learn\Simple Libraries\mylibrary

Then I cut and pasted the #include "spiasm.h" under #include "simpletools.h"

The c program is attached below.

By the way is there a way to make the font size in code blocks smaller?

Tom




Sign In or Register to comment.