Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 219 → Rev 226

/branches/v0.60_MicroMag3_Nick666/compass.c
20,14 → 20,13
void init_spi(void)
//############################################################################
{
SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Interrupt an, Master, 156 kHz Oszillator
//SPSR = (1<<SPI2X);
DDRB |= (1<<PB2)|(1<<PB7)|(1<<PB5); // J8, SCK, MOSI Ausgang (MicroMag3)
DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang
MM3.AXIS = MM3_X;
MM3.STATE = MM3_RESET;
PORTD &= ~(1<<PD3); // J5 einmalig auf Low -> SSNOT
}
 
 
44,26 → 43,26
break;
case MM3_START_TRANSFER:
PORTB &= ~(1<<PB2); // J8 auf Low
PORTB &= ~(1<<PB2); // J8 auf Low (war ~125 µs auf High)
if (MM3.AXIS == MM3_X) SPDR = 0x41; // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
else if (MM3.AXIS == MM3_Y) SPDR = 0x42;
else if (MM3.AXIS == MM3_Z) SPDR = 0x43;
if (MM3.AXIS == MM3_X) SPDR = 0x51; // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus
else if (MM3.AXIS == MM3_Y) SPDR = 0x52; // Micromag Period Select ist auf 1024 (0x50)
else if (MM3.AXIS == MM3_Z) SPDR = 0x53; // 1: x-Achse, 2: Y-Achse, 3: Z-Achse
else {MM3.STATE == MM3_IDLE;break;}
MM3.DRDY = SetDelay(8);
MM3.DRDY = SetDelay(15); // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024)
MM3.STATE = MM3_WAIT_DRDY;
break;
case MM3_WAIT_DRDY:
if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird
if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei
break;
case MM3_IDLE:
break;
}
}
 
//############################################################################
//SPI byte ready
72,28 → 71,59
{
switch (MM3.STATE)
{
case MM3_DRDY:
case MM3_DRDY:
// 1. Byte ist da, abspeichern, an die MSB-Stelle rücken und Übertragung von 2. Byte auslösen
if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;}
if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;}
if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;}
case MM3_X_BYTE2:
MM3.x_axis |= SPDR;
case MM3_X_BYTE2: // 2. Byte der entsprechenden Achse ist da.
MM3.x_axis |= SPDR;
MM3.x_axis -= OFF_X; // Sofort Offset aus der Kalibrierung berücksichtigen
//MM3.x_axis /= GAIN_X;
MM3.AXIS = MM3_Y;
MM3.STATE = MM3_RESET;
break;
case MM3_Y_BYTE2:
MM3.y_axis |= SPDR;
MM3.y_axis |= SPDR;
MM3.y_axis -= OFF_Y;
//MM3.y_axis /= GAIN_Y;
MM3.AXIS = MM3_Z;
MM3.STATE = MM3_RESET;
break;
case MM3_Z_BYTE2:
MM3.z_axis |= SPDR;
MM3.z_axis |= SPDR;
MM3.z_axis -= OFF_Z;
//MM3.z_axis /= GAIN_Z;
MM3.AXIS = MM3_X;
MM3.STATE = MM3_RESET;
MM3.STATE = MM3_RESET;
// Zeitnahe Berechnung der Nick-/Rollneigung in °
MM3.NickGrad = IntegralNick/Int2Grad_Faktor;
MM3.RollGrad = IntegralRoll/Int2Grad_Faktor;
break;
}
}
 
signed int MM3_heading(void)
{
float sin_nick, cos_nick, sin_roll, cos_roll;
signed int x_corr, y_corr;
signed int heading;
//Berechung von sinus und cosinus
sin_nick = (float)sin_i(MM3.NickGrad)/1000;
cos_nick = (float)cos_i(MM3.NickGrad)/1000;
sin_roll = (float)sin_i(MM3.RollGrad)/1000;
cos_roll = (float)cos_i(MM3.RollGrad)/1000;
//Neigungskompensation
y_corr = ((cos_roll * MM3.y_axis) + (sin_roll * MM3.z_axis));
x_corr = (((sin_roll * MM3.y_axis) - (cos_roll * MM3.z_axis)) * sin_nick) + (cos_nick * MM3.x_axis);
 
//Winkelberechnung
heading = arctan_f(x_corr, y_corr);
 
return (heading);
}