SCP1000-D01 MEMS pressure sensor library

Committer:
MichaelW
Date:
Tue Sep 28 08:28:48 2010 +0000
Revision:
4:d509ab38e2b5
Parent:
3:29c98bdee20b
Code improvements

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MichaelW 1:a45810688644 1 /**
MichaelW 1:a45810688644 2 * @section LICENSE
MichaelW 1:a45810688644 3 *Copyright (c) 2010 ARM Ltd.
MichaelW 1:a45810688644 4 *
MichaelW 1:a45810688644 5 *Permission is hereby granted, free of charge, to any person obtaining a copy
MichaelW 1:a45810688644 6 *of this software and associated documentation files (the "Software"), to deal
MichaelW 1:a45810688644 7 *in the Software without restriction, including without limitation the rights
MichaelW 1:a45810688644 8 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
MichaelW 1:a45810688644 9 *copies of the Software, and to permit persons to whom the Software is
MichaelW 1:a45810688644 10 *furnished to do so, subject to the following conditions:
MichaelW 0:dfe5ae6ea375 11 *
MichaelW 1:a45810688644 12 *The above copyright notice and this permission notice shall be included in
MichaelW 1:a45810688644 13 *all copies or substantial portions of the Software.
MichaelW 1:a45810688644 14 *
MichaelW 1:a45810688644 15 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MichaelW 1:a45810688644 16 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MichaelW 1:a45810688644 17 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
MichaelW 1:a45810688644 18 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MichaelW 1:a45810688644 19 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
MichaelW 1:a45810688644 20 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
MichaelW 1:a45810688644 21 *THE SOFTWARE.
MichaelW 1:a45810688644 22 *
MichaelW 1:a45810688644 23 *
MichaelW 1:a45810688644 24 * @section DESCRIPTION
MichaelW 3:29c98bdee20b 25 * Library for using the SCP1000-D01 MEMS Pressure sensor, this is the SPI version. This library only supports high resolution mode.
MichaelW 3:29c98bdee20b 26 * Communication with the sensor is via a 4 wire interface
MichaelW 0:dfe5ae6ea375 27 *
MichaelW 0:dfe5ae6ea375 28 */
MichaelW 0:dfe5ae6ea375 29
MichaelW 0:dfe5ae6ea375 30 #include "scp1000.h"
MichaelW 0:dfe5ae6ea375 31
MichaelW 0:dfe5ae6ea375 32 SCP1000::SCP1000(PinName mosi, PinName miso, PinName sck, PinName CSB):
MichaelW 0:dfe5ae6ea375 33 _spi(mosi, miso, sck),
MichaelW 0:dfe5ae6ea375 34 _CSB(CSB)
MichaelW 3:29c98bdee20b 35 {
MichaelW 0:dfe5ae6ea375 36 _CSB = 1;
MichaelW 0:dfe5ae6ea375 37 wait(0.1);
MichaelW 0:dfe5ae6ea375 38 _CSB = 0;
MichaelW 0:dfe5ae6ea375 39 //Force reset
MichaelW 0:dfe5ae6ea375 40 _spi.write(((0x06<< 2) | 0x02));
MichaelW 0:dfe5ae6ea375 41 _spi.write(0x01);
MichaelW 0:dfe5ae6ea375 42 _CSB = 1;
MichaelW 0:dfe5ae6ea375 43 wait(0.06);
MichaelW 0:dfe5ae6ea375 44 _CSB = 0;
MichaelW 0:dfe5ae6ea375 45 //Check starup Procedure has finished
MichaelW 0:dfe5ae6ea375 46 int status;
MichaelW 0:dfe5ae6ea375 47 do{
MichaelW 0:dfe5ae6ea375 48 _spi.write(0x07 << 2);
MichaelW 0:dfe5ae6ea375 49 status = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 50 //printf("waiting for startup to finish %i\n", status);
MichaelW 0:dfe5ae6ea375 51 wait(0.1);
MichaelW 0:dfe5ae6ea375 52 }while((status & 0x01)); //Wait for LSB to go low
MichaelW 4:d509ab38e2b5 53 //Test for error in intialisation
MichaelW 0:dfe5ae6ea375 54 _spi.write(0x1F << 2);
MichaelW 0:dfe5ae6ea375 55 status = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 56 if(!(status & 0x01)){
MichaelW 4:d509ab38e2b5 57 //printf("Error in Intialisation");
MichaelW 4:d509ab38e2b5 58 return;
MichaelW 0:dfe5ae6ea375 59 }
MichaelW 0:dfe5ae6ea375 60
MichaelW 0:dfe5ae6ea375 61
MichaelW 0:dfe5ae6ea375 62 //Set mode as 0x00
MichaelW 0:dfe5ae6ea375 63 _spi.write((0x03 << 2) | 0x02);
MichaelW 0:dfe5ae6ea375 64 _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 65 wait(0.05);
MichaelW 0:dfe5ae6ea375 66
MichaelW 0:dfe5ae6ea375 67 //Check DRDDY is low, if not read data
MichaelW 0:dfe5ae6ea375 68 _spi.write(0x07 << 2);
MichaelW 0:dfe5ae6ea375 69 status = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 70 if(status & 0x20){
MichaelW 4:d509ab38e2b5 71 //printf("Data to be read");
MichaelW 4:d509ab38e2b5 72 _spi.write(0x1F <<2);
MichaelW 4:d509ab38e2b5 73 _spi.write(0x00); //read and discard data
MichaelW 4:d509ab38e2b5 74 _spi.write(0x20 <<2);
MichaelW 4:d509ab38e2b5 75 _spi.write(0x00); //read and discard data
MichaelW 4:d509ab38e2b5 76 _spi.write(0x00); //read and discard data
MichaelW 0:dfe5ae6ea375 77 }
MichaelW 0:dfe5ae6ea375 78
MichaelW 0:dfe5ae6ea375 79 //Check OPStatus bit
MichaelW 0:dfe5ae6ea375 80 _spi.write(0x04 << 2);
MichaelW 0:dfe5ae6ea375 81 status = _spi.write(0x0);
MichaelW 0:dfe5ae6ea375 82 if(status & 0x01){
MichaelW 4:d509ab38e2b5 83 //printf("Not finished");
MichaelW 0:dfe5ae6ea375 84 }
MichaelW 0:dfe5ae6ea375 85
MichaelW 0:dfe5ae6ea375 86 //Activate new mode
MichaelW 0:dfe5ae6ea375 87 _spi.write((0x03 << 2) | 0x02);
MichaelW 0:dfe5ae6ea375 88 _spi.write(0x0A);
MichaelW 0:dfe5ae6ea375 89 }
MichaelW 0:dfe5ae6ea375 90 float SCP1000::read(){
MichaelW 0:dfe5ae6ea375 91 _CSB = 0;
MichaelW 0:dfe5ae6ea375 92
MichaelW 2:076ba33b1895 93 if(_waitReady() == 1){
MichaelW 0:dfe5ae6ea375 94 _spi.write(0x1F <<2);
MichaelW 0:dfe5ae6ea375 95 int PressureHighest = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 96 _spi.write(0x20 <<2);
MichaelW 0:dfe5ae6ea375 97 int PressureHigh = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 98 int Pressurelow = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 99
MichaelW 4:d509ab38e2b5 100 int pressureValue = Pressurelow | PressureHigh << 8 | (PressureHighest & 0x07) << 16;
MichaelW 0:dfe5ae6ea375 101 float pressure = ((float)pressureValue) / 4;
MichaelW 4:d509ab38e2b5 102
MichaelW 0:dfe5ae6ea375 103 _CSB = 1;
MichaelW 4:d509ab38e2b5 104
MichaelW 0:dfe5ae6ea375 105 return(pressure);
MichaelW 0:dfe5ae6ea375 106 }else{
MichaelW 0:dfe5ae6ea375 107 return(0);
MichaelW 0:dfe5ae6ea375 108 }
MichaelW 0:dfe5ae6ea375 109 }
MichaelW 0:dfe5ae6ea375 110 float SCP1000::readTemperature(){
MichaelW 0:dfe5ae6ea375 111
MichaelW 0:dfe5ae6ea375 112 _CSB = 0;
MichaelW 0:dfe5ae6ea375 113
MichaelW 2:076ba33b1895 114 if(_waitReady() == 1){
MichaelW 0:dfe5ae6ea375 115 //ready so now read
MichaelW 0:dfe5ae6ea375 116 _spi.write(0x21 << 2);
MichaelW 0:dfe5ae6ea375 117 int TempHigh = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 118 int TempLow = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 119
MichaelW 0:dfe5ae6ea375 120 signed int temperatureValue = (TempLow | ((TempHigh & 0x1F) << 8));
MichaelW 0:dfe5ae6ea375 121 if(TempHigh & 0x20){
MichaelW 0:dfe5ae6ea375 122 //negative
MichaelW 0:dfe5ae6ea375 123 temperatureValue = -8192 + temperatureValue;
MichaelW 0:dfe5ae6ea375 124 }else{
MichaelW 4:d509ab38e2b5 125 //positive
MichaelW 0:dfe5ae6ea375 126 }
MichaelW 4:d509ab38e2b5 127
MichaelW 0:dfe5ae6ea375 128 float temperature = ((float)temperatureValue) * 0.05;
MichaelW 0:dfe5ae6ea375 129 _CSB = 1;
MichaelW 0:dfe5ae6ea375 130 return(temperature);
MichaelW 0:dfe5ae6ea375 131 }else{
MichaelW 0:dfe5ae6ea375 132 return(0);
MichaelW 0:dfe5ae6ea375 133 }
MichaelW 0:dfe5ae6ea375 134 }
MichaelW 0:dfe5ae6ea375 135
MichaelW 2:076ba33b1895 136 int SCP1000::_waitReady(){
MichaelW 4:d509ab38e2b5 137 //Depending on mode wait for it to be ready - only supports high resolution mode - wait for bit 5 to be set in 00
MichaelW 0:dfe5ae6ea375 138 int status;
MichaelW 0:dfe5ae6ea375 139 _CSB = 0;
MichaelW 0:dfe5ae6ea375 140 do{
MichaelW 0:dfe5ae6ea375 141 _spi.write(0x07 << 2);
MichaelW 0:dfe5ae6ea375 142 status = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 143 //printf("waiting %i\n", status);
MichaelW 0:dfe5ae6ea375 144 wait(0.2);
MichaelW 0:dfe5ae6ea375 145 if(status & 0x10){
MichaelW 0:dfe5ae6ea375 146 //bit 4 high - real time error, interrupt has not been read in time - read DataRD16
MichaelW 0:dfe5ae6ea375 147 _spi.write(0x20 << 2);
MichaelW 3:29c98bdee20b 148 int data = _spi.write(0x00);
MichaelW 3:29c98bdee20b 149 data = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 150 }
MichaelW 0:dfe5ae6ea375 151 }while(!(status & 0x20));
MichaelW 2:076ba33b1895 152 return(1);
MichaelW 0:dfe5ae6ea375 153 }