Library to interface the SCP1000 temperature and pressure sensor.
Dependents: SCP1000_Example ku-make_sensor ku-make_sensor201302 SCP1000_Example ... more
SCP1000.h
- Committer:
- kadams6
- Date:
- 2010-10-05
- Revision:
- 3:4d8b8ca54451
- Parent:
- 2:fe7826024fd1
- Child:
- 4:295e118b6e85
File content as of revision 3:4d8b8ca54451:
/** \file SCP1000 header file */ #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 */ }; ///Reads the pressure /** Reads the pressure from the sensor @return Pressure in pascals. */ 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