For LIGHT

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Tue Sep 03 11:59:55 2019 +0000
Revision:
17:1865016ca2e7
Parent:
0:51c43836c1d7
190903

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GiJeongKim 0:51c43836c1d7 1 #include "mbed.h"
GiJeongKim 0:51c43836c1d7 2 #include "setting.h"
GiJeongKim 0:51c43836c1d7 3
GiJeongKim 0:51c43836c1d7 4 void look_for_hardware_i2c()
GiJeongKim 0:51c43836c1d7 5 {
Lightvalve 17:1865016ca2e7 6 //pc.printf("\r\n\n\n");
Lightvalve 17:1865016ca2e7 7 //pc.printf("Note I2C address 0x1C used by FXOS8700CQ 3-axis accelerometer and 3-axis magetometer\r\n");
Lightvalve 17:1865016ca2e7 8 //pc.printf("Start hardware search..... \r\n");
GiJeongKim 0:51c43836c1d7 9
GiJeongKim 0:51c43836c1d7 10 int count = 0;
GiJeongKim 0:51c43836c1d7 11 for (int address=12; address<256; address+=2) {
GiJeongKim 0:51c43836c1d7 12 if (!i2c.write(address, NULL, 0)) { // 0 returned is OK
Lightvalve 17:1865016ca2e7 13 //pc.printf(" - I2C device found at address 0x%02X\n\r", address >>1);
GiJeongKim 0:51c43836c1d7 14 count++;
GiJeongKim 0:51c43836c1d7 15 }
GiJeongKim 0:51c43836c1d7 16 }
Lightvalve 17:1865016ca2e7 17 //pc.printf("%d devices found \n\r", count);
GiJeongKim 0:51c43836c1d7 18 }
GiJeongKim 0:51c43836c1d7 19
GiJeongKim 0:51c43836c1d7 20 void init_as5510(int i2c_address)
GiJeongKim 0:51c43836c1d7 21 {
GiJeongKim 0:51c43836c1d7 22 int i2c_adrs=0;
GiJeongKim 0:51c43836c1d7 23 char idata[2];
GiJeongKim 0:51c43836c1d7 24 int result=0;
GiJeongKim 0:51c43836c1d7 25
Lightvalve 17:1865016ca2e7 26 //pc.printf("\r\n");
Lightvalve 17:1865016ca2e7 27 //pc.printf("Start AS5510 init.. \r\n");
GiJeongKim 0:51c43836c1d7 28
GiJeongKim 0:51c43836c1d7 29 i2c_adrs= (i2c_address << 1); // AS5510 Slave address lsb= 0 for write
GiJeongKim 0:51c43836c1d7 30
GiJeongKim 0:51c43836c1d7 31 //---------- Magnet selection --------------------------------
GiJeongKim 0:51c43836c1d7 32 //----0x00= <50mT-----------Strong magnet
GiJeongKim 0:51c43836c1d7 33 //----0x01= <25mT
GiJeongKim 0:51c43836c1d7 34 //----0x02= <18.7mT
GiJeongKim 0:51c43836c1d7 35 //----0x03= <12.5mT---------Weak magnet
GiJeongKim 0:51c43836c1d7 36 //-----------------------------------------------------------
GiJeongKim 0:51c43836c1d7 37 idata[0]=0x0B; // Register for Sensitivity
GiJeongKim 0:51c43836c1d7 38 idata[1]=0x00; // Byte
GiJeongKim 0:51c43836c1d7 39 result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_sensitivity
Lightvalve 17:1865016ca2e7 40 // if (result != 0) pc.printf("No ACK bit! (09)\n\r");
GiJeongKim 0:51c43836c1d7 41
GiJeongKim 0:51c43836c1d7 42 //----------- Operation mode selection------------------------
GiJeongKim 0:51c43836c1d7 43 idata[0]=0x02; // 0x02 address setup register for operation, speed, polarity
GiJeongKim 0:51c43836c1d7 44 idata[1]=0x04; // Normal Operation, Slow mode (1), NORMAL Polarity (0), Power Up (0)
GiJeongKim 0:51c43836c1d7 45 result= i2c.write(i2c_adrs, idata, 2, 0); // Now write_operation
Lightvalve 17:1865016ca2e7 46 // if (result != 0) pc.printf("No ACK bit! (11)\n\r");
GiJeongKim 0:51c43836c1d7 47
Lightvalve 17:1865016ca2e7 48 //pc.printf("AS5510 init done\r\n");
GiJeongKim 0:51c43836c1d7 49 }
GiJeongKim 0:51c43836c1d7 50
GiJeongKim 0:51c43836c1d7 51
GiJeongKim 0:51c43836c1d7 52 int offset_comp(int i2c_address)
GiJeongKim 0:51c43836c1d7 53 {
GiJeongKim 0:51c43836c1d7 54 int adrss=0;
GiJeongKim 0:51c43836c1d7 55 int oresult=0;
GiJeongKim 0:51c43836c1d7 56 char off_data[2];
GiJeongKim 0:51c43836c1d7 57 int ocf_done=0;
GiJeongKim 0:51c43836c1d7 58
GiJeongKim 0:51c43836c1d7 59 // First, now Write pointer to register 0x00----------------------------
GiJeongKim 0:51c43836c1d7 60 adrss= (i2c_address << 1); // AS5510 Slave address lsb= 0 for write
GiJeongKim 0:51c43836c1d7 61 oresult= i2c.write(adrss, 0x00, 1, 0); // write one byte
Lightvalve 17:1865016ca2e7 62 if (oresult != 0) //pc.printf("No ACK bit! (33)\n\r");
GiJeongKim 0:51c43836c1d7 63
GiJeongKim 0:51c43836c1d7 64 // Second, now Read register 0x00 and 0x01--------------------------------
GiJeongKim 0:51c43836c1d7 65 memset(off_data, 0, sizeof(off_data));
GiJeongKim 0:51c43836c1d7 66 adrss= (i2c_address << 1) | 0x01; // AS5510 address lsb= 1 for read
GiJeongKim 0:51c43836c1d7 67 oresult= i2c.read(adrss, off_data, 2, 0); // read two bytes
GiJeongKim 0:51c43836c1d7 68
GiJeongKim 0:51c43836c1d7 69 // Now analyse register 0x01 ----------------------------------------------
GiJeongKim 0:51c43836c1d7 70 ocf_done= off_data[1] & 0x08; // mask off bits, 1= done
GiJeongKim 0:51c43836c1d7 71 if (ocf_done== 0) return(0); // return(0)= compensation process is pending
GiJeongKim 0:51c43836c1d7 72 else return(1); // return(1)= compensation process is completed
GiJeongKim 0:51c43836c1d7 73 }
GiJeongKim 0:51c43836c1d7 74
GiJeongKim 0:51c43836c1d7 75
GiJeongKim 0:51c43836c1d7 76 void read_field(int i2c_address)
GiJeongKim 0:51c43836c1d7 77 {
GiJeongKim 0:51c43836c1d7 78 int adr=0;
GiJeongKim 0:51c43836c1d7 79 char rx_data[2];
GiJeongKim 0:51c43836c1d7 80 int rresult=0;
GiJeongKim 0:51c43836c1d7 81 char lsb, msb;
GiJeongKim 0:51c43836c1d7 82
GiJeongKim 0:51c43836c1d7 83 // First, now Write pointer to register 0x00----------------------------
GiJeongKim 0:51c43836c1d7 84 adr= (i2c_address << 1); // AS5510 address lsb= 0 for write
GiJeongKim 0:51c43836c1d7 85 rresult= i2c.write(adr, 0x00, 1, 0); // write one byte to register 0x00 for magnetic field strength
GiJeongKim 0:51c43836c1d7 86 // if (rresult != 0) pc.printf("No ACK bit! (22)\n\r");
GiJeongKim 0:51c43836c1d7 87
GiJeongKim 0:51c43836c1d7 88 // Second, now Read register 0x00 and 0x01--------------------------------
GiJeongKim 0:51c43836c1d7 89 memset(rx_data, 0, sizeof(rx_data));
GiJeongKim 0:51c43836c1d7 90 adr= (i2c_address << 1) | 0x01; // AS5510 address lsb= 1 for read
GiJeongKim 0:51c43836c1d7 91 rresult= i2c.read(adr, rx_data, 2, 0); // read two bytes
GiJeongKim 0:51c43836c1d7 92
GiJeongKim 0:51c43836c1d7 93
GiJeongKim 0:51c43836c1d7 94 // Now analyse register 0x01 ----------------------------------------------
GiJeongKim 0:51c43836c1d7 95 lsb= rx_data[0]; // get LSB
GiJeongKim 0:51c43836c1d7 96 msb= rx_data[1]&0x03; // need only 2 low bits og MSB
GiJeongKim 0:51c43836c1d7 97 value = ((msb & 0x03)<<8) + lsb;
GiJeongKim 0:51c43836c1d7 98 // 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);
Lightvalve 17:1865016ca2e7 99
GiJeongKim 0:51c43836c1d7 100 }