SCP1000-D01 MEMS pressure sensor library
scp1000.cpp@1:a45810688644, 2010-09-27 (annotated)
- Committer:
- MichaelW
- Date:
- Mon Sep 27 08:30:39 2010 +0000
- Revision:
- 1:a45810688644
- Parent:
- 0:dfe5ae6ea375
- Child:
- 2:076ba33b1895
License info added
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:a45810688644 | 25 | * |
MichaelW | 0:dfe5ae6ea375 | 26 | * |
MichaelW | 0:dfe5ae6ea375 | 27 | */ |
MichaelW | 0:dfe5ae6ea375 | 28 | |
MichaelW | 0:dfe5ae6ea375 | 29 | #include "scp1000.h" |
MichaelW | 0:dfe5ae6ea375 | 30 | |
MichaelW | 0:dfe5ae6ea375 | 31 | SCP1000::SCP1000(PinName mosi, PinName miso, PinName sck, PinName CSB): |
MichaelW | 0:dfe5ae6ea375 | 32 | _spi(mosi, miso, sck), |
MichaelW | 0:dfe5ae6ea375 | 33 | _CSB(CSB) |
MichaelW | 0:dfe5ae6ea375 | 34 | /* _DRDY(NULL), |
MichaelW | 0:dfe5ae6ea375 | 35 | _TRIG(Null), |
MichaelW | 0:dfe5ae6ea375 | 36 | _PD(NULL)*/{ |
MichaelW | 0:dfe5ae6ea375 | 37 | _CSB = 1; |
MichaelW | 0:dfe5ae6ea375 | 38 | wait(0.1); |
MichaelW | 0:dfe5ae6ea375 | 39 | _CSB = 0; |
MichaelW | 0:dfe5ae6ea375 | 40 | //Force reset |
MichaelW | 0:dfe5ae6ea375 | 41 | _spi.write(((0x06<< 2) | 0x02)); |
MichaelW | 0:dfe5ae6ea375 | 42 | _spi.write(0x01); |
MichaelW | 0:dfe5ae6ea375 | 43 | _CSB = 1; |
MichaelW | 0:dfe5ae6ea375 | 44 | wait(0.06); |
MichaelW | 0:dfe5ae6ea375 | 45 | _CSB = 0; |
MichaelW | 0:dfe5ae6ea375 | 46 | //Check starup Procedure has finished |
MichaelW | 0:dfe5ae6ea375 | 47 | int status; |
MichaelW | 0:dfe5ae6ea375 | 48 | do{ |
MichaelW | 0:dfe5ae6ea375 | 49 | _spi.write(0x07 << 2); |
MichaelW | 0:dfe5ae6ea375 | 50 | status = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 51 | //printf("waiting for startup to finish %i\n", status); |
MichaelW | 0:dfe5ae6ea375 | 52 | wait(0.1); |
MichaelW | 0:dfe5ae6ea375 | 53 | }while((status & 0x01)); //Wait for LSB to go low |
MichaelW | 0:dfe5ae6ea375 | 54 | |
MichaelW | 0:dfe5ae6ea375 | 55 | _spi.write(0x1F << 2); |
MichaelW | 0:dfe5ae6ea375 | 56 | status = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 57 | if(!(status & 0x01)){ |
MichaelW | 0:dfe5ae6ea375 | 58 | printf("Error in Intialisation"); |
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 | 0:dfe5ae6ea375 | 71 | printf("Data to be read"); |
MichaelW | 0:dfe5ae6ea375 | 72 | } |
MichaelW | 0:dfe5ae6ea375 | 73 | |
MichaelW | 0:dfe5ae6ea375 | 74 | //Check OPStatus bit |
MichaelW | 0:dfe5ae6ea375 | 75 | _spi.write(0x04 << 2); |
MichaelW | 0:dfe5ae6ea375 | 76 | status = _spi.write(0x0); |
MichaelW | 0:dfe5ae6ea375 | 77 | if(status & 0x01){ |
MichaelW | 0:dfe5ae6ea375 | 78 | printf("Not finished"); |
MichaelW | 0:dfe5ae6ea375 | 79 | } |
MichaelW | 0:dfe5ae6ea375 | 80 | |
MichaelW | 0:dfe5ae6ea375 | 81 | //Activate new mode |
MichaelW | 0:dfe5ae6ea375 | 82 | _spi.write((0x03 << 2) | 0x02); |
MichaelW | 0:dfe5ae6ea375 | 83 | _spi.write(0x0A); |
MichaelW | 0:dfe5ae6ea375 | 84 | |
MichaelW | 0:dfe5ae6ea375 | 85 | |
MichaelW | 0:dfe5ae6ea375 | 86 | |
MichaelW | 0:dfe5ae6ea375 | 87 | } |
MichaelW | 0:dfe5ae6ea375 | 88 | |
MichaelW | 0:dfe5ae6ea375 | 89 | |
MichaelW | 0:dfe5ae6ea375 | 90 | void SCP1000::SetMode(SCPMode mode){ |
MichaelW | 0:dfe5ae6ea375 | 91 | _CSB = 0; |
MichaelW | 0:dfe5ae6ea375 | 92 | |
MichaelW | 0:dfe5ae6ea375 | 93 | |
MichaelW | 0:dfe5ae6ea375 | 94 | |
MichaelW | 0:dfe5ae6ea375 | 95 | _CSB = 1; |
MichaelW | 0:dfe5ae6ea375 | 96 | |
MichaelW | 0:dfe5ae6ea375 | 97 | } |
MichaelW | 0:dfe5ae6ea375 | 98 | |
MichaelW | 0:dfe5ae6ea375 | 99 | float SCP1000::read(){ |
MichaelW | 0:dfe5ae6ea375 | 100 | _CSB = 0; |
MichaelW | 0:dfe5ae6ea375 | 101 | |
MichaelW | 0:dfe5ae6ea375 | 102 | if(_isReady() == 1){ |
MichaelW | 0:dfe5ae6ea375 | 103 | _spi.write(0x1F <<2); |
MichaelW | 0:dfe5ae6ea375 | 104 | int PressureHighest = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 105 | _spi.write(0x20 <<2); |
MichaelW | 0:dfe5ae6ea375 | 106 | int PressureHigh = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 107 | int Pressurelow = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 108 | |
MichaelW | 0:dfe5ae6ea375 | 109 | |
MichaelW | 0:dfe5ae6ea375 | 110 | int pressureValue = Pressurelow | PressureHigh << 8 | (PressureHighest & 0x07) << 16; |
MichaelW | 0:dfe5ae6ea375 | 111 | |
MichaelW | 0:dfe5ae6ea375 | 112 | float pressure = ((float)pressureValue) / 4; |
MichaelW | 0:dfe5ae6ea375 | 113 | |
MichaelW | 0:dfe5ae6ea375 | 114 | _CSB = 1; |
MichaelW | 0:dfe5ae6ea375 | 115 | |
MichaelW | 0:dfe5ae6ea375 | 116 | |
MichaelW | 0:dfe5ae6ea375 | 117 | return(pressure); |
MichaelW | 0:dfe5ae6ea375 | 118 | }else{ |
MichaelW | 0:dfe5ae6ea375 | 119 | return(0); |
MichaelW | 0:dfe5ae6ea375 | 120 | } |
MichaelW | 0:dfe5ae6ea375 | 121 | } |
MichaelW | 0:dfe5ae6ea375 | 122 | float SCP1000::readTemperature(){ |
MichaelW | 0:dfe5ae6ea375 | 123 | |
MichaelW | 0:dfe5ae6ea375 | 124 | _CSB = 0; |
MichaelW | 0:dfe5ae6ea375 | 125 | |
MichaelW | 0:dfe5ae6ea375 | 126 | if(_isReady() == 1){ |
MichaelW | 0:dfe5ae6ea375 | 127 | //ready so now read |
MichaelW | 0:dfe5ae6ea375 | 128 | _spi.write(0x21 << 2); |
MichaelW | 0:dfe5ae6ea375 | 129 | int TempHigh = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 130 | int TempLow = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 131 | |
MichaelW | 0:dfe5ae6ea375 | 132 | signed int temperatureValue = (TempLow | ((TempHigh & 0x1F) << 8)); |
MichaelW | 0:dfe5ae6ea375 | 133 | if(TempHigh & 0x20){ |
MichaelW | 0:dfe5ae6ea375 | 134 | //negative |
MichaelW | 0:dfe5ae6ea375 | 135 | temperatureValue = -8192 + temperatureValue; |
MichaelW | 0:dfe5ae6ea375 | 136 | }else{ |
MichaelW | 0:dfe5ae6ea375 | 137 | //positive |
MichaelW | 0:dfe5ae6ea375 | 138 | |
MichaelW | 0:dfe5ae6ea375 | 139 | } |
MichaelW | 0:dfe5ae6ea375 | 140 | |
MichaelW | 0:dfe5ae6ea375 | 141 | float temperature = ((float)temperatureValue) * 0.05; |
MichaelW | 0:dfe5ae6ea375 | 142 | _CSB = 1; |
MichaelW | 0:dfe5ae6ea375 | 143 | return(temperature); |
MichaelW | 0:dfe5ae6ea375 | 144 | }else{ |
MichaelW | 0:dfe5ae6ea375 | 145 | return(0); |
MichaelW | 0:dfe5ae6ea375 | 146 | } |
MichaelW | 0:dfe5ae6ea375 | 147 | } |
MichaelW | 0:dfe5ae6ea375 | 148 | |
MichaelW | 0:dfe5ae6ea375 | 149 | int SCP1000::_isReady(){ |
MichaelW | 0:dfe5ae6ea375 | 150 | //Depending on mode wait for it to be ready |
MichaelW | 0:dfe5ae6ea375 | 151 | int status; |
MichaelW | 0:dfe5ae6ea375 | 152 | _CSB = 0; |
MichaelW | 0:dfe5ae6ea375 | 153 | do{ |
MichaelW | 0:dfe5ae6ea375 | 154 | _spi.write(0x07 << 2); |
MichaelW | 0:dfe5ae6ea375 | 155 | status = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 156 | //printf("waiting %i\n", status); |
MichaelW | 0:dfe5ae6ea375 | 157 | wait(0.2); |
MichaelW | 0:dfe5ae6ea375 | 158 | if(status & 0x10){ |
MichaelW | 0:dfe5ae6ea375 | 159 | //bit 4 high - real time error, interrupt has not been read in time - read DataRD16 |
MichaelW | 0:dfe5ae6ea375 | 160 | int temp; |
MichaelW | 0:dfe5ae6ea375 | 161 | _spi.write(0x20 << 2); |
MichaelW | 0:dfe5ae6ea375 | 162 | temp = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 163 | temp = _spi.write(0x00); |
MichaelW | 0:dfe5ae6ea375 | 164 | } |
MichaelW | 0:dfe5ae6ea375 | 165 | }while(!(status & 0x20)); |
MichaelW | 0:dfe5ae6ea375 | 166 | return(1); |
MichaelW | 0:dfe5ae6ea375 | 167 | } |