Chris Elsholz / Mbed 2 deprecated Quadrocopter

Dependencies:   mbed TextLCD

Fork of Quadrocopter by Marco Friedmann

messen/messen.cpp

Committer:
chriselsholz
Date:
2017-08-02
Revision:
5:584acd257531
Parent:
messen.cpp@ 4:3eaf38e4809f
Child:
6:27a09e8bebfb

File content as of revision 5:584acd257531:

#include "mbed.h"
#include "stdio.h"



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
    ncs = 1;               
    wait_ms(1); 
    
    ncs = 0;
    spi.write(0x18);     // Register 24
    spi.write(0x17);     // Offset Low Byte
    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*/
/***************/

int aktuell_gyro_z()
{
    int16_t z_high, z_low, z_high_low;   
    float messwert[7];
    char j, i;
    
    ncs = 0;
    spi.write(0xc7);                    //Z_OUT_H
    z_high = spi.write(0x0);
    ncs = 1;
    wait_us(1);
       
    ncs = 0;
    spi.write(0xc8);                    //Z_OUT_L
    z_low = spi.write(0x0);
    ncs = 1;
    wait_us(1);  
          
    z_high_low = z_low | z_high << 8;   //Low und High Byte zusammenfügen
    return z_high_low;                  
}

/************/
/*Messen Acc*/
/************/

int16_t aktuell_acc_x ()
{   
    ncs=0;
    spi.write(0xbb);            
    int16_t x_high = spi.write(0x0);
    ncs=1;
    wait_us(0.1);
   
    ncs=0;
    spi.write(0xbc);            
    int16_t x_low = spi.write(0x0);
    ncs=1;
    wait_us(0.1);  
    
    int16_t x = (x_high<<8) | x_low;
    return x; 
}    

int16_t aktuell_acc_y ()
{   
    ncs=0;
    spi.write(0xbd);            
    int16_t y_high = spi.write(0x0);
    ncs=1;
    wait_us(0.1);
   
    ncs=0;
    spi.write(0xbe);            
    int16_t y_low = spi.write(0x0);
    ncs=1;
    wait_us(0.1);  
    
    int16_t y = (y_high<<8) | y_low;
    return y; 
} 

int16_t aktuell_acc_z ()
{   
    ncs=0;
    spi.write(0xbf);            
    int16_t z_high = spi.write(0x0);
    ncs=1;
    wait_us(0.1);
   
    ncs=0;
    spi.write(0xc0);            
    int16_t z_low = spi.write(0x0);
    ncs=1;
    wait_us(0.1);  
    
    int16_t z = (z_high<<8) | z_low;
    return z; 
}