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

Revision:
2:dc991201ff3c
Parent:
1:79e6ae3a23d8
Child:
3:c57454632e8d
--- a/main.cpp	Sat May 30 18:06:37 2015 +0000
+++ b/main.cpp	Sat May 30 19:41:25 2015 +0000
@@ -1,20 +1,23 @@
 #include "mbed.h"
-#define TRUE 1
-#define FALSE 0
-
 //////////////////////////////////////////////////
 // Testing AS5510 sensor with K22F              //
+// test results are sent to  Tera Term          //
+// I2C frequency set @ 400kHz                   //
 //                                              //
-// Test at  400kHz                              //
-// I2C address 0x1C used by FXOS8700CQ          //
+// Note 1)                                      //
+// I2C address 0x1C is used by FXOS8700CQ       //
 // 3-axis accelerometer and 3-axis magetometer  //
+//                                              //
+// Note 2)                                      //
+// Without magnet present the AS5510 sensor is  //
+// expected to output a value of approx 511     //
+// (1023/2= 511                                 //
 //////////////////////////////////////////////////
-Serial pc(USBTX, USBRX);            // tx, rx
-DigitalOut led_red(LED_RED);        //
-DigitalOut led_green(LED_GREEN);    //
-I2C i2c(PTB3, PTB2);                // SDA, SCL (for K22F)
+Serial pc(USBTX, USBRX);                // tx, rx
+I2C i2c(PTB3, PTB2);                    // SDA, SCL (for K22F)
 
-const int i2c_slave_addr1 =  0x56;  // sensor AS5510 number 1 (7 bits), 0x56 or 0x57
+const int i2c_slave_addr1 =  0x56;      // sensor AS5510 number 1 (7 bits), 0x56 or 0x57
+//const int i2c_slave_addr2 =  0x57;    // sensor AS5510 number 2 (7 bits), 0x56 or 0x57
 
 //--- public functions---
 void init_as5510(void);
@@ -25,19 +28,19 @@
 
 int main()
 {
-    int done=0;
+    i2c.frequency(400 * 1000);          // 0.1/0.4/1.0 mHz
+    wait_ms(2);                         // Power Up wait
 
-    i2c.frequency(400 * 1000);          // 0.1/0.4/1.0 mHz
-    wait_ms(2);                         // power up wait
-
+    look_for_hardware_i2c();            // Hardware present ?
+    init_as5510();                      // Initialize
 
-    //look_for_hardware_i2c();          // Harware present ?
-    init_as5510();
     //----------Setup register----------------------
-    while (done != 1)
-        done= offset_comp();            // Only one time required
+    while (!offset_comp());
+    pc.printf("Offset compensation process is completed \r\n");
+
+    //----------Get the results----------------------
     while (true)
-        if (done == 1) read_field();    //----------Read magnetic Field-----------------
+        read_field();    //----------Read magnetic Field-----------------
 }
 
 
@@ -49,7 +52,7 @@
 
     int count = 0;
     for (int address=12; address<256; address+=2) {
-        if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
+        if (!i2c.write(address, NULL, 0)) {         // 0 returned is OK
             pc.printf(" - I2C device found at address 0x%02X\n\r", address >>1);
             count++;
         }
@@ -67,13 +70,19 @@
     pc.printf("Start AS5510 init.. \r\n");
 
     i2c_adrs= (i2c_slave_addr1 << 1);               // AS5510 Slave address lsb= 0 for write
-    //---------------------------------------------------------------------------
+    
+    //---------- Magnet selection --------------------------------
+    //----0x00= <50mT-----------Strong magnet 
+    //----0x01= <25mT
+    //----0x02= <18.7mT
+    //----0x03= <12.5mT---------Weak magnet 
+    //-----------------------------------------------------------
     idata[0]=0x0B;                                  // Register for Sensitivity
-    idata[1]=0x00;                                  // byte
+    idata[1]=0x00;                                  // Byte
     result= i2c.write(i2c_adrs, idata, 2, 0);       // Now write_sensitivity
     if (result != 0) pc.printf("No ACK bit! (09)\n\r");
 
-    //------------------------------------------------------------------------------------------
+    //----------- Operation mode selection------------------------
     idata[0]=0x02;                                  // 0x02 address setup register for operation, speed, polarity
     idata[1]=0x04;                                  // Normal Operation, Slow mode (1), NORMAL Polarity (0), Power Up (0)
     result= i2c.write(i2c_adrs, idata, 2, 0);       // Now write_operation
@@ -102,8 +111,8 @@
 
     // Now analyse register 0x01 ----------------------------------------------
     ocf_done= off_data[1] & 0x08;               // mask off bits, 1= done
-    if (ocf_done== 0)  return(0);
-    else return(1);
+    if (ocf_done== 0)  return(0);               // return(0)= compensation process is pending
+    else return(1);                             // return(1)= compensation process is completed
 }