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