i2c trial - does'nt work

Dependencies:   ACD_ePaper aconno_I2C aconno_bsp mbed

Fork of acd52832_LSM9DS1 by Jurica Resetar

Committer:
med2017
Date:
Wed Feb 14 21:16:43 2018 +0000
Revision:
1:e97c56fb9629
Parent:
0:940647793667
testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
med2017 1:e97c56fb9629 1 //plan of debugging and what the result was
med2017 1:e97c56fb9629 2 //what the problem is and how :lessons learned what was tried
med2017 1:e97c56fb9629 3 //your own development and knowledge
med2017 1:e97c56fb9629 4 //analysis report - working hard thinking throug the problems
med2017 1:e97c56fb9629 5 //less is more - essential information
med2017 1:e97c56fb9629 6 //what your thinkijg is what problems are and what u tried
med2017 1:e97c56fb9629 7 //point form
med2017 1:e97c56fb9629 8
med2017 1:e97c56fb9629 9
med2017 1:e97c56fb9629 10 /* Copyright (c) 2016 Aconno. All Rights Reserved.
jurica238814 0:940647793667 11 *
jurica238814 0:940647793667 12 * Licensees are granted free, non-transferable use of the information. NO
jurica238814 0:940647793667 13 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
jurica238814 0:940647793667 14 * the file.
jurica238814 0:940647793667 15 *
jurica238814 0:940647793667 16 */
jurica238814 0:940647793667 17
jurica238814 0:940647793667 18
jurica238814 0:940647793667 19 #include "mbed.h"
jurica238814 0:940647793667 20 #include "acd52832_bsp.h"
med2017 1:e97c56fb9629 21 //#include "LSM9DS1_regs.h"
med2017 1:e97c56fb9629 22 //#include "LSM9DS1_defVals.h"
med2017 1:e97c56fb9629 23 #include "LSM9DS1.h"
jurica238814 0:940647793667 24 #include "GDEP015OC1.h"
med2017 1:e97c56fb9629 25 #include "aconno_i2c.h"
med2017 1:e97c56fb9629 26 //#include "main.h"
jurica238814 0:940647793667 27
jurica238814 0:940647793667 28 // #define GYRO
jurica238814 0:940647793667 29
med2017 1:e97c56fb9629 30 SPI spi(p3, NC, p4); //keep this spi connection - for e paper display
jurica238814 0:940647793667 31 GDEP015OC1 epd = GDEP015OC1(spi, p5, p6, p7, p8);
jurica238814 0:940647793667 32
jurica238814 0:940647793667 33 // Initialize I2C protocol
jurica238814 0:940647793667 34 I2C mems(PIN_EXP_SDA, PIN_EXP_SCL);
med2017 1:e97c56fb9629 35 char memsI2CAddress = I2C_ADDRESS;
med2017 1:e97c56fb9629 36 LSM9DS1 mems(&i2c, memsI2CAddress);
med2017 1:e97c56fb9629 37
jurica238814 0:940647793667 38
jurica238814 0:940647793667 39 DigitalOut RED(PIN_LED_RED);
jurica238814 0:940647793667 40 DigitalOut GREEN(PIN_LED_GREEN);
jurica238814 0:940647793667 41 DigitalOut BLUE(PIN_LED_BLUE);
jurica238814 0:940647793667 42 DigitalOut LEDD(PIN_LED);
jurica238814 0:940647793667 43
jurica238814 0:940647793667 44
med2017 1:e97c56fb9629 45 uint8_t LSM9DS1::enableAxes(Axis axis){
med2017 1:e97c56fb9629 46 char ctrl1Copy;
med2017 1:e97c56fb9629 47 i2c.readFromReg((char)CTRL_REG5_XL, &ctrl1Copy, 1);
med2017 1:e97c56fb9629 48 ctrl1Copy |= axis;
med2017 1:e97c56fb9629 49 i2c.writeToReg((char)CTRL_REG6_XL, &ctrl1Copy, 1);
med2017 1:e97c56fb9629 50 return 0;
med2017 1:e97c56fb9629 51 }
med2017 1:e97c56fb9629 52
med2017 1:e97c56fb9629 53 int16_t LSM9DS1::readXAxis(){
med2017 1:e97c56fb9629 54 int16_t rawData;
med2017 1:e97c56fb9629 55 char tempData;
med2017 1:e97c56fb9629 56 // Make sure new data is ready
med2017 1:e97c56fb9629 57 do{
med2017 1:e97c56fb9629 58 i2c.readFromReg((char)STATUS_REG_0, &tempData, 1);
med2017 1:e97c56fb9629 59 }while(!(tempData & 0x08));
med2017 1:e97c56fb9629 60 do{
med2017 1:e97c56fb9629 61 i2c.readFromReg((char)STATUS_REG_0, &tempData, 1);
med2017 1:e97c56fb9629 62 }while(!(tempData & 0x80));
med2017 1:e97c56fb9629 63 // Same data have been overwritten
med2017 1:e97c56fb9629 64
med2017 1:e97c56fb9629 65 i2c.readFromReg((char)OUT_X_H_XL, &tempData, 1);
med2017 1:e97c56fb9629 66 rawData = (int8_t)tempData << 8;
med2017 1:e97c56fb9629 67 i2c.readFromReg((char)OUT_X_L_XL, &tempData, 1);
med2017 1:e97c56fb9629 68 rawData |= (int8_t)tempData;
med2017 1:e97c56fb9629 69 return rawData;
med2017 1:e97c56fb9629 70 }
med2017 1:e97c56fb9629 71
med2017 1:e97c56fb9629 72 int16_t LSM9DS1::readYAxis(){
med2017 1:e97c56fb9629 73 int16_t rawData;
med2017 1:e97c56fb9629 74 char tempData;
med2017 1:e97c56fb9629 75 i2c.readFromReg((char)OUT_Y_H_XL, &tempData, 1);
med2017 1:e97c56fb9629 76 rawData = (int8_t)tempData << 8;
med2017 1:e97c56fb9629 77 i2c.readFromReg((char)OUT_Y_L_XL, &tempData, 1);
med2017 1:e97c56fb9629 78 rawData |= (int8_t)tempData;
med2017 1:e97c56fb9629 79 return rawData;
med2017 1:e97c56fb9629 80 }
med2017 1:e97c56fb9629 81
med2017 1:e97c56fb9629 82 int16_t LSM9DS1::readZAxis(){
med2017 1:e97c56fb9629 83 int16_t rawData;
med2017 1:e97c56fb9629 84 char tempData;
med2017 1:e97c56fb9629 85 i2c.readFromReg((char)OUT_Z_H_XL, &tempData, 1);
med2017 1:e97c56fb9629 86 rawData = (int8_t)tempData << 8;
med2017 1:e97c56fb9629 87 i2c.readFromReg((char)OUT_Z_L_XL, &tempData, 1);
med2017 1:e97c56fb9629 88 rawData |= (int8_t)tempData;
med2017 1:e97c56fb9629 89 return rawData;
med2017 1:e97c56fb9629 90 }
med2017 1:e97c56fb9629 91
med2017 1:e97c56fb9629 92
med2017 1:e97c56fb9629 93
med2017 1:e97c56fb9629 94
jurica238814 0:940647793667 95 void check(bool success)
jurica238814 0:940647793667 96 {
jurica238814 0:940647793667 97 if(!success) {
jurica238814 0:940647793667 98 // serial.printf("Success.\n");
jurica238814 0:940647793667 99 RED = 1;
jurica238814 0:940647793667 100 GREEN = 0;
jurica238814 0:940647793667 101 } else {
jurica238814 0:940647793667 102 // serial.printf("Unsuccess!\n");
jurica238814 0:940647793667 103 RED = 0;
jurica238814 0:940647793667 104 GREEN = 1;
jurica238814 0:940647793667 105 wait(2);
jurica238814 0:940647793667 106 }
jurica238814 0:940647793667 107 wait (0.05);
jurica238814 0:940647793667 108 RED = 1;
jurica238814 0:940647793667 109 GREEN = 1;
jurica238814 0:940647793667 110 }
jurica238814 0:940647793667 111
jurica238814 0:940647793667 112
jurica238814 0:940647793667 113
med2017 1:e97c56fb9629 114
med2017 1:e97c56fb9629 115
med2017 1:e97c56fb9629 116
med2017 1:e97c56fb9629 117
med2017 1:e97c56fb9629 118 void start_acc()
jurica238814 0:940647793667 119 {
jurica238814 0:940647793667 120 char data[2];
jurica238814 0:940647793667 121 bool success;
med2017 1:e97c56fb9629 122
med2017 1:e97c56fb9629 123 data[0] = (char)CTRL_REG5_XL; // Target register
med2017 1:e97c56fb9629 124 data[1] = (char)0x38; // Data to write
med2017 1:e97c56fb9629 125 success = mems.write(TWI_AG_ADDR, data, 0x02,0);
jurica238814 0:940647793667 126 check(success);
med2017 1:e97c56fb9629 127
med2017 1:e97c56fb9629 128 data[0] = (char)CTRL_REG6_XL; // Target register
med2017 1:e97c56fb9629 129 data[1] = (char)0xC7; // Data to write
med2017 1:e97c56fb9629 130 success = mems.write(TWI_AG_ADDR, data, 0x02,0);
jurica238814 0:940647793667 131 check(success);
med2017 1:e97c56fb9629 132
jurica238814 0:940647793667 133 }
jurica238814 0:940647793667 134
med2017 1:e97c56fb9629 135 void read_acc(float *results){
jurica238814 0:940647793667 136 char results_[6];
jurica238814 0:940647793667 137 float res_final[3];
med2017 1:e97c56fb9629 138 char out_x_l_xl = OUT_X_L_XL;
jurica238814 0:940647793667 139
med2017 1:e97c56fb9629 140 check (mems.write(TWI_AG_ADDR, &out_x_l_xl, 1, true));
med2017 1:e97c56fb9629 141 check (mems.read(TWI_AG_ADDR, results_, 6, 0));
jurica238814 0:940647793667 142 res_final[0] = ((results_[1]<<8) | results_[0]);
jurica238814 0:940647793667 143 res_final[1] = ((results_[3]<<8) | results_[2]);
jurica238814 0:940647793667 144 res_final[2] = ((results_[5]<<8) | results_[4]);
jurica238814 0:940647793667 145
jurica238814 0:940647793667 146 *(results) = res_final[0];
jurica238814 0:940647793667 147 *(results + 1) = res_final[1];
jurica238814 0:940647793667 148 *(results + 2) = res_final[2];
jurica238814 0:940647793667 149 }
jurica238814 0:940647793667 150
med2017 1:e97c56fb9629 151 //////////////////////////////////////////////////////
med2017 1:e97c56fb9629 152 void read_bb(float *res) {
med2017 1:e97c56fb9629 153 char res_[6];
med2017 1:e97c56fb9629 154 float final_res[3];
med2017 1:e97c56fb9629 155 char out_bb = out_xl_bb;
med2017 1:e97c56fb9629 156 }//char out_xl_bb
med2017 1:e97c56fb9629 157 /////////////////////////////////////////////////////////
jurica238814 0:940647793667 158
jurica238814 0:940647793667 159 int main()
jurica238814 0:940647793667 160 {
jurica238814 0:940647793667 161 float old_res = 0;
jurica238814 0:940647793667 162 float results[3];
jurica238814 0:940647793667 163 char buffer[6];
jurica238814 0:940647793667 164
jurica238814 0:940647793667 165 // Clear LEDs
jurica238814 0:940647793667 166 RED = 1;
jurica238814 0:940647793667 167 GREEN = 1;
med2017 1:e97c56fb9629 168 //-----------------
med2017 1:e97c56fb9629 169
med2017 1:e97c56fb9629 170
jurica238814 0:940647793667 171
jurica238814 0:940647793667 172 // Start acceleration sensor
jurica238814 0:940647793667 173 start_acc();
jurica238814 0:940647793667 174
jurica238814 0:940647793667 175 while (1) {
med2017 1:e97c56fb9629 176 // Get data from ag sensor
med2017 1:e97c56fb9629 177
jurica238814 0:940647793667 178 read_acc(results);
jurica238814 0:940647793667 179
jurica238814 0:940647793667 180 if (*results != old_res) {
jurica238814 0:940647793667 181 // Write new value on display
jurica238814 0:940647793667 182 epd.empty();
med2017 1:e97c56fb9629 183 ////////////////////
med2017 1:e97c56fb9629 184 epd.writeString("Aconno Accelerometer Data:",10,50,0);
med2017 1:e97c56fb9629 185 //////////////////
jurica238814 0:940647793667 186 epd.write();
jurica238814 0:940647793667 187
med2017 1:e97c56fb9629 188
med2017 1:e97c56fb9629 189
med2017 1:e97c56fb9629 190 // Write ag_x
jurica238814 0:940647793667 191 sprintf(buffer, "%+2.1f", *results); //Create a string
jurica238814 0:940647793667 192 epd.writeString(buffer,85,70,0); //Write new data to the buffer
jurica238814 0:940647793667 193 epd.write();
jurica238814 0:940647793667 194
med2017 1:e97c56fb9629 195 // Write ag_y
jurica238814 0:940647793667 196 sprintf(buffer, "%+2.1f", *(results+1)); //Create a string
jurica238814 0:940647793667 197 epd.writeString(buffer,85,80,0); //Write new data to the buffer
jurica238814 0:940647793667 198 epd.write();
jurica238814 0:940647793667 199
med2017 1:e97c56fb9629 200 // Write ag_z
jurica238814 0:940647793667 201 sprintf(buffer, "%+2.1f", *(results+2)); //Create a string
jurica238814 0:940647793667 202 epd.writeString(buffer,85,90,0); //Write new data to the buffer
jurica238814 0:940647793667 203 epd.write();
jurica238814 0:940647793667 204
jurica238814 0:940647793667 205 old_res = *results;
jurica238814 0:940647793667 206 BLUE = 0;
jurica238814 0:940647793667 207 wait (1);
jurica238814 0:940647793667 208 BLUE = 1;
jurica238814 0:940647793667 209 wait(1);
med2017 1:e97c56fb9629 210
med2017 1:e97c56fb9629 211
med2017 1:e97c56fb9629 212
jurica238814 0:940647793667 213 }
jurica238814 0:940647793667 214 LEDD = 0;
jurica238814 0:940647793667 215 wait(1);
jurica238814 0:940647793667 216 LEDD = 1;
jurica238814 0:940647793667 217 wait(1);
jurica238814 0:940647793667 218 }
jurica238814 0:940647793667 219
med2017 1:e97c56fb9629 220 // from the bb:
med2017 1:e97c56fb9629 221 //while(1){
med2017 1:e97c56fb9629 222
med2017 1:e97c56fb9629 223 //the functions are in the wrong class
med2017 1:e97c56fb9629 224 enableI2C();
med2017 1:e97c56fb9629 225 mems.enableAxes(X_axis); ////enable axis not woking????
med2017 1:e97c56fb9629 226 mems.readXaxis();
med2017 1:e97c56fb9629 227 mems.disableAxes(X_axis);
med2017 1:e97c56fb9629 228 mems.enableAxes(Y_axis);
med2017 1:e97c56fb9629 229 mems.readYaxis();
med2017 1:e97c56fb9629 230 mems.disableAxes(Y_axis);
med2017 1:e97c56fb9629 231 mems.enableAxes(Z_axis);
med2017 1:e97c56fb9629 232 mems.readZaxis();
med2017 1:e97c56fb9629 233 mems.disableAxes(Z_axis);
med2017 1:e97c56fb9629 234 epd.writeString("BB Accelerometer Data:",10,90,0);
med2017 1:e97c56fb9629 235 epd.write();
med2017 1:e97c56fb9629 236 ///////////// write to epd
med2017 1:e97c56fb9629 237 ///add in epd for each
jurica238814 0:940647793667 238
med2017 1:e97c56fb9629 239
med2017 1:e97c56fb9629 240 //}