Library to interface the SCP1000 temperature and pressure sensor.

Dependents:   SCP1000_Example ku-make_sensor ku-make_sensor201302 SCP1000_Example ... more

SCP1000.cpp

Committer:
kadams6
Date:
2010-10-05
Revision:
4:295e118b6e85
Child:
5:8e06eb237dd2

File content as of revision 4:295e118b6e85:

#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);
}