Test

Dependencies:   mbed AccelSensor

Committer:
Alegrowin
Date:
Wed Jan 16 00:10:09 2013 +0000
Revision:
5:e5fbf322c180
Parent:
4:3397631c2f65
Menage dans le code ajout de condition de pre compileur

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Alegrowin 0:b325845b05af 1 #include "mbed.h"
Alegrowin 4:3397631c2f65 2 #include "AccelSensor.h"
Alegrowin 0:b325845b05af 3
Alegrowin 4:3397631c2f65 4 const int addr = 0x9A; // define the I2C Address for TC74-A0
Alegrowin 4:3397631c2f65 5 const int Afficheur_addr = 0xE2; // define the I2C Address for 4*7seg display
Alegrowin 4:3397631c2f65 6 const int Accel_addr = 0x3A; // define the I2C Address for the Accelerometer
Alegrowin 4:3397631c2f65 7
Alegrowin 4:3397631c2f65 8 //Defines for the serial output for debug
Alegrowin 5:e5fbf322c180 9 #define Output_Temperature true
Alegrowin 5:e5fbf322c180 10 #define Output_Accel true
Alegrowin 5:e5fbf322c180 11 #define Output_Accel_debug false
Alegrowin 4:3397631c2f65 12
Alegrowin 4:3397631c2f65 13 //Creating the desired objetcs
Alegrowin 2:3e6b509d7eca 14 I2C i2c(p9, p10); // sda, scl
Alegrowin 0:b325845b05af 15 Serial pc(USBTX, USBRX); // tx, rx
Alegrowin 5:e5fbf322c180 16 Serial Damn(p28,p27); // Used as shortcut for printing result on 7seg using printf function as it is not available in I2C
Alegrowin 5:e5fbf322c180 17 AccelSensor acc(p9,p10); //Should be commented as it was a part of the prototype and test, we may want to use our own function
Alegrowin 4:3397631c2f65 18 //Mainly used to calibrate and start the device
Alegrowin 0:b325845b05af 19
Alegrowin 4:3397631c2f65 20 void init(void);
Alegrowin 5:e5fbf322c180 21 char get_Temperature(void);
Alegrowin 5:e5fbf322c180 22 void Display8888(void);
Alegrowin 5:e5fbf322c180 23 void Update_Accel(void);
Alegrowin 0:b325845b05af 24
Alegrowin 0:b325845b05af 25 int main()
Alegrowin 0:b325845b05af 26 {
Alegrowin 5:e5fbf322c180 27 char temp;
Alegrowin 4:3397631c2f65 28
Alegrowin 4:3397631c2f65 29 init();
Alegrowin 1:60bb79c9a01e 30 wait(1); //Make sure system is fully initialized
Alegrowin 4:3397631c2f65 31
Alegrowin 5:e5fbf322c180 32 Display8888(); //Display initial text 8888 on the display
Alegrowin 4:3397631c2f65 33
Alegrowin 4:3397631c2f65 34
Alegrowin 5:e5fbf322c180 35 while(1) {
Alegrowin 5:e5fbf322c180 36 pc.printf("\r\n\nStart reading the temperature of TC74 on I2C\r\n");
Alegrowin 5:e5fbf322c180 37 temp = get_Temperature();
Alegrowin 5:e5fbf322c180 38 Update_Accel();
Alegrowin 5:e5fbf322c180 39
Alegrowin 5:e5fbf322c180 40 wait(2);
Alegrowin 4:3397631c2f65 41
Alegrowin 5:e5fbf322c180 42 }
Alegrowin 5:e5fbf322c180 43 }
Alegrowin 5:e5fbf322c180 44 //------------------Accelerometre ---------------------------
Alegrowin 4:3397631c2f65 45
Alegrowin 5:e5fbf322c180 46 void Update_Accel(void)
Alegrowin 5:e5fbf322c180 47 {
Alegrowin 5:e5fbf322c180 48 //Variables
Alegrowin 5:e5fbf322c180 49 char cmd[7];
Alegrowin 4:3397631c2f65 50
Alegrowin 5:e5fbf322c180 51 //Initialisation
Alegrowin 5:e5fbf322c180 52 cmd[0] = 0x00; //Command :: Status
Alegrowin 4:3397631c2f65 53
Alegrowin 5:e5fbf322c180 54 //Read all the register for X,Y and Z forces
Alegrowin 5:e5fbf322c180 55 i2c.write(Accel_addr, cmd, 1); //Issue required command to perform a write of the command
Alegrowin 5:e5fbf322c180 56 i2c.read(Accel_addr, cmd, 7); //Read the Data from the device
Alegrowin 5:e5fbf322c180 57 #if Output_Accel_debug
Alegrowin 5:e5fbf322c180 58 pc.printf("Values :: %d %d %d %d %d %d %d \r\n\n", cmd[0], cmd[1], cmd[2],cmd[3],cmd[4],cmd[5],cmd[6] );
Alegrowin 5:e5fbf322c180 59 #endif
Alegrowin 5:e5fbf322c180 60 //Damn.printf("v");
Alegrowin 5:e5fbf322c180 61 long double anglez, anglex,angley;
Alegrowin 5:e5fbf322c180 62 bool negatif;
Alegrowin 5:e5fbf322c180 63 //calcul angle Z
Alegrowin 5:e5fbf322c180 64 if (cmd[5]<0x7F) {
Alegrowin 5:e5fbf322c180 65 anglez = acos((cmd[5]/62.0))/3.1416*180;
Alegrowin 5:e5fbf322c180 66 negatif=false;
Alegrowin 4:3397631c2f65 67
Alegrowin 5:e5fbf322c180 68 } else {
Alegrowin 5:e5fbf322c180 69 cmd[5]=(cmd[5]-256)*-1;
Alegrowin 5:e5fbf322c180 70 anglez = acos((cmd[5]/62.0))/3.1416*180;
Alegrowin 4:3397631c2f65 71
Alegrowin 5:e5fbf322c180 72 }
Alegrowin 4:3397631c2f65 73
Alegrowin 5:e5fbf322c180 74 //calcul angle Y
Alegrowin 5:e5fbf322c180 75 if (cmd[3]<0x7F) {
Alegrowin 5:e5fbf322c180 76 angley = asin((cmd[3]/62.0))/3.1416*180;
Alegrowin 4:3397631c2f65 77
Alegrowin 5:e5fbf322c180 78 } else {
Alegrowin 5:e5fbf322c180 79 cmd[3]=(cmd[3]-256)*-1;
Alegrowin 5:e5fbf322c180 80 angley = asin((cmd[3]/62.0))/3.1416*180;
Alegrowin 4:3397631c2f65 81
Alegrowin 5:e5fbf322c180 82 }
Alegrowin 5:e5fbf322c180 83 // calcul angle X
Alegrowin 5:e5fbf322c180 84 if (cmd[1]<0x7F) {
Alegrowin 5:e5fbf322c180 85 anglex = asin((cmd[1]/62.0))/3.1416*180;
Alegrowin 4:3397631c2f65 86
Alegrowin 5:e5fbf322c180 87 } else {
Alegrowin 5:e5fbf322c180 88 cmd[1]=(cmd[1]-256)*-1;
Alegrowin 5:e5fbf322c180 89 anglex = asin((cmd[1]/62.0))/3.1416*180;
Alegrowin 4:3397631c2f65 90
Alegrowin 5:e5fbf322c180 91 }
Alegrowin 4:3397631c2f65 92
Alegrowin 4:3397631c2f65 93
Alegrowin 5:e5fbf322c180 94 //if (anglez<45
Alegrowin 5:e5fbf322c180 95 int b = anglez;
Alegrowin 5:e5fbf322c180 96 #if Output_Accel
Alegrowin 5:e5fbf322c180 97 pc.printf("TEST: X=%f Y=%f Z=%f \r\n", anglex, angley, anglez );
Alegrowin 5:e5fbf322c180 98 #endif
Alegrowin 5:e5fbf322c180 99 if (negatif==false)
Alegrowin 5:e5fbf322c180 100 Damn.printf(" ");
Alegrowin 5:e5fbf322c180 101 else
Alegrowin 5:e5fbf322c180 102 Damn.printf(" -");
Alegrowin 4:3397631c2f65 103
Alegrowin 5:e5fbf322c180 104 Damn.printf("%2d", b );
Alegrowin 4:3397631c2f65 105
Alegrowin 4:3397631c2f65 106
Alegrowin 5:e5fbf322c180 107 //-------------------Accel status form Imported Class --------
Alegrowin 5:e5fbf322c180 108 #if Output_Accel_debug
Alegrowin 5:e5fbf322c180 109 int result[3];
Alegrowin 5:e5fbf322c180 110 acc.readData(result);
Alegrowin 5:e5fbf322c180 111 pc.printf("X: %d Y: %d Z: %d \r\n", result[0],result[1],result[2]);
Alegrowin 5:e5fbf322c180 112 #endif
Alegrowin 4:3397631c2f65 113
Alegrowin 5:e5fbf322c180 114
Alegrowin 4:3397631c2f65 115
Alegrowin 4:3397631c2f65 116 }
Alegrowin 4:3397631c2f65 117
Alegrowin 4:3397631c2f65 118 void init(void)
Alegrowin 4:3397631c2f65 119 {
Alegrowin 4:3397631c2f65 120 //Let's clear the I2C display
Alegrowin 4:3397631c2f65 121
Alegrowin 4:3397631c2f65 122 i2c.write(Afficheur_addr,"v",1);
Alegrowin 4:3397631c2f65 123
Alegrowin 5:e5fbf322c180 124
Alegrowin 4:3397631c2f65 125 acc.init();
Alegrowin 4:3397631c2f65 126 acc.active();
Alegrowin 4:3397631c2f65 127 }
Alegrowin 4:3397631c2f65 128
Alegrowin 4:3397631c2f65 129 char get_Temperature(void)
Alegrowin 4:3397631c2f65 130 {
Alegrowin 4:3397631c2f65 131 char cmd[1];
Alegrowin 4:3397631c2f65 132 //Méthode 1
Alegrowin 4:3397631c2f65 133 //Utilisation des fonctions bas niveau
Alegrowin 4:3397631c2f65 134 /*
Alegrowin 1:60bb79c9a01e 135 i2c.start(); // Start condition
Alegrowin 1:60bb79c9a01e 136 a = i2c.write(addr); // Write Device Address
Alegrowin 1:60bb79c9a01e 137 b = i2c.write(0x00); // Write READ command of TC74 (voir page 8 de la datasheet du TC74)
Alegrowin 1:60bb79c9a01e 138
Alegrowin 1:60bb79c9a01e 139 i2c.start(); //Reissue start condition
Alegrowin 4:3397631c2f65 140 //Au lieu de faire Stop condition et Start de nouveau
Alegrowin 4:3397631c2f65 141
Alegrowin 1:60bb79c9a01e 142 c= i2c.write(addr|1); //Adresse du Device en mode Lecture
Alegrowin 1:60bb79c9a01e 143 temp = i2c.read(0); //Lecture de la valeur du registre de température
Alegrowin 1:60bb79c9a01e 144 i2c.stop(); //Fermeture de la trame
Alegrowin 4:3397631c2f65 145 */
Alegrowin 4:3397631c2f65 146 //Méthode 2
Alegrowin 4:3397631c2f65 147 //Utilisation des fonctions haut niveau
Alegrowin 1:60bb79c9a01e 148
Alegrowin 4:3397631c2f65 149 cmd[0] = 0x0; //Command :: READ
Alegrowin 4:3397631c2f65 150 i2c.write(addr, cmd, 1); //Issue required command to perform a write of the command
Alegrowin 4:3397631c2f65 151 i2c.read(addr, cmd, 1); //Read the Data from the device
Alegrowin 4:3397631c2f65 152
Alegrowin 4:3397631c2f65 153 //-----------------Print out section ----------------------
Alegrowin 4:3397631c2f65 154 //Display device Address and informations
Alegrowin 0:b325845b05af 155
Alegrowin 4:3397631c2f65 156 // pc.printf("Device with address 0x%x with\r\n", addr);
Alegrowin 4:3397631c2f65 157
Alegrowin 4:3397631c2f65 158 //Prints out the result of Method 1
Alegrowin 4:3397631c2f65 159 //pc.printf("ACK1 :: %d\n\rACK2 :: %d\n\rACK3 :: %d\n\r", a,b,c); //ACK bits
Alegrowin 4:3397631c2f65 160 //pc.printf("Method 1 :: %d\n\r", temp);
Alegrowin 4:3397631c2f65 161
Alegrowin 4:3397631c2f65 162 //Prints out the Data from Method 2
Alegrowin 4:3397631c2f65 163 #if Output_Temperature
Alegrowin 5:e5fbf322c180 164 pc.printf("Temperature :: %d\r\n\n", cmd[0]);
Alegrowin 4:3397631c2f65 165 #endif
Alegrowin 4:3397631c2f65 166
Alegrowin 4:3397631c2f65 167 return cmd[0];
Alegrowin 5:e5fbf322c180 168 }
Alegrowin 5:e5fbf322c180 169
Alegrowin 5:e5fbf322c180 170 /*
Alegrowin 5:e5fbf322c180 171 ** This function display '8888' on the Hex Display using I2C transactions
Alegrowin 5:e5fbf322c180 172 ** For prototyping purpose, we will use the Serial protocol for transferring the data to the Hex display
Alegrowin 5:e5fbf322c180 173 */
Alegrowin 5:e5fbf322c180 174 void Display8888(void)
Alegrowin 5:e5fbf322c180 175 {
Alegrowin 5:e5fbf322c180 176 char cmd[2];
Alegrowin 5:e5fbf322c180 177 //--------------Afficheur LCD ------------------------------
Alegrowin 5:e5fbf322c180 178
Alegrowin 5:e5fbf322c180 179 cmd[0] = 0x7b; //Segment 1
Alegrowin 5:e5fbf322c180 180 cmd[1] = 0x7f;
Alegrowin 5:e5fbf322c180 181
Alegrowin 5:e5fbf322c180 182 i2c.write(Afficheur_addr,cmd,2);
Alegrowin 5:e5fbf322c180 183
Alegrowin 5:e5fbf322c180 184 wait(0.07);
Alegrowin 5:e5fbf322c180 185
Alegrowin 5:e5fbf322c180 186 cmd[0] = 0x7c; //Segment 2
Alegrowin 5:e5fbf322c180 187 cmd[1] = 0x7f;
Alegrowin 5:e5fbf322c180 188
Alegrowin 5:e5fbf322c180 189 i2c.write(Afficheur_addr,cmd,2);
Alegrowin 5:e5fbf322c180 190
Alegrowin 5:e5fbf322c180 191 wait(0.07);
Alegrowin 5:e5fbf322c180 192
Alegrowin 5:e5fbf322c180 193 cmd[0] = 0x7d; //Segment 3
Alegrowin 5:e5fbf322c180 194 cmd[1] = 0x7f;
Alegrowin 5:e5fbf322c180 195
Alegrowin 5:e5fbf322c180 196 i2c.write(Afficheur_addr,cmd,2);
Alegrowin 5:e5fbf322c180 197
Alegrowin 5:e5fbf322c180 198 wait(0.07);
Alegrowin 5:e5fbf322c180 199
Alegrowin 5:e5fbf322c180 200 cmd[0] = 0x7e; //Segment 4
Alegrowin 5:e5fbf322c180 201 cmd[1] = 0x7f;
Alegrowin 5:e5fbf322c180 202
Alegrowin 5:e5fbf322c180 203 i2c.write(Afficheur_addr,cmd,2);
Alegrowin 0:b325845b05af 204 }