Michael Walker / Mbed 2 deprecated RPC_RemoteSensing

Dependencies:   EthernetNetIf mbed TMP102 HTTPServer ADJD-S371_ColourSens

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers scp1000.cpp Source File

scp1000.cpp

00001 /**
00002 * @section LICENSE
00003 *Copyright (c) 2010 ARM Ltd.
00004 *
00005 *Permission is hereby granted, free of charge, to any person obtaining a copy
00006 *of this software and associated documentation files (the "Software"), to deal
00007 *in the Software without restriction, including without limitation the rights
00008 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 *copies of the Software, and to permit persons to whom the Software is
00010 *furnished to do so, subject to the following conditions:
00011 * 
00012 *The above copyright notice and this permission notice shall be included in
00013 *all copies or substantial portions of the Software.
00014 * 
00015 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 *THE SOFTWARE.
00022 * 
00023 *
00024 * @section DESCRIPTION
00025 * Library for using the SCP1000-D01 MEMS Pressure sensor, this is the SPI version. This library only supports high resolution mode.
00026 * Communication with the sensor is via a 4 wire interface
00027 *
00028 */
00029 
00030 #include "scp1000.h"
00031 
00032 SCP1000::SCP1000(PinName mosi, PinName miso, PinName sck, PinName CSB):
00033      _spi(mosi, miso, sck),
00034     _CSB(CSB)
00035     {
00036     _CSB = 1;
00037     wait(0.1);
00038     _CSB = 0;
00039     //Force reset
00040     _spi.write(((0x06<< 2) | 0x02));
00041     _spi.write(0x01);
00042     _CSB = 1;
00043     wait(0.06);
00044     _CSB = 0;
00045     //Check starup Procedure has finished
00046     int status;
00047      do{
00048         _spi.write(0x07 << 2);
00049         status = _spi.write(0x00);
00050         //printf("waiting for startup to finish %i\n", status);
00051         wait(0.1);
00052     }while((status & 0x01));                                  //Wait for LSB to go low
00053     //Test for error in intialisation
00054     _spi.write(0x1F << 2);
00055     status = _spi.write(0x00);
00056     if(!(status & 0x01)){
00057         //printf("Error in Intialisation");
00058         return;
00059     }
00060     
00061     
00062     //Set mode as 0x00
00063     _spi.write((0x03 << 2) | 0x02);
00064     _spi.write(0x00);
00065     wait(0.05);
00066     
00067     //Check DRDDY is low, if not read data
00068     _spi.write(0x07 << 2);
00069     status = _spi.write(0x00);
00070     if(status & 0x20){
00071        //printf("Data to be read");
00072        _spi.write(0x1F <<2);
00073        _spi.write(0x00);        //read and discard data
00074        _spi.write(0x20 <<2);
00075        _spi.write(0x00);       //read and discard data
00076        _spi.write(0x00);      //read and discard data
00077     }
00078     
00079     //Check OPStatus bit
00080     _spi.write(0x04 << 2);
00081     status = _spi.write(0x0);
00082     if(status & 0x01){
00083         //printf("Not finished");
00084     }   
00085     
00086     //Activate new mode
00087     _spi.write((0x03 << 2) | 0x02);
00088     _spi.write(0x0A);
00089 }
00090 float SCP1000::read(){
00091     _CSB = 0;
00092 
00093     if(_waitReady() == 1){ 
00094         _spi.write(0x1F <<2);
00095         int PressureHighest = _spi.write(0x00);
00096         _spi.write(0x20 <<2);
00097         int PressureHigh = _spi.write(0x00);
00098         int Pressurelow = _spi.write(0x00);
00099         
00100         int pressureValue = Pressurelow | PressureHigh << 8 | (PressureHighest & 0x07) << 16;       
00101         float pressure = ((float)pressureValue) / 4;
00102  
00103         _CSB = 1;
00104 
00105         return(pressure);
00106     }else{
00107         return(0);
00108     }
00109 }
00110 float SCP1000::readTemperature(){
00111     
00112     _CSB = 0;
00113     
00114     if(_waitReady() == 1){ 
00115         //ready so now read
00116          _spi.write(0x21 << 2);
00117         int TempHigh = _spi.write(0x00);
00118         int TempLow = _spi.write(0x00);
00119         
00120         signed int temperatureValue = (TempLow | ((TempHigh & 0x1F) << 8));
00121         if(TempHigh & 0x20){
00122             //negative
00123             temperatureValue = -8192 + temperatureValue;
00124         }else{
00125             //positive         
00126         }
00127 
00128         float temperature = ((float)temperatureValue) * 0.05;
00129         _CSB = 1;
00130         return(temperature);
00131     }else{
00132         return(0);
00133     }
00134 }
00135     
00136  int SCP1000::_waitReady(){
00137     //Depending on mode wait for it to be ready - only supports high resolution mode - wait for bit 5 to be set in 00
00138     int status;
00139     _CSB = 0;
00140     do{
00141         _spi.write(0x07 << 2);
00142         status = _spi.write(0x00);
00143         //printf("waiting %i\n", status);
00144         wait(0.2);
00145         if(status & 0x10){
00146             //bit 4 high - real time error, interrupt has not been read in time - read DataRD16
00147             _spi.write(0x20 << 2);
00148             int data = _spi.write(0x00);
00149             data = _spi.write(0x00);
00150         }
00151     }while(!(status & 0x20));
00152     return(1);
00153  }