Subversion Repositories Projects

Rev

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

/****************************************************************************
 *   Copyright (C) 2009-2011 by Claas Anders "CaScAdE" Rathje               *
 *   admiralcascade@gmail.com                                               *
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
 *                                                                          *
 *   This program is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by   *
 *   the Free Software Foundation; either version 2 of the License.         *
 *                                                                          *
 *   This program is distributed in the hope that it will be useful,        *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for more details.                           *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with this program; if not, write to the                          *
 *   Free Software Foundation, Inc.,                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
 ****************************************************************************/


#include "main.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include "ppm.h"
#include "max7456_software_spi.h" // clearing

#if !(ALLCHARSDEBUG|(WRITECHARS != -1))

volatile uint16_t old_timer1 = 0, ppm = 0;

/**
 * init ppm, including timer1 for measurement
 */

void ppm_init() {
    PORTE |= (1 << PORTE0); // enable pullup on INT0 pin
    GICR |= (1 << INT2); // External Interrupt Request 2 Enable
    EMCUCR |= (1 << ISC2); // a rising edge on INT2 activates the interrupt
    TCCR1B |= (0 << CS12) | (1 << CS10) | (1 << CS11); // timer1 up with prescaler 64
}

/**
 * Handle INT2 interrupts that occur on changing edges of ppm signal
 */

ISR(INT2_vect) {
    // since the pin might get bogus reads we wait for 123 signals
    static uint8_t valid_ppm_to_go = 123;
    if (EMCUCR & (1 << ISC2)) { // rising
        old_timer1 = TCNT1;
        EMCUCR &= ~(1 << ISC2); // next one is falling
    } else {
        if (valid_ppm_to_go) {
            valid_ppm_to_go--;
        } else {
            ppm = TCNT1 - old_timer1;
            ppm -= 256;
            if (ppm < 128) { // we want HUD
                if (!(COSD_FLAGS_MODES & COSD_FLAG_HUD)) {
                    COSD_FLAGS_RUNTIME &= ~COSD_ICONS_WRITTEN;
                }
                COSD_FLAGS_MODES |= COSD_FLAG_HUD;
            } else { // we do not want hud
                if (COSD_FLAGS_MODES & COSD_FLAG_HUD) {
                    clear();
                }
                COSD_FLAGS_MODES &= ~COSD_FLAG_HUD;
            }
        }
        EMCUCR |= (1 << ISC2); // next one is rising
    }
    //write_ndigit_number_u(2, 5, ppm, 1000, 0); // debug
    //write_ndigit_number_u(2, 6, valid_ppm_to_go, 100, 0); // debug
    //write_ndigit_number_u(2, 7, COSD_FLAGS_CONFIG, 100, 0); // debug
    PORTC ^= (1 << PC3);
    //uptime = ppm;
}

#endif