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
- Committer:
- MarcoF89
- Date:
- 2017-08-02
- Revision:
- 6:27a09e8bebfb
- Parent:
- 5:584acd257531
- Child:
- 7:a54c97795013
File content as of revision 6:27a09e8bebfb:
#include "mbed.h" #include "stdio.h" int16_t high, low; extern Serial pc(SERIAL_TX, SERIAL_RX); extern SPI spi(PE_6,PE_5,PE_2); //mosi,miso,sclk extern DigitalOut ncs(PE_4); //ssel /**********************/ /*Initialisieren**Gyro*/ /**********************/ int initialisierung_gyro() { spi.format(8,0); spi.frequency(1000000); ncs = 0; spi.write(0x6B); // Register 107 spi.write(0x80); //Reset // Standby off ncs = 1; wait_ms(1000); ncs=0; spi.write(0x1A); //CONFIG write // DLPF_CFG // Register 26 spi.write(0x06); //Bandwidth: 250Hz// Delay: 0.97ms// Fs: 8kHz ncs = 1; wait_ms(1); ncs=0; spi.write(0x1B); //Gyro_CONFIG write spi.write(0x18); //Max. Skalenwert//00=+250dps;08=+500dps;10=+1000dps;18=+2000dps ncs = 1; wait_ms(1); ncs = 0; spi.write(0x17); // Register 23 spi.write(0x00); // Offset High Byte Z ncs = 1; wait_ms(1); ncs = 0; spi.write(0x18); // Register 24 spi.write(0x17); // Offset Low Byte Z ncs = 1; wait_ms(1000); } /**********************/ /*Initialisieren Acc */ /**********************/ int16_t initialisierung_acc () { int i,faktor = 0x00; for(i=0;i<=2;i++) { ncs=0; spi.write(0x1c); //ACC_CONFIG write spi.write(faktor); //Skalierung 00=2g;08=4g;10=8g;18=16g ncs=1; //Teilung 16384;8192;4096;2048 wait_us(0.1); ncs=0; spi.write(0x1d); //ACC_CONFIG_2 08=460Hz;09=184Hz;0a=92Hz spi.write(0x0e); //TP-Filter 0b=41Hz;0c=20Hz;0d=10Hz;0e=5Hz ncs=1; wait_us(0.1); } switch (faktor) { case 0x00: return 16384; break; case 0x08: return 8192; break; case 0x10: return 4096; break; case 0x18: return 2048; break; } } /***************/ /*Messen Gyro Z*/ /***************/ int16_t aktuell_gyro_z() { ncs = 0; spi.write(0xc7); //Z_OUT_H high = spi.write(0x0); ncs = 1; wait_us(0.1); ncs = 0; spi.write(0xc8); //Z_OUT_L low = spi.write(0x0); ncs = 1; wait_us(0.1); return (low | high << 8); } /***************/ /*Messen Gyro X*/ /***************/ int16_t aktuell_gyro_x() { ncs = 0; spi.write(0xc3); //Z_OUT_H high = spi.write(0x0); ncs = 1; wait_us(1); ncs = 0; spi.write(0xc4); //Z_OUT_L low = spi.write(0x0); ncs = 1; wait_us(0.1); return low | high << 8;; } /***************/ /*Messen Gyro Y*/ /***************/ int16_t aktuell_gyro_y() { ncs = 0; spi.write(0xc5); //Z_OUT_H high = spi.write(0x0); ncs = 1; wait_us(0.1); ncs = 0; spi.write(0xc6); //Z_OUT_L low = spi.write(0x0); ncs = 1; wait_us(0.1); return low | high << 8; } /************/ /*Messen Acc*/ /************/ int16_t aktuell_acc_x () { ncs=0; spi.write(0xbb); high = spi.write(0x0); ncs=1; wait_us(0.1); ncs=0; spi.write(0xbc); low = spi.write(0x0); ncs=1; wait_us(0.1); return low | high<<8; } int16_t aktuell_acc_y () { ncs=0; spi.write(0xbd); high = spi.write(0x0); ncs=1; wait_us(0.1); ncs=0; spi.write(0xbe); low = spi.write(0x0); ncs=1; wait_us(0.1); return low | high<<8; } int16_t aktuell_acc_z () { ncs=0; spi.write(0xbf); high = spi.write(0x0); ncs=1; wait_us(0.1); ncs=0; spi.write(0xc0); low = spi.write(0x0); ncs=1; wait_us(0.1); return low | high<<8; }