Test
Dependencies: mbed AccelSensor
Revision 5:e5fbf322c180, committed 2013-01-16
- Comitter:
- Alegrowin
- Date:
- Wed Jan 16 00:10:09 2013 +0000
- Parent:
- 4:3397631c2f65
- Commit message:
- Menage dans le code ajout de condition de pre compileur
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 3397631c2f65 -r e5fbf322c180 main.cpp --- a/main.cpp Tue Jan 15 23:40:30 2013 +0000 +++ b/main.cpp Wed Jan 16 00:10:09 2013 +0000 @@ -1,136 +1,117 @@ #include "mbed.h" #include "AccelSensor.h" -#include <math.h> const int addr = 0x9A; // define the I2C Address for TC74-A0 const int Afficheur_addr = 0xE2; // define the I2C Address for 4*7seg display const int Accel_addr = 0x3A; // define the I2C Address for the Accelerometer //Defines for the serial output for debug -#define Output_Temperature true; +#define Output_Temperature true +#define Output_Accel true +#define Output_Accel_debug false //Creating the desired objetcs I2C i2c(p9, p10); // sda, scl Serial pc(USBTX, USBRX); // tx, rx -//Serial Damn(p28,p27); // Used as shortcut for printing result on 7seg using printf function as it is not available in I2C -//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 +Serial Damn(p28,p27); // Used as shortcut for printing result on 7seg using printf function as it is not available in I2C +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 //Mainly used to calibrate and start the device void init(void); +char get_Temperature(void); +void Display8888(void); +void Update_Accel(void); int main() { - int temp = 0; - int a,b,c; - char cmd[7]; - int result[3]; + char temp; init(); wait(1); //Make sure system is fully initialized - while(1) { - pc.printf("\r\n\nStart reading the temperature of TC74 on I2C\r\n"); + Display8888(); //Display initial text 8888 on the display - //--------------Afficheur LCD ------------------------------ - /* - cmd[0] = 0x7b; //Segment 1 - cmd[1] = 0x7f; - - i2c.write(Afficheur_addr,cmd,2); - - wait(0.07); - - cmd[0] = 0x7c; //Segment 2 - cmd[1] = 0x7f; - - i2c.write(Afficheur_addr,cmd,2); - - wait(0.07); - - cmd[0] = 0x7d; //Segment 3 - cmd[1] = 0x7f; + while(1) { + pc.printf("\r\n\nStart reading the temperature of TC74 on I2C\r\n"); + temp = get_Temperature(); + Update_Accel(); + + wait(2); - i2c.write(Afficheur_addr,cmd,2); - - wait(0.07); - - cmd[0] = 0x7e; //Segment 4 - cmd[1] = 0x7f; - - i2c.write(Afficheur_addr,cmd,2);*/ + } +} +//------------------Accelerometre --------------------------- - //------------------Accelerometre --------------------------- - //char temporary[2]; +void Update_Accel(void) +{ + //Variables + char cmd[7]; - /* cmd[0] = 0x2A; //Command :: CTRL reg - i2c.write(Accel_addr, cmd, 1); //Issue required command to perform a write of the command - i2c.read(Accel_addr, temporary,1); - - cmd[1] = temporary[0] |1; - i2c.write(Accel_addr, cmd, 2); //Issue required command to perform a write of the command - - pc.printf("ctrl Reg1 status :: %x\r\n\n", cmd[0]); + //Initialisation + cmd[0] = 0x00; //Command :: Status - */ - - cmd[0] = 0x00; //Command :: Status - i2c.write(Accel_addr, cmd, 1); //Issue required command to perform a write of the command - i2c.read(Accel_addr, cmd, 7); //Read the Data from the device + //Read all the register for X,Y and Z forces + i2c.write(Accel_addr, cmd, 1); //Issue required command to perform a write of the command + i2c.read(Accel_addr, cmd, 7); //Read the Data from the device +#if Output_Accel_debug + 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] ); +#endif + //Damn.printf("v"); + long double anglez, anglex,angley; + bool negatif; + //calcul angle Z + if (cmd[5]<0x7F) { + anglez = acos((cmd[5]/62.0))/3.1416*180; + negatif=false; - 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] ); - //Damn.printf("v"); - long double anglez, anglex,angley; - bool negatif; - //calcul angle Z - if (cmd[5]<0x7F) { - anglez = acos((cmd[5]/62.0))/3.1416*180; - negatif=false; - - } else { - cmd[5]=(cmd[5]-256)*-1; - anglez = acos((cmd[5]/62.0))/3.1416*180; + } else { + cmd[5]=(cmd[5]-256)*-1; + anglez = acos((cmd[5]/62.0))/3.1416*180; - } + } - //calcul angle Y - if (cmd[3]<0x7F) { - angley = asin((cmd[3]/62.0))/3.1416*180; + //calcul angle Y + if (cmd[3]<0x7F) { + angley = asin((cmd[3]/62.0))/3.1416*180; - } else { - cmd[3]=(cmd[3]-256)*-1; - angley = asin((cmd[3]/62.0))/3.1416*180; + } else { + cmd[3]=(cmd[3]-256)*-1; + angley = asin((cmd[3]/62.0))/3.1416*180; - } - // calcul angle X - if (cmd[1]<0x7F) { - anglex = asin((cmd[1]/62.0))/3.1416*180; + } + // calcul angle X + if (cmd[1]<0x7F) { + anglex = asin((cmd[1]/62.0))/3.1416*180; - } else { - cmd[1]=(cmd[1]-256)*-1; - anglex = asin((cmd[1]/62.0))/3.1416*180; + } else { + cmd[1]=(cmd[1]-256)*-1; + anglex = asin((cmd[1]/62.0))/3.1416*180; - } + } - //if (anglez<45 - int b = anglez; + //if (anglez<45 + int b = anglez; +#if Output_Accel + pc.printf("TEST: X=%f Y=%f Z=%f \r\n", anglex, angley, anglez ); +#endif + if (negatif==false) + Damn.printf(" "); + else + Damn.printf(" -"); - pc.printf("TEST: X=%f Y=%f Z=%f \r\n", anglex, angley, anglez ); - if (negatif==false) - Damn.printf(" "); - else - Damn.printf(" -"); - - Damn.printf("%2d", b ); + Damn.printf("%2d", b ); - //-------------------Accel status form Imported Class -------- - acc.readData(result); - pc.printf("X: %d Y: %d Z: %d \r\n", result[0],result[1],result[2]); + //-------------------Accel status form Imported Class -------- +#if Output_Accel_debug + int result[3]; + acc.readData(result); + pc.printf("X: %d Y: %d Z: %d \r\n", result[0],result[1],result[2]); +#endif - wait(2); - } + } @@ -140,6 +121,7 @@ i2c.write(Afficheur_addr,"v",1); + acc.init(); acc.active(); } @@ -179,8 +161,44 @@ //Prints out the Data from Method 2 #if Output_Temperature - pc.printf("Method 2 :: %d\r\n\n", cmd[0]); + pc.printf("Temperature :: %d\r\n\n", cmd[0]); #endif return cmd[0]; +} + +/* +** This function display '8888' on the Hex Display using I2C transactions +** For prototyping purpose, we will use the Serial protocol for transferring the data to the Hex display +*/ +void Display8888(void) +{ + char cmd[2]; + //--------------Afficheur LCD ------------------------------ + + cmd[0] = 0x7b; //Segment 1 + cmd[1] = 0x7f; + + i2c.write(Afficheur_addr,cmd,2); + + wait(0.07); + + cmd[0] = 0x7c; //Segment 2 + cmd[1] = 0x7f; + + i2c.write(Afficheur_addr,cmd,2); + + wait(0.07); + + cmd[0] = 0x7d; //Segment 3 + cmd[1] = 0x7f; + + i2c.write(Afficheur_addr,cmd,2); + + wait(0.07); + + cmd[0] = 0x7e; //Segment 4 + cmd[1] = 0x7f; + + i2c.write(Afficheur_addr,cmd,2); } \ No newline at end of file