SCP1000 Library

Dependents:   SCP1000Example 0sample_SCP1000_USB SCP1000_Fastsensing

Revision:
0:23fab9b42ad4
Child:
10:11a40ecf2361
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SCP1000.cpp	Wed Feb 02 08:00:23 2011 +0000
@@ -0,0 +1,80 @@
+#include "SCP1000.h"
+
+SCP1000::SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs, OperationMode mode)
+        : spi(mosi, miso, sclk), selectPin(cs) {
+    selectPin = 1;
+    spi.frequency(1000000);
+    spi.format(8, 0);   // frame bits = 8, mode = 0
+    reset();
+    setOperationMode(mode);
+}
+
+int SCP1000::revision() {
+    return readRegister(REVID);
+}
+
+bool SCP1000::isReady() {
+    const char DRDY = 0x20; // Data Ready
+    
+    return readRegister(STATUS) & DRDY;
+}
+
+void SCP1000::setOperationMode(OperationMode mode) {
+    writeRegister(OPERATION, CANCEL);
+    while (readRegister(OPSTATUS)) // wait until current operation is canncelled
+        wait(0.01);
+    if (mode != CANCEL)
+        writeRegister(OPERATION, mode);
+}
+
+void SCP1000::reset() {
+    writeRegister(RSTR, 0x01);
+    wait(0.15);
+}
+
+bool SCP1000::performSelfTest() {
+    setOperationMode(SELF_TEST);
+
+    return readRegister(DATARD8) & 1;
+}
+
+float SCP1000::readPressure(bool waitForReady) {
+    if (waitForReady)
+        while (!isReady())
+            wait(0.01);
+
+    // Pressure value is stored in 19-bit unsigned int (unit: 0.25 Pa)
+    long value = readRegister(DATARD8) & 0x07;
+    value <<= 16;
+    value |= readRegister(DATARD16, true) & 0xFFFF;
+
+    return value / 400.0;
+}
+
+float SCP1000::readTemperature(bool waitForDataReady) {
+    if (waitForDataReady)
+        while (!isReady())
+            wait(0.01);
+
+    return ((short)(readRegister(TEMPOUT, true) << 3) >> 3) / 20.0;
+}
+
+int SCP1000::readRegister(RegisterName registerName, bool twoBytes) {
+    selectPin = 0;
+    spi.write((registerName << 2) | READ);
+    int result = spi.write(0x00);
+    if (twoBytes) {
+        result <<= 8;
+        result |= spi.write(0x00);
+    }
+    selectPin = 1;
+
+    return result;
+}
+
+void SCP1000::writeRegister(RegisterName registerName, char value) {
+    selectPin = 0;
+    spi.write((registerName << 2) | WRITE);
+    spi.write(value);
+    selectPin = 1;
+}
\ No newline at end of file