SCP1000 Library

Dependents:   SCP1000Example 0sample_SCP1000_USB SCP1000_Fastsensing

Committer:
yamaguch
Date:
Wed Feb 02 08:00:23 2011 +0000
Revision:
0:23fab9b42ad4
Child:
10:11a40ecf2361
0.9

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 0:23fab9b42ad4 1 #include "SCP1000.h"
yamaguch 0:23fab9b42ad4 2
yamaguch 0:23fab9b42ad4 3 SCP1000::SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs, OperationMode mode)
yamaguch 0:23fab9b42ad4 4 : spi(mosi, miso, sclk), selectPin(cs) {
yamaguch 0:23fab9b42ad4 5 selectPin = 1;
yamaguch 0:23fab9b42ad4 6 spi.frequency(1000000);
yamaguch 0:23fab9b42ad4 7 spi.format(8, 0); // frame bits = 8, mode = 0
yamaguch 0:23fab9b42ad4 8 reset();
yamaguch 0:23fab9b42ad4 9 setOperationMode(mode);
yamaguch 0:23fab9b42ad4 10 }
yamaguch 0:23fab9b42ad4 11
yamaguch 0:23fab9b42ad4 12 int SCP1000::revision() {
yamaguch 0:23fab9b42ad4 13 return readRegister(REVID);
yamaguch 0:23fab9b42ad4 14 }
yamaguch 0:23fab9b42ad4 15
yamaguch 0:23fab9b42ad4 16 bool SCP1000::isReady() {
yamaguch 0:23fab9b42ad4 17 const char DRDY = 0x20; // Data Ready
yamaguch 0:23fab9b42ad4 18
yamaguch 0:23fab9b42ad4 19 return readRegister(STATUS) & DRDY;
yamaguch 0:23fab9b42ad4 20 }
yamaguch 0:23fab9b42ad4 21
yamaguch 0:23fab9b42ad4 22 void SCP1000::setOperationMode(OperationMode mode) {
yamaguch 0:23fab9b42ad4 23 writeRegister(OPERATION, CANCEL);
yamaguch 0:23fab9b42ad4 24 while (readRegister(OPSTATUS)) // wait until current operation is canncelled
yamaguch 0:23fab9b42ad4 25 wait(0.01);
yamaguch 0:23fab9b42ad4 26 if (mode != CANCEL)
yamaguch 0:23fab9b42ad4 27 writeRegister(OPERATION, mode);
yamaguch 0:23fab9b42ad4 28 }
yamaguch 0:23fab9b42ad4 29
yamaguch 0:23fab9b42ad4 30 void SCP1000::reset() {
yamaguch 0:23fab9b42ad4 31 writeRegister(RSTR, 0x01);
yamaguch 0:23fab9b42ad4 32 wait(0.15);
yamaguch 0:23fab9b42ad4 33 }
yamaguch 0:23fab9b42ad4 34
yamaguch 0:23fab9b42ad4 35 bool SCP1000::performSelfTest() {
yamaguch 0:23fab9b42ad4 36 setOperationMode(SELF_TEST);
yamaguch 0:23fab9b42ad4 37
yamaguch 0:23fab9b42ad4 38 return readRegister(DATARD8) & 1;
yamaguch 0:23fab9b42ad4 39 }
yamaguch 0:23fab9b42ad4 40
yamaguch 0:23fab9b42ad4 41 float SCP1000::readPressure(bool waitForReady) {
yamaguch 0:23fab9b42ad4 42 if (waitForReady)
yamaguch 0:23fab9b42ad4 43 while (!isReady())
yamaguch 0:23fab9b42ad4 44 wait(0.01);
yamaguch 0:23fab9b42ad4 45
yamaguch 0:23fab9b42ad4 46 // Pressure value is stored in 19-bit unsigned int (unit: 0.25 Pa)
yamaguch 0:23fab9b42ad4 47 long value = readRegister(DATARD8) & 0x07;
yamaguch 0:23fab9b42ad4 48 value <<= 16;
yamaguch 0:23fab9b42ad4 49 value |= readRegister(DATARD16, true) & 0xFFFF;
yamaguch 0:23fab9b42ad4 50
yamaguch 0:23fab9b42ad4 51 return value / 400.0;
yamaguch 0:23fab9b42ad4 52 }
yamaguch 0:23fab9b42ad4 53
yamaguch 0:23fab9b42ad4 54 float SCP1000::readTemperature(bool waitForDataReady) {
yamaguch 0:23fab9b42ad4 55 if (waitForDataReady)
yamaguch 0:23fab9b42ad4 56 while (!isReady())
yamaguch 0:23fab9b42ad4 57 wait(0.01);
yamaguch 0:23fab9b42ad4 58
yamaguch 0:23fab9b42ad4 59 return ((short)(readRegister(TEMPOUT, true) << 3) >> 3) / 20.0;
yamaguch 0:23fab9b42ad4 60 }
yamaguch 0:23fab9b42ad4 61
yamaguch 0:23fab9b42ad4 62 int SCP1000::readRegister(RegisterName registerName, bool twoBytes) {
yamaguch 0:23fab9b42ad4 63 selectPin = 0;
yamaguch 0:23fab9b42ad4 64 spi.write((registerName << 2) | READ);
yamaguch 0:23fab9b42ad4 65 int result = spi.write(0x00);
yamaguch 0:23fab9b42ad4 66 if (twoBytes) {
yamaguch 0:23fab9b42ad4 67 result <<= 8;
yamaguch 0:23fab9b42ad4 68 result |= spi.write(0x00);
yamaguch 0:23fab9b42ad4 69 }
yamaguch 0:23fab9b42ad4 70 selectPin = 1;
yamaguch 0:23fab9b42ad4 71
yamaguch 0:23fab9b42ad4 72 return result;
yamaguch 0:23fab9b42ad4 73 }
yamaguch 0:23fab9b42ad4 74
yamaguch 0:23fab9b42ad4 75 void SCP1000::writeRegister(RegisterName registerName, char value) {
yamaguch 0:23fab9b42ad4 76 selectPin = 0;
yamaguch 0:23fab9b42ad4 77 spi.write((registerName << 2) | WRITE);
yamaguch 0:23fab9b42ad4 78 spi.write(value);
yamaguch 0:23fab9b42ad4 79 selectPin = 1;
yamaguch 0:23fab9b42ad4 80 }