CMPS03 digital compass library.
Fork of CMPS03 by
CMPS03.cpp@0:c6bcc390612a, 2010-11-27 (annotated)
- Committer:
- aberk
- Date:
- Sat Nov 27 12:09:27 2010 +0000
- Revision:
- 0:c6bcc390612a
- Child:
- 1:1585ed4179f0
Version 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aberk | 0:c6bcc390612a | 1 | /** |
aberk | 0:c6bcc390612a | 2 | * @author Aaron Berk |
aberk | 0:c6bcc390612a | 3 | * |
aberk | 0:c6bcc390612a | 4 | * @section LICENSE |
aberk | 0:c6bcc390612a | 5 | * |
aberk | 0:c6bcc390612a | 6 | * Copyright (c) 2010 ARM Limited |
aberk | 0:c6bcc390612a | 7 | * |
aberk | 0:c6bcc390612a | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
aberk | 0:c6bcc390612a | 9 | * of this software and associated documentation files (the "Software"), to deal |
aberk | 0:c6bcc390612a | 10 | * in the Software without restriction, including without limitation the rights |
aberk | 0:c6bcc390612a | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
aberk | 0:c6bcc390612a | 12 | * copies of the Software, and to permit persons to whom the Software is |
aberk | 0:c6bcc390612a | 13 | * furnished to do so, subject to the following conditions: |
aberk | 0:c6bcc390612a | 14 | * |
aberk | 0:c6bcc390612a | 15 | * The above copyright notice and this permission notice shall be included in |
aberk | 0:c6bcc390612a | 16 | * all copies or substantial portions of the Software. |
aberk | 0:c6bcc390612a | 17 | * |
aberk | 0:c6bcc390612a | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
aberk | 0:c6bcc390612a | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
aberk | 0:c6bcc390612a | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
aberk | 0:c6bcc390612a | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
aberk | 0:c6bcc390612a | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
aberk | 0:c6bcc390612a | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
aberk | 0:c6bcc390612a | 24 | * THE SOFTWARE. |
aberk | 0:c6bcc390612a | 25 | * |
aberk | 0:c6bcc390612a | 26 | * @section DESCRIPTION |
aberk | 0:c6bcc390612a | 27 | * |
aberk | 0:c6bcc390612a | 28 | * CMPS03 digital compass module. |
aberk | 0:c6bcc390612a | 29 | * |
aberk | 0:c6bcc390612a | 30 | * Datasheet: |
aberk | 0:c6bcc390612a | 31 | * |
aberk | 0:c6bcc390612a | 32 | * http://www.robot-electronics.co.uk/htm/cmps3tech.htm |
aberk | 0:c6bcc390612a | 33 | */ |
aberk | 0:c6bcc390612a | 34 | |
aberk | 0:c6bcc390612a | 35 | /** |
aberk | 0:c6bcc390612a | 36 | * Includes |
aberk | 0:c6bcc390612a | 37 | */ |
aberk | 0:c6bcc390612a | 38 | #include "CMPS03.h" |
aberk | 0:c6bcc390612a | 39 | |
aberk | 0:c6bcc390612a | 40 | CMPS03::CMPS03(PinName sda, PinName scl, int address) { |
aberk | 0:c6bcc390612a | 41 | |
aberk | 0:c6bcc390612a | 42 | i2c = new I2C(sda, scl); |
aberk | 0:c6bcc390612a | 43 | //Compass designed to work at 100KHz. See datasheet for details. |
aberk | 0:c6bcc390612a | 44 | i2c->frequency(100000); |
aberk | 0:c6bcc390612a | 45 | i2cAddress = address; |
aberk | 0:c6bcc390612a | 46 | |
aberk | 0:c6bcc390612a | 47 | } |
aberk | 0:c6bcc390612a | 48 | |
aberk | 0:c6bcc390612a | 49 | char CMPS03::readSoftwareRevision(void){ |
aberk | 0:c6bcc390612a | 50 | |
aberk | 0:c6bcc390612a | 51 | char registerNumber = SOFTWARE_REVISION_REG; |
aberk | 0:c6bcc390612a | 52 | char registerContents = 0; |
aberk | 0:c6bcc390612a | 53 | |
aberk | 0:c6bcc390612a | 54 | //First, send the number of register we wish to read, |
aberk | 0:c6bcc390612a | 55 | //in this case, command register, number 0. |
aberk | 0:c6bcc390612a | 56 | i2c->write(i2cAddress, ®isterNumber, 1); |
aberk | 0:c6bcc390612a | 57 | |
aberk | 0:c6bcc390612a | 58 | //Now, read one byte, which will be the contents of the command register. |
aberk | 0:c6bcc390612a | 59 | i2c->read(i2cAddress, ®isterContents, 1); |
aberk | 0:c6bcc390612a | 60 | |
aberk | 0:c6bcc390612a | 61 | return registerContents; |
aberk | 0:c6bcc390612a | 62 | |
aberk | 0:c6bcc390612a | 63 | } |
aberk | 0:c6bcc390612a | 64 | |
aberk | 0:c6bcc390612a | 65 | int CMPS03::readBearing(void){ |
aberk | 0:c6bcc390612a | 66 | |
aberk | 0:c6bcc390612a | 67 | char registerNumber = COMPASS_BEARING_WORD_REG; |
aberk | 0:c6bcc390612a | 68 | char registerContents[2] = {0x00, 0x00}; |
aberk | 0:c6bcc390612a | 69 | |
aberk | 0:c6bcc390612a | 70 | //First, send the number of register we wish to read, |
aberk | 0:c6bcc390612a | 71 | //in this case, register numbers 2, 3, which hold the |
aberk | 0:c6bcc390612a | 72 | //compass bearing as a 16-bit word. |
aberk | 0:c6bcc390612a | 73 | i2c->write(i2cAddress, ®isterNumber, 1); |
aberk | 0:c6bcc390612a | 74 | |
aberk | 0:c6bcc390612a | 75 | //Now read two bytes which will be the contents of |
aberk | 0:c6bcc390612a | 76 | //these registers. |
aberk | 0:c6bcc390612a | 77 | i2c->read(i2cAddress, registerContents, 2); |
aberk | 0:c6bcc390612a | 78 | |
aberk | 0:c6bcc390612a | 79 | //Register 2 [read first], was the high byte, followed by |
aberk | 0:c6bcc390612a | 80 | //register 3 [read second], which was the low byte. |
aberk | 0:c6bcc390612a | 81 | int bearing = ((int)registerContents[0] << 8) | ((int)registerContents[1]); |
aberk | 0:c6bcc390612a | 82 | |
aberk | 0:c6bcc390612a | 83 | return bearing; |
aberk | 0:c6bcc390612a | 84 | |
aberk | 0:c6bcc390612a | 85 | } |