Raphaël Drouin / Mbed 2 deprecated I2C_SPI

Dependencies:   mbed

Fork of I2C_comm by Raphaël Drouin

Committer:
ShaolinPoutine
Date:
Tue Jan 17 15:56:39 2017 +0000
Revision:
3:10f685ebc7ef
Parent:
2:a83e46376681
k

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