SCA61T Single Axis Inclinometer

Dependents:   SCA61T_example

Committer:
Veino
Date:
Thu Mar 10 16:07:16 2011 +0000
Revision:
2:b0d8ca64cb0f
Parent:
1:663ebf72b607
v1.1, some typos fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Veino 1:663ebf72b607 1 /* mbed SCA61T Single Axis Inclinometer
Veino 1:663ebf72b607 2 * Copyright (c) 2010 Veikko Soininen
Veino 1:663ebf72b607 3 *
Veino 1:663ebf72b607 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Veino 1:663ebf72b607 5 * of this software and associated documentation files (the "Software"), to deal
Veino 1:663ebf72b607 6 * in the Software without restriction, including without limitation the rights
Veino 1:663ebf72b607 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Veino 1:663ebf72b607 8 * copies of the Software, and to permit persons to whom the Software is
Veino 1:663ebf72b607 9 * furnished to do so, subject to the following conditions:
Veino 1:663ebf72b607 10 *
Veino 1:663ebf72b607 11 * The above copyright notice and this permission notice shall be included in
Veino 1:663ebf72b607 12 * all copies or substantial portions of the Software.
Veino 1:663ebf72b607 13 *
Veino 1:663ebf72b607 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Veino 1:663ebf72b607 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Veino 1:663ebf72b607 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Veino 1:663ebf72b607 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Veino 1:663ebf72b607 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Veino 1:663ebf72b607 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Veino 1:663ebf72b607 20 * THE SOFTWARE.
Veino 1:663ebf72b607 21 */
Veino 1:663ebf72b607 22
Veino 0:eaca4c1a258f 23 #include "SCA61T.h"
Veino 0:eaca4c1a258f 24
Veino 0:eaca4c1a258f 25 // ** Class
Veino 0:eaca4c1a258f 26
Veino 2:b0d8ca64cb0f 27 static int sel;
Veino 1:663ebf72b607 28
Veino 2:b0d8ca64cb0f 29 SCA61T::SCA61T(PinName mosi, PinName miso, PinName sclk, PinName csb, int device_sel)
Veino 0:eaca4c1a258f 30 : SPI_m(mosi, miso, sclk)
Veino 2:b0d8ca64cb0f 31 , CSB_m(csb) {
Veino 1:663ebf72b607 32 sel=device_sel;
Veino 2:b0d8ca64cb0f 33 CSB_m=1;
Veino 0:eaca4c1a258f 34 SPI_m.frequency(500000);
Veino 0:eaca4c1a258f 35 }
Veino 0:eaca4c1a258f 36
Veino 0:eaca4c1a258f 37 // ** SPI
Veino 0:eaca4c1a258f 38
Veino 0:eaca4c1a258f 39 uint8_t SCA61T::SPI_ReadReg(uint8_t reg)
Veino 0:eaca4c1a258f 40 {
Veino 0:eaca4c1a258f 41 uint8_t reply;
Veino 0:eaca4c1a258f 42
Veino 2:b0d8ca64cb0f 43 CSB_m=0;
Veino 0:eaca4c1a258f 44 SPI_m.write(reg);
Veino 0:eaca4c1a258f 45 reply = SPI_m.write(0x00);
Veino 2:b0d8ca64cb0f 46 CSB_m=1;
Veino 0:eaca4c1a258f 47
Veino 0:eaca4c1a258f 48 return reply;
Veino 0:eaca4c1a258f 49 }
Veino 0:eaca4c1a258f 50
Veino 0:eaca4c1a258f 51 void SCA61T::SPI_ReadWord(uint8_t cmd, char* table)
Veino 0:eaca4c1a258f 52 {
Veino 2:b0d8ca64cb0f 53 CSB_m=0;
Veino 0:eaca4c1a258f 54 SPI_m.write(cmd);
Veino 0:eaca4c1a258f 55 for(int i=0;i<2;i++)
Veino 0:eaca4c1a258f 56 *table++ = SPI_m.write(0x00);
Veino 2:b0d8ca64cb0f 57 CSB_m=1;
Veino 0:eaca4c1a258f 58 }
Veino 0:eaca4c1a258f 59
Veino 0:eaca4c1a258f 60 void SCA61T::SPI_Command(uint8_t cmd)
Veino 0:eaca4c1a258f 61 {
Veino 2:b0d8ca64cb0f 62 CSB_m=0;
Veino 0:eaca4c1a258f 63 SPI_m.write(cmd);
Veino 2:b0d8ca64cb0f 64 CSB_m=1;
Veino 0:eaca4c1a258f 65 }
Veino 0:eaca4c1a258f 66
Veino 0:eaca4c1a258f 67 // ** SCA61T
Veino 0:eaca4c1a258f 68
Veino 1:663ebf72b607 69 float SCA61T::ReadX()
Veino 0:eaca4c1a258f 70 {
Veino 0:eaca4c1a258f 71 uint8_t temp[2];
Veino 0:eaca4c1a258f 72 uint16_t data;
Veino 1:663ebf72b607 73 float device_sens;
Veino 1:663ebf72b607 74 float angle;
Veino 1:663ebf72b607 75
Veino 1:663ebf72b607 76 if(sel)
Veino 1:663ebf72b607 77 device_sens = 819.00;
Veino 1:663ebf72b607 78 else
Veino 1:663ebf72b607 79 device_sens = 1638.00;
Veino 0:eaca4c1a258f 80
Veino 0:eaca4c1a258f 81 SPI_ReadWord(0x10,(char*)temp);
Veino 0:eaca4c1a258f 82
Veino 0:eaca4c1a258f 83 temp[1] = (temp[1]>>5);
Veino 0:eaca4c1a258f 84 data=(temp[0]<<3)+temp[1];
Veino 0:eaca4c1a258f 85
Veino 1:663ebf72b607 86 angle=asinf((data-1024.00f)/device_sens)*(180.00f/3.14f);
Veino 0:eaca4c1a258f 87
Veino 0:eaca4c1a258f 88 return angle;
Veino 0:eaca4c1a258f 89 }
Veino 0:eaca4c1a258f 90
Veino 0:eaca4c1a258f 91 int8_t SCA61T::ReadTemp()
Veino 0:eaca4c1a258f 92 {
Veino 0:eaca4c1a258f 93 int temp;
Veino 0:eaca4c1a258f 94
Veino 0:eaca4c1a258f 95 temp=SPI_ReadReg(0x08);
Veino 0:eaca4c1a258f 96 temp=rint((temp-197.000)/(-1.083));
Veino 0:eaca4c1a258f 97
Veino 0:eaca4c1a258f 98 return temp;
Veino 0:eaca4c1a258f 99 }
Veino 0:eaca4c1a258f 100
Veino 0:eaca4c1a258f 101 void SCA61T::MeasMode()
Veino 0:eaca4c1a258f 102 {
Veino 0:eaca4c1a258f 103 SPI_Command(0x00);
Veino 0:eaca4c1a258f 104 }
Veino 0:eaca4c1a258f 105
Veino 0:eaca4c1a258f 106 uint8_t SCA61T::ReadStatus()
Veino 0:eaca4c1a258f 107 {
Veino 0:eaca4c1a258f 108 return SPI_ReadReg(0x0A);
Veino 0:eaca4c1a258f 109 }
Veino 0:eaca4c1a258f 110
Veino 0:eaca4c1a258f 111 void SCA61T::ReloadNV()
Veino 0:eaca4c1a258f 112 {
Veino 0:eaca4c1a258f 113 SPI_Command(0x0B);
Veino 0:eaca4c1a258f 114 }
Veino 0:eaca4c1a258f 115
Veino 0:eaca4c1a258f 116 void SCA61T::SelfTest()
Veino 0:eaca4c1a258f 117 {
Veino 0:eaca4c1a258f 118 SPI_Command(0x0E);
Veino 0:eaca4c1a258f 119 }