Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Quadrocopter by
messen/messen.cpp@6:27a09e8bebfb, 2017-08-02 (annotated)
- Committer:
- MarcoF89
- Date:
- Wed Aug 02 13:11:30 2017 +0000
- Revision:
- 6:27a09e8bebfb
- Parent:
- 5:584acd257531
- Child:
- 7:a54c97795013
Initialisierung; Auslesen der Rohdaten
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MarcoF89 | 4:3eaf38e4809f | 1 | #include "mbed.h" |
MarcoF89 | 4:3eaf38e4809f | 2 | #include "stdio.h" |
MarcoF89 | 4:3eaf38e4809f | 3 | |
MarcoF89 | 6:27a09e8bebfb | 4 | int16_t high, low; |
MarcoF89 | 4:3eaf38e4809f | 5 | |
MarcoF89 | 4:3eaf38e4809f | 6 | extern Serial pc(SERIAL_TX, SERIAL_RX); |
MarcoF89 | 4:3eaf38e4809f | 7 | extern SPI spi(PE_6,PE_5,PE_2); //mosi,miso,sclk |
MarcoF89 | 4:3eaf38e4809f | 8 | extern DigitalOut ncs(PE_4); //ssel |
MarcoF89 | 4:3eaf38e4809f | 9 | |
chriselsholz | 5:584acd257531 | 10 | /**********************/ |
chriselsholz | 5:584acd257531 | 11 | /*Initialisieren**Gyro*/ |
chriselsholz | 5:584acd257531 | 12 | /**********************/ |
chriselsholz | 5:584acd257531 | 13 | int initialisierung_gyro() |
chriselsholz | 5:584acd257531 | 14 | { |
chriselsholz | 5:584acd257531 | 15 | spi.format(8,0); |
chriselsholz | 5:584acd257531 | 16 | spi.frequency(1000000); |
chriselsholz | 5:584acd257531 | 17 | |
chriselsholz | 5:584acd257531 | 18 | ncs = 0; |
chriselsholz | 5:584acd257531 | 19 | spi.write(0x6B); // Register 107 |
chriselsholz | 5:584acd257531 | 20 | spi.write(0x80); //Reset // Standby off |
chriselsholz | 5:584acd257531 | 21 | ncs = 1; |
chriselsholz | 5:584acd257531 | 22 | wait_ms(1000); |
chriselsholz | 5:584acd257531 | 23 | |
chriselsholz | 5:584acd257531 | 24 | |
chriselsholz | 5:584acd257531 | 25 | ncs=0; |
chriselsholz | 5:584acd257531 | 26 | spi.write(0x1A); //CONFIG write // DLPF_CFG // Register 26 |
chriselsholz | 5:584acd257531 | 27 | spi.write(0x06); //Bandwidth: 250Hz// Delay: 0.97ms// Fs: 8kHz |
chriselsholz | 5:584acd257531 | 28 | ncs = 1; |
chriselsholz | 5:584acd257531 | 29 | wait_ms(1); |
chriselsholz | 5:584acd257531 | 30 | |
chriselsholz | 5:584acd257531 | 31 | ncs=0; |
chriselsholz | 5:584acd257531 | 32 | spi.write(0x1B); //Gyro_CONFIG write |
chriselsholz | 5:584acd257531 | 33 | spi.write(0x18); //Max. Skalenwert//00=+250dps;08=+500dps;10=+1000dps;18=+2000dps |
chriselsholz | 5:584acd257531 | 34 | ncs = 1; |
chriselsholz | 5:584acd257531 | 35 | wait_ms(1); |
chriselsholz | 5:584acd257531 | 36 | |
chriselsholz | 5:584acd257531 | 37 | ncs = 0; |
chriselsholz | 5:584acd257531 | 38 | spi.write(0x17); // Register 23 |
MarcoF89 | 6:27a09e8bebfb | 39 | spi.write(0x00); // Offset High Byte Z |
chriselsholz | 5:584acd257531 | 40 | ncs = 1; |
chriselsholz | 5:584acd257531 | 41 | wait_ms(1); |
chriselsholz | 5:584acd257531 | 42 | |
chriselsholz | 5:584acd257531 | 43 | ncs = 0; |
chriselsholz | 5:584acd257531 | 44 | spi.write(0x18); // Register 24 |
MarcoF89 | 6:27a09e8bebfb | 45 | spi.write(0x17); // Offset Low Byte Z |
chriselsholz | 5:584acd257531 | 46 | ncs = 1; |
chriselsholz | 5:584acd257531 | 47 | wait_ms(1000); |
chriselsholz | 5:584acd257531 | 48 | } |
chriselsholz | 5:584acd257531 | 49 | |
chriselsholz | 5:584acd257531 | 50 | /**********************/ |
chriselsholz | 5:584acd257531 | 51 | /*Initialisieren Acc */ |
chriselsholz | 5:584acd257531 | 52 | /**********************/ |
chriselsholz | 5:584acd257531 | 53 | |
chriselsholz | 5:584acd257531 | 54 | int16_t initialisierung_acc () |
chriselsholz | 5:584acd257531 | 55 | { |
chriselsholz | 5:584acd257531 | 56 | int i,faktor = 0x00; |
chriselsholz | 5:584acd257531 | 57 | for(i=0;i<=2;i++) |
chriselsholz | 5:584acd257531 | 58 | { |
chriselsholz | 5:584acd257531 | 59 | ncs=0; |
chriselsholz | 5:584acd257531 | 60 | spi.write(0x1c); //ACC_CONFIG write |
chriselsholz | 5:584acd257531 | 61 | spi.write(faktor); //Skalierung 00=2g;08=4g;10=8g;18=16g |
chriselsholz | 5:584acd257531 | 62 | ncs=1; //Teilung 16384;8192;4096;2048 |
chriselsholz | 5:584acd257531 | 63 | wait_us(0.1); |
chriselsholz | 5:584acd257531 | 64 | |
chriselsholz | 5:584acd257531 | 65 | ncs=0; |
chriselsholz | 5:584acd257531 | 66 | spi.write(0x1d); //ACC_CONFIG_2 08=460Hz;09=184Hz;0a=92Hz |
MarcoF89 | 6:27a09e8bebfb | 67 | spi.write(0x0e); //TP-Filter 0b=41Hz;0c=20Hz;0d=10Hz;0e=5Hz |
chriselsholz | 5:584acd257531 | 68 | ncs=1; |
chriselsholz | 5:584acd257531 | 69 | wait_us(0.1); |
chriselsholz | 5:584acd257531 | 70 | } |
chriselsholz | 5:584acd257531 | 71 | switch (faktor) |
chriselsholz | 5:584acd257531 | 72 | { |
chriselsholz | 5:584acd257531 | 73 | case 0x00: return 16384; break; |
chriselsholz | 5:584acd257531 | 74 | case 0x08: return 8192; break; |
chriselsholz | 5:584acd257531 | 75 | case 0x10: return 4096; break; |
chriselsholz | 5:584acd257531 | 76 | case 0x18: return 2048; break; |
chriselsholz | 5:584acd257531 | 77 | } |
chriselsholz | 5:584acd257531 | 78 | } |
chriselsholz | 5:584acd257531 | 79 | |
chriselsholz | 5:584acd257531 | 80 | |
chriselsholz | 5:584acd257531 | 81 | /***************/ |
chriselsholz | 5:584acd257531 | 82 | /*Messen Gyro Z*/ |
chriselsholz | 5:584acd257531 | 83 | /***************/ |
MarcoF89 | 4:3eaf38e4809f | 84 | |
MarcoF89 | 6:27a09e8bebfb | 85 | int16_t aktuell_gyro_z() |
MarcoF89 | 6:27a09e8bebfb | 86 | { |
MarcoF89 | 4:3eaf38e4809f | 87 | ncs = 0; |
MarcoF89 | 4:3eaf38e4809f | 88 | spi.write(0xc7); //Z_OUT_H |
MarcoF89 | 6:27a09e8bebfb | 89 | high = spi.write(0x0); |
MarcoF89 | 6:27a09e8bebfb | 90 | ncs = 1; |
MarcoF89 | 6:27a09e8bebfb | 91 | wait_us(0.1); |
MarcoF89 | 6:27a09e8bebfb | 92 | |
MarcoF89 | 6:27a09e8bebfb | 93 | ncs = 0; |
MarcoF89 | 6:27a09e8bebfb | 94 | spi.write(0xc8); //Z_OUT_L |
MarcoF89 | 6:27a09e8bebfb | 95 | low = spi.write(0x0); |
MarcoF89 | 6:27a09e8bebfb | 96 | ncs = 1; |
MarcoF89 | 6:27a09e8bebfb | 97 | wait_us(0.1); |
MarcoF89 | 6:27a09e8bebfb | 98 | return (low | high << 8); |
MarcoF89 | 6:27a09e8bebfb | 99 | } |
MarcoF89 | 6:27a09e8bebfb | 100 | |
MarcoF89 | 6:27a09e8bebfb | 101 | /***************/ |
MarcoF89 | 6:27a09e8bebfb | 102 | /*Messen Gyro X*/ |
MarcoF89 | 6:27a09e8bebfb | 103 | /***************/ |
MarcoF89 | 6:27a09e8bebfb | 104 | |
MarcoF89 | 6:27a09e8bebfb | 105 | int16_t aktuell_gyro_x() |
MarcoF89 | 6:27a09e8bebfb | 106 | { |
MarcoF89 | 6:27a09e8bebfb | 107 | ncs = 0; |
MarcoF89 | 6:27a09e8bebfb | 108 | spi.write(0xc3); //Z_OUT_H |
MarcoF89 | 6:27a09e8bebfb | 109 | high = spi.write(0x0); |
MarcoF89 | 4:3eaf38e4809f | 110 | ncs = 1; |
MarcoF89 | 4:3eaf38e4809f | 111 | wait_us(1); |
MarcoF89 | 4:3eaf38e4809f | 112 | |
MarcoF89 | 4:3eaf38e4809f | 113 | ncs = 0; |
MarcoF89 | 6:27a09e8bebfb | 114 | spi.write(0xc4); //Z_OUT_L |
MarcoF89 | 6:27a09e8bebfb | 115 | low = spi.write(0x0); |
MarcoF89 | 4:3eaf38e4809f | 116 | ncs = 1; |
MarcoF89 | 6:27a09e8bebfb | 117 | wait_us(0.1); |
MarcoF89 | 6:27a09e8bebfb | 118 | return low | high << 8;; |
MarcoF89 | 6:27a09e8bebfb | 119 | } |
MarcoF89 | 6:27a09e8bebfb | 120 | |
MarcoF89 | 6:27a09e8bebfb | 121 | /***************/ |
MarcoF89 | 6:27a09e8bebfb | 122 | /*Messen Gyro Y*/ |
MarcoF89 | 6:27a09e8bebfb | 123 | /***************/ |
MarcoF89 | 6:27a09e8bebfb | 124 | |
MarcoF89 | 6:27a09e8bebfb | 125 | int16_t aktuell_gyro_y() |
MarcoF89 | 6:27a09e8bebfb | 126 | { |
MarcoF89 | 6:27a09e8bebfb | 127 | ncs = 0; |
MarcoF89 | 6:27a09e8bebfb | 128 | spi.write(0xc5); //Z_OUT_H |
MarcoF89 | 6:27a09e8bebfb | 129 | high = spi.write(0x0); |
MarcoF89 | 6:27a09e8bebfb | 130 | ncs = 1; |
MarcoF89 | 6:27a09e8bebfb | 131 | wait_us(0.1); |
MarcoF89 | 6:27a09e8bebfb | 132 | |
MarcoF89 | 6:27a09e8bebfb | 133 | ncs = 0; |
MarcoF89 | 6:27a09e8bebfb | 134 | spi.write(0xc6); //Z_OUT_L |
MarcoF89 | 6:27a09e8bebfb | 135 | low = spi.write(0x0); |
MarcoF89 | 6:27a09e8bebfb | 136 | ncs = 1; |
MarcoF89 | 6:27a09e8bebfb | 137 | wait_us(0.1); |
MarcoF89 | 6:27a09e8bebfb | 138 | return low | high << 8; |
MarcoF89 | 4:3eaf38e4809f | 139 | } |
MarcoF89 | 4:3eaf38e4809f | 140 | |
chriselsholz | 5:584acd257531 | 141 | /************/ |
chriselsholz | 5:584acd257531 | 142 | /*Messen Acc*/ |
chriselsholz | 5:584acd257531 | 143 | /************/ |
MarcoF89 | 4:3eaf38e4809f | 144 | |
chriselsholz | 5:584acd257531 | 145 | int16_t aktuell_acc_x () |
chriselsholz | 5:584acd257531 | 146 | { |
MarcoF89 | 4:3eaf38e4809f | 147 | ncs=0; |
chriselsholz | 5:584acd257531 | 148 | spi.write(0xbb); |
MarcoF89 | 6:27a09e8bebfb | 149 | high = spi.write(0x0); |
chriselsholz | 5:584acd257531 | 150 | ncs=1; |
chriselsholz | 5:584acd257531 | 151 | wait_us(0.1); |
chriselsholz | 5:584acd257531 | 152 | |
chriselsholz | 5:584acd257531 | 153 | ncs=0; |
chriselsholz | 5:584acd257531 | 154 | spi.write(0xbc); |
MarcoF89 | 6:27a09e8bebfb | 155 | low = spi.write(0x0); |
chriselsholz | 5:584acd257531 | 156 | ncs=1; |
MarcoF89 | 6:27a09e8bebfb | 157 | wait_us(0.1); |
MarcoF89 | 6:27a09e8bebfb | 158 | return low | high<<8; |
chriselsholz | 5:584acd257531 | 159 | } |
MarcoF89 | 4:3eaf38e4809f | 160 | |
chriselsholz | 5:584acd257531 | 161 | int16_t aktuell_acc_y () |
chriselsholz | 5:584acd257531 | 162 | { |
chriselsholz | 5:584acd257531 | 163 | ncs=0; |
chriselsholz | 5:584acd257531 | 164 | spi.write(0xbd); |
MarcoF89 | 6:27a09e8bebfb | 165 | high = spi.write(0x0); |
chriselsholz | 5:584acd257531 | 166 | ncs=1; |
chriselsholz | 5:584acd257531 | 167 | wait_us(0.1); |
chriselsholz | 5:584acd257531 | 168 | |
chriselsholz | 5:584acd257531 | 169 | ncs=0; |
chriselsholz | 5:584acd257531 | 170 | spi.write(0xbe); |
MarcoF89 | 6:27a09e8bebfb | 171 | low = spi.write(0x0); |
chriselsholz | 5:584acd257531 | 172 | ncs=1; |
MarcoF89 | 6:27a09e8bebfb | 173 | wait_us(0.1); |
MarcoF89 | 6:27a09e8bebfb | 174 | return low | high<<8; |
chriselsholz | 5:584acd257531 | 175 | } |
MarcoF89 | 4:3eaf38e4809f | 176 | |
chriselsholz | 5:584acd257531 | 177 | int16_t aktuell_acc_z () |
chriselsholz | 5:584acd257531 | 178 | { |
chriselsholz | 5:584acd257531 | 179 | ncs=0; |
chriselsholz | 5:584acd257531 | 180 | spi.write(0xbf); |
MarcoF89 | 6:27a09e8bebfb | 181 | high = spi.write(0x0); |
chriselsholz | 5:584acd257531 | 182 | ncs=1; |
chriselsholz | 5:584acd257531 | 183 | wait_us(0.1); |
chriselsholz | 5:584acd257531 | 184 | |
chriselsholz | 5:584acd257531 | 185 | ncs=0; |
chriselsholz | 5:584acd257531 | 186 | spi.write(0xc0); |
MarcoF89 | 6:27a09e8bebfb | 187 | low = spi.write(0x0); |
MarcoF89 | 6:27a09e8bebfb | 188 | ncs=1; |
MarcoF89 | 6:27a09e8bebfb | 189 | wait_us(0.1); |
MarcoF89 | 6:27a09e8bebfb | 190 | return low | high<<8; |
chriselsholz | 5:584acd257531 | 191 | } |