Subversion Repositories MK3Mag

Rev

Rev 51 | Details | Compare with Previous | Last modification | View Log | RSS feed

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