Subversion Repositories MK3Mag

Rev

Rev 22 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22 Rev 29
1
/*#######################################################################################
1
/*#######################################################################################
2
MK3Mag 3D-Magnet sensor
2
MK3Mag 3D-Magnet sensor
3
!!! THIS IS NOT FREE SOFTWARE !!!
3
!!! THIS IS NOT FREE SOFTWARE !!!
4
#######################################################################################*/
4
#######################################################################################*/
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Copyright (c) 05.2008 Holger Buss
6
// + Copyright (c) 05.2008 Holger Buss
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
8
// + Nur für den privaten Gebrauch
8
// + Nur für den privaten Gebrauch
9
// + www.MikroKopter.com
9
// + www.MikroKopter.com
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
11
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
12
// + mit unserer Zustimmung zulässig
12
// + mit unserer Zustimmung zulässig
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
14
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
15
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
15
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
16
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
16
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
17
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
17
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
20
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
20
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
22
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
23
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
23
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
24
// + eindeutig als Ursprung verlinkt werden
24
// + eindeutig als Ursprung verlinkt werden
25
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
25
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
26
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
27
// + Benutzung auf eigene Gefahr
27
// + Benutzung auf eigene Gefahr
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + this list of conditions and the following disclaimer.
33
// + this list of conditions and the following disclaimer.
34
// +   * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
34
// +   * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
41
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
42
// +     In case of doubt please contact: info@MikroKopter.de
42
// +     In case of doubt please contact: info@MikroKopter.de
43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
44
// +     clearly linked as origin
44
// +     clearly linked as origin
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <inttypes.h>
57
#include <inttypes.h>
58
#include <avr/io.h>
58
#include <avr/io.h>
59
#include <avr/interrupt.h>
59
#include <avr/interrupt.h>
60
#include "main.h"
60
#include "main.h"
61
 
61
 
62
volatile uint16_t CountMilliseconds = 0;
62
volatile uint16_t CountMilliseconds = 0;
63
volatile uint16_t I2C_Timeout = 0;
63
volatile uint16_t I2C_Timeout = 0;
64
 
64
 
65
 
65
 
66
/*****************************************************/
66
/*****************************************************/
67
/*              Initialize Timer 0                   */
67
/*              Initialize Timer 0                   */
68
/*****************************************************/
68
/*****************************************************/
69
void TIMER0_Init(void)
69
void TIMER0_Init(void)
70
{
70
{
71
        // set PB2 as output for the PWM used to signal compass heading
71
        // set PB2 as output for the PWM used to signal compass heading
72
        DDRB |= (1<<DDB2);
72
        DDRB |= (1<<DDB2);
73
        PORTB &= ~(1<<PORTB2);
73
        PORTB &= ~(1<<PORTB2);
74
 
74
 
75
 
75
 
76
        // Timer/Counter 0 Control Register A
76
        // Timer/Counter 0 Control Register A
77
 
77
 
78
        // Normal Timer Counter Mode (Bits WGM02 = 0, WGM01 = 0, WGM00 = 0)
78
        // Normal Timer Counter Mode (Bits WGM02 = 0, WGM01 = 0, WGM00 = 0)
79
    // OC0A disconnected (Bits COM0A1 = 0, COM0A0 = 0)
79
    // OC0A disconnected (Bits COM0A1 = 0, COM0A0 = 0)
80
    // OC0B disconnected (Bits COM0B1 = 0, COM0B0 = 0)
80
    // OC0B disconnected (Bits COM0B1 = 0, COM0B0 = 0)
81
    TCCR0A &= ~((1<<COM0A1)|(1<<COM0A0)|(1<<COM0B1)|(1<<COM0B0)|(1<<WGM01)|(1<<WGM00));
81
    TCCR0A &= ~((1<<COM0A1)|(1<<COM0A0)|(1<<COM0B1)|(1<<COM0B0)|(1<<WGM01)|(1<<WGM00));
82
 
82
 
83
        // Timer/Counter 0 Control Register B
83
        // Timer/Counter 0 Control Register B
84
 
84
 
85
        // set clock devider for timer 0 to SYSKLOCK/8 = 8MHz / 8 = 1MHz
85
        // set clock devider for timer 0 to SYSKLOCK/8 = 8MHz / 8 = 1MHz
86
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
86
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
87
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
87
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
88
 
88
 
89
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
89
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
90
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)|(1<<CS02));
90
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)|(1<<CS02));
91
    TCCR0B = (1<<CS01)|(0<<CS00);
91
    TCCR0B = (1<<CS01)|(0<<CS00);
92
 
92
 
93
        // init Timer/Counter 0 Register
93
        // init Timer/Counter 0 Register
94
    TCNT0 = 0;
94
    TCNT0 = 0;
95
 
95
 
96
        // Timer/Counter 0 Interrupt Mask Register
96
        // Timer/Counter 0 Interrupt Mask Register
97
        // enable timer overflow interrupt only
97
        // enable timer overflow interrupt only
98
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
98
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
99
        TIMSK0 |= (1<<TOIE0);
99
        TIMSK0 |= (1<<TOIE0);
100
 
100
 
101
}
101
}
102
 
102
 
103
 
103
 
104
// -----------------------------------------------------------------------
104
// -----------------------------------------------------------------------
105
ISR(TIMER0_OVF_vect)
105
ISR(TIMER0_OVF_vect)
106
{
106
{
107
        static uint8_t cnt;
107
        static uint8_t cnt;
108
        static uint16_t cmps_cnt;
108
        static uint16_t cmps_cnt;
109
 
109
 
110
        // reload timer register so that overflow occurs after 100 increments at 1 MHz
110
        // reload timer register so that overflow occurs after 100 increments at 1 MHz
111
        // resulting in a calling rate of this ISR of 10kHz or 0.1 ms.
111
        // resulting in a calling rate of this ISR of 10kHz or 0.1 ms.
112
        TCNT0 -= 101;
112
        TCNT0 -= 101;
113
 
113
 
114
        // disable PWM when bad compass heading value
114
        // disable PWM when bad compass heading value
115
        if(Heading < 0)
115
        if(Heading < 0)
116
        {
116
        {
117
                PORTB &= ~(PORTB2);
117
                PORTB &= ~(1<<PORTB2);
118
                cmps_cnt = 0;
118
                cmps_cnt = 0;
119
        }
119
        }
120
        else
120
        else
121
        {
121
        {
122
                // if a periode of 38.0 ms is over
122
                // if a periode of 38.0 ms is over
123
                if(++cmps_cnt >= 380)
123
                if(++cmps_cnt >= 380)
124
                {
124
                {
125
                        // set PWM out to high
125
                        // set PWM out to high
126
                        PORTB |= PORTB2;
126
                        PORTB |= (1<<PORTB2);
127
                        // reset periode counter
127
                        // reset periode counter
128
                        cmps_cnt = 0;
128
                        cmps_cnt = 0;
129
                }
129
                }
130
                // if the delay in 0.1 ms is equal to Heading + 10
130
                // if the delay in 0.1 ms is equal to Heading + 10
131
                else if(cmps_cnt >= (Heading + 10))
131
                else if(cmps_cnt >= (Heading + 10))
132
                {
132
                {
133
                        // set PWM out to low
133
                        // set PWM out to low
134
                        PORTB &= ~(PORTB2);
134
                        PORTB &= ~(1<<PORTB2);
135
                }
135
                }
136
        }
136
        }
137
 
137
 
138
        if(!--cnt)
138
        if(!--cnt)
139
        {
139
        {
140
                // every 10th run (1kHz or 1ms)
140
                // every 10th run (1kHz or 1ms)
141
                cnt = 10;
141
                cnt = 10;
142
                CountMilliseconds++;
142
                CountMilliseconds++;
143
                if(I2C_Timeout) I2C_Timeout--;
143
                if(I2C_Timeout) I2C_Timeout--;
144
        }
144
        }
145
}
145
}
146
 
146
 
147
 
147
 
148
// -----------------------------------------------------------------------
148
// -----------------------------------------------------------------------
149
uint16_t SetDelay (uint16_t t)
149
uint16_t SetDelay (uint16_t t)
150
{
150
{
151
  return(CountMilliseconds + t - 1);
151
  return(CountMilliseconds + t - 1);
152
}
152
}
153
 
153
 
154
// -----------------------------------------------------------------------
154
// -----------------------------------------------------------------------
155
int8_t CheckDelay(uint16_t t)
155
int8_t CheckDelay(uint16_t t)
156
{
156
{
157
        return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
157
        return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
158
}
158
}
159
 
159
 
160
// -----------------------------------------------------------------------
160
// -----------------------------------------------------------------------
161
void Delay_ms(uint16_t wait)
161
void Delay_ms(uint16_t wait)
162
{
162
{
163
        uint16_t t_stop;
163
        uint16_t t_stop;
164
        t_stop = SetDelay(wait);
164
        t_stop = SetDelay(wait);
165
        while (!CheckDelay(t_stop));
165
        while (!CheckDelay(t_stop));
166
}
166
}
167
 
167
 
168
 
168
 
169
 
169
 
170
 
170
 
171
 
171