Dependents:   V2_GPSRTC

Files at this revision

API Documentation at this revision

Comitter:
joosttromp
Date:
Fri Jun 17 12:37:27 2011 +0000
Commit message:

Changed in this revision

SCP1000.cpp Show annotated file Show diff for this revision Revisions of this file
SCP1000.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 55ea30b5de1e SCP1000.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SCP1000.cpp	Fri Jun 17 12:37:27 2011 +0000
@@ -0,0 +1,85 @@
+/**
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation; either version 2 of the License, or 
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
+ * for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along 
+ * with this program; if not, write to the Free Software Foundation, Inc., 
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * @section DESCRIPTION
+ * Library to interface with the SCP1000 pressure and temperature sensor.
+ */
+
+#include "SCP1000.h"
+
+SCP1000::SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs)
+    : m_spi(mosi, miso, sclk)
+    , m_cs(cs) {
+    m_cs=1;
+    m_spi.frequency(500000); // the fastest of the sensor
+    m_spi.format(8, 0); // duda son dos palabras de 8 bits? 
+    wait(0.5);
+    //------------------------------------------------
+    // pc.printf("RESET\r\n");
+    write_register(0x06,0x01);
+    wait(0.5);
+
+    // pc.printf("Initialize High Resolution Constant Reading Mode\r\n");
+    write_register(0x03,0x0A);
+    wait(0.5);
+}
+
+unsigned long SCP1000::readPressure() {
+    unsigned long pressure_msb = read_register(PRESSURE);
+    pressure_msb &= 0x07;
+    unsigned long pressure_lsb = read_register16(PRESSURE_LSB);
+    unsigned long pressure = ((pressure_msb<<16)| pressure_lsb);
+    pressure /= 4;
+    
+    return pressure;
+}
+
+float SCP1000::readTemperature() {
+    float temp_in = read_register16(TEMP);
+    temp_in /= 20;
+    return temp_in;
+}
+
+ char SCP1000::read_register(char register_name) {
+    register_name <<=2;
+    register_name &= 0xFC;
+    m_cs=0; //Select SPI device
+    m_spi.write(register_name); //Send register location
+    char register_value=m_spi.write(0x00);
+    m_cs=1;
+    return register_value;  
+}
+
+void SCP1000::write_register(char register_name, char register_value) {
+    register_name <<= 2;
+    register_name |= 0x02; //le estamos diciendo que escriba
+    m_cs=0; //Select SPI device
+    m_spi.write(register_name); //Send register location
+    m_spi.write(register_value); //Send value to record into register
+    m_cs=1;
+}
+
+float SCP1000::read_register16(char register_name) {   
+    register_name <<= 2;
+    register_name &= 0xFC; //Read command
+    m_cs=0; //Select SPI Device
+    m_spi.write(register_name); //Write byte to device
+    int in_byte1 = m_spi.write(0x00);    
+    int in_byte2 = m_spi.write(0x00);
+    m_cs=1;
+    float in_word= (in_byte1<<=8) | (in_byte2);   
+    return(in_word);
+}
\ No newline at end of file
diff -r 000000000000 -r 55ea30b5de1e SCP1000.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SCP1000.h	Fri Jun 17 12:37:27 2011 +0000
@@ -0,0 +1,71 @@
+/**
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation; either version 2 of the License, or 
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
+ * for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along 
+ * with this program; if not, write to the Free Software Foundation, Inc., 
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * @section DESCRIPTION
+ * Library to interface with the SCP1000 pressure and temperature sensor.
+ */
+
+#ifndef _SCP1000_H
+#define _SCP1000_H
+
+#include "mbed.h"
+
+/**
+ * Class to interface with the SCP1000 pressure and temperature sensor.
+ */
+class SCP1000 {
+    public:
+        /**
+         * Constructor.
+         *
+         * @param mosi SPI MOSI pin
+         * @param miso SPI MISO pin
+         * @param sclk SPI SCLK pin
+         * @param cs Chip select pin
+         */
+        SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs);
+        
+        ~SCP1000() { /* empty */ };
+        
+        /**
+         * Read the pressure.
+         *
+         * @returns The pressure in pascals.
+         */
+        unsigned long readPressure();
+        
+        /**
+         * Read the temperature.
+         *
+         * @returns The temperature in Celsius.
+         */
+        float readTemperature();
+        
+        
+    private:
+        static const char PRESSURE = 0x1F;   //Pressure 3 MSB
+        static const char PRESSURE_LSB = 0x20; //Pressure 16 LSB
+        static const char TEMP = 0x21;       //16 bit temp
+        SPI m_spi;
+        DigitalOut m_cs;
+        
+        char read_register(char register_name);
+        void write_register(char register_name, char register_value);
+        float read_register16(char register_name);
+
+};
+
+#endif // _SCP1000_H
\ No newline at end of file