Gerrit Pathuis / Mbed 2 deprecated K22F_AS5510_I2C_tst

Dependencies:   mbed

Committer:
GerritPathuis
Date:
Sat May 30 19:47:00 2015 +0000
Revision:
3:c57454632e8d
Parent:
2:dc991201ff3c
Child:
4:536bd8cce754
First Issue Notes added

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 2:dc991201ff3c 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 0:de08120b71bd 29 void init_as5510(void);
GerritPathuis 0:de08120b71bd 30 void read_field(void);
GerritPathuis 0:de08120b71bd 31 int offset_comp(void);
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 2:dc991201ff3c 37 i2c.frequency(400 * 1000); // 0.1/0.4/1.0 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 2:dc991201ff3c 41 init_as5510(); // Initialize
GerritPathuis 0:de08120b71bd 42
GerritPathuis 0:de08120b71bd 43 //----------Setup register----------------------
GerritPathuis 2:dc991201ff3c 44 while (!offset_comp());
GerritPathuis 2:dc991201ff3c 45 pc.printf("Offset compensation process is completed \r\n");
GerritPathuis 2:dc991201ff3c 46
GerritPathuis 2:dc991201ff3c 47 //----------Get the results----------------------
GerritPathuis 0:de08120b71bd 48 while (true)
GerritPathuis 2:dc991201ff3c 49 read_field(); //----------Read magnetic Field-----------------
GerritPathuis 0:de08120b71bd 50 }
GerritPathuis 0:de08120b71bd 51
GerritPathuis 0:de08120b71bd 52
GerritPathuis 0:de08120b71bd 53 void look_for_hardware_i2c()
GerritPathuis 0:de08120b71bd 54 {
GerritPathuis 0:de08120b71bd 55 pc.printf("\r\n\n\n");
GerritPathuis 0:de08120b71bd 56 pc.printf("Note I2C address 0x1C used by FXOS8700CQ 3-axis accelerometer and 3-axis magetometer\r\n");
GerritPathuis 0:de08120b71bd 57 pc.printf("Start hardware search..... \r\n");
GerritPathuis 0:de08120b71bd 58
GerritPathuis 0:de08120b71bd 59 int count = 0;
GerritPathuis 0:de08120b71bd 60 for (int address=12; address<256; address+=2) {
GerritPathuis 2:dc991201ff3c 61 if (!i2c.write(address, NULL, 0)) { // 0 returned is OK
GerritPathuis 0:de08120b71bd 62 pc.printf(" - I2C device found at address 0x%02X\n\r", address >>1);
GerritPathuis 0:de08120b71bd 63 count++;
GerritPathuis 0:de08120b71bd 64 }
GerritPathuis 0:de08120b71bd 65 }
GerritPathuis 0:de08120b71bd 66 pc.printf("%d devices found \n\r", count);
GerritPathuis 0:de08120b71bd 67 }
GerritPathuis 0:de08120b71bd 68
GerritPathuis 0:de08120b71bd 69 void init_as5510()
GerritPathuis 0:de08120b71bd 70 {
GerritPathuis 0:de08120b71bd 71 int i2c_adrs=0;
GerritPathuis 0:de08120b71bd 72 char idata[2];
GerritPathuis 0:de08120b71bd 73 int result=0;
GerritPathuis 0:de08120b71bd 74
GerritPathuis 0:de08120b71bd 75 pc.printf("\r\n");
GerritPathuis 0:de08120b71bd 76 pc.printf("Start AS5510 init.. \r\n");
GerritPathuis 0:de08120b71bd 77
GerritPathuis 0:de08120b71bd 78 i2c_adrs= (i2c_slave_addr1 << 1); // AS5510 Slave address lsb= 0 for write
GerritPathuis 2:dc991201ff3c 79
GerritPathuis 2:dc991201ff3c 80 //---------- Magnet selection --------------------------------
GerritPathuis 2:dc991201ff3c 81 //----0x00= <50mT-----------Strong magnet
GerritPathuis 2:dc991201ff3c 82 //----0x01= <25mT
GerritPathuis 2:dc991201ff3c 83 //----0x02= <18.7mT
GerritPathuis 2:dc991201ff3c 84 //----0x03= <12.5mT---------Weak magnet
GerritPathuis 2:dc991201ff3c 85 //-----------------------------------------------------------
GerritPathuis 0:de08120b71bd 86 idata[0]=0x0B; // Register for Sensitivity
GerritPathuis 2:dc991201ff3c 87 idata[1]=0x00; // Byte
GerritPathuis 0:de08120b71bd 88 result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_sensitivity
GerritPathuis 0:de08120b71bd 89 if (result != 0) pc.printf("No ACK bit! (09)\n\r");
GerritPathuis 0:de08120b71bd 90
GerritPathuis 2:dc991201ff3c 91 //----------- Operation mode selection------------------------
GerritPathuis 0:de08120b71bd 92 idata[0]=0x02; // 0x02 address setup register for operation, speed, polarity
GerritPathuis 0:de08120b71bd 93 idata[1]=0x04; // Normal Operation, Slow mode (1), NORMAL Polarity (0), Power Up (0)
GerritPathuis 0:de08120b71bd 94 result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_operation
GerritPathuis 0:de08120b71bd 95 if (result != 0) pc.printf("No ACK bit! (11)\n\r");
GerritPathuis 0:de08120b71bd 96
GerritPathuis 0:de08120b71bd 97 pc.printf("AS5510 init done\r\n");
GerritPathuis 0:de08120b71bd 98 }
GerritPathuis 0:de08120b71bd 99
GerritPathuis 0:de08120b71bd 100
GerritPathuis 0:de08120b71bd 101 int offset_comp(void)
GerritPathuis 0:de08120b71bd 102 {
GerritPathuis 0:de08120b71bd 103 int adrss=0;
GerritPathuis 0:de08120b71bd 104 int oresult=0;
GerritPathuis 0:de08120b71bd 105 char off_data[2];
GerritPathuis 0:de08120b71bd 106 int ocf_done=0;
GerritPathuis 0:de08120b71bd 107
GerritPathuis 0:de08120b71bd 108 // First, now Write pointer to register 0x00----------------------------
GerritPathuis 0:de08120b71bd 109 adrss= (i2c_slave_addr1 << 1); // AS5510 Slave address lsb= 0 for write
GerritPathuis 0:de08120b71bd 110 oresult= i2c.write(adrss, 0x00, 1, 0); // write one byte
GerritPathuis 0:de08120b71bd 111 if (oresult != 0) pc.printf("No ACK bit! (33)\n\r");
GerritPathuis 0:de08120b71bd 112
GerritPathuis 0:de08120b71bd 113 // Second, now Read register 0x00 and 0x01--------------------------------
GerritPathuis 0:de08120b71bd 114 memset(off_data, 0, sizeof(off_data));
GerritPathuis 0:de08120b71bd 115 adrss= (i2c_slave_addr1 << 1) | 0x01; // AS5510 address lsb= 1 for read
GerritPathuis 0:de08120b71bd 116 oresult= i2c.read(adrss, off_data, 2, 0); // read two bytes
GerritPathuis 0:de08120b71bd 117
GerritPathuis 0:de08120b71bd 118 // Now analyse register 0x01 ----------------------------------------------
GerritPathuis 0:de08120b71bd 119 ocf_done= off_data[1] & 0x08; // mask off bits, 1= done
GerritPathuis 2:dc991201ff3c 120 if (ocf_done== 0) return(0); // return(0)= compensation process is pending
GerritPathuis 2:dc991201ff3c 121 else return(1); // return(1)= compensation process is completed
GerritPathuis 0:de08120b71bd 122 }
GerritPathuis 0:de08120b71bd 123
GerritPathuis 0:de08120b71bd 124
GerritPathuis 0:de08120b71bd 125 void read_field()
GerritPathuis 0:de08120b71bd 126 {
GerritPathuis 0:de08120b71bd 127 int adr=0;
GerritPathuis 0:de08120b71bd 128 char rx_data[2];
GerritPathuis 0:de08120b71bd 129 int rresult=0;
GerritPathuis 0:de08120b71bd 130 char lsb, msb;
GerritPathuis 0:de08120b71bd 131 unsigned int value;
GerritPathuis 0:de08120b71bd 132
GerritPathuis 0:de08120b71bd 133 // First, now Write pointer to register 0x00----------------------------
GerritPathuis 0:de08120b71bd 134 adr= (i2c_slave_addr1 << 1); // AS5510 address lsb= 0 for write
GerritPathuis 0:de08120b71bd 135 rresult= i2c.write(adr, 0x00, 1, 0); // write one byte to register 0x00 for magnetic field strength
GerritPathuis 0:de08120b71bd 136 if (rresult != 0) pc.printf("No ACK bit! (22)\n\r");
GerritPathuis 0:de08120b71bd 137
GerritPathuis 0:de08120b71bd 138 // Second, now Read register 0x00 and 0x01--------------------------------
GerritPathuis 0:de08120b71bd 139 memset(rx_data, 0, sizeof(rx_data));
GerritPathuis 0:de08120b71bd 140 adr= (i2c_slave_addr1 << 1) | 0x01; // AS5510 address lsb= 1 for read
GerritPathuis 0:de08120b71bd 141 rresult= i2c.read(adr, rx_data, 2, 0); // read two bytes
GerritPathuis 0:de08120b71bd 142
GerritPathuis 0:de08120b71bd 143
GerritPathuis 0:de08120b71bd 144 // Now analyse register 0x01 ----------------------------------------------
GerritPathuis 0:de08120b71bd 145 lsb= rx_data[0]; // get LSB
GerritPathuis 0:de08120b71bd 146 msb= rx_data[1]&0x03; // need only 2 low bits og MSB
GerritPathuis 0:de08120b71bd 147 value = ((msb & 0x03)<<8) + lsb;
GerritPathuis 0:de08120b71bd 148 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 149 }
GerritPathuis 0:de08120b71bd 150
GerritPathuis 0:de08120b71bd 151