Library to interface the SCP1000 temperature and pressure sensor.
Dependents: SCP1000_Example ku-make_sensor ku-make_sensor201302 SCP1000_Example ... more
Diff: SCP1000.h
- Revision:
- 0:0b8b5828e1a3
- Child:
- 1:5fcf1fe4a161
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SCP1000.h Tue Oct 05 04:07:01 2010 +0000 @@ -0,0 +1,84 @@ +#ifndef _SCP1000_H +#define _SCP1000_H + +#include "mbed.h" + +class SCP1000 { + public: + 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); + }; + + ~SCP1000() { /* empty */ }; + + unsigned long 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 readTemperature() { + float temp_in = read_register16(TEMP); + temp_in /= 20; + return temp_in; + }; + + + 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) { + 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 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 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); + }; + +}; + +#endif // _SCP1000_H \ No newline at end of file