Library to interface the SCP1000 temperature and pressure sensor.
Dependents: SCP1000_Example ku-make_sensor ku-make_sensor201302 SCP1000_Example ... more
SCP1000.cpp@4:295e118b6e85, 2010-10-05 (annotated)
- Committer:
- kadams6
- Date:
- Tue Oct 05 19:59:20 2010 +0000
- Revision:
- 4:295e118b6e85
- Child:
- 5:8e06eb237dd2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadams6 | 4:295e118b6e85 | 1 | #include "SCP1000.h" |
kadams6 | 4:295e118b6e85 | 2 | |
kadams6 | 4:295e118b6e85 | 3 | SCP1000:SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs) |
kadams6 | 4:295e118b6e85 | 4 | : m_spi(mosi, miso, sclk) |
kadams6 | 4:295e118b6e85 | 5 | , m_cs(cs) { |
kadams6 | 4:295e118b6e85 | 6 | m_cs=1; |
kadams6 | 4:295e118b6e85 | 7 | m_spi.frequency(500000); // the fastest of the sensor |
kadams6 | 4:295e118b6e85 | 8 | m_spi.format(8, 0); // duda son dos palabras de 8 bits? |
kadams6 | 4:295e118b6e85 | 9 | wait(0.5); |
kadams6 | 4:295e118b6e85 | 10 | //------------------------------------------------ |
kadams6 | 4:295e118b6e85 | 11 | // pc.printf("RESET\r\n"); |
kadams6 | 4:295e118b6e85 | 12 | write_register(0x06,0x01); |
kadams6 | 4:295e118b6e85 | 13 | wait(0.5); |
kadams6 | 4:295e118b6e85 | 14 | |
kadams6 | 4:295e118b6e85 | 15 | // pc.printf("Initialize High Resolution Constant Reading Mode\r\n"); |
kadams6 | 4:295e118b6e85 | 16 | write_register(0x03,0x0A); |
kadams6 | 4:295e118b6e85 | 17 | wait(0.5); |
kadams6 | 4:295e118b6e85 | 18 | } |
kadams6 | 4:295e118b6e85 | 19 | |
kadams6 | 4:295e118b6e85 | 20 | unsigned long SCP1000::readPressure() { |
kadams6 | 4:295e118b6e85 | 21 | unsigned long pressure_msb = read_register(PRESSURE); |
kadams6 | 4:295e118b6e85 | 22 | pressure_msb &= 0x07; |
kadams6 | 4:295e118b6e85 | 23 | unsigned long pressure_lsb = read_register16(PRESSURE_LSB); |
kadams6 | 4:295e118b6e85 | 24 | unsigned long pressure = ((pressure_msb<<16)| pressure_lsb); |
kadams6 | 4:295e118b6e85 | 25 | pressure /= 4; |
kadams6 | 4:295e118b6e85 | 26 | |
kadams6 | 4:295e118b6e85 | 27 | return pressure; |
kadams6 | 4:295e118b6e85 | 28 | } |
kadams6 | 4:295e118b6e85 | 29 | |
kadams6 | 4:295e118b6e85 | 30 | float SCP1000::readTemperature() { |
kadams6 | 4:295e118b6e85 | 31 | float temp_in = read_register16(TEMP); |
kadams6 | 4:295e118b6e85 | 32 | temp_in /= 20; |
kadams6 | 4:295e118b6e85 | 33 | return temp_in; |
kadams6 | 4:295e118b6e85 | 34 | } |
kadams6 | 4:295e118b6e85 | 35 | |
kadams6 | 4:295e118b6e85 | 36 | char SCP1000::read_register(char register_name) { |
kadams6 | 4:295e118b6e85 | 37 | register_name <<=2; |
kadams6 | 4:295e118b6e85 | 38 | register_name &= 0xFC; |
kadams6 | 4:295e118b6e85 | 39 | m_cs=0; //Select SPI device |
kadams6 | 4:295e118b6e85 | 40 | m_spi.write(register_name); //Send register location |
kadams6 | 4:295e118b6e85 | 41 | char register_value=m_spi.write(0x00); |
kadams6 | 4:295e118b6e85 | 42 | m_cs=1; |
kadams6 | 4:295e118b6e85 | 43 | return register_value; |
kadams6 | 4:295e118b6e85 | 44 | } |
kadams6 | 4:295e118b6e85 | 45 | |
kadams6 | 4:295e118b6e85 | 46 | void SCP1000::write_register(char register_name, char register_value) { |
kadams6 | 4:295e118b6e85 | 47 | register_name <<= 2; |
kadams6 | 4:295e118b6e85 | 48 | register_name |= 0x02; //le estamos diciendo que escriba |
kadams6 | 4:295e118b6e85 | 49 | m_cs=0; //Select SPI device |
kadams6 | 4:295e118b6e85 | 50 | m_spi.write(register_name); //Send register location |
kadams6 | 4:295e118b6e85 | 51 | m_spi.write(register_value); //Send value to record into register |
kadams6 | 4:295e118b6e85 | 52 | m_cs=1; |
kadams6 | 4:295e118b6e85 | 53 | } |
kadams6 | 4:295e118b6e85 | 54 | |
kadams6 | 4:295e118b6e85 | 55 | float SCP1000::read_register16(char register_name) { |
kadams6 | 4:295e118b6e85 | 56 | register_name <<= 2; |
kadams6 | 4:295e118b6e85 | 57 | register_name &= 0xFC; //Read command |
kadams6 | 4:295e118b6e85 | 58 | m_cs=0; //Select SPI Device |
kadams6 | 4:295e118b6e85 | 59 | m_spi.write(register_name); //Write byte to device |
kadams6 | 4:295e118b6e85 | 60 | int in_byte1 = m_spi.write(0x00); |
kadams6 | 4:295e118b6e85 | 61 | int in_byte2 = m_spi.write(0x00); |
kadams6 | 4:295e118b6e85 | 62 | m_cs=1; |
kadams6 | 4:295e118b6e85 | 63 | float in_word= (in_byte1<<=8) | (in_byte2); |
kadams6 | 4:295e118b6e85 | 64 | return(in_word); |
kadams6 | 4:295e118b6e85 | 65 | } |