I2C example

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 
00023 Serial pc(USBTX, USBRX);                // tx, rx
00024 I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
00025 AnalogOut dac(PA_4);
00026 unsigned int value;
00027 
00028 const int i2c_slave_addr1 =  0x56;    // sensor AS5510 number 1 (7 bits), 0x56 or 0x57
00029 DigitalOut check(PC_3);
00030 
00031 //--- public functions---
00032 void init_as5510(int);
00033 void read_field(int);
00034 int offset_comp(int);
00035 void look_for_hardware_i2c(void);
00036 
00037 
00038 int main()
00039 {
00040     i2c.frequency(400 * 1000);          // 0.4 mHz
00041     wait_ms(2);                         // Power Up wait
00042 
00043     look_for_hardware_i2c();            // Hardware present ?
00044     init_as5510(i2c_slave_addr1);       // Initialize
00045 //    //----------Setup register----------------------
00046 //    while (!offset_comp(i2c_slave_addr1));
00047 //    while (!offset_comp(i2c_slave_addr2));
00048 //    pc.printf("Offset compensation process is completed \r\n");
00049 
00050     //----------Get the results----------------------
00051 //    flipper.attach(&flip, 0.0002f);
00052     while (true) {
00053         check=1;
00054         read_field(i2c_slave_addr1); // Read magnetic Field sensor #1
00055         check=0;     
00056         wait(0.005f);
00057         dac=(float) value/1024.0;
00058         pc.printf("%d\n",value);
00059     }
00060 }
00061 
00062 
00063 void look_for_hardware_i2c()
00064 {
00065     pc.printf("\r\n\n\n");
00066     pc.printf("Note I2C address 0x1C used by FXOS8700CQ 3-axis accelerometer and 3-axis magetometer\r\n");
00067     pc.printf("Start hardware search..... \r\n");
00068 
00069     int count = 0;
00070     for (int address=12; address<256; address+=2) {
00071         if (!i2c.write(address, NULL, 0)) {         // 0 returned is OK
00072             pc.printf(" - I2C device found at address 0x%02X\n\r", address >>1);
00073             count++;
00074         }
00075     }
00076     pc.printf("%d devices found \n\r", count);
00077 }
00078 
00079 void init_as5510(int i2c_address)
00080 {
00081     int i2c_adrs=0;
00082     char idata[2];
00083     int result=0;
00084 
00085     pc.printf("\r\n");
00086     pc.printf("Start AS5510 init.. \r\n");
00087 
00088     i2c_adrs= (i2c_address << 1);                   // AS5510 Slave address lsb= 0 for write
00089 
00090     //---------- Magnet selection --------------------------------
00091     //----0x00= <50mT-----------Strong magnet
00092     //----0x01= <25mT
00093     //----0x02= <18.7mT
00094     //----0x03= <12.5mT---------Weak magnet
00095     //-----------------------------------------------------------
00096     idata[0]=0x0B;                                  // Register for Sensitivity
00097     idata[1]=0x00;                                  // Byte
00098     result= i2c.write(i2c_adrs, idata, 2, 0);       // Now write_sensitivity
00099     if (result != 0) pc.printf("No ACK bit! (09)\n\r");
00100 
00101     //----------- Operation mode selection------------------------
00102     idata[0]=0x02;                                  // 0x02 address setup register for operation, speed, polarity
00103     idata[1]=0x04;                                  // Normal Operation, Slow mode (1), NORMAL Polarity (0), Power Up (0)
00104     result= i2c.write(i2c_adrs, idata, 2, 0);       // Now write_operation
00105     if (result != 0) pc.printf("No ACK bit! (11)\n\r");
00106 
00107     pc.printf("AS5510 init done\r\n");
00108 }
00109 
00110 
00111 int offset_comp(int i2c_address)
00112 {
00113     int adrss=0;
00114     int oresult=0;
00115     char off_data[2];
00116     int ocf_done=0;
00117 
00118     // First, now Write pointer to register 0x00----------------------------
00119     adrss= (i2c_address << 1);                  // AS5510 Slave address lsb= 0 for write
00120     oresult= i2c.write(adrss, 0x00, 1, 0);      // write one byte
00121     if (oresult != 0) pc.printf("No ACK bit! (33)\n\r");
00122 
00123     // Second, now Read register 0x00 and 0x01--------------------------------
00124     memset(off_data, 0, sizeof(off_data));
00125     adrss= (i2c_address << 1) | 0x01;           // AS5510 address lsb= 1 for read
00126     oresult= i2c.read(adrss, off_data, 2, 0);   // read two bytes
00127 
00128     // Now analyse register 0x01 ----------------------------------------------
00129     ocf_done= off_data[1] & 0x08;               // mask off bits, 1= done
00130     if (ocf_done== 0)  return(0);               // return(0)= compensation process is pending
00131     else return(1);                             // return(1)= compensation process is completed
00132 }
00133 
00134 
00135 void read_field(int i2c_address)
00136 {
00137     int adr=0;
00138     char rx_data[2];
00139     int rresult=0;
00140     char lsb, msb;
00141 
00142     // First, now Write pointer to register 0x00----------------------------
00143     adr= (i2c_address << 1);                        // AS5510 address lsb= 0 for write
00144     rresult= i2c.write(adr, 0x00, 1, 0);            // write one byte to register 0x00 for magnetic field strength
00145 //    if (rresult != 0) pc.printf("No ACK bit! (22)\n\r");
00146 
00147     // Second, now Read register 0x00 and 0x01--------------------------------
00148 //    memset(rx_data, 0, sizeof(rx_data));
00149     adr= (i2c_address << 1) | 0x01;                 // AS5510 address lsb= 1 for read
00150     rresult= i2c.read(adr, rx_data, 2, 0);          // read two bytes
00151 
00152 
00153     // Now analyse register 0x01 ----------------------------------------------
00154     lsb= rx_data[0];                                // get LSB
00155     msb= rx_data[1]&0x03;                           // need only 2 low bits og MSB
00156     value = ((msb & 0x03)<<8) + lsb;
00157 //    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);
00158 }
00159 
00160