Dependents:   V2_GPSRTC

Committer:
joosttromp
Date:
Fri Jun 17 12:37:27 2011 +0000
Revision:
0:55ea30b5de1e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joosttromp 0:55ea30b5de1e 1 /**
joosttromp 0:55ea30b5de1e 2 * @section LICENSE
joosttromp 0:55ea30b5de1e 3 * This program is free software; you can redistribute it and/or modify
joosttromp 0:55ea30b5de1e 4 * it under the terms of the GNU General Public License as published by
joosttromp 0:55ea30b5de1e 5 * the Free Software Foundation; either version 2 of the License, or
joosttromp 0:55ea30b5de1e 6 * (at your option) any later version.
joosttromp 0:55ea30b5de1e 7 *
joosttromp 0:55ea30b5de1e 8 * This program is distributed in the hope that it will be useful, but
joosttromp 0:55ea30b5de1e 9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
joosttromp 0:55ea30b5de1e 10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
joosttromp 0:55ea30b5de1e 11 * for more details.
joosttromp 0:55ea30b5de1e 12 *
joosttromp 0:55ea30b5de1e 13 * You should have received a copy of the GNU General Public License along
joosttromp 0:55ea30b5de1e 14 * with this program; if not, write to the Free Software Foundation, Inc.,
joosttromp 0:55ea30b5de1e 15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
joosttromp 0:55ea30b5de1e 16 *
joosttromp 0:55ea30b5de1e 17 * @section DESCRIPTION
joosttromp 0:55ea30b5de1e 18 * Library to interface with the SCP1000 pressure and temperature sensor.
joosttromp 0:55ea30b5de1e 19 */
joosttromp 0:55ea30b5de1e 20
joosttromp 0:55ea30b5de1e 21 #include "SCP1000.h"
joosttromp 0:55ea30b5de1e 22
joosttromp 0:55ea30b5de1e 23 SCP1000::SCP1000(PinName mosi, PinName miso, PinName sclk, PinName cs)
joosttromp 0:55ea30b5de1e 24 : m_spi(mosi, miso, sclk)
joosttromp 0:55ea30b5de1e 25 , m_cs(cs) {
joosttromp 0:55ea30b5de1e 26 m_cs=1;
joosttromp 0:55ea30b5de1e 27 m_spi.frequency(500000); // the fastest of the sensor
joosttromp 0:55ea30b5de1e 28 m_spi.format(8, 0); // duda son dos palabras de 8 bits?
joosttromp 0:55ea30b5de1e 29 wait(0.5);
joosttromp 0:55ea30b5de1e 30 //------------------------------------------------
joosttromp 0:55ea30b5de1e 31 // pc.printf("RESET\r\n");
joosttromp 0:55ea30b5de1e 32 write_register(0x06,0x01);
joosttromp 0:55ea30b5de1e 33 wait(0.5);
joosttromp 0:55ea30b5de1e 34
joosttromp 0:55ea30b5de1e 35 // pc.printf("Initialize High Resolution Constant Reading Mode\r\n");
joosttromp 0:55ea30b5de1e 36 write_register(0x03,0x0A);
joosttromp 0:55ea30b5de1e 37 wait(0.5);
joosttromp 0:55ea30b5de1e 38 }
joosttromp 0:55ea30b5de1e 39
joosttromp 0:55ea30b5de1e 40 unsigned long SCP1000::readPressure() {
joosttromp 0:55ea30b5de1e 41 unsigned long pressure_msb = read_register(PRESSURE);
joosttromp 0:55ea30b5de1e 42 pressure_msb &= 0x07;
joosttromp 0:55ea30b5de1e 43 unsigned long pressure_lsb = read_register16(PRESSURE_LSB);
joosttromp 0:55ea30b5de1e 44 unsigned long pressure = ((pressure_msb<<16)| pressure_lsb);
joosttromp 0:55ea30b5de1e 45 pressure /= 4;
joosttromp 0:55ea30b5de1e 46
joosttromp 0:55ea30b5de1e 47 return pressure;
joosttromp 0:55ea30b5de1e 48 }
joosttromp 0:55ea30b5de1e 49
joosttromp 0:55ea30b5de1e 50 float SCP1000::readTemperature() {
joosttromp 0:55ea30b5de1e 51 float temp_in = read_register16(TEMP);
joosttromp 0:55ea30b5de1e 52 temp_in /= 20;
joosttromp 0:55ea30b5de1e 53 return temp_in;
joosttromp 0:55ea30b5de1e 54 }
joosttromp 0:55ea30b5de1e 55
joosttromp 0:55ea30b5de1e 56 char SCP1000::read_register(char register_name) {
joosttromp 0:55ea30b5de1e 57 register_name <<=2;
joosttromp 0:55ea30b5de1e 58 register_name &= 0xFC;
joosttromp 0:55ea30b5de1e 59 m_cs=0; //Select SPI device
joosttromp 0:55ea30b5de1e 60 m_spi.write(register_name); //Send register location
joosttromp 0:55ea30b5de1e 61 char register_value=m_spi.write(0x00);
joosttromp 0:55ea30b5de1e 62 m_cs=1;
joosttromp 0:55ea30b5de1e 63 return register_value;
joosttromp 0:55ea30b5de1e 64 }
joosttromp 0:55ea30b5de1e 65
joosttromp 0:55ea30b5de1e 66 void SCP1000::write_register(char register_name, char register_value) {
joosttromp 0:55ea30b5de1e 67 register_name <<= 2;
joosttromp 0:55ea30b5de1e 68 register_name |= 0x02; //le estamos diciendo que escriba
joosttromp 0:55ea30b5de1e 69 m_cs=0; //Select SPI device
joosttromp 0:55ea30b5de1e 70 m_spi.write(register_name); //Send register location
joosttromp 0:55ea30b5de1e 71 m_spi.write(register_value); //Send value to record into register
joosttromp 0:55ea30b5de1e 72 m_cs=1;
joosttromp 0:55ea30b5de1e 73 }
joosttromp 0:55ea30b5de1e 74
joosttromp 0:55ea30b5de1e 75 float SCP1000::read_register16(char register_name) {
joosttromp 0:55ea30b5de1e 76 register_name <<= 2;
joosttromp 0:55ea30b5de1e 77 register_name &= 0xFC; //Read command
joosttromp 0:55ea30b5de1e 78 m_cs=0; //Select SPI Device
joosttromp 0:55ea30b5de1e 79 m_spi.write(register_name); //Write byte to device
joosttromp 0:55ea30b5de1e 80 int in_byte1 = m_spi.write(0x00);
joosttromp 0:55ea30b5de1e 81 int in_byte2 = m_spi.write(0x00);
joosttromp 0:55ea30b5de1e 82 m_cs=1;
joosttromp 0:55ea30b5de1e 83 float in_word= (in_byte1<<=8) | (in_byte2);
joosttromp 0:55ea30b5de1e 84 return(in_word);
joosttromp 0:55ea30b5de1e 85 }