Subversion Repositories MK3Mag

Rev

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

/*#######################################################################################
MK3Mag 3D-Magnet sensor
!!! THIS IS NOT FREE SOFTWARE !!!
#######################################################################################*/

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 05.2008 Holger Buss
// + Thanks to Ilja Fähnrich (P_Latzhalter)
// + Nur für den privaten Gebrauch
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// +   * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// +     from this software without specific prior written permission.
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
// +     for non-commercial use (directly or indirectly)
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// +     with our written permission
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
// +     In case of doubt please contact: info@MikroKopter.de
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
// +     clearly linked as origin
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// +  POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "main.h"

volatile uint16_t CountMilliseconds = 0;
volatile uint16_t I2C_Timeout = 0;


/*****************************************************/
/*              Initialize Timer 0                   */
/*****************************************************/
void TIMER0_Init(void)
{
        // set PB2 as output for the PWM used to signal compass heading
        DDRB |= (1<<DDB2);
        PORTB &= ~(1<<PORTB2);


        // Timer/Counter 0 Control Register A

        // Normal Timer Counter Mode (Bits WGM02 = 0, WGM01 = 0, WGM00 = 0)
    // OC0A disconnected (Bits COM0A1 = 0, COM0A0 = 0)
    // OC0B disconnected (Bits COM0B1 = 0, COM0B0 = 0)
    TCCR0A &= ~((1<<COM0A1)|(1<<COM0A0)|(1<<COM0B1)|(1<<COM0B0)|(1<<WGM01)|(1<<WGM00));

        // Timer/Counter 0 Control Register B

        // set clock devider for timer 0 to SYSKLOCK/8 = 8MHz / 8 = 1MHz
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz

        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)|(1<<CS02));
    TCCR0B = (1<<CS01)|(0<<CS00);

        // init Timer/Counter 0 Register
    TCNT0 = 0;

        // Timer/Counter 0 Interrupt Mask Register
        // enable timer overflow interrupt only
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
        TIMSK0 |= (1<<TOIE0);

}


// -----------------------------------------------------------------------
ISR(TIMER0_OVF_vect)
{
        static uint8_t cnt;
        static uint16_t cmps_cnt;

        // reload timer register so that overflow occurs after 100 increments at 1 MHz
        // resulting in a calling rate of this ISR of 10kHz or 0.1 ms.
        TCNT0 -= 101;

        // disable PWM when bad compass heading value
        if(Heading < 0)
        {
                PORTB &= ~(PORTB2);
                cmps_cnt = 0;
        }
        else
        {
                // if a periode of 38.0 ms is over
                if(++cmps_cnt >= 380)
                {
                        // set PWM out to high
                        PORTB |= PORTB2;
                        // reset periode counter
                        cmps_cnt = 0;
                }
                // if the delay in 0.1 ms is equal to Heading + 10
                else if(cmps_cnt >= (Heading + 10))
                {
                        // set PWM out to low
                        PORTB &= ~(PORTB2);
                }
        }

        if(!--cnt)
        {
                // every 10th run (1kHz or 1ms)
                cnt = 10;
                CountMilliseconds++;
                if(I2C_Timeout) I2C_Timeout--;
        }
}


// -----------------------------------------------------------------------
uint16_t SetDelay (uint16_t t)
{
  return(CountMilliseconds + t - 1);
}

// -----------------------------------------------------------------------
int8_t CheckDelay(uint16_t t)
{
        return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
}

// -----------------------------------------------------------------------
void Delay_ms(uint16_t wait)
{
        uint16_t t_stop;
        t_stop = SetDelay(wait);
        while (!CheckDelay(t_stop));
}