SCP1000 Library

Dependents:   SCP1000Example 0sample_SCP1000_USB SCP1000_Fastsensing

Committer:
yamaguch
Date:
Fri Oct 28 09:23:18 2011 +0000
Revision:
10:11a40ecf2361
Parent:
0:23fab9b42ad4
Child:
13:faf2decbd1d4
0.92

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 10:11a40ecf2361 1 /*
yamaguch 10:11a40ecf2361 2 Copyright (c) 2011, Senio Networks, Inc.
yamaguch 10:11a40ecf2361 3
yamaguch 10:11a40ecf2361 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yamaguch 10:11a40ecf2361 5 of this software and associated documentation files (the "Software"), to deal
yamaguch 10:11a40ecf2361 6 in the Software without restriction, including without limitation the rights
yamaguch 10:11a40ecf2361 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yamaguch 10:11a40ecf2361 8 copies of the Software, and to permit persons to whom the Software is
yamaguch 10:11a40ecf2361 9 furnished to do so, subject to the following conditions:
yamaguch 10:11a40ecf2361 10
yamaguch 10:11a40ecf2361 11 The above copyright notice and this permission notice shall be included in
yamaguch 10:11a40ecf2361 12 all copies or substantial portions of the Software.
yamaguch 10:11a40ecf2361 13
yamaguch 10:11a40ecf2361 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yamaguch 10:11a40ecf2361 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yamaguch 10:11a40ecf2361 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yamaguch 10:11a40ecf2361 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yamaguch 10:11a40ecf2361 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yamaguch 10:11a40ecf2361 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yamaguch 10:11a40ecf2361 20 THE SOFTWARE.
yamaguch 10:11a40ecf2361 21 */
yamaguch 10:11a40ecf2361 22
yamaguch 0:23fab9b42ad4 23 #include "SCP1000.h"
yamaguch 0:23fab9b42ad4 24
yamaguch 0:23fab9b42ad4 25 SCP1000::SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs, OperationMode mode)
yamaguch 0:23fab9b42ad4 26 : spi(mosi, miso, sclk), selectPin(cs) {
yamaguch 0:23fab9b42ad4 27 selectPin = 1;
yamaguch 0:23fab9b42ad4 28 spi.frequency(1000000);
yamaguch 0:23fab9b42ad4 29 spi.format(8, 0); // frame bits = 8, mode = 0
yamaguch 0:23fab9b42ad4 30 reset();
yamaguch 0:23fab9b42ad4 31 setOperationMode(mode);
yamaguch 0:23fab9b42ad4 32 }
yamaguch 0:23fab9b42ad4 33
yamaguch 0:23fab9b42ad4 34 int SCP1000::revision() {
yamaguch 0:23fab9b42ad4 35 return readRegister(REVID);
yamaguch 0:23fab9b42ad4 36 }
yamaguch 0:23fab9b42ad4 37
yamaguch 0:23fab9b42ad4 38 bool SCP1000::isReady() {
yamaguch 0:23fab9b42ad4 39 const char DRDY = 0x20; // Data Ready
yamaguch 0:23fab9b42ad4 40
yamaguch 0:23fab9b42ad4 41 return readRegister(STATUS) & DRDY;
yamaguch 0:23fab9b42ad4 42 }
yamaguch 0:23fab9b42ad4 43
yamaguch 0:23fab9b42ad4 44 void SCP1000::setOperationMode(OperationMode mode) {
yamaguch 0:23fab9b42ad4 45 writeRegister(OPERATION, CANCEL);
yamaguch 0:23fab9b42ad4 46 while (readRegister(OPSTATUS)) // wait until current operation is canncelled
yamaguch 0:23fab9b42ad4 47 wait(0.01);
yamaguch 0:23fab9b42ad4 48 if (mode != CANCEL)
yamaguch 0:23fab9b42ad4 49 writeRegister(OPERATION, mode);
yamaguch 0:23fab9b42ad4 50 }
yamaguch 0:23fab9b42ad4 51
yamaguch 0:23fab9b42ad4 52 void SCP1000::reset() {
yamaguch 0:23fab9b42ad4 53 writeRegister(RSTR, 0x01);
yamaguch 0:23fab9b42ad4 54 wait(0.15);
yamaguch 0:23fab9b42ad4 55 }
yamaguch 0:23fab9b42ad4 56
yamaguch 0:23fab9b42ad4 57 bool SCP1000::performSelfTest() {
yamaguch 0:23fab9b42ad4 58 setOperationMode(SELF_TEST);
yamaguch 0:23fab9b42ad4 59
yamaguch 0:23fab9b42ad4 60 return readRegister(DATARD8) & 1;
yamaguch 0:23fab9b42ad4 61 }
yamaguch 0:23fab9b42ad4 62
yamaguch 0:23fab9b42ad4 63 float SCP1000::readPressure(bool waitForReady) {
yamaguch 0:23fab9b42ad4 64 if (waitForReady)
yamaguch 0:23fab9b42ad4 65 while (!isReady())
yamaguch 0:23fab9b42ad4 66 wait(0.01);
yamaguch 0:23fab9b42ad4 67
yamaguch 0:23fab9b42ad4 68 // Pressure value is stored in 19-bit unsigned int (unit: 0.25 Pa)
yamaguch 0:23fab9b42ad4 69 long value = readRegister(DATARD8) & 0x07;
yamaguch 0:23fab9b42ad4 70 value <<= 16;
yamaguch 0:23fab9b42ad4 71 value |= readRegister(DATARD16, true) & 0xFFFF;
yamaguch 0:23fab9b42ad4 72
yamaguch 0:23fab9b42ad4 73 return value / 400.0;
yamaguch 0:23fab9b42ad4 74 }
yamaguch 0:23fab9b42ad4 75
yamaguch 0:23fab9b42ad4 76 float SCP1000::readTemperature(bool waitForDataReady) {
yamaguch 0:23fab9b42ad4 77 if (waitForDataReady)
yamaguch 0:23fab9b42ad4 78 while (!isReady())
yamaguch 0:23fab9b42ad4 79 wait(0.01);
yamaguch 0:23fab9b42ad4 80
yamaguch 0:23fab9b42ad4 81 return ((short)(readRegister(TEMPOUT, true) << 3) >> 3) / 20.0;
yamaguch 0:23fab9b42ad4 82 }
yamaguch 0:23fab9b42ad4 83
yamaguch 0:23fab9b42ad4 84 int SCP1000::readRegister(RegisterName registerName, bool twoBytes) {
yamaguch 0:23fab9b42ad4 85 selectPin = 0;
yamaguch 0:23fab9b42ad4 86 spi.write((registerName << 2) | READ);
yamaguch 0:23fab9b42ad4 87 int result = spi.write(0x00);
yamaguch 0:23fab9b42ad4 88 if (twoBytes) {
yamaguch 0:23fab9b42ad4 89 result <<= 8;
yamaguch 0:23fab9b42ad4 90 result |= spi.write(0x00);
yamaguch 0:23fab9b42ad4 91 }
yamaguch 0:23fab9b42ad4 92 selectPin = 1;
yamaguch 0:23fab9b42ad4 93
yamaguch 0:23fab9b42ad4 94 return result;
yamaguch 0:23fab9b42ad4 95 }
yamaguch 0:23fab9b42ad4 96
yamaguch 0:23fab9b42ad4 97 void SCP1000::writeRegister(RegisterName registerName, char value) {
yamaguch 0:23fab9b42ad4 98 selectPin = 0;
yamaguch 0:23fab9b42ad4 99 spi.write((registerName << 2) | WRITE);
yamaguch 0:23fab9b42ad4 100 spi.write(value);
yamaguch 0:23fab9b42ad4 101 selectPin = 1;
yamaguch 0:23fab9b42ad4 102 }