CMPS03 digital compass library.
Dependents: CMPS03_HelloWorld Final_Sonar xbeetx Compass ... more
CMPS03.cpp
00001 /** 00002 * @author Aaron Berk 00003 * 00004 * @section LICENSE 00005 * 00006 * Copyright (c) 2010 ARM Limited 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy 00009 * of this software and associated documentation files (the "Software"), to deal 00010 * in the Software without restriction, including without limitation the rights 00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the Software is 00013 * furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in 00016 * all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00024 * THE SOFTWARE. 00025 * 00026 * @section DESCRIPTION 00027 * 00028 * CMPS03 digital compass module. 00029 * 00030 * Datasheet: 00031 * 00032 * http://www.robot-electronics.co.uk/htm/cmps3tech.htm 00033 */ 00034 00035 /** 00036 * Includes 00037 */ 00038 #include "CMPS03.h" 00039 00040 CMPS03::CMPS03(PinName sda, PinName scl, int address) { 00041 00042 i2c = new I2C(sda, scl); 00043 //Compass designed to work at 100KHz. See datasheet for details. 00044 i2c->frequency(100000); 00045 i2cAddress = address; 00046 00047 } 00048 00049 char CMPS03::readSoftwareRevision(void){ 00050 00051 char registerNumber = SOFTWARE_REVISION_REG; 00052 char registerContents = 0; 00053 00054 //First, send the number of register we wish to read, 00055 //in this case, command register, number 0. 00056 i2c->write(i2cAddress, ®isterNumber, 1); 00057 00058 //Now, read one byte, which will be the contents of the command register. 00059 i2c->read(i2cAddress, ®isterContents, 1); 00060 00061 return registerContents; 00062 00063 } 00064 00065 int CMPS03::readBearing(void){ 00066 00067 char registerNumber = COMPASS_BEARING_WORD_REG; 00068 char registerContents[2] = {0x00, 0x00}; 00069 00070 //First, send the number of register we wish to read, 00071 //in this case, register numbers 2, 3, which hold the 00072 //compass bearing as a 16-bit word. 00073 i2c->write(i2cAddress, ®isterNumber, 1); 00074 00075 //Now read two bytes which will be the contents of 00076 //these registers. 00077 i2c->read(i2cAddress, registerContents, 2); 00078 00079 //Register 2 [read first], was the high byte, followed by 00080 //register 3 [read second], which was the low byte. 00081 int bearing = ((int)registerContents[0] << 8) | ((int)registerContents[1]); 00082 00083 return bearing; 00084 00085 }
Generated on Tue Jul 12 2022 11:37:21 by 1.7.2