Subversion Repositories Projects

Rev

Rev 514 | Rev 728 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 514 Rev 528
1
/****************************************************************************
1
/****************************************************************************
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
2
 *   Copyright (C) 2009 by Claas Anders "CaScAdE" Rathje                    *
3
 *   admiralcascade@gmail.com                                               *
3
 *   admiralcascade@gmail.com                                               *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
4
 *   Project-URL: http://www.mylifesucks.de/oss/c-osd/                      *
5
 *                                                                          *
5
 *                                                                          *
6
 *   This program is free software; you can redistribute it and/or modify   *
6
 *   This program is free software; you can redistribute it and/or modify   *
7
 *   it under the terms of the GNU General Public License as published by   *
7
 *   it under the terms of the GNU General Public License as published by   *
8
 *   the Free Software Foundation; either version 2 of the License.         *
8
 *   the Free Software Foundation; either version 2 of the License.         *
9
 *                                                                          *
9
 *                                                                          *
10
 *   This program is distributed in the hope that it will be useful,        *
10
 *   This program is distributed in the hope that it will be useful,        *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
13
 *   GNU General Public License for more details.                           *
13
 *   GNU General Public License for more details.                           *
14
 *                                                                          *
14
 *                                                                          *
15
 *   You should have received a copy of the GNU General Public License      *
15
 *   You should have received a copy of the GNU General Public License      *
16
 *   along with this program; if not, write to the                          *
16
 *   along with this program; if not, write to the                          *
17
 *   Free Software Foundation, Inc.,                                        *
17
 *   Free Software Foundation, Inc.,                                        *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
19
 ****************************************************************************/
19
 ****************************************************************************/
20
 
20
 
21
#include <avr/io.h>
21
#include <avr/io.h>
22
#include <avr/interrupt.h>
22
#include <avr/interrupt.h>
23
#include "ppm.h"
23
#include "ppm.h"
24
#include "main.h"
24
#include "main.h"
25
#include "max7456_software_spi.h" // clearing
25
#include "max7456_software_spi.h" // clearing
26
 
26
 
27
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
27
#if !(ALLCHARSDEBUG|(WRITECHARS != -1))
28
 
28
 
29
volatile uint16_t old_timer1 = 0, ppm = 0;
29
volatile uint16_t old_timer1 = 0, ppm = 0;
30
 
30
 
31
/**
31
/**
32
 * init ppm, including timer1 for measurement
32
 * init ppm, including timer1 for measurement
33
 */
33
 */
34
void ppm_init() {
34
void ppm_init() {
35
        PORTE |= (1 << PORTE0);         // enable pullup on INT0 pin
35
        PORTE |= (1 << PORTE0);         // enable pullup on INT0 pin
36
        GICR |= (1<<INT2);                      // External Interrupt Request 2 Enable
36
        GICR |= (1<<INT2);                      // External Interrupt Request 2 Enable
37
        EMCUCR |= (1<<ISC2);            // a rising edge on INT2 activates the interrupt
37
        EMCUCR |= (1<<ISC2);            // a rising edge on INT2 activates the interrupt
38
        TCCR1B |= (0<<CS12)|(1<<CS10)|(1<<CS11); // timer1 up with prescaler 64
38
        TCCR1B |= (0<<CS12)|(1<<CS10)|(1<<CS11); // timer1 up with prescaler 64
39
}
39
}
40
 
40
 
41
/**
41
/**
42
 * Handle INT2 interrupts that occur on changing edges of ppm signal
42
 * Handle INT2 interrupts that occur on changing edges of ppm signal
43
 */
43
 */
44
ISR(INT2_vect) {
44
ISR(INT2_vect) {
45
        // since the pin might get bogus reads we wait for 123 signals
45
        // since the pin might get bogus reads we wait for 123 signals
46
        static uint8_t valid_ppm_to_go = 123;
46
        static uint8_t valid_ppm_to_go = 123;
47
        if (EMCUCR & (1<<ISC2)) { // rising
47
        if (EMCUCR & (1<<ISC2)) { // rising
48
                old_timer1 = TCNT1;
48
                old_timer1 = TCNT1;
49
                EMCUCR &= ~(1<<ISC2); // next one is falling
49
                EMCUCR &= ~(1<<ISC2); // next one is falling
50
        } else {               
50
        } else {               
51
                if (valid_ppm_to_go) {
51
                if (valid_ppm_to_go) {
52
                        valid_ppm_to_go--;
52
                        valid_ppm_to_go--;
53
                } else {
53
                } else {
54
                        ppm = TCNT1 - old_timer1;
54
                        ppm = TCNT1 - old_timer1;
55
                        ppm -= 256;
55
                        ppm -= 256;
56
                        if (ppm < 128) { // we want HUD
56
                        if (ppm < 128) { // we want HUD
57
                                COSD_FLAGS |= COSD_FLAG_HUD;
57
                                COSD_FLAGS_CONFIG |= COSD_FLAG_HUD;
58
                        } else { // we do not want hud
58
                        } else { // we do not want hud
59
                                if (COSD_FLAGS & COSD_FLAG_HUD) {
59
                                if (COSD_FLAGS_CONFIG & COSD_FLAG_HUD) {
60
                                        clear();
60
                                        clear();
61
                                }
61
                                }
62
                                COSD_FLAGS &= ~COSD_FLAG_HUD;
62
                                COSD_FLAGS_CONFIG &= ~COSD_FLAG_HUD;
63
                        }
63
                        }
64
                }
64
                }
65
                EMCUCR |= (1<<ISC2); // next one is rising
65
                EMCUCR |= (1<<ISC2); // next one is rising
66
        }
66
        }
67
        //write_ndigit_number_u(2, 2, ppm, 1000, 0); // debug 
67
        //write_ndigit_number_u(2, 2, ppm, 1000, 0); // debug 
68
        //write_ndigit_number_u(2, 3, valid_ppm_to_go, 100, 0); // debug 
68
        //write_ndigit_number_u(2, 3, valid_ppm_to_go, 100, 0); // debug 
69
        //write_ndigit_number_u(2, 4, COSD_FLAGS, 100, 0); // debug 
69
        //write_ndigit_number_u(2, 4, COSD_FLAGS, 100, 0); // debug 
70
}
70
}
71
 
71
 
72
#endif
72
#endif
73
 
73