V 1.1

Fork of HMC5883L by GM AT

Files at this revision

API Documentation at this revision

Comitter:
fadi_lad
Date:
Tue Jan 03 09:22:45 2017 +0000
Parent:
0:cad18db1e431
Commit message:
V 1.1 bracelet connect?

Changed in this revision

HMC5883L.cpp Show annotated file Show diff for this revision Revisions of this file
HMC5883L.h Show annotated file Show diff for this revision Revisions of this file
--- a/HMC5883L.cpp	Thu Dec 12 02:25:11 2013 +0000
+++ b/HMC5883L.cpp	Tue Jan 03 09:22:45 2017 +0000
@@ -1,174 +1,133 @@
-/**
- * @author Jose R. Padron
- * @author Used HMC6352 library  developed by Aaron Berk as template
- * @section LICENSE
- 
- * Copyright (c) 2010 ARM Limited
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @section DESCRIPTION
- *
- * Honeywell HMC5883Ldigital compass.
- *
- * Datasheet:
- *
- * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5883L.pdf
- */
- 
-/**
- * Includes
- */
 #include "HMC5883L.h"
  
-HMC5883L::HMC5883L(PinName sda, PinName scl) {
- 
-    i2c_ = new I2C(sda, scl);
+HMC5883L::HMC5883L(PinName sda, PinName scl): i2c(sda, scl)
+{
     //100KHz, as specified by the datasheet.
-    i2c_->frequency(100000);
- 
- 
-}
- 
- 
-void HMC5883L::write(int address, int data) {
-   
-    char tx[2];
-   
-    tx[0]=address;
-    tx[1]=data;
+    char rx;
  
-    i2c_->write(HMC5883L_I2C_WRITE,tx,2);
-   
-    wait_ms(100);
- 
-}
- 
- 
-void HMC5883L::setSleepMode() {
     
-    write(HMC5883L_MODE, HMC5883L_SLEEP);
-}
- 
-void HMC5883L::setDefault(void) {
-   
-   write(HMC5883L_CONFIG_A,HMC5883L_10HZ_NORMAL);
-   write(HMC5883L_CONFIG_B,HMC5883L_1_0GA);
-   write(HMC5883L_MODE,HMC5883L_CONTINUOUS);
-   wait_ms(100);
+    i2c.frequency(100000);
+    //Testar depois com 400KHz
+    //==========================================================================================================
+    // Read chip_id
+    //==========================================================================================================
+    rx = Read(HMC5883L_IDENT_A);
+    if (rx != 0x48)//ID do chip
+        printf("\ninvalid chip id %d\r\n", rx); 
+     
+    //==========================================================================================================
+    // Let's set the Configuration Register A
+    //==========================================================================================================
+    // This register set's the number of samples averaged per measurement output, the rate at which data is written
+    // to all three data output registers and the measurement flow of the device.
+    // -------------------------------------------------------
+    // |CRA7 CRA6   CRA5   CRA4   CRA3   CRA2   CRA1   CRA0  |
+    // |(1)  MA1(1) MA0(1) DO2(1) DO1(0) DO0(0) MS1(0) MS0(0)| -> This is the default value
+    // -------------------------------------------------------
+    // CRA7 -> we have to clear this bit for correct operation                                      (0)
+    // CRA6 to CRA5 -> Let's select the maximum number of samples averaged per measurement output   (11)
+    // CRA4 to CRA2 -> Also let's select the maximum data output rate                               (110)
+    // CRA1 to CRA0 -> The measurement flow is defined to normal                                    (00)
+    // -------------------------------------------------------
+    // |CRA7 CRA6   CRA5   CRA4   CRA3   CRA2   CRA1   CRA0  |
+    // |(0)  MA1(1) MA0(1) DO2(1) DO1(1) DO0(0) MS1(0) MS0(0)| -> This is the new value, 0x78 in hex
+    // -------------------------------------------------------
+    //Write(HMC5883L_CONFIG_A,0x78);
+    //Write(HMC5883L_CONFIG_A,0x70);
+        
+    //==========================================================================================================
+    // The Configuration Register B is set to 0010 0000 by default, this is a +/- 1.3 Ga sensor field range and
+    // the gain of LSB/gauss is 1090. This is the maximum value, so let's leave it like that.
+    //==========================================================================================================
+    //Datasheet page 13. I will explain later
+    //Write(HMC5883L_CONFIG_B,0x20);
+    //Write(HMC5883L_CONFIG_B,0xA0);
+  
+    //==========================================================================================================
+    // Let's set the Mode Register
+    //==========================================================================================================
+    // This register set's the operation mode, from continuous-measurements mode, single-measurement mode and idle mode.
+    // We will set to Continuouse-measurement mode, so the device continuously performs measurements and places the
+    // result in the data register
+    // ---------------------------------------------
+    // |MR7  MR6  MR5  MR4  MR3  MR2  MR1    MR0   |  -> This is the new value, 0x78 in hex, we are going to change
+    // |(1)  (0)  (0)  (0)  (0)  (0)  MD1(0) MD0(1)|     the MD1 and MD0 to 00 and clear the MR7 for correct operation.
+    // ---------------------------------------------     The final value is 0000 0000 (0x00).
+    Write(HMC5883L_MODE,0x00);
 }
  
  
-void HMC5883L::getAddress(char *buffer) {
-    
-   char rx[3];
-   char tx[1];
-   tx[0]=HMC5883L_IDENT_A;
-    
-       
-    i2c_->write(HMC5883L_I2C_WRITE, tx,1);
-   
-    wait_ms(1);
-    
-    i2c_->read(HMC5883L_I2C_READ,rx,3);
-    
-    buffer[0]=rx[0];
-    buffer[1]=rx[1];
-    buffer[2]=rx[2];
+void HMC5883L::Write(char reg_address, char data)
+{
+    char tx[2];
+    tx[0]=reg_address;
+    tx[1]=data;
+ 
+    i2c.write(HMC5883L_I2C_WRITE,tx,2);
+}
+ 
+char HMC5883L::Read(char data)
+{
+    char tx = data;
+    char rx;
+ 
+    i2c.write(HMC5883L_I2C_WRITE, &tx, 1);
+    i2c.read(HMC5883L_I2C_READ, &rx, 1);
+    return rx;
+}
+ 
+void HMC5883L::MultiByteRead(char address, char* output, int size) 
+{
+    i2c.write(HMC5883L_I2C_WRITE, &address, 1);      //tell it where to read from
+    i2c.read(HMC5883L_I2C_READ, output, size);     //tell it where to store the data read
 }
  
+float HMC5883L::getMx()
+{
+    //return (x * m_Scale);
+    char lsb_byte = 0;
+    signed short msb_byte;
+ 
+    lsb_byte = Read(HMC5883L_X_MSB);
+    msb_byte = lsb_byte << 8;
+    msb_byte |= Read(HMC5883L_X_LSB);
+    return (float)msb_byte;
+    /*
+    char tx[1];
+    char rx[2];
  
  
-void HMC5883L::setOpMode(int mode, int ConfigA, int ConfigB) {
-    
-    
-    write(HMC5883L_CONFIG_A,ConfigA);
-    write(HMC5883L_CONFIG_B,ConfigB);
-    write(HMC5883L_MODE,mode);
-    
+    tx[0]=HMC5883L_X_MSB;
+    i2c.write(HMC5883L_I2C_READ,tx,1);
+    i2c.read(HMC5883L_I2C_READ,rx,2);
+    return ((int)rx[0]<<8|(int)rx[1]);
+    */
+ 
+}
  
+float HMC5883L::getMy()
+{
+    //return (y * m_Scale);
+ 
+    char lsb_byte = 0;
+    signed short msb_byte;
+ 
+    lsb_byte = Read(HMC5883L_Y_MSB);
+    msb_byte = lsb_byte << 8;
+    msb_byte |= Read(HMC5883L_Y_LSB);
+    return (float)msb_byte;
 }
  
  
- 
- 
-void HMC5883L::readData(int* readings) {
- 
-  
-    char tx[1];
-    char rx[2];
-    
-    
-    tx[0]=HMC5883L_X_MSB;
-    i2c_->write(HMC5883L_I2C_READ,tx,1);
-    i2c_->read(HMC5883L_I2C_READ,rx,2);
-    readings[0]= (int)rx[0]<<8|(int)rx[1];
- 
-     
-    tx[0]=HMC5883L_Y_MSB;
-    i2c_->write(HMC5883L_I2C_READ,tx,1);
-    i2c_->read(HMC5883L_I2C_READ,rx,2);
-    readings[1]= (int)rx[0]<<8|(int)rx[1];
-     
-    tx[0]=HMC5883L_Z_MSB;
-    i2c_->write(HMC5883L_I2C_READ,tx,1);
-    i2c_->read(HMC5883L_I2C_READ,rx,2);
-    readings[2]= (int)rx[0]<<8|(int)rx[1];
-    
-}
- 
-int HMC5883L::getMx() {
- 
-    char tx[1];
-    char rx[2];
-    
+float HMC5883L::getMz()
+{
+    //return (z * m_Scale);
     
-    tx[0]=HMC5883L_X_MSB;
-    i2c_->write(HMC5883L_I2C_READ,tx,1);
-    i2c_->read(HMC5883L_I2C_READ,rx,2);
-    return ((int)rx[0]<<8|(int)rx[1]);
- 
-}
- 
-int HMC5883L::getMy() {
+    char lsb_byte = 0;
+    signed short msb_byte;
  
-    char tx[1];
-    char rx[2];
-    
-    
-    tx[0]=HMC5883L_Y_MSB;
-    i2c_->write(HMC5883L_I2C_READ,tx,1);
-    i2c_->read(HMC5883L_I2C_READ,rx,2);
-    return ((int)rx[0]<<8|(int)rx[1]);
- 
-}
- 
- 
-int HMC5883L::getMz(){
- 
-    char tx[1];
-    char rx[2];
-    
-    
-    tx[0]=HMC5883L_Z_MSB;
-    i2c_->write(HMC5883L_I2C_READ,tx,1);
-    i2c_->read(HMC5883L_I2C_READ,rx,2);
-    return ((int)rx[0]<<8|(int)rx[1]);
- 
-}
+    lsb_byte = Read(HMC5883L_Z_MSB);
+    msb_byte = lsb_byte << 8;
+    msb_byte |= Read(HMC5883L_Z_LSB);
+    return (float)msb_byte;
+ }
\ No newline at end of file
--- a/HMC5883L.h	Thu Dec 12 02:25:11 2013 +0000
+++ b/HMC5883L.h	Tue Jan 03 09:22:45 2017 +0000
@@ -1,219 +1,42 @@
-/**
- * @author Jose R. Padron
- * @author Used HMC5883L library  developed by Aaron Berk as template
- * @section LICENSE
- *
- * Copyright (c) 2010 ARM Limited
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @section DESCRIPTION
- *
- * Honeywell HMC5883L digital compass.
- *
- * Datasheet:
- *
- * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5883L.pdf
- */
- 
 #ifndef HMC5883L_H
 #define HMC5883L_H
  
-/**
- * Includes
- */
 #include "mbed.h"
  
-/**
- * Defines
- */
-#define HMC5883L_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read.
-#define HMC5883L_I2C_WRITE   0x3C 
-#define HMC5883L_I2C_READ    0x3D 
- 
-//Values Config A
-#define HMC5883L_0_5HZ_NORMAL         0x00
-#define HMC5883L_0_5HZ_POSITIVE       0x01
-#define HMC5883L_0_5HZ_NEGATIVE       0x02
- 
-#define HMC5883L_1HZ_NORMAL           0x04
-#define HMC5883L_1HZ_POSITIVE         0x05
-#define HMC5883L_1HZ_NEGATIVE         0x06
- 
-#define HMC5883L_2HZ_NORMAL           0x08
-#define HMC5883L_2HZ_POSITIVE         0x09
-#define HMC5883L_2HZ_NEGATIVE         0x0A
- 
-#define HMC5883L_5HZ_NORMAL           0x0C
-#define HMC5883L_5HZ_POSITIVE         0x0D
-#define HMC5883L_5HZ_NEGATIVE         0x0E
+#define HMC5883L_IDENT_A        0x0A // In this case the identification register A is used to identify the devide. ASCII value H
+#define HMC5883L_I2C            0x1E // 7-bit address. 0x3C write, 0x3D read.
+#define HMC5883L_I2C_WRITE      0x3C // Same as (& 0xFE), ensure that the MSB bit is being set to zero (RW=0 -> Writing)
+#define HMC5883L_I2C_READ       0x3D // Same as (| 0x01), ensure that the MSB bit is being set to one  (RW=1 -> Reading)
  
-#define HMC5883L_10HZ_NORMAL           0x10
-#define HMC5883L_10HZ_POSITIVE         0x11
-#define HMC5883L_10HZ_NEGATIVE         0x12
- 
-#define HMC5883L_20HZ_NORMAL           0x14
-#define HMC5883L_20HZ_POSITIVE         0x15
-#define HMC5883L_20HZ_NEGATIVE         0x16
- 
-#define HMC5883L_50HZ_NORMAL           0x18
-#define HMC5883L_50HZ_POSITIVE         0x19
-#define HMC5883L_50HZ_NEGATIVE         0x1A
+#define HMC5883L_CONFIG_A       0x00
+#define HMC5883L_CONFIG_B       0x01
+#define HMC5883L_MODE           0x02
+#define HMC5883L_STATUS         0x09
  
-//Values Config B
-#define HMC5883L_0_7GA         0x00
-#define HMC5883L_1_0GA         0x20
-#define HMC5883L_1_5GA         0x40
-#define HMC5883L_2_0GA         0x60
-#define HMC5883L_3_2GA         0x80
-#define HMC5883L_3_8GA         0xA0
-#define HMC5883L_4_5GA         0xC0
-#define HMC5883L_6_5GA         0xE0
- 
-//Values MODE
-#define HMC5883L_CONTINUOUS   0x00
-#define HMC5883L_SINGLE       0x01
-#define HMC5883L_IDLE         0x02
-#define HMC5883L_SLEEP        0x03
+#define HMC5883L_X_MSB          0x03
+#define HMC5883L_X_LSB          0x04
+#define HMC5883L_Z_MSB          0x05
+#define HMC5883L_Z_LSB          0x06
+#define HMC5883L_Y_MSB          0x07
+#define HMC5883L_Y_LSB          0x08
  
  
- 
-#define HMC5883L_CONFIG_A     0x00
-#define HMC5883L_CONFIG_B     0x01
-#define HMC5883L_MODE         0x02
-#define HMC5883L_X_MSB        0x03
-#define HMC5883L_X_LSB        0x04
-#define HMC5883L_Y_MSB        0x05
-#define HMC5883L_Y_LSB        0x06
-#define HMC5883L_Z_MSB        0x07
-#define HMC5883L_Z_LSB        0x08
-#define HMC5883L_STATUS       0x09
-#define HMC5883L_IDENT_A      0x0A
-#define HMC5883L_IDENT_B      0x0B
-#define HMC5883L_IDENT_C      0x0C
- 
- 
- 
-/**
- * Honeywell HMC5883L digital compass.
- */
-class HMC5883L {
+class HMC5883L 
+{
  
 public:
  
-    /**
-     * Constructor.
-     *
-     * @param sda mbed pin to use for SDA line of I2C interface.
-     * @param scl mbed pin to use for SCL line of I2C interface.
-     */
     HMC5883L(PinName sda, PinName scl);
- 
-        
-     /**
-     * Enter into sleep mode.
-     *
-     */
-    void setSleepMode();
-    
-       
-     /**
-     * Set Device in Default Mode.
-     * HMC5883L_CONTINUOUS, HMC5883L_10HZ_NORMAL HMC5883L_1_0GA
-     */
-    void setDefault();
-    
-       
-    /**
-     * Read the memory location on the device which contains the address.
-     *
-     * @param Pointer to a buffer to hold the address value
-     * Expected     H, 4 and 3.
-     */
-    void getAddress(char * address);
- 
- 
-    
-    /**
-     * Set the operation mode.
-     *
-     * @param mode 0x00 -> Continuous
-     *             0x01 -> Single
-     *             0x02 -> Idle
-     * @param ConfigA values
-    * @param ConfigB values
-     */
-    void setOpMode(int mode, int ConfigA, int ConfigB);
+    float getMx();
+    float getMy();
+    float getMz();
+private:
+    void Write(char reg_address, char data);
+    char Read(char data);
+    void MultiByteRead(char address, char* output, int size); 
+    I2C i2c;
     
-     /**
-     * Write to  on the device.
-     *
-     * @param address Address to write to.
-     * @param data Data to write.
-     */
-    
-    void write(int address, int data);
- 
-     /**
-     * Get the output of all three axes.
-     *
-     * @param Pointer to a buffer to hold the magnetics value for the
-     *        x-axis, y-axis and z-axis [in that order].
-     */
-    void readData(int* readings);
-    
-    /**
-     * Get the output of X axis.
-     *
-     * @return x-axis magnetic value
-     */
-    int getMx();
-    
-    /**
-     * Get the output of Y axis.
-     *
-     * @return y-axis magnetic value
-     */
-    int getMy();
-    
-    /**
-     * Get the output of Z axis.
-     *
-     * @return z-axis magnetic value
-     */
-    int getMz();
-   
-    
-    /**
-     * Get the current operation mode.
-     *
-     * @return Status register values
-     */
-    int getStatus(void);
- 
-  
- 
-    I2C* i2c_;
- 
-   
- 
 };
  
 #endif /* HMC5883L_H */
- 
\ No newline at end of file
+