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