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