SCP1000-D01 MEMS pressure sensor library

Committer:
MichaelW
Date:
Mon Sep 27 08:29:02 2010 +0000
Revision:
0:dfe5ae6ea375
Child:
1:a45810688644
Initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MichaelW 0:dfe5ae6ea375 1 /**
MichaelW 0:dfe5ae6ea375 2 *
MichaelW 0:dfe5ae6ea375 3 *
MichaelW 0:dfe5ae6ea375 4 */
MichaelW 0:dfe5ae6ea375 5
MichaelW 0:dfe5ae6ea375 6 #include "scp1000.h"
MichaelW 0:dfe5ae6ea375 7
MichaelW 0:dfe5ae6ea375 8 SCP1000::SCP1000(PinName mosi, PinName miso, PinName sck, PinName CSB):
MichaelW 0:dfe5ae6ea375 9 _spi(mosi, miso, sck),
MichaelW 0:dfe5ae6ea375 10 _CSB(CSB)
MichaelW 0:dfe5ae6ea375 11 /* _DRDY(NULL),
MichaelW 0:dfe5ae6ea375 12 _TRIG(Null),
MichaelW 0:dfe5ae6ea375 13 _PD(NULL)*/{
MichaelW 0:dfe5ae6ea375 14 _CSB = 1;
MichaelW 0:dfe5ae6ea375 15 wait(0.1);
MichaelW 0:dfe5ae6ea375 16 _CSB = 0;
MichaelW 0:dfe5ae6ea375 17 //Force reset
MichaelW 0:dfe5ae6ea375 18 _spi.write(((0x06<< 2) | 0x02));
MichaelW 0:dfe5ae6ea375 19 _spi.write(0x01);
MichaelW 0:dfe5ae6ea375 20 _CSB = 1;
MichaelW 0:dfe5ae6ea375 21 wait(0.06);
MichaelW 0:dfe5ae6ea375 22 _CSB = 0;
MichaelW 0:dfe5ae6ea375 23 //Check starup Procedure has finished
MichaelW 0:dfe5ae6ea375 24 int status;
MichaelW 0:dfe5ae6ea375 25 do{
MichaelW 0:dfe5ae6ea375 26 _spi.write(0x07 << 2);
MichaelW 0:dfe5ae6ea375 27 status = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 28 //printf("waiting for startup to finish %i\n", status);
MichaelW 0:dfe5ae6ea375 29 wait(0.1);
MichaelW 0:dfe5ae6ea375 30 }while((status & 0x01)); //Wait for LSB to go low
MichaelW 0:dfe5ae6ea375 31
MichaelW 0:dfe5ae6ea375 32 _spi.write(0x1F << 2);
MichaelW 0:dfe5ae6ea375 33 status = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 34 if(!(status & 0x01)){
MichaelW 0:dfe5ae6ea375 35 printf("Error in Intialisation");
MichaelW 0:dfe5ae6ea375 36 }
MichaelW 0:dfe5ae6ea375 37
MichaelW 0:dfe5ae6ea375 38
MichaelW 0:dfe5ae6ea375 39 //Set mode as 0x00
MichaelW 0:dfe5ae6ea375 40 _spi.write((0x03 << 2) | 0x02);
MichaelW 0:dfe5ae6ea375 41 _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 42 wait(0.05);
MichaelW 0:dfe5ae6ea375 43
MichaelW 0:dfe5ae6ea375 44 //Check DRDDY is low, if not read data
MichaelW 0:dfe5ae6ea375 45 _spi.write(0x07 << 2);
MichaelW 0:dfe5ae6ea375 46 status = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 47 if(status & 0x20){
MichaelW 0:dfe5ae6ea375 48 printf("Data to be read");
MichaelW 0:dfe5ae6ea375 49 }
MichaelW 0:dfe5ae6ea375 50
MichaelW 0:dfe5ae6ea375 51 //Check OPStatus bit
MichaelW 0:dfe5ae6ea375 52 _spi.write(0x04 << 2);
MichaelW 0:dfe5ae6ea375 53 status = _spi.write(0x0);
MichaelW 0:dfe5ae6ea375 54 if(status & 0x01){
MichaelW 0:dfe5ae6ea375 55 printf("Not finished");
MichaelW 0:dfe5ae6ea375 56 }
MichaelW 0:dfe5ae6ea375 57
MichaelW 0:dfe5ae6ea375 58 //Activate new mode
MichaelW 0:dfe5ae6ea375 59 _spi.write((0x03 << 2) | 0x02);
MichaelW 0:dfe5ae6ea375 60 _spi.write(0x0A);
MichaelW 0:dfe5ae6ea375 61
MichaelW 0:dfe5ae6ea375 62
MichaelW 0:dfe5ae6ea375 63
MichaelW 0:dfe5ae6ea375 64 }
MichaelW 0:dfe5ae6ea375 65
MichaelW 0:dfe5ae6ea375 66
MichaelW 0:dfe5ae6ea375 67 void SCP1000::SetMode(SCPMode mode){
MichaelW 0:dfe5ae6ea375 68 _CSB = 0;
MichaelW 0:dfe5ae6ea375 69
MichaelW 0:dfe5ae6ea375 70
MichaelW 0:dfe5ae6ea375 71
MichaelW 0:dfe5ae6ea375 72 _CSB = 1;
MichaelW 0:dfe5ae6ea375 73
MichaelW 0:dfe5ae6ea375 74 }
MichaelW 0:dfe5ae6ea375 75
MichaelW 0:dfe5ae6ea375 76 float SCP1000::read(){
MichaelW 0:dfe5ae6ea375 77 _CSB = 0;
MichaelW 0:dfe5ae6ea375 78
MichaelW 0:dfe5ae6ea375 79 if(_isReady() == 1){
MichaelW 0:dfe5ae6ea375 80 _spi.write(0x1F <<2);
MichaelW 0:dfe5ae6ea375 81 int PressureHighest = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 82 _spi.write(0x20 <<2);
MichaelW 0:dfe5ae6ea375 83 int PressureHigh = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 84 int Pressurelow = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 85
MichaelW 0:dfe5ae6ea375 86
MichaelW 0:dfe5ae6ea375 87 int pressureValue = Pressurelow | PressureHigh << 8 | (PressureHighest & 0x07) << 16;
MichaelW 0:dfe5ae6ea375 88
MichaelW 0:dfe5ae6ea375 89 float pressure = ((float)pressureValue) / 4;
MichaelW 0:dfe5ae6ea375 90
MichaelW 0:dfe5ae6ea375 91 _CSB = 1;
MichaelW 0:dfe5ae6ea375 92
MichaelW 0:dfe5ae6ea375 93
MichaelW 0:dfe5ae6ea375 94 return(pressure);
MichaelW 0:dfe5ae6ea375 95 }else{
MichaelW 0:dfe5ae6ea375 96 return(0);
MichaelW 0:dfe5ae6ea375 97 }
MichaelW 0:dfe5ae6ea375 98 }
MichaelW 0:dfe5ae6ea375 99 float SCP1000::readTemperature(){
MichaelW 0:dfe5ae6ea375 100
MichaelW 0:dfe5ae6ea375 101 _CSB = 0;
MichaelW 0:dfe5ae6ea375 102
MichaelW 0:dfe5ae6ea375 103 if(_isReady() == 1){
MichaelW 0:dfe5ae6ea375 104 //ready so now read
MichaelW 0:dfe5ae6ea375 105 _spi.write(0x21 << 2);
MichaelW 0:dfe5ae6ea375 106 int TempHigh = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 107 int TempLow = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 108
MichaelW 0:dfe5ae6ea375 109 signed int temperatureValue = (TempLow | ((TempHigh & 0x1F) << 8));
MichaelW 0:dfe5ae6ea375 110 if(TempHigh & 0x20){
MichaelW 0:dfe5ae6ea375 111 //negative
MichaelW 0:dfe5ae6ea375 112 temperatureValue = -8192 + temperatureValue;
MichaelW 0:dfe5ae6ea375 113 }else{
MichaelW 0:dfe5ae6ea375 114 //positive
MichaelW 0:dfe5ae6ea375 115
MichaelW 0:dfe5ae6ea375 116 }
MichaelW 0:dfe5ae6ea375 117
MichaelW 0:dfe5ae6ea375 118 float temperature = ((float)temperatureValue) * 0.05;
MichaelW 0:dfe5ae6ea375 119 _CSB = 1;
MichaelW 0:dfe5ae6ea375 120 return(temperature);
MichaelW 0:dfe5ae6ea375 121 }else{
MichaelW 0:dfe5ae6ea375 122 return(0);
MichaelW 0:dfe5ae6ea375 123 }
MichaelW 0:dfe5ae6ea375 124 }
MichaelW 0:dfe5ae6ea375 125
MichaelW 0:dfe5ae6ea375 126 int SCP1000::_isReady(){
MichaelW 0:dfe5ae6ea375 127 //Depending on mode wait for it to be ready
MichaelW 0:dfe5ae6ea375 128 int status;
MichaelW 0:dfe5ae6ea375 129 _CSB = 0;
MichaelW 0:dfe5ae6ea375 130 do{
MichaelW 0:dfe5ae6ea375 131 _spi.write(0x07 << 2);
MichaelW 0:dfe5ae6ea375 132 status = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 133 //printf("waiting %i\n", status);
MichaelW 0:dfe5ae6ea375 134 wait(0.2);
MichaelW 0:dfe5ae6ea375 135 if(status & 0x10){
MichaelW 0:dfe5ae6ea375 136 //bit 4 high - real time error, interrupt has not been read in time - read DataRD16
MichaelW 0:dfe5ae6ea375 137 int temp;
MichaelW 0:dfe5ae6ea375 138 _spi.write(0x20 << 2);
MichaelW 0:dfe5ae6ea375 139 temp = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 140 temp = _spi.write(0x00);
MichaelW 0:dfe5ae6ea375 141 }
MichaelW 0:dfe5ae6ea375 142 }while(!(status & 0x20));
MichaelW 0:dfe5ae6ea375 143 return(1);
MichaelW 0:dfe5ae6ea375 144 }