I2C_comm

Dependencies:   mbed

Fork of I2C_comm by Raphaël Drouin

Committer:
ShaolinPoutine
Date:
Mon Jan 16 19:02:49 2017 +0000
Revision:
2:a83e46376681
Parent:
1:7f23dfc59683
Child:
3:10f685ebc7ef
Ca fonstionne

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShaolinPoutine 0:656742ec86ae 1 #include "mbed.h"
ShaolinPoutine 1:7f23dfc59683 2
ShaolinPoutine 1:7f23dfc59683 3 Serial pc(USBTX,USBRX);
ShaolinPoutine 1:7f23dfc59683 4 I2C accelero(p28, p27);
ShaolinPoutine 1:7f23dfc59683 5 SPI afficheurSPI(p5, p6, p7);
ShaolinPoutine 1:7f23dfc59683 6 DigitalOut afficheurSelect(p8);
ShaolinPoutine 1:7f23dfc59683 7
ShaolinPoutine 0:656742ec86ae 8
ShaolinPoutine 1:7f23dfc59683 9 const char reg[] = {0x0d};
ShaolinPoutine 1:7f23dfc59683 10 const int device = 0x3A;
ShaolinPoutine 0:656742ec86ae 11 char data[1];
ShaolinPoutine 1:7f23dfc59683 12 int radtodeg = 57;
ShaolinPoutine 1:7f23dfc59683 13
ShaolinPoutine 1:7f23dfc59683 14 int16_t normal[3] = {0,0,1};
ShaolinPoutine 1:7f23dfc59683 15
ShaolinPoutine 1:7f23dfc59683 16 int printDecimalNumber(int i){
ShaolinPoutine 1:7f23dfc59683 17 char chiffres[4];
ShaolinPoutine 1:7f23dfc59683 18 if (i > 9999) {
ShaolinPoutine 1:7f23dfc59683 19 afficheurSelect = 0;
ShaolinPoutine 1:7f23dfc59683 20 afficheurSPI.write(0x76);
ShaolinPoutine 1:7f23dfc59683 21 afficheurSPI.write('e');
ShaolinPoutine 1:7f23dfc59683 22 afficheurSPI.write('e');
ShaolinPoutine 1:7f23dfc59683 23 afficheurSPI.write('e');
ShaolinPoutine 1:7f23dfc59683 24 afficheurSPI.write('e');
ShaolinPoutine 1:7f23dfc59683 25 wait(0.001);
ShaolinPoutine 1:7f23dfc59683 26 afficheurSelect = 1;
ShaolinPoutine 1:7f23dfc59683 27 }
ShaolinPoutine 1:7f23dfc59683 28 else {
ShaolinPoutine 1:7f23dfc59683 29 // Convert number to char list
ShaolinPoutine 1:7f23dfc59683 30 chiffres[0] = i / 1000;
ShaolinPoutine 1:7f23dfc59683 31 chiffres[1] = (i - int(chiffres[0]) * 1000) / 100;
ShaolinPoutine 1:7f23dfc59683 32 chiffres[2] = (i - int(chiffres[0]) * 1000 - int(chiffres[1]) * 100) / 10;
ShaolinPoutine 1:7f23dfc59683 33 chiffres[3] = i - int(chiffres[0]) * 1000 - int(chiffres[1]) * 100 - int(chiffres[2]) * 10;
ShaolinPoutine 1:7f23dfc59683 34
ShaolinPoutine 1:7f23dfc59683 35 afficheurSelect= 0;
ShaolinPoutine 1:7f23dfc59683 36 afficheurSPI.write(0x76);
ShaolinPoutine 1:7f23dfc59683 37 afficheurSPI.write(0x77);
ShaolinPoutine 1:7f23dfc59683 38 afficheurSPI.write(0b00000010);
ShaolinPoutine 1:7f23dfc59683 39 for (int x = 0; x < 4; x++) {
ShaolinPoutine 1:7f23dfc59683 40 afficheurSPI.write(chiffres[x]);
ShaolinPoutine 1:7f23dfc59683 41 }
ShaolinPoutine 1:7f23dfc59683 42 wait(0.001);
ShaolinPoutine 1:7f23dfc59683 43 afficheurSelect = 1;
ShaolinPoutine 1:7f23dfc59683 44 }
ShaolinPoutine 1:7f23dfc59683 45 return 0;
ShaolinPoutine 1:7f23dfc59683 46 }
ShaolinPoutine 1:7f23dfc59683 47
ShaolinPoutine 0:656742ec86ae 48 int main() {
ShaolinPoutine 0:656742ec86ae 49
ShaolinPoutine 1:7f23dfc59683 50 afficheurSPI.frequency(100000);
ShaolinPoutine 1:7f23dfc59683 51
ShaolinPoutine 1:7f23dfc59683 52 accelero.write(device,reg,1,true);
ShaolinPoutine 1:7f23dfc59683 53 accelero.read(device,data,1);
ShaolinPoutine 0:656742ec86ae 54
ShaolinPoutine 1:7f23dfc59683 55 pc.printf("Result : %d \r\n", data[0]);
ShaolinPoutine 1:7f23dfc59683 56 printDecimalNumber((int) data[0]);
ShaolinPoutine 1:7f23dfc59683 57
ShaolinPoutine 1:7f23dfc59683 58 // Set 2g max
ShaolinPoutine 1:7f23dfc59683 59 char XYZ_DATA_CFG[2] = {0x0e, 0};
ShaolinPoutine 1:7f23dfc59683 60 char value[1];
ShaolinPoutine 1:7f23dfc59683 61 accelero.read(device, XYZ_DATA_CFG, 1, true);
ShaolinPoutine 1:7f23dfc59683 62 accelero.write(device, value, 1);
ShaolinPoutine 1:7f23dfc59683 63 value[0] = value[0] & 0xFC;
ShaolinPoutine 1:7f23dfc59683 64
ShaolinPoutine 1:7f23dfc59683 65 XYZ_DATA_CFG[1] = value[0];
ShaolinPoutine 1:7f23dfc59683 66 accelero.write(device, XYZ_DATA_CFG, 2);
ShaolinPoutine 1:7f23dfc59683 67
ShaolinPoutine 1:7f23dfc59683 68 // Activate accelerometer
ShaolinPoutine 1:7f23dfc59683 69 char ctrlReg1[2] = {0x2a, 0};
ShaolinPoutine 1:7f23dfc59683 70 accelero.write(device, ctrlReg1, 1, true);
ShaolinPoutine 1:7f23dfc59683 71 accelero.read(device, value, 1);
ShaolinPoutine 1:7f23dfc59683 72 value[0] = value[0] | 0x1;
ShaolinPoutine 1:7f23dfc59683 73
ShaolinPoutine 1:7f23dfc59683 74 ctrlReg1[1] = value[0];
ShaolinPoutine 1:7f23dfc59683 75 accelero.write(device, ctrlReg1, 2);
ShaolinPoutine 1:7f23dfc59683 76
ShaolinPoutine 1:7f23dfc59683 77 char XYZ_Data = 0x01;
ShaolinPoutine 1:7f23dfc59683 78 char data[6];
ShaolinPoutine 1:7f23dfc59683 79 int16_t x, y, z;
ShaolinPoutine 1:7f23dfc59683 80 int16_t buffer;
ShaolinPoutine 2:a83e46376681 81
ShaolinPoutine 2:a83e46376681 82 int mean[32];
ShaolinPoutine 2:a83e46376681 83 int angle;
ShaolinPoutine 2:a83e46376681 84
ShaolinPoutine 2:a83e46376681 85 int iteration = 0;
ShaolinPoutine 1:7f23dfc59683 86 while(1) {
ShaolinPoutine 1:7f23dfc59683 87 accelero.write(device, &XYZ_Data, 1, true);
ShaolinPoutine 1:7f23dfc59683 88 accelero.read(device, data, 6);
ShaolinPoutine 1:7f23dfc59683 89
ShaolinPoutine 1:7f23dfc59683 90 ((char*) &buffer)[0]= data[1]; //LSB
ShaolinPoutine 1:7f23dfc59683 91 ((char*) &buffer)[1]= data[0]; //MSB
ShaolinPoutine 1:7f23dfc59683 92 x = buffer >> 4;
ShaolinPoutine 1:7f23dfc59683 93 ((char*) &buffer)[0]= data[3]; //LSB
ShaolinPoutine 1:7f23dfc59683 94 ((char*) &buffer)[1]= data[2]; //MSB
ShaolinPoutine 1:7f23dfc59683 95 y = buffer >> 4;
ShaolinPoutine 1:7f23dfc59683 96 ((char*) &buffer)[0]= data[5]; //LSB
ShaolinPoutine 1:7f23dfc59683 97 ((char*) &buffer)[1]= data[4]; //MSB
ShaolinPoutine 1:7f23dfc59683 98 z = buffer >> 4;
ShaolinPoutine 1:7f23dfc59683 99
ShaolinPoutine 2:a83e46376681 100 if (z > 1024)
ShaolinPoutine 2:a83e46376681 101 z = 1024;
ShaolinPoutine 2:a83e46376681 102 if (z < -1024)
ShaolinPoutine 2:a83e46376681 103 z = -1024;
ShaolinPoutine 2:a83e46376681 104
ShaolinPoutine 2:a83e46376681 105 int theta = std::acos((double) z / (double) 1024) * radtodeg * 100;
ShaolinPoutine 2:a83e46376681 106 mean[iteration % 32] = theta >> 5;
ShaolinPoutine 2:a83e46376681 107
ShaolinPoutine 2:a83e46376681 108 if (iteration % 16 == 0){
ShaolinPoutine 2:a83e46376681 109 angle = 0;
ShaolinPoutine 2:a83e46376681 110 for (int i = 0; i < 32; i++){
ShaolinPoutine 2:a83e46376681 111 angle += mean[i];
ShaolinPoutine 2:a83e46376681 112 }
ShaolinPoutine 2:a83e46376681 113 printDecimalNumber(angle);
ShaolinPoutine 2:a83e46376681 114 }
ShaolinPoutine 2:a83e46376681 115 wait(0.006);
ShaolinPoutine 2:a83e46376681 116 iteration++;
ShaolinPoutine 1:7f23dfc59683 117 }
ShaolinPoutine 0:656742ec86ae 118 }