Subversion Repositories MK3Mag

Rev

Rev 21 | Rev 23 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 Rev 22
Line 89... Line 89...
89
Calibration_t eeCalibration EEMEM; // calibration data in EEProm
89
Calibration_t eeCalibration EEMEM; // calibration data in EEProm
90
Calibration_t Calibration;         // calibration data in RAM
90
Calibration_t Calibration;         // calibration data in RAM
Line 91... Line 91...
91
 
91
 
92
 
92
 
Line 93... Line 93...
93
int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ;       // sensor signal difference without Scaling
93
int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ;       // sensor signal difference without Scaling
Line 94... Line 94...
94
int16_t MagnetX, MagnetY, MagnetZ;                                      // rescaled magnetic field readings
94
int16_t MagnetX, MagnetY, MagnetZ;                      // rescaled magnetic field readings
Line 102... Line 102...
102
{
102
{
103
        UncalMagnetX = (1 * UncalMagnetX + (RawMagnet1a - RawMagnet1b)) / 2;
103
        UncalMagnetX = (1 * UncalMagnetX + (RawMagnet1a - RawMagnet1b)) / 2;
104
        UncalMagnetY = (1 * UncalMagnetY + (RawMagnet3a - RawMagnet3b)) / 2;
104
        UncalMagnetY = (1 * UncalMagnetY + (RawMagnet3a - RawMagnet3b)) / 2;
105
        UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2;
105
        UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2;
Line 106... Line 106...
106
 
106
 
-
 
107
        if(Calibration.X.Range != 0) MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range);
107
        MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range);
108
        else MagnetX = 0;
-
 
109
        if(Calibration.Y.Range != 0) MagnetY = (1024L * (int32_t)(UncalMagnetY - Calibration.Y.Offset)) / (Calibration.Y.Range);
108
        MagnetY = (1024L * (int32_t)(UncalMagnetY - Calibration.Y.Offset)) / (Calibration.Y.Range);
110
        else MagnetY = 0;
-
 
111
        if(Calibration.Y.Range != 0) MagnetZ = (1024L * (int32_t)(UncalMagnetZ - Calibration.Z.Offset)) / (Calibration.Z.Range);
109
        MagnetZ = (1024L * (int32_t)(UncalMagnetZ - Calibration.Z.Offset)) / (Calibration.Z.Range);
112
        else MagnetZ = 0;
Line 110... Line 113...
110
}
113
}
111
 
114
 
Line 157... Line 160...
157
{
160
{
158
        uint8_t cal;
161
        uint8_t cal;
159
        static uint8_t calold = 0;
162
        static uint8_t calold = 0;
160
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
163
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
161
        static uint8_t blinkcount = 0;
164
        static uint8_t blinkcount = 0;
162
/*
165
 
163
        // check both sources of communication for calibration request
166
        // check both sources of communication for calibration request
164
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
167
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
165
        else                     cal = ExternData.CalState;*/
168
        else                     cal = ExternData.CalState;
166
 
-
 
167
        cal = ExternData.CalState;
-
 
Line -... Line 169...
-
 
169
 
168
 
170
        DebugOut.Analog[30] = cal;
169
        // blink code for current calibration state
171
        // blink code for current calibration state
170
        if(cal)
172
        if(cal)
171
        {
173
        {
172
                if(CheckDelay(Led_Timer) || (cal != calold))
174
                if(CheckDelay(Led_Timer) || (cal != calold))
Line 278... Line 280...
278
        DebugOut.Analog[13] = Calibration.Z.Range;
280
        DebugOut.Analog[13] = Calibration.Z.Range;
279
        DebugOut.Analog[14] = ExternData.CalState;
281
        DebugOut.Analog[14] = ExternData.CalState;
280
        DebugOut.Analog[15] = Heading;
282
        DebugOut.Analog[15] = Heading;
281
        DebugOut.Analog[16] = ExternData.UserParam[0];
283
        DebugOut.Analog[16] = ExternData.UserParam[0];
282
        DebugOut.Analog[17] = ExternData.UserParam[1];
284
        DebugOut.Analog[17] = ExternData.UserParam[1];
-
 
285
        DebugOut.Analog[30] = I2C_WriteCal.CalByte;
283
        DebugOut.Analog[31] = PC_Connected;
286
        DebugOut.Analog[31] = PC_Connected;
284
}
287
}
Line 285... Line 288...
285
 
288
 
286
 
289
 
-
 
290
int main (void)
-
 
291
{
287
int main (void)
292
        // reset input pullup
-
 
293
        DDRC &=~((1<<DDC6));
288
{
294
        PORTC |= (1<<PORTC6);
289
    Led_Init();
295
 
290
    LED_GRN_ON;
296
    LED_Init();
291
    TIMER0_Init();
297
    TIMER0_Init();
292
    USART0_Init();
298
    USART0_Init();
Line -... Line 299...
-
 
299
    ADC_Init();
Line 293... Line 300...
293
    ADC_Init();
300
        I2C_Init();
294
        I2C_Init();
-
 
295
 
-
 
Line 296... Line 301...
296
 
301
 
297
    sei(); //Globale Interrupts Einschalten
302
    sei(); // enable globale interrupts
Line 298... Line 303...
298
 
303
 
299
 
304
    LED_GRN_ON;
Line 300... Line 305...
300
 
305
 
301
    Debug_Timer = SetDelay(100);   // Sendeintervall
306
    Debug_Timer = SetDelay(200);
302
    Led_Timer = SetDelay(100);
307
    Led_Timer = SetDelay(200);
Line -... Line 308...
-
 
308
 
-
 
309
        // read calibration info from eeprom
303
 
310
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
304
        // read calibration info from eeprom
311
 
305
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
312
    ExternData.Orientation = 0;
-
 
313
    ExternData.CalState = 0;
-
 
314
    I2C_WriteCal.CalByte = 0;
306
 
315
 
307
    ExternData.Orientation = 0;
316
        Heading = 101;
308
    ExternData.CalState = 0;
317
 
309
    I2C_WriteCal.CalByte = 0;
318
        // main loop
310
 
319
    while (1)
Line 326... Line 335...
326
                RawMagnet3b = ADC_GetValue(ADC7);
335
                RawMagnet3b = ADC_GetValue(ADC7);
327
                Delay_ms(1);
336
                Delay_ms(1);
Line 328... Line 337...
328
 
337
 
Line 329... Line 338...
329
                CalcFields();
338
                CalcFields();
330
 
-
 
331
                //if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
339
 
Line 332... Line 340...
332
                if(ExternData.CalState) Calibrate();
340
                if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
333
                else CalcHeading();
341
                else CalcHeading();