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@0:de08120b71bd, 2015-05-30 (annotated)
- Committer:
- GerritPathuis
- Date:
- Sat May 30 18:02:46 2015 +0000
- Revision:
- 0:de08120b71bd
- Child:
- 1:79e6ae3a23d8
Best result so far
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 | #define TRUE 1 |
GerritPathuis | 0:de08120b71bd | 3 | #define FALSE 0 |
GerritPathuis | 0:de08120b71bd | 4 | |
GerritPathuis | 0:de08120b71bd | 5 | ////////////////////////////////////////////////// |
GerritPathuis | 0:de08120b71bd | 6 | // Testing AS5510 sensor with K22F // |
GerritPathuis | 0:de08120b71bd | 7 | // // |
GerritPathuis | 0:de08120b71bd | 8 | // Test at 100kHz // |
GerritPathuis | 0:de08120b71bd | 9 | // I2C address 0x1C used by FXOS8700CQ // |
GerritPathuis | 0:de08120b71bd | 10 | // 3-axis accelerometer and 3-axis magetometer // |
GerritPathuis | 0:de08120b71bd | 11 | ////////////////////////////////////////////////// |
GerritPathuis | 0:de08120b71bd | 12 | Serial pc(USBTX, USBRX); // tx, rx |
GerritPathuis | 0:de08120b71bd | 13 | DigitalOut led_red(LED_RED); // |
GerritPathuis | 0:de08120b71bd | 14 | DigitalOut led_green(LED_GREEN); // |
GerritPathuis | 0:de08120b71bd | 15 | I2C i2c(PTB3, PTB2); // SDA, SCL (for K22F) |
GerritPathuis | 0:de08120b71bd | 16 | |
GerritPathuis | 0:de08120b71bd | 17 | const int i2c_slave_addr1 = 0x56; // sensor AS5510 number 1 (7 bits), 0x56 or 0x57 |
GerritPathuis | 0:de08120b71bd | 18 | |
GerritPathuis | 0:de08120b71bd | 19 | //--- public functions--- |
GerritPathuis | 0:de08120b71bd | 20 | void init_as5510(void); |
GerritPathuis | 0:de08120b71bd | 21 | void read_field(void); |
GerritPathuis | 0:de08120b71bd | 22 | int offset_comp(void); |
GerritPathuis | 0:de08120b71bd | 23 | void look_for_hardware_i2c(void); |
GerritPathuis | 0:de08120b71bd | 24 | |
GerritPathuis | 0:de08120b71bd | 25 | |
GerritPathuis | 0:de08120b71bd | 26 | int main() |
GerritPathuis | 0:de08120b71bd | 27 | { |
GerritPathuis | 0:de08120b71bd | 28 | int done=0; |
GerritPathuis | 0:de08120b71bd | 29 | |
GerritPathuis | 0:de08120b71bd | 30 | i2c.frequency(400 * 1000); // 0.1/0.4/1.0 mHz |
GerritPathuis | 0:de08120b71bd | 31 | wait_ms(2); // power up wait |
GerritPathuis | 0:de08120b71bd | 32 | |
GerritPathuis | 0:de08120b71bd | 33 | |
GerritPathuis | 0:de08120b71bd | 34 | //look_for_hardware_i2c(); // Harware present ? |
GerritPathuis | 0:de08120b71bd | 35 | init_as5510(); |
GerritPathuis | 0:de08120b71bd | 36 | //----------Setup register---------------------- |
GerritPathuis | 0:de08120b71bd | 37 | while (done != 1) |
GerritPathuis | 0:de08120b71bd | 38 | done= offset_comp(); // Only one time required |
GerritPathuis | 0:de08120b71bd | 39 | while (true) |
GerritPathuis | 0:de08120b71bd | 40 | if (done == 1) read_field(); //----------Read magnetic Field----------------- |
GerritPathuis | 0:de08120b71bd | 41 | } |
GerritPathuis | 0:de08120b71bd | 42 | |
GerritPathuis | 0:de08120b71bd | 43 | |
GerritPathuis | 0:de08120b71bd | 44 | void look_for_hardware_i2c() |
GerritPathuis | 0:de08120b71bd | 45 | { |
GerritPathuis | 0:de08120b71bd | 46 | pc.printf("\r\n\n\n"); |
GerritPathuis | 0:de08120b71bd | 47 | pc.printf("Note I2C address 0x1C used by FXOS8700CQ 3-axis accelerometer and 3-axis magetometer\r\n"); |
GerritPathuis | 0:de08120b71bd | 48 | pc.printf("Start hardware search..... \r\n"); |
GerritPathuis | 0:de08120b71bd | 49 | |
GerritPathuis | 0:de08120b71bd | 50 | int count = 0; |
GerritPathuis | 0:de08120b71bd | 51 | for (int address=12; address<256; address+=2) { |
GerritPathuis | 0:de08120b71bd | 52 | if (!i2c.write(address, NULL, 0)) { // 0 returned is ok |
GerritPathuis | 0:de08120b71bd | 53 | pc.printf(" - I2C device found at address 0x%02X\n\r", address >>1); |
GerritPathuis | 0:de08120b71bd | 54 | count++; |
GerritPathuis | 0:de08120b71bd | 55 | } |
GerritPathuis | 0:de08120b71bd | 56 | } |
GerritPathuis | 0:de08120b71bd | 57 | pc.printf("%d devices found \n\r", count); |
GerritPathuis | 0:de08120b71bd | 58 | } |
GerritPathuis | 0:de08120b71bd | 59 | |
GerritPathuis | 0:de08120b71bd | 60 | void init_as5510() |
GerritPathuis | 0:de08120b71bd | 61 | { |
GerritPathuis | 0:de08120b71bd | 62 | int i2c_adrs=0; |
GerritPathuis | 0:de08120b71bd | 63 | char idata[2]; |
GerritPathuis | 0:de08120b71bd | 64 | int result=0; |
GerritPathuis | 0:de08120b71bd | 65 | |
GerritPathuis | 0:de08120b71bd | 66 | pc.printf("\r\n"); |
GerritPathuis | 0:de08120b71bd | 67 | pc.printf("Start AS5510 init.. \r\n"); |
GerritPathuis | 0:de08120b71bd | 68 | |
GerritPathuis | 0:de08120b71bd | 69 | i2c_adrs= (i2c_slave_addr1 << 1); // AS5510 Slave address lsb= 0 for write |
GerritPathuis | 0:de08120b71bd | 70 | //--------------------------------------------------------------------------- |
GerritPathuis | 0:de08120b71bd | 71 | idata[0]=0x0B; // Register for Sensitivity |
GerritPathuis | 0:de08120b71bd | 72 | idata[1]=0x00; // byte |
GerritPathuis | 0:de08120b71bd | 73 | result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_sensitivity |
GerritPathuis | 0:de08120b71bd | 74 | if (result != 0) pc.printf("No ACK bit! (09)\n\r"); |
GerritPathuis | 0:de08120b71bd | 75 | |
GerritPathuis | 0:de08120b71bd | 76 | //------------------------------------------------------------------------------------------ |
GerritPathuis | 0:de08120b71bd | 77 | idata[0]=0x02; // 0x02 address setup register for operation, speed, polarity |
GerritPathuis | 0:de08120b71bd | 78 | idata[1]=0x04; // Normal Operation, Slow mode (1), NORMAL Polarity (0), Power Up (0) |
GerritPathuis | 0:de08120b71bd | 79 | result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_operation |
GerritPathuis | 0:de08120b71bd | 80 | if (result != 0) pc.printf("No ACK bit! (11)\n\r"); |
GerritPathuis | 0:de08120b71bd | 81 | |
GerritPathuis | 0:de08120b71bd | 82 | pc.printf("AS5510 init done\r\n"); |
GerritPathuis | 0:de08120b71bd | 83 | } |
GerritPathuis | 0:de08120b71bd | 84 | |
GerritPathuis | 0:de08120b71bd | 85 | |
GerritPathuis | 0:de08120b71bd | 86 | int offset_comp(void) |
GerritPathuis | 0:de08120b71bd | 87 | { |
GerritPathuis | 0:de08120b71bd | 88 | int adrss=0; |
GerritPathuis | 0:de08120b71bd | 89 | int oresult=0; |
GerritPathuis | 0:de08120b71bd | 90 | char off_data[2]; |
GerritPathuis | 0:de08120b71bd | 91 | int ocf_done=0; |
GerritPathuis | 0:de08120b71bd | 92 | |
GerritPathuis | 0:de08120b71bd | 93 | // First, now Write pointer to register 0x00---------------------------- |
GerritPathuis | 0:de08120b71bd | 94 | adrss= (i2c_slave_addr1 << 1); // AS5510 Slave address lsb= 0 for write |
GerritPathuis | 0:de08120b71bd | 95 | oresult= i2c.write(adrss, 0x00, 1, 0); // write one byte |
GerritPathuis | 0:de08120b71bd | 96 | if (oresult != 0) pc.printf("No ACK bit! (33)\n\r"); |
GerritPathuis | 0:de08120b71bd | 97 | |
GerritPathuis | 0:de08120b71bd | 98 | // Second, now Read register 0x00 and 0x01-------------------------------- |
GerritPathuis | 0:de08120b71bd | 99 | memset(off_data, 0, sizeof(off_data)); |
GerritPathuis | 0:de08120b71bd | 100 | adrss= (i2c_slave_addr1 << 1) | 0x01; // AS5510 address lsb= 1 for read |
GerritPathuis | 0:de08120b71bd | 101 | oresult= i2c.read(adrss, off_data, 2, 0); // read two bytes |
GerritPathuis | 0:de08120b71bd | 102 | |
GerritPathuis | 0:de08120b71bd | 103 | // Now analyse register 0x01 ---------------------------------------------- |
GerritPathuis | 0:de08120b71bd | 104 | ocf_done= off_data[1] & 0x08; // mask off bits, 1= done |
GerritPathuis | 0:de08120b71bd | 105 | if (ocf_done== 0) return(0); |
GerritPathuis | 0:de08120b71bd | 106 | else return(1); |
GerritPathuis | 0:de08120b71bd | 107 | } |
GerritPathuis | 0:de08120b71bd | 108 | |
GerritPathuis | 0:de08120b71bd | 109 | |
GerritPathuis | 0:de08120b71bd | 110 | void read_field() |
GerritPathuis | 0:de08120b71bd | 111 | { |
GerritPathuis | 0:de08120b71bd | 112 | int adr=0; |
GerritPathuis | 0:de08120b71bd | 113 | char rx_data[2]; |
GerritPathuis | 0:de08120b71bd | 114 | int rresult=0; |
GerritPathuis | 0:de08120b71bd | 115 | char lsb, msb; |
GerritPathuis | 0:de08120b71bd | 116 | unsigned int value; |
GerritPathuis | 0:de08120b71bd | 117 | |
GerritPathuis | 0:de08120b71bd | 118 | // First, now Write pointer to register 0x00---------------------------- |
GerritPathuis | 0:de08120b71bd | 119 | adr= (i2c_slave_addr1 << 1); // AS5510 address lsb= 0 for write |
GerritPathuis | 0:de08120b71bd | 120 | rresult= i2c.write(adr, 0x00, 1, 0); // write one byte to register 0x00 for magnetic field strength |
GerritPathuis | 0:de08120b71bd | 121 | if (rresult != 0) pc.printf("No ACK bit! (22)\n\r"); |
GerritPathuis | 0:de08120b71bd | 122 | |
GerritPathuis | 0:de08120b71bd | 123 | // Second, now Read register 0x00 and 0x01-------------------------------- |
GerritPathuis | 0:de08120b71bd | 124 | memset(rx_data, 0, sizeof(rx_data)); |
GerritPathuis | 0:de08120b71bd | 125 | adr= (i2c_slave_addr1 << 1) | 0x01; // AS5510 address lsb= 1 for read |
GerritPathuis | 0:de08120b71bd | 126 | rresult= i2c.read(adr, rx_data, 2, 0); // read two bytes |
GerritPathuis | 0:de08120b71bd | 127 | |
GerritPathuis | 0:de08120b71bd | 128 | |
GerritPathuis | 0:de08120b71bd | 129 | // Now analyse register 0x01 ---------------------------------------------- |
GerritPathuis | 0:de08120b71bd | 130 | lsb= rx_data[0]; // get LSB |
GerritPathuis | 0:de08120b71bd | 131 | msb= rx_data[1]&0x03; // need only 2 low bits og MSB |
GerritPathuis | 0:de08120b71bd | 132 | value = ((msb & 0x03)<<8) + lsb; |
GerritPathuis | 0:de08120b71bd | 133 | 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 | 134 | } |
GerritPathuis | 0:de08120b71bd | 135 | |
GerritPathuis | 0:de08120b71bd | 136 |