AS5510, 10 bit Absolute linear Hall Field Sensor with I2C output. Lateral movement measurement for simple 2-pole magnet. Magnet diameter 1mm, length 2mm. Output 10 bit resolution, distance per LSB is 2.0mm/1024, ADC sampling frequency 50 kHz.
main.cpp@2:dc991201ff3c, 2015-05-30 (annotated)
- Committer:
- GerritPathuis
- Date:
- Sat May 30 19:41:25 2015 +0000
- Revision:
- 2:dc991201ff3c
- Parent:
- 1:79e6ae3a23d8
- Child:
- 3:c57454632e8d
Fist issue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GerritPathuis | 0:de08120b71bd | 1 | #include "mbed.h" |
GerritPathuis | 0:de08120b71bd | 2 | ////////////////////////////////////////////////// |
GerritPathuis | 0:de08120b71bd | 3 | // Testing AS5510 sensor with K22F // |
GerritPathuis | 2:dc991201ff3c | 4 | // test results are sent to Tera Term // |
GerritPathuis | 2:dc991201ff3c | 5 | // I2C frequency set @ 400kHz // |
GerritPathuis | 0:de08120b71bd | 6 | // // |
GerritPathuis | 2:dc991201ff3c | 7 | // Note 1) // |
GerritPathuis | 2:dc991201ff3c | 8 | // I2C address 0x1C is used by FXOS8700CQ // |
GerritPathuis | 0:de08120b71bd | 9 | // 3-axis accelerometer and 3-axis magetometer // |
GerritPathuis | 2:dc991201ff3c | 10 | // // |
GerritPathuis | 2:dc991201ff3c | 11 | // Note 2) // |
GerritPathuis | 2:dc991201ff3c | 12 | // Without magnet present the AS5510 sensor is // |
GerritPathuis | 2:dc991201ff3c | 13 | // expected to output a value of approx 511 // |
GerritPathuis | 2:dc991201ff3c | 14 | // (1023/2= 511 // |
GerritPathuis | 0:de08120b71bd | 15 | ////////////////////////////////////////////////// |
GerritPathuis | 2:dc991201ff3c | 16 | Serial pc(USBTX, USBRX); // tx, rx |
GerritPathuis | 2:dc991201ff3c | 17 | I2C i2c(PTB3, PTB2); // SDA, SCL (for K22F) |
GerritPathuis | 0:de08120b71bd | 18 | |
GerritPathuis | 2:dc991201ff3c | 19 | const int i2c_slave_addr1 = 0x56; // sensor AS5510 number 1 (7 bits), 0x56 or 0x57 |
GerritPathuis | 2:dc991201ff3c | 20 | //const int i2c_slave_addr2 = 0x57; // sensor AS5510 number 2 (7 bits), 0x56 or 0x57 |
GerritPathuis | 0:de08120b71bd | 21 | |
GerritPathuis | 0:de08120b71bd | 22 | //--- public functions--- |
GerritPathuis | 0:de08120b71bd | 23 | void init_as5510(void); |
GerritPathuis | 0:de08120b71bd | 24 | void read_field(void); |
GerritPathuis | 0:de08120b71bd | 25 | int offset_comp(void); |
GerritPathuis | 0:de08120b71bd | 26 | void look_for_hardware_i2c(void); |
GerritPathuis | 0:de08120b71bd | 27 | |
GerritPathuis | 0:de08120b71bd | 28 | |
GerritPathuis | 0:de08120b71bd | 29 | int main() |
GerritPathuis | 0:de08120b71bd | 30 | { |
GerritPathuis | 2:dc991201ff3c | 31 | i2c.frequency(400 * 1000); // 0.1/0.4/1.0 mHz |
GerritPathuis | 2:dc991201ff3c | 32 | wait_ms(2); // Power Up wait |
GerritPathuis | 0:de08120b71bd | 33 | |
GerritPathuis | 2:dc991201ff3c | 34 | look_for_hardware_i2c(); // Hardware present ? |
GerritPathuis | 2:dc991201ff3c | 35 | init_as5510(); // Initialize |
GerritPathuis | 0:de08120b71bd | 36 | |
GerritPathuis | 0:de08120b71bd | 37 | //----------Setup register---------------------- |
GerritPathuis | 2:dc991201ff3c | 38 | while (!offset_comp()); |
GerritPathuis | 2:dc991201ff3c | 39 | pc.printf("Offset compensation process is completed \r\n"); |
GerritPathuis | 2:dc991201ff3c | 40 | |
GerritPathuis | 2:dc991201ff3c | 41 | //----------Get the results---------------------- |
GerritPathuis | 0:de08120b71bd | 42 | while (true) |
GerritPathuis | 2:dc991201ff3c | 43 | read_field(); //----------Read magnetic Field----------------- |
GerritPathuis | 0:de08120b71bd | 44 | } |
GerritPathuis | 0:de08120b71bd | 45 | |
GerritPathuis | 0:de08120b71bd | 46 | |
GerritPathuis | 0:de08120b71bd | 47 | void look_for_hardware_i2c() |
GerritPathuis | 0:de08120b71bd | 48 | { |
GerritPathuis | 0:de08120b71bd | 49 | pc.printf("\r\n\n\n"); |
GerritPathuis | 0:de08120b71bd | 50 | pc.printf("Note I2C address 0x1C used by FXOS8700CQ 3-axis accelerometer and 3-axis magetometer\r\n"); |
GerritPathuis | 0:de08120b71bd | 51 | pc.printf("Start hardware search..... \r\n"); |
GerritPathuis | 0:de08120b71bd | 52 | |
GerritPathuis | 0:de08120b71bd | 53 | int count = 0; |
GerritPathuis | 0:de08120b71bd | 54 | for (int address=12; address<256; address+=2) { |
GerritPathuis | 2:dc991201ff3c | 55 | if (!i2c.write(address, NULL, 0)) { // 0 returned is OK |
GerritPathuis | 0:de08120b71bd | 56 | pc.printf(" - I2C device found at address 0x%02X\n\r", address >>1); |
GerritPathuis | 0:de08120b71bd | 57 | count++; |
GerritPathuis | 0:de08120b71bd | 58 | } |
GerritPathuis | 0:de08120b71bd | 59 | } |
GerritPathuis | 0:de08120b71bd | 60 | pc.printf("%d devices found \n\r", count); |
GerritPathuis | 0:de08120b71bd | 61 | } |
GerritPathuis | 0:de08120b71bd | 62 | |
GerritPathuis | 0:de08120b71bd | 63 | void init_as5510() |
GerritPathuis | 0:de08120b71bd | 64 | { |
GerritPathuis | 0:de08120b71bd | 65 | int i2c_adrs=0; |
GerritPathuis | 0:de08120b71bd | 66 | char idata[2]; |
GerritPathuis | 0:de08120b71bd | 67 | int result=0; |
GerritPathuis | 0:de08120b71bd | 68 | |
GerritPathuis | 0:de08120b71bd | 69 | pc.printf("\r\n"); |
GerritPathuis | 0:de08120b71bd | 70 | pc.printf("Start AS5510 init.. \r\n"); |
GerritPathuis | 0:de08120b71bd | 71 | |
GerritPathuis | 0:de08120b71bd | 72 | i2c_adrs= (i2c_slave_addr1 << 1); // AS5510 Slave address lsb= 0 for write |
GerritPathuis | 2:dc991201ff3c | 73 | |
GerritPathuis | 2:dc991201ff3c | 74 | //---------- Magnet selection -------------------------------- |
GerritPathuis | 2:dc991201ff3c | 75 | //----0x00= <50mT-----------Strong magnet |
GerritPathuis | 2:dc991201ff3c | 76 | //----0x01= <25mT |
GerritPathuis | 2:dc991201ff3c | 77 | //----0x02= <18.7mT |
GerritPathuis | 2:dc991201ff3c | 78 | //----0x03= <12.5mT---------Weak magnet |
GerritPathuis | 2:dc991201ff3c | 79 | //----------------------------------------------------------- |
GerritPathuis | 0:de08120b71bd | 80 | idata[0]=0x0B; // Register for Sensitivity |
GerritPathuis | 2:dc991201ff3c | 81 | idata[1]=0x00; // Byte |
GerritPathuis | 0:de08120b71bd | 82 | result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_sensitivity |
GerritPathuis | 0:de08120b71bd | 83 | if (result != 0) pc.printf("No ACK bit! (09)\n\r"); |
GerritPathuis | 0:de08120b71bd | 84 | |
GerritPathuis | 2:dc991201ff3c | 85 | //----------- Operation mode selection------------------------ |
GerritPathuis | 0:de08120b71bd | 86 | idata[0]=0x02; // 0x02 address setup register for operation, speed, polarity |
GerritPathuis | 0:de08120b71bd | 87 | idata[1]=0x04; // Normal Operation, Slow mode (1), NORMAL Polarity (0), Power Up (0) |
GerritPathuis | 0:de08120b71bd | 88 | result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_operation |
GerritPathuis | 0:de08120b71bd | 89 | if (result != 0) pc.printf("No ACK bit! (11)\n\r"); |
GerritPathuis | 0:de08120b71bd | 90 | |
GerritPathuis | 0:de08120b71bd | 91 | pc.printf("AS5510 init done\r\n"); |
GerritPathuis | 0:de08120b71bd | 92 | } |
GerritPathuis | 0:de08120b71bd | 93 | |
GerritPathuis | 0:de08120b71bd | 94 | |
GerritPathuis | 0:de08120b71bd | 95 | int offset_comp(void) |
GerritPathuis | 0:de08120b71bd | 96 | { |
GerritPathuis | 0:de08120b71bd | 97 | int adrss=0; |
GerritPathuis | 0:de08120b71bd | 98 | int oresult=0; |
GerritPathuis | 0:de08120b71bd | 99 | char off_data[2]; |
GerritPathuis | 0:de08120b71bd | 100 | int ocf_done=0; |
GerritPathuis | 0:de08120b71bd | 101 | |
GerritPathuis | 0:de08120b71bd | 102 | // First, now Write pointer to register 0x00---------------------------- |
GerritPathuis | 0:de08120b71bd | 103 | adrss= (i2c_slave_addr1 << 1); // AS5510 Slave address lsb= 0 for write |
GerritPathuis | 0:de08120b71bd | 104 | oresult= i2c.write(adrss, 0x00, 1, 0); // write one byte |
GerritPathuis | 0:de08120b71bd | 105 | if (oresult != 0) pc.printf("No ACK bit! (33)\n\r"); |
GerritPathuis | 0:de08120b71bd | 106 | |
GerritPathuis | 0:de08120b71bd | 107 | // Second, now Read register 0x00 and 0x01-------------------------------- |
GerritPathuis | 0:de08120b71bd | 108 | memset(off_data, 0, sizeof(off_data)); |
GerritPathuis | 0:de08120b71bd | 109 | adrss= (i2c_slave_addr1 << 1) | 0x01; // AS5510 address lsb= 1 for read |
GerritPathuis | 0:de08120b71bd | 110 | oresult= i2c.read(adrss, off_data, 2, 0); // read two bytes |
GerritPathuis | 0:de08120b71bd | 111 | |
GerritPathuis | 0:de08120b71bd | 112 | // Now analyse register 0x01 ---------------------------------------------- |
GerritPathuis | 0:de08120b71bd | 113 | ocf_done= off_data[1] & 0x08; // mask off bits, 1= done |
GerritPathuis | 2:dc991201ff3c | 114 | if (ocf_done== 0) return(0); // return(0)= compensation process is pending |
GerritPathuis | 2:dc991201ff3c | 115 | else return(1); // return(1)= compensation process is completed |
GerritPathuis | 0:de08120b71bd | 116 | } |
GerritPathuis | 0:de08120b71bd | 117 | |
GerritPathuis | 0:de08120b71bd | 118 | |
GerritPathuis | 0:de08120b71bd | 119 | void read_field() |
GerritPathuis | 0:de08120b71bd | 120 | { |
GerritPathuis | 0:de08120b71bd | 121 | int adr=0; |
GerritPathuis | 0:de08120b71bd | 122 | char rx_data[2]; |
GerritPathuis | 0:de08120b71bd | 123 | int rresult=0; |
GerritPathuis | 0:de08120b71bd | 124 | char lsb, msb; |
GerritPathuis | 0:de08120b71bd | 125 | unsigned int value; |
GerritPathuis | 0:de08120b71bd | 126 | |
GerritPathuis | 0:de08120b71bd | 127 | // First, now Write pointer to register 0x00---------------------------- |
GerritPathuis | 0:de08120b71bd | 128 | adr= (i2c_slave_addr1 << 1); // AS5510 address lsb= 0 for write |
GerritPathuis | 0:de08120b71bd | 129 | rresult= i2c.write(adr, 0x00, 1, 0); // write one byte to register 0x00 for magnetic field strength |
GerritPathuis | 0:de08120b71bd | 130 | if (rresult != 0) pc.printf("No ACK bit! (22)\n\r"); |
GerritPathuis | 0:de08120b71bd | 131 | |
GerritPathuis | 0:de08120b71bd | 132 | // Second, now Read register 0x00 and 0x01-------------------------------- |
GerritPathuis | 0:de08120b71bd | 133 | memset(rx_data, 0, sizeof(rx_data)); |
GerritPathuis | 0:de08120b71bd | 134 | adr= (i2c_slave_addr1 << 1) | 0x01; // AS5510 address lsb= 1 for read |
GerritPathuis | 0:de08120b71bd | 135 | rresult= i2c.read(adr, rx_data, 2, 0); // read two bytes |
GerritPathuis | 0:de08120b71bd | 136 | |
GerritPathuis | 0:de08120b71bd | 137 | |
GerritPathuis | 0:de08120b71bd | 138 | // Now analyse register 0x01 ---------------------------------------------- |
GerritPathuis | 0:de08120b71bd | 139 | lsb= rx_data[0]; // get LSB |
GerritPathuis | 0:de08120b71bd | 140 | msb= rx_data[1]&0x03; // need only 2 low bits og MSB |
GerritPathuis | 0:de08120b71bd | 141 | value = ((msb & 0x03)<<8) + lsb; |
GerritPathuis | 0:de08120b71bd | 142 | pc.printf("Magnetic Field => msb= 0x%02X, lsb= 0x%02X, decimal 10-bit value = %u \r\n ", rx_data[0],rx_data[1], value); |
GerritPathuis | 0:de08120b71bd | 143 | } |
GerritPathuis | 0:de08120b71bd | 144 | |
GerritPathuis | 0:de08120b71bd | 145 |