Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 ////////////////////////////////////////////////// 00003 // Testing AS5510 sensor with K22F // 00004 // test results are sent to Tera Term // 00005 // I2C frequency set @ 400kHz // 00006 // // 00007 // Note 1) // 00008 // I2C address 0x1C is used by FXOS8700CQ // 00009 // 3-axis accelerometer and 3-axis magetometer // 00010 // // 00011 // Note 2) // 00012 // Without magnet present the AS5510 sensor is // 00013 // expected to output a value of approx 511 // 00014 // (1023/2= 511 // 00015 // // 00016 // Note 3) // 00017 // Lateral Movement of a simple 2-pole magnet. // 00018 // Magnet size diameter 1mm length 2mm // 00019 // Distance to sensor approx 0.8mm // 00020 // Output 10bit resolution // 00021 ////////////////////////////////////////////////// 00022 Serial pc(USBTX, USBRX); // tx, rx 00023 I2C i2c(PTB3, PTB2); // SDA, SCL (for K22F) 00024 00025 const int i2c_slave_addr1 = 0x56; // sensor AS5510 number 1 (7 bits), 0x56 or 0x57 00026 const int i2c_slave_addr2 = 0x57; // sensor AS5510 number 2 (7 bits), 0x56 or 0x57 00027 00028 //--- public functions--- 00029 void init_as5510(int); 00030 void read_field(int); 00031 int offset_comp(int); 00032 void look_for_hardware_i2c(void); 00033 00034 00035 int main() 00036 { 00037 i2c.frequency(400 * 1000); // 0.4 mHz 00038 wait_ms(2); // Power Up wait 00039 00040 look_for_hardware_i2c(); // Hardware present ? 00041 init_as5510(i2c_slave_addr1); // Initialize 00042 init_as5510(i2c_slave_addr2); // Initialize 00043 00044 //----------Setup register---------------------- 00045 while (!offset_comp(i2c_slave_addr1)); 00046 while (!offset_comp(i2c_slave_addr2)); 00047 pc.printf("Offset compensation process is completed \r\n"); 00048 00049 //----------Get the results---------------------- 00050 while (true) { 00051 read_field(i2c_slave_addr1); // Read magnetic Field sensor #1 00052 read_field(i2c_slave_addr2); // Read magnetic Field sensor #2 00053 } 00054 } 00055 00056 00057 void look_for_hardware_i2c() 00058 { 00059 pc.printf("\r\n\n\n"); 00060 pc.printf("Note I2C address 0x1C used by FXOS8700CQ 3-axis accelerometer and 3-axis magetometer\r\n"); 00061 pc.printf("Start hardware search..... \r\n"); 00062 00063 int count = 0; 00064 for (int address=12; address<256; address+=2) { 00065 if (!i2c.write(address, NULL, 0)) { // 0 returned is OK 00066 pc.printf(" - I2C device found at address 0x%02X\n\r", address >>1); 00067 count++; 00068 } 00069 } 00070 pc.printf("%d devices found \n\r", count); 00071 } 00072 00073 void init_as5510(int i2c_address) 00074 { 00075 int i2c_adrs=0; 00076 char idata[2]; 00077 int result=0; 00078 00079 pc.printf("\r\n"); 00080 pc.printf("Start AS5510 init.. \r\n"); 00081 00082 i2c_adrs= (i2c_address << 1); // AS5510 Slave address lsb= 0 for write 00083 00084 //---------- Magnet selection -------------------------------- 00085 //----0x00= <50mT-----------Strong magnet 00086 //----0x01= <25mT 00087 //----0x02= <18.7mT 00088 //----0x03= <12.5mT---------Weak magnet 00089 //----------------------------------------------------------- 00090 idata[0]=0x0B; // Register for Sensitivity 00091 idata[1]=0x00; // Byte 00092 result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_sensitivity 00093 if (result != 0) pc.printf("No ACK bit! (09)\n\r"); 00094 00095 //----------- Operation mode selection------------------------ 00096 idata[0]=0x02; // 0x02 address setup register for operation, speed, polarity 00097 idata[1]=0x04; // Normal Operation, Slow mode (1), NORMAL Polarity (0), Power Up (0) 00098 result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_operation 00099 if (result != 0) pc.printf("No ACK bit! (11)\n\r"); 00100 00101 pc.printf("AS5510 init done\r\n"); 00102 } 00103 00104 00105 int offset_comp(int i2c_address) 00106 { 00107 int adrss=0; 00108 int oresult=0; 00109 char off_data[2]; 00110 int ocf_done=0; 00111 00112 // First, now Write pointer to register 0x00---------------------------- 00113 adrss= (i2c_address << 1); // AS5510 Slave address lsb= 0 for write 00114 oresult= i2c.write(adrss, 0x00, 1, 0); // write one byte 00115 if (oresult != 0) pc.printf("No ACK bit! (33)\n\r"); 00116 00117 // Second, now Read register 0x00 and 0x01-------------------------------- 00118 memset(off_data, 0, sizeof(off_data)); 00119 adrss= (i2c_address << 1) | 0x01; // AS5510 address lsb= 1 for read 00120 oresult= i2c.read(adrss, off_data, 2, 0); // read two bytes 00121 00122 // Now analyse register 0x01 ---------------------------------------------- 00123 ocf_done= off_data[1] & 0x08; // mask off bits, 1= done 00124 if (ocf_done== 0) return(0); // return(0)= compensation process is pending 00125 else return(1); // return(1)= compensation process is completed 00126 } 00127 00128 00129 void read_field(int i2c_address) 00130 { 00131 int adr=0; 00132 char rx_data[2]; 00133 int rresult=0; 00134 char lsb, msb; 00135 unsigned int value; 00136 00137 // First, now Write pointer to register 0x00---------------------------- 00138 adr= (i2c_address << 1); // AS5510 address lsb= 0 for write 00139 rresult= i2c.write(adr, 0x00, 1, 0); // write one byte to register 0x00 for magnetic field strength 00140 if (rresult != 0) pc.printf("No ACK bit! (22)\n\r"); 00141 00142 // Second, now Read register 0x00 and 0x01-------------------------------- 00143 memset(rx_data, 0, sizeof(rx_data)); 00144 adr= (i2c_address << 1) | 0x01; // AS5510 address lsb= 1 for read 00145 rresult= i2c.read(adr, rx_data, 2, 0); // read two bytes 00146 00147 00148 // Now analyse register 0x01 ---------------------------------------------- 00149 lsb= rx_data[0]; // get LSB 00150 msb= rx_data[1]&0x03; // need only 2 low bits og MSB 00151 value = ((msb & 0x03)<<8) + lsb; 00152 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); 00153 } 00154 00155
Generated on Sat Jul 16 2022 09:10:43 by
1.7.2