temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IKobayashi 0:c88c3b616c00 1 #include "HMC5883L.h"
IKobayashi 0:c88c3b616c00 2
IKobayashi 0:c88c3b616c00 3 HMC5883L::HMC5883L(PinName sda, PinName scl): i2c(sda, scl)
IKobayashi 0:c88c3b616c00 4 {
IKobayashi 0:c88c3b616c00 5 //100KHz, as specified by the datasheet.
IKobayashi 0:c88c3b616c00 6 char rx;
IKobayashi 0:c88c3b616c00 7
IKobayashi 0:c88c3b616c00 8
IKobayashi 0:c88c3b616c00 9 i2c.frequency(100000);
IKobayashi 0:c88c3b616c00 10 //Testar depois com 400KHz
IKobayashi 0:c88c3b616c00 11 //==========================================================================================================
IKobayashi 0:c88c3b616c00 12 // Read chip_id
IKobayashi 0:c88c3b616c00 13 //==========================================================================================================
IKobayashi 0:c88c3b616c00 14 rx = Read(HMC5883L_IDENT_A);
IKobayashi 0:c88c3b616c00 15 if (rx != 0x48)//ID do chip
IKobayashi 0:c88c3b616c00 16 printf("\ninvalid chip id %d\r\n", rx);
IKobayashi 0:c88c3b616c00 17
IKobayashi 0:c88c3b616c00 18 //==========================================================================================================
IKobayashi 0:c88c3b616c00 19 // Let's set the Configuration Register A
IKobayashi 0:c88c3b616c00 20 //==========================================================================================================
IKobayashi 0:c88c3b616c00 21 // This register set's the number of samples averaged per measurement output, the rate at which data is written
IKobayashi 0:c88c3b616c00 22 // to all three data output registers and the measurement flow of the device.
IKobayashi 0:c88c3b616c00 23 // -------------------------------------------------------
IKobayashi 0:c88c3b616c00 24 // |CRA7 CRA6 CRA5 CRA4 CRA3 CRA2 CRA1 CRA0 |
IKobayashi 0:c88c3b616c00 25 // |(1) MA1(1) MA0(1) DO2(1) DO1(0) DO0(0) MS1(0) MS0(0)| -> This is the default value
IKobayashi 0:c88c3b616c00 26 // -------------------------------------------------------
IKobayashi 0:c88c3b616c00 27 // CRA7 -> we have to clear this bit for correct operation (0)
IKobayashi 0:c88c3b616c00 28 // CRA6 to CRA5 -> Let's select the maximum number of samples averaged per measurement output (11)
IKobayashi 0:c88c3b616c00 29 // CRA4 to CRA2 -> Also let's select the maximum data output rate (110)
IKobayashi 0:c88c3b616c00 30 // CRA1 to CRA0 -> The measurement flow is defined to normal (00)
IKobayashi 0:c88c3b616c00 31 // -------------------------------------------------------
IKobayashi 0:c88c3b616c00 32 // |CRA7 CRA6 CRA5 CRA4 CRA3 CRA2 CRA1 CRA0 |
IKobayashi 0:c88c3b616c00 33 // |(0) MA1(1) MA0(1) DO2(1) DO1(1) DO0(0) MS1(0) MS0(0)| -> This is the new value, 0x78 in hex
IKobayashi 0:c88c3b616c00 34 // -------------------------------------------------------
IKobayashi 0:c88c3b616c00 35 //Write(HMC5883L_CONFIG_A,0x78);
IKobayashi 0:c88c3b616c00 36 //Write(HMC5883L_CONFIG_A,0x70);
IKobayashi 0:c88c3b616c00 37
IKobayashi 0:c88c3b616c00 38 //==========================================================================================================
IKobayashi 0:c88c3b616c00 39 // The Configuration Register B is set to 0010 0000 by default, this is a +/- 1.3 Ga sensor field range and
IKobayashi 0:c88c3b616c00 40 // the gain of LSB/gauss is 1090. This is the maximum value, so let's leave it like that.
IKobayashi 0:c88c3b616c00 41 //==========================================================================================================
IKobayashi 0:c88c3b616c00 42 //Datasheet page 13. I will explain later
IKobayashi 0:c88c3b616c00 43 //Write(HMC5883L_CONFIG_B,0x20);
IKobayashi 0:c88c3b616c00 44 //Write(HMC5883L_CONFIG_B,0xA0);
IKobayashi 0:c88c3b616c00 45
IKobayashi 0:c88c3b616c00 46 //==========================================================================================================
IKobayashi 0:c88c3b616c00 47 // Let's set the Mode Register
IKobayashi 0:c88c3b616c00 48 //==========================================================================================================
IKobayashi 0:c88c3b616c00 49 // This register set's the operation mode, from continuous-measurements mode, single-measurement mode and idle mode.
IKobayashi 0:c88c3b616c00 50 // We will set to Continuouse-measurement mode, so the device continuously performs measurements and places the
IKobayashi 0:c88c3b616c00 51 // result in the data register
IKobayashi 0:c88c3b616c00 52 // ---------------------------------------------
IKobayashi 0:c88c3b616c00 53 // |MR7 MR6 MR5 MR4 MR3 MR2 MR1 MR0 | -> This is the new value, 0x78 in hex, we are going to change
IKobayashi 0:c88c3b616c00 54 // |(1) (0) (0) (0) (0) (0) MD1(0) MD0(1)| the MD1 and MD0 to 00 and clear the MR7 for correct operation.
IKobayashi 0:c88c3b616c00 55 // --------------------------------------------- The final value is 0000 0000 (0x00).
IKobayashi 0:c88c3b616c00 56 Write(HMC5883L_MODE,0x00);
IKobayashi 0:c88c3b616c00 57 }
IKobayashi 0:c88c3b616c00 58
IKobayashi 0:c88c3b616c00 59
IKobayashi 0:c88c3b616c00 60 void HMC5883L::Write(char reg_address, char data)
IKobayashi 0:c88c3b616c00 61 {
IKobayashi 0:c88c3b616c00 62 char tx[2];
IKobayashi 0:c88c3b616c00 63 tx[0]=reg_address;
IKobayashi 0:c88c3b616c00 64 tx[1]=data;
IKobayashi 0:c88c3b616c00 65
IKobayashi 0:c88c3b616c00 66 i2c.write(HMC5883L_I2C_WRITE,tx,2);
IKobayashi 0:c88c3b616c00 67 }
IKobayashi 0:c88c3b616c00 68
IKobayashi 0:c88c3b616c00 69 char HMC5883L::Read(char data)
IKobayashi 0:c88c3b616c00 70 {
IKobayashi 0:c88c3b616c00 71 char tx = data;
IKobayashi 0:c88c3b616c00 72 char rx;
IKobayashi 0:c88c3b616c00 73
IKobayashi 0:c88c3b616c00 74 i2c.write(HMC5883L_I2C_WRITE, &tx, 1);
IKobayashi 0:c88c3b616c00 75 i2c.read(HMC5883L_I2C_READ, &rx, 1);
IKobayashi 0:c88c3b616c00 76 return rx;
IKobayashi 0:c88c3b616c00 77 }
IKobayashi 0:c88c3b616c00 78
IKobayashi 0:c88c3b616c00 79 void HMC5883L::MultiByteRead(char address, char* output, int size)
IKobayashi 0:c88c3b616c00 80 {
IKobayashi 0:c88c3b616c00 81 i2c.write(HMC5883L_I2C_WRITE, &address, 1); //tell it where to read from
IKobayashi 0:c88c3b616c00 82 i2c.read(HMC5883L_I2C_READ, output, size); //tell it where to store the data read
IKobayashi 0:c88c3b616c00 83 }
IKobayashi 0:c88c3b616c00 84
IKobayashi 0:c88c3b616c00 85 float HMC5883L::getMx()
IKobayashi 0:c88c3b616c00 86 {
IKobayashi 0:c88c3b616c00 87 //return (x * m_Scale);
IKobayashi 0:c88c3b616c00 88 char lsb_byte = 0;
IKobayashi 0:c88c3b616c00 89 signed short msb_byte;
IKobayashi 0:c88c3b616c00 90
IKobayashi 0:c88c3b616c00 91 lsb_byte = Read(HMC5883L_X_MSB);
IKobayashi 0:c88c3b616c00 92 msb_byte = lsb_byte << 8;
IKobayashi 0:c88c3b616c00 93 msb_byte |= Read(HMC5883L_X_LSB);
IKobayashi 0:c88c3b616c00 94 return (float)msb_byte;
IKobayashi 0:c88c3b616c00 95 /*
IKobayashi 0:c88c3b616c00 96 char tx[1];
IKobayashi 0:c88c3b616c00 97 char rx[2];
IKobayashi 0:c88c3b616c00 98
IKobayashi 0:c88c3b616c00 99
IKobayashi 0:c88c3b616c00 100 tx[0]=HMC5883L_X_MSB;
IKobayashi 0:c88c3b616c00 101 i2c.write(HMC5883L_I2C_READ,tx,1);
IKobayashi 0:c88c3b616c00 102 i2c.read(HMC5883L_I2C_READ,rx,2);
IKobayashi 0:c88c3b616c00 103 return ((int)rx[0]<<8|(int)rx[1]);
IKobayashi 0:c88c3b616c00 104 */
IKobayashi 0:c88c3b616c00 105
IKobayashi 0:c88c3b616c00 106 }
IKobayashi 0:c88c3b616c00 107
IKobayashi 0:c88c3b616c00 108 float HMC5883L::getMy()
IKobayashi 0:c88c3b616c00 109 {
IKobayashi 0:c88c3b616c00 110 //return (y * m_Scale);
IKobayashi 0:c88c3b616c00 111
IKobayashi 0:c88c3b616c00 112 char lsb_byte = 0;
IKobayashi 0:c88c3b616c00 113 signed short msb_byte;
IKobayashi 0:c88c3b616c00 114
IKobayashi 0:c88c3b616c00 115 lsb_byte = Read(HMC5883L_Y_MSB);
IKobayashi 0:c88c3b616c00 116 msb_byte = lsb_byte << 8;
IKobayashi 0:c88c3b616c00 117 msb_byte |= Read(HMC5883L_Y_LSB);
IKobayashi 0:c88c3b616c00 118 return (float)msb_byte;
IKobayashi 0:c88c3b616c00 119 }
IKobayashi 0:c88c3b616c00 120
IKobayashi 0:c88c3b616c00 121
IKobayashi 0:c88c3b616c00 122 float HMC5883L::getMz()
IKobayashi 0:c88c3b616c00 123 {
IKobayashi 0:c88c3b616c00 124 //return (z * m_Scale);
IKobayashi 0:c88c3b616c00 125
IKobayashi 0:c88c3b616c00 126 char lsb_byte = 0;
IKobayashi 0:c88c3b616c00 127 signed short msb_byte;
IKobayashi 0:c88c3b616c00 128
IKobayashi 0:c88c3b616c00 129 lsb_byte = Read(HMC5883L_Z_MSB);
IKobayashi 0:c88c3b616c00 130 msb_byte = lsb_byte << 8;
IKobayashi 0:c88c3b616c00 131 msb_byte |= Read(HMC5883L_Z_LSB);
IKobayashi 0:c88c3b616c00 132 return (float)msb_byte;
IKobayashi 0:c88c3b616c00 133 }