![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Iniversity work
main.cpp@2:8d0e48d38611, 2017-09-02 (annotated)
- Committer:
- vinceP
- Date:
- Sat Sep 02 20:02:17 2017 +0000
- Revision:
- 2:8d0e48d38611
- Parent:
- 1:0630912bb400
- Child:
- 3:7db4d7f82176
t
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vinceP | 2:8d0e48d38611 | 1 | #include "mbed.h" |
vinceP | 2:8d0e48d38611 | 2 | #include "MMA8452.h" |
vinceP | 2:8d0e48d38611 | 3 | #include <math.h> |
vinceP | 2:8d0e48d38611 | 4 | |
vinceP | 0:ca687da7d225 | 5 | Serial pc(USBTX, USBRX); |
vinceP | 2:8d0e48d38611 | 6 | SPI spiTest(p5, p6, p7,p8); |
vinceP | 2:8d0e48d38611 | 7 | MMA8452 acc(p9, p10, 100000); |
vinceP | 0:ca687da7d225 | 8 | |
vinceP | 1:0630912bb400 | 9 | PwmOut led1(LED1); |
vinceP | 1:0630912bb400 | 10 | PwmOut led2(LED2); |
vinceP | 1:0630912bb400 | 11 | PwmOut led3(LED3); |
vinceP | 1:0630912bb400 | 12 | |
vinceP | 1:0630912bb400 | 13 | void calculateAngle(); |
vinceP | 2:8d0e48d38611 | 14 | void display(int number); |
vinceP | 1:0630912bb400 | 15 | |
vinceP | 0:ca687da7d225 | 16 | int main() { |
vinceP | 2:8d0e48d38611 | 17 | int number = 10; |
vinceP | 2:8d0e48d38611 | 18 | char c[50]; |
vinceP | 2:8d0e48d38611 | 19 | while(1) { |
vinceP | 2:8d0e48d38611 | 20 | sprintf(c,"%f", number); |
vinceP | 2:8d0e48d38611 | 21 | pc.printf("x:%c \r\n",c[0]); |
vinceP | 2:8d0e48d38611 | 22 | pc.printf("x:%s \r\n",c); |
vinceP | 2:8d0e48d38611 | 23 | //calculateAngle(); |
vinceP | 1:0630912bb400 | 24 | wait(1); |
vinceP | 2:8d0e48d38611 | 25 | number--; |
vinceP | 1:0630912bb400 | 26 | } |
vinceP | 1:0630912bb400 | 27 | } |
vinceP | 1:0630912bb400 | 28 | |
vinceP | 2:8d0e48d38611 | 29 | void calculateAngle(int x,int y, int z){ |
vinceP | 2:8d0e48d38611 | 30 | //double x, y, z; |
vinceP | 1:0630912bb400 | 31 | float val = 180.0 / 3.1416; |
vinceP | 2:8d0e48d38611 | 32 | double calcule = (1-2*((x*x+y*y)/(x*x+y*y+z*z))); |
vinceP | 2:8d0e48d38611 | 33 | //acc.readXYZGravity(&x,&y,&z); |
vinceP | 2:8d0e48d38611 | 34 | /*x = -0.016; |
vinceP | 2:8d0e48d38611 | 35 | y = -0.877; |
vinceP | 2:8d0e48d38611 | 36 | z = 0.499;*/ |
vinceP | 1:0630912bb400 | 37 | led1 = abs(x); |
vinceP | 1:0630912bb400 | 38 | led2 = abs(y); |
vinceP | 1:0630912bb400 | 39 | led3 = abs(z); |
vinceP | 2:8d0e48d38611 | 40 | //float amplitude =sqrt(x*x+y*y+z*z); |
vinceP | 2:8d0e48d38611 | 41 | //float crossProductVector = sqrt(x*x+y*y); |
vinceP | 2:8d0e48d38611 | 42 | //float t = -1*asin(crossProductVector/amplitude)*val; //was z before |
vinceP | 2:8d0e48d38611 | 43 | //float e = 0.5*acos(1-2*((pow(x,2)+pow(y,2))/((pow(x,2)+pow(y,2)+pow(z,2)))))*val; |
vinceP | 2:8d0e48d38611 | 44 | float e2 = 0.5*acos(calcule)*val; |
vinceP | 1:0630912bb400 | 45 | pc.printf("x:%lf y:%lf z:%lf\r\n",x,y,z); |
vinceP | 2:8d0e48d38611 | 46 | pc.printf("angle: %lf\r\n",e2); |
vinceP | 2:8d0e48d38611 | 47 | } |
vinceP | 2:8d0e48d38611 | 48 | void display(int number){ |
vinceP | 2:8d0e48d38611 | 49 | char c[6]; |
vinceP | 2:8d0e48d38611 | 50 | //Explication de la fonction sprintf https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm |
vinceP | 2:8d0e48d38611 | 51 | int test = sprintf(c,"%*.4Lf", number); |
vinceP | 2:8d0e48d38611 | 52 | //sprintf(c,"%ld", number); |
vinceP | 2:8d0e48d38611 | 53 | spiTest.write(0x76); |
vinceP | 2:8d0e48d38611 | 54 | pc.printf("c = %u\r\n", c[3] == '\0'); |
vinceP | 2:8d0e48d38611 | 55 | pc.printf("test = %d\r\n", test); |
vinceP | 2:8d0e48d38611 | 56 | |
vinceP | 2:8d0e48d38611 | 57 | switch(test) { |
vinceP | 2:8d0e48d38611 | 58 | case 1: |
vinceP | 2:8d0e48d38611 | 59 | spiTest.write(0x7E); |
vinceP | 2:8d0e48d38611 | 60 | spiTest.write(c[0]); |
vinceP | 2:8d0e48d38611 | 61 | break; |
vinceP | 2:8d0e48d38611 | 62 | case 2: |
vinceP | 2:8d0e48d38611 | 63 | spiTest.write(0x7D|c[0]); |
vinceP | 2:8d0e48d38611 | 64 | spiTest.write(0x7E|c[1]); |
vinceP | 2:8d0e48d38611 | 65 | break; |
vinceP | 2:8d0e48d38611 | 66 | case 3: |
vinceP | 2:8d0e48d38611 | 67 | break; |
vinceP | 2:8d0e48d38611 | 68 | } |
vinceP | 2:8d0e48d38611 | 69 | |
vinceP | 2:8d0e48d38611 | 70 | int n = sizeof(c)/sizeof(c[0]); |
vinceP | 2:8d0e48d38611 | 71 | pc.printf("n = %i\r\n", n); |
vinceP | 2:8d0e48d38611 | 72 | //control pour le décimal |
vinceP | 2:8d0e48d38611 | 73 | spiTest.write(0x77); // Decimal control command |
vinceP | 2:8d0e48d38611 | 74 | //valeur possible |
vinceP | 2:8d0e48d38611 | 75 | //0b00000001 = 1.234 |
vinceP | 2:8d0e48d38611 | 76 | //0b00000010 = 12.34 |
vinceP | 2:8d0e48d38611 | 77 | //0b00000100 = 123.4 |
vinceP | 2:8d0e48d38611 | 78 | spiTest.write(0b00000001); |
vinceP | 2:8d0e48d38611 | 79 | /*for (int i=4; i>=0; i--){ |
vinceP | 2:8d0e48d38611 | 80 | cs = 0; |
vinceP | 2:8d0e48d38611 | 81 | spiTest.write(c[i]); |
vinceP | 2:8d0e48d38611 | 82 | cs = 1; |
vinceP | 2:8d0e48d38611 | 83 | }*/ |
vinceP | 2:8d0e48d38611 | 84 | /*spiTest.write(c); |
vinceP | 2:8d0e48d38611 | 85 | ; |
vinceP | 2:8d0e48d38611 | 86 | spiTest.write(c[0]);*/ |
vinceP | 1:0630912bb400 | 87 | } |
vinceP | 1:0630912bb400 | 88 | /* |
vinceP | 2:8d0e48d38611 | 89 | |
vinceP | 1:0630912bb400 | 90 | |
vinceP | 1:0630912bb400 | 91 | int main() { |
vinceP | 1:0630912bb400 | 92 | //https://developer.mbed.org/handbook/SerialPC |
vinceP | 1:0630912bb400 | 93 | //https://developer.mbed.org/handbook/mbed-NXP-LPC1768-Getting-Started |
vinceP | 1:0630912bb400 | 94 | //pc.printf("Echoes back to the screen anything you type\n"); |
vinceP | 1:0630912bb400 | 95 | int nombre = 0; |
vinceP | 1:0630912bb400 | 96 | cs = 1; |
vinceP | 1:0630912bb400 | 97 | spiTest.frequency(250000); |
vinceP | 1:0630912bb400 | 98 | cs = 0; |
vinceP | 1:0630912bb400 | 99 | spiTest.write(0x81); |
vinceP | 1:0630912bb400 | 100 | //int t = spiTest.write(nombre); |
vinceP | 1:0630912bb400 | 101 | //pc.printf("Nombre = %X\r\n", t); |
vinceP | 1:0630912bb400 | 102 | cs = 1; |
vinceP | 0:ca687da7d225 | 103 | while(1) { |
vinceP | 1:0630912bb400 | 104 | //uart.printf("%c",pc.getc()); |
vinceP | 1:0630912bb400 | 105 | wait(0.7); |
vinceP | 1:0630912bb400 | 106 | nombre++; |
vinceP | 1:0630912bb400 | 107 | display(nombre); |
vinceP | 1:0630912bb400 | 108 | pc.printf("Nombre = %X\r\n", nombre); |
vinceP | 1:0630912bb400 | 109 | if(nombre == 15){ |
vinceP | 1:0630912bb400 | 110 | return(0); |
vinceP | 1:0630912bb400 | 111 | } |
vinceP | 0:ca687da7d225 | 112 | } |
vinceP | 0:ca687da7d225 | 113 | } |
vinceP | 2:8d0e48d38611 | 114 | |
vinceP | 0:ca687da7d225 | 115 | |
vinceP | 1:0630912bb400 | 116 | /* Ignore cette partie c'Est le lab |
vinceP | 0:ca687da7d225 | 117 | int main() { |
vinceP | 0:ca687da7d225 | 118 | //https://developer.mbed.org/handbook/SerialPC |
vinceP | 0:ca687da7d225 | 119 | //https://developer.mbed.org/handbook/mbed-NXP-LPC1768-Getting-Started |
vinceP | 0:ca687da7d225 | 120 | //pc.printf("Echoes back to the screen anything you type\n"); |
vinceP | 0:ca687da7d225 | 121 | int nombre = 2; |
vinceP | 0:ca687da7d225 | 122 | cs = 1; |
vinceP | 0:ca687da7d225 | 123 | //uart.printf("%d",nombre); |
vinceP | 0:ca687da7d225 | 124 | // Setup the spi for 8 bit data, high steady state clock, |
vinceP | 0:ca687da7d225 | 125 | // second edge capture, with a 1MHz clock rate |
vinceP | 0:ca687da7d225 | 126 | // spiTest.format(8,3); |
vinceP | 0:ca687da7d225 | 127 | // spiTest.frequency(1000000); |
vinceP | 0:ca687da7d225 | 128 | cs = 0; |
vinceP | 0:ca687da7d225 | 129 | |
vinceP | 0:ca687da7d225 | 130 | // Send 0x8f, the command to read the WHOAMI register |
vinceP | 0:ca687da7d225 | 131 | int t = spiTest.write(0x8F); |
vinceP | 0:ca687da7d225 | 132 | pc.printf("WHOAMI register = %X\r\n", t); |
vinceP | 0:ca687da7d225 | 133 | |
vinceP | 0:ca687da7d225 | 134 | // Send a dummy byte to receive the contents of the WHOAMI register |
vinceP | 0:ca687da7d225 | 135 | //int whoami = spiTest.write(0x00); |
vinceP | 0:ca687da7d225 | 136 | //printf("WHOAMI register = 0x%X\n", whoami); |
vinceP | 0:ca687da7d225 | 137 | cs = 1; |
vinceP | 0:ca687da7d225 | 138 | while(1) { |
vinceP | 0:ca687da7d225 | 139 | //uart.printf("%c",pc.getc()); |
vinceP | 0:ca687da7d225 | 140 | cs = 0; |
vinceP | 0:ca687da7d225 | 141 | |
vinceP | 0:ca687da7d225 | 142 | // Send 0x8f, the command to read the WHOAMI register |
vinceP | 0:ca687da7d225 | 143 | pc.scanf("%d",&nombre); |
vinceP | 0:ca687da7d225 | 144 | t = spiTest.write(nombre); |
vinceP | 0:ca687da7d225 | 145 | pc.printf("WHOAMI register = %X\r\n", t); |
vinceP | 0:ca687da7d225 | 146 | cs = 0; |
vinceP | 0:ca687da7d225 | 147 | myled = 1; |
vinceP | 0:ca687da7d225 | 148 | wait(0.2); |
vinceP | 0:ca687da7d225 | 149 | myled = 0; |
vinceP | 0:ca687da7d225 | 150 | wait(0.2); |
vinceP | 0:ca687da7d225 | 151 | //pc.putc(pc.getc()); |
vinceP | 0:ca687da7d225 | 152 | } |
vinceP | 0:ca687da7d225 | 153 | } |
vinceP | 0:ca687da7d225 | 154 | */ |