Hiroshi Yamaguchi / SCP1000

Dependents:   SCP1000Example 0sample_SCP1000_USB SCP1000_Fastsensing

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SCP1000.cpp Source File

SCP1000.cpp

00001 /*
00002 Copyright (c) 2011, Senio Networks, Inc.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #include "SCP1000.h"
00024 
00025 SCP1000::SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs, OperationMode mode)
00026         : spi(mosi, miso, sclk), selectPin(cs) {
00027     selectPin = 1;
00028     spi.frequency(500000);
00029     spi.format(8, 0);   // frame bits = 8, mode = 0
00030     reset();
00031     setOperationMode(mode);
00032 }
00033 
00034 int SCP1000::revision() {
00035     return readRegister(REVID);
00036 }
00037 
00038 bool SCP1000::isReady() {
00039     const char DRDY = 0x20; // Data Ready
00040     
00041     return readRegister(STATUS) & DRDY;
00042 }
00043 
00044 void SCP1000::setOperationMode(OperationMode mode) {
00045     writeRegister(OPERATION, CANCEL);
00046     while (readRegister(OPSTATUS)) // wait until current operation is canncelled
00047         wait(0.01);
00048     if (mode != CANCEL)
00049         writeRegister(OPERATION, mode);
00050 }
00051 
00052 void SCP1000::reset() {
00053     writeRegister(RSTR, 0x01);
00054     wait(0.15);
00055 }
00056 
00057 bool SCP1000::performSelfTest() {
00058     setOperationMode(SELF_TEST);
00059     wait(1); // wait enough time for the test to completes (> 0.25secs)
00060     return readRegister(DATARD8) & 1;
00061 }
00062 
00063 float SCP1000::readPressure(bool waitForReady) {
00064     if (waitForReady)
00065         while (!isReady())
00066             wait(0.01);
00067 
00068     // Pressure value is stored in 19-bit unsigned int (unit: 0.25 Pa)
00069     long value = readRegister(DATARD8) & 0x07;
00070     value <<= 16;
00071     value |= readRegister(DATARD16, true) & 0xFFFF;
00072 
00073     return value / 400.0;
00074 }
00075 
00076 float SCP1000::readTemperature(bool waitForDataReady) {
00077     if (waitForDataReady)
00078         while (!isReady())
00079             wait(0.01);
00080 
00081     return ((short)(readRegister(TEMPOUT, true) << 3) >> 3) / 20.0;
00082 }
00083 
00084 int SCP1000::readRegister(RegisterName registerName, bool twoBytes) {
00085     selectPin = 0;
00086     spi.write((registerName << 2) | READ);
00087     int result = spi.write(0x00);
00088     if (twoBytes) {
00089         result <<= 8;
00090         result |= spi.write(0x00);
00091     }
00092     selectPin = 1;
00093 
00094     return result;
00095 }
00096 
00097 void SCP1000::writeRegister(RegisterName registerName, char value) {
00098     selectPin = 0;
00099     spi.write((registerName << 2) | WRITE);
00100     spi.write(value);
00101     selectPin = 1;
00102 }