Subversion Repositories FlightCtrl

Rev

Rev 2044 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
#include <avr/io.h>
2
#include <stdlib.h>
3
#include <inttypes.h>
4
#include "timer0.h"
5
#include "eeprom.h"
6
#include "mk3mag.h"
7
 
8
uint8_t PWMTimeout = 12;
2051 - 9
// This will updated in interrupt handler. Should not be processed by main, other than just atomic copy.
10
volatile uint16_t volatileMagneticHeading;
1969 - 11
ToMk3Mag_t toMk3Mag;
1612 dongfang 12
 
13
/*********************************************/
14
/*  Initialize Interface to MK3MAG Compass   */
15
/*********************************************/
2039 - 16
void MK3MAG_init(void) {
1801 - 17
        // Port PC4 connected to PWM output from compass module
18
        DDRC &= ~(1 << DDC4); // set as input
19
        PORTC |= (1 << PORTC4); // pull up  to increase PWM counter also if nothing is connected
20
        PWMTimeout = 0;
2039 - 21
        toMk3Mag.calState = 0;
22
        toMk3Mag.orientation = 1;
1612 dongfang 23
}
24
 
25
/*********************************************/
26
/*  Get PWM from MK3MAG                      */
27
/*********************************************/
2039 - 28
void MK3MAG_periodicTask(void) {// called every 102.4 us by timer 0 ISR
1801 - 29
        static uint16_t PWMCount = 0;
1887 - 30
        static uint16_t beepDelay = 0;
2041 - 31
        // static uint16_t debugCounter = 0;
1801 - 32
        // The pulse width varies from 1ms (0°) to 36.99ms (359.9°)
33
        // in other words 100us/° with a +1ms offset.
34
        // The signal goes low for 65ms between pulses,
35
        // so the cycle time is 65mS + the pulse width.
36
        // pwm is high
1612 dongfang 37
 
2041 - 38
        // if (debugCounter++ == 5000) {
39
    // DebugOut.Digital[0] ^= DEBUG_MK3MAG;
40
        // debugCounter = 0;
41
        // }
1805 - 42
 
1801 - 43
        if (PINC & (1 << PINC4)) {
44
                // If PWM signal is high increment PWM high counter
45
                // This counter is incremented by a periode of 102.4us,
46
                // i.e. the resoluton of pwm coded heading is approx. 1 deg.
47
                PWMCount++;
48
                // pwm overflow?
49
                if (PWMCount > 400) {
50
                        if (PWMTimeout)
51
                                PWMTimeout--; // decrement timeout
2051 - 52
                        volatileMagneticHeading = -1; // unknown heading
1801 - 53
                        PWMCount = 0; // reset PWM Counter
54
                }
55
        } else { // pwm is low
1805 - 56
                // ignore pwm values values of 0 and higher than 37 ms;
1801 - 57
                if ((PWMCount) && (PWMCount < 362)) { // 362 * 102.4us = 37.0688 ms
58
                        if (PWMCount < 10)
2051 - 59
                          volatileMagneticHeading = 0;
1805 - 60
                        else {
2051 - 61
                          volatileMagneticHeading = ((uint32_t) (PWMCount - 10) * 1049L) / 1024; // correct timebase and offset
1845 - 62
                                //DebugOut.Digital[1] ^= DEBUG_MK3MAG; // correct signal recd.
1805 - 63
                        }
64
                        /*
65
                         compassHeading - compassCourse on a -180..179 range.
66
                         compassHeading 20 compassCourse 30 --> ((540 - 10)%360) - 180 = -10
67
                         compassHeading 30 compassCourse 20 --> ((540 + 10)%360) - 180 = 10
68
                         compassHeading 350 compassCourse 10 --> ((540 + 340)%360) - 180 = -20
69
                         compassHeading 10 compassCourse 350 --> ((540 - 340)%360) - 180 = 20
70
                         */
71
                        //compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
1801 - 72
                        PWMTimeout = 12; // if 12 periodes long no valid PWM was detected the data are invalid
73
                        // 12 * 362 counts * 102.4 us
74
                }
75
                PWMCount = 0; // reset pwm counter
1805 - 76
        }
2041 - 77
 
1805 - 78
        if (!PWMTimeout) {
1887 - 79
                if (checkDelay(beepDelay)) {
80
                        if (!beepTime)
2044 - 81
                                beep(50); // make noise with 10Hz to signal the compass problem
1887 - 82
                        beepDelay = setDelay(100);
1801 - 83
                }
1612 dongfang 84
        }
85
}
86