Remote Sensing demo. This is the code that runs on mbed to interface with the Remote Sensing Applet Demo

Dependencies:   EthernetNetIf mbed TMP102 HTTPServer ADJD-S371_ColourSens

Committer:
MichaelW
Date:
Fri Feb 04 11:10:22 2011 +0000
Revision:
2:1cc34c25b99d
Updated RPCInterface Library

Who changed what in which revision?

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