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.

Dependencies:   mbed

Committer:
GerritPathuis
Date:
Sun May 31 09:18:11 2015 +0000
Revision:
4:536bd8cce754
Parent:
3:c57454632e8d
Child:
5:cec4c7caf8b7
Functions declarations changed

Who changed what in which revision?

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