
App sans librairie
main.cpp@0:69f8eade26c2, 2017-01-18 (annotated)
- Committer:
- ABuche
- Date:
- Wed Jan 18 15:10:41 2017 +0000
- Revision:
- 0:69f8eade26c2
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ABuche | 0:69f8eade26c2 | 1 | #include "mbed.h" |
ABuche | 0:69f8eade26c2 | 2 | |
ABuche | 0:69f8eade26c2 | 3 | // SANS LIBRAIRIE |
ABuche | 0:69f8eade26c2 | 4 | |
ABuche | 0:69f8eade26c2 | 5 | DigitalOut myled(LED1); |
ABuche | 0:69f8eade26c2 | 6 | I2C i2c(p9, p10); |
ABuche | 0:69f8eade26c2 | 7 | |
ABuche | 0:69f8eade26c2 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
ABuche | 0:69f8eade26c2 | 9 | |
ABuche | 0:69f8eade26c2 | 10 | void initUART(unsigned int baudRate){ |
ABuche | 0:69f8eade26c2 | 11 | volatile int* const PCONP = (int*) 0x400FC0C4; |
ABuche | 0:69f8eade26c2 | 12 | volatile int* const PCLKSEL1 = (int*) 0x400FC1AC; |
ABuche | 0:69f8eade26c2 | 13 | volatile int* const U2LCR = (int*) 0x4009800C; |
ABuche | 0:69f8eade26c2 | 14 | volatile int* const U2DLL = (int*) 0x40098000; |
ABuche | 0:69f8eade26c2 | 15 | volatile int* const U2DLM = (int*) 0x40098004; |
ABuche | 0:69f8eade26c2 | 16 | volatile int* const PINSEL0 = (int*) 0x4002C000; |
ABuche | 0:69f8eade26c2 | 17 | |
ABuche | 0:69f8eade26c2 | 18 | int const clock = 100000000; |
ABuche | 0:69f8eade26c2 | 19 | int const clockDiv = 3; |
ABuche | 0:69f8eade26c2 | 20 | int const peripheralClock = clock >> clockDiv; |
ABuche | 0:69f8eade26c2 | 21 | int const dlmDll = peripheralClock / (baudRate << 4); |
ABuche | 0:69f8eade26c2 | 22 | |
ABuche | 0:69f8eade26c2 | 23 | *PCONP |= 0x1000000; |
ABuche | 0:69f8eade26c2 | 24 | *PCLKSEL1 |= 0x30000; |
ABuche | 0:69f8eade26c2 | 25 | *U2LCR |= 0x80; |
ABuche | 0:69f8eade26c2 | 26 | *U2DLL = dlmDll & 0xFF; |
ABuche | 0:69f8eade26c2 | 27 | *U2DLM = dlmDll & 0xFF00; |
ABuche | 0:69f8eade26c2 | 28 | *U2LCR = 0x3; |
ABuche | 0:69f8eade26c2 | 29 | *PINSEL0 |= 0x500000; |
ABuche | 0:69f8eade26c2 | 30 | } |
ABuche | 0:69f8eade26c2 | 31 | |
ABuche | 0:69f8eade26c2 | 32 | void sendCharToSS(char data){ |
ABuche | 0:69f8eade26c2 | 33 | volatile int* const U2LSR = (int*) 0x40098014; |
ABuche | 0:69f8eade26c2 | 34 | volatile int* const THR = (int*) 0x40098000; |
ABuche | 0:69f8eade26c2 | 35 | |
ABuche | 0:69f8eade26c2 | 36 | while (*U2LSR & 0x20 == 0) wait_us(25); |
ABuche | 0:69f8eade26c2 | 37 | |
ABuche | 0:69f8eade26c2 | 38 | *THR = data; |
ABuche | 0:69f8eade26c2 | 39 | } |
ABuche | 0:69f8eade26c2 | 40 | |
ABuche | 0:69f8eade26c2 | 41 | int main() { |
ABuche | 0:69f8eade26c2 | 42 | initUART(9600); |
ABuche | 0:69f8eade26c2 | 43 | int addr = 0x1D << 1; |
ABuche | 0:69f8eade26c2 | 44 | |
ABuche | 0:69f8eade26c2 | 45 | char cmd[2]; |
ABuche | 0:69f8eade26c2 | 46 | |
ABuche | 0:69f8eade26c2 | 47 | cmd[0] = 0x0D; |
ABuche | 0:69f8eade26c2 | 48 | i2c.write(addr, cmd, 1, true); |
ABuche | 0:69f8eade26c2 | 49 | i2c.read(addr, cmd, 1); |
ABuche | 0:69f8eade26c2 | 50 | |
ABuche | 0:69f8eade26c2 | 51 | pc.printf("Reponse = 0x%02x \r\n",cmd[0]); |
ABuche | 0:69f8eade26c2 | 52 | pc.printf((cmd[0] == 0x2a)?"Everything is working fine!":"Oopps, it seems there is an issue..."); |
ABuche | 0:69f8eade26c2 | 53 | |
ABuche | 0:69f8eade26c2 | 54 | cmd[1] = 0x01; |
ABuche | 0:69f8eade26c2 | 55 | i2c.write(addr, cmd, 2); |
ABuche | 0:69f8eade26c2 | 56 | |
ABuche | 0:69f8eade26c2 | 57 | cmd[0] = 0x0F; |
ABuche | 0:69f8eade26c2 | 58 | cmd[1] = 0x30; |
ABuche | 0:69f8eade26c2 | 59 | i2c.write(addr, cmd, 2); |
ABuche | 0:69f8eade26c2 | 60 | |
ABuche | 0:69f8eade26c2 | 61 | cac(0x7A); |
ABuche | 0:69f8eade26c2 | 62 | cac(0xFF); |
ABuche | 0:69f8eade26c2 | 63 | cac(0x76); |
ABuche | 0:69f8eade26c2 | 64 | cac(0x77); |
ABuche | 0:69f8eade26c2 | 65 | cac(0x02); |
ABuche | 0:69f8eade26c2 | 66 | |
ABuche | 0:69f8eade26c2 | 67 | int his[20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
ABuche | 0:69f8eade26c2 | 68 | int length = 20; |
ABuche | 0:69f8eade26c2 | 69 | int cpt = 0; |
ABuche | 0:69f8eade26c2 | 70 | |
ABuche | 0:69f8eade26c2 | 71 | while(1) { |
ABuche | 0:69f8eade26c2 | 72 | char acc[2]; |
ABuche | 0:69f8eade26c2 | 73 | cmd[0] = 0x05; |
ABuche | 0:69f8eade26c2 | 74 | i2c.write(addr, cmd, 1, true); |
ABuche | 0:69f8eade26c2 | 75 | i2c.read(addr, acc, 2); |
ABuche | 0:69f8eade26c2 | 76 | |
ABuche | 0:69f8eade26c2 | 77 | // Si négatif, propagation du bit de signe |
ABuche | 0:69f8eade26c2 | 78 | int z = acc[0] > 127 ? 0xFFFFF000 | (acc[0] << 4) : acc[0] << 4; |
ABuche | 0:69f8eade26c2 | 79 | |
ABuche | 0:69f8eade26c2 | 80 | // Ajout des LSB |
ABuche | 0:69f8eade26c2 | 81 | z += acc[1] >> 4; |
ABuche | 0:69f8eade26c2 | 82 | |
ABuche | 0:69f8eade26c2 | 83 | z = z > 2047 ? 2048 - z : z; |
ABuche | 0:69f8eade26c2 | 84 | z = z > 1024 ? 1024 : z; |
ABuche | 0:69f8eade26c2 | 85 | z = z < -1024 ? -1024 : z; |
ABuche | 0:69f8eade26c2 | 86 | |
ABuche | 0:69f8eade26c2 | 87 | pc.printf("%d \r\n", z); |
ABuche | 0:69f8eade26c2 | 88 | |
ABuche | 0:69f8eade26c2 | 89 | int angle = acos((double)abs(z)/1024.0) * 5729; //5729 = 180 / pi * 100 |
ABuche | 0:69f8eade26c2 | 90 | his[cpt] = angle; |
ABuche | 0:69f8eade26c2 | 91 | cpt++; |
ABuche | 0:69f8eade26c2 | 92 | cpt %= length; |
ABuche | 0:69f8eade26c2 | 93 | |
ABuche | 0:69f8eade26c2 | 94 | angle = 0; |
ABuche | 0:69f8eade26c2 | 95 | for (int i = 0; i < length; i++) angle += his[i]; |
ABuche | 0:69f8eade26c2 | 96 | |
ABuche | 0:69f8eade26c2 | 97 | angle /= length; |
ABuche | 0:69f8eade26c2 | 98 | |
ABuche | 0:69f8eade26c2 | 99 | pc.printf("Angle : %d\r\n", angle); |
ABuche | 0:69f8eade26c2 | 100 | |
ABuche | 0:69f8eade26c2 | 101 | sendCharToSS((char)(angle / 1000)); |
ABuche | 0:69f8eade26c2 | 102 | sendCharToSS((char)((angle / 100) % 10)); |
ABuche | 0:69f8eade26c2 | 103 | sendCharToSS((char)((angle / 10) % 10)); |
ABuche | 0:69f8eade26c2 | 104 | sendCharToSS((char)(angle % 10)); |
ABuche | 0:69f8eade26c2 | 105 | |
ABuche | 0:69f8eade26c2 | 106 | pc.printf("--------- \r\n"); |
ABuche | 0:69f8eade26c2 | 107 | |
ABuche | 0:69f8eade26c2 | 108 | wait(0.025); |
ABuche | 0:69f8eade26c2 | 109 | } |
ABuche | 0:69f8eade26c2 | 110 | } |