Subversion Repositories Projects

Rev

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

Rev 274 Rev 296
1
#include <inttypes.h>
1
#include <inttypes.h>
2
#include <avr/io.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
3
#include <avr/interrupt.h>
4
 
4
 
5
#include "timer0.h"
5
#include "timer0.h"
6
 
6
 
7
volatile uint16_t CountMilliseconds = 0;
7
volatile uint16_t CountMilliseconds = 0;
8
DateTime_t SystemTime;
8
DateTime_t SystemTime;
9
 
9
 
10
#ifdef USE_FOLLOWME
10
#ifdef USE_FOLLOWME
11
volatile uint16_t BeepTime = 0;
11
volatile uint16_t BeepTime = 0;
12
volatile uint16_t BeepModulation = 0xFFFF;
12
volatile uint16_t BeepModulation = 0xFFFF;
13
#endif
13
#endif
14
 
14
 
15
 
15
 
16
/*****************************************************/
16
/*****************************************************/
17
/*              Initialize Timer 0                   */
17
/*              Initialize Timer 0                   */
18
/*****************************************************/
18
/*****************************************************/
19
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
19
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
20
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
20
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
21
void TIMER0_Init(void)
21
void TIMER0_Init(void)
22
{
22
{
23
        uint8_t sreg = SREG;
23
        uint8_t sreg = SREG;
24
 
24
 
25
        // disable all interrupts before reconfiguration
25
        // disable all interrupts before reconfiguration
26
        cli();
26
        cli();
27
 
27
 
28
 
28
 
29
        // configure speaker port as output
29
        // configure speaker port as output
30
 
30
 
31
        #ifdef USE_FOLLOWME
31
        #ifdef USE_FOLLOWME
32
        // Speaker at PC7
32
        // Speaker at PC7
33
        DDRC |= (1<<DDC7);
33
        DDRC |= (1<<DDC7);
34
        PORTC &= ~(1<<PORTC7);
34
        PORTC &= ~(1<<PORTC7);
35
        #endif
35
        #endif
36
 
36
 
37
        // Timer/Counter 0 Control Register A
37
        // Timer/Counter 0 Control Register A
38
 
38
 
39
        // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
-
 
40
    // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
-
 
41
    // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
-
 
42
    TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0));
39
        // Waveform Generation None (Bits WGM02 = 0, WGM01 = 0, WGM00 = 0)
43
    TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
40
    TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0)|(1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00));
44
 
41
 
45
        // Timer/Counter 0 Control Register B
42
        // Timer/Counter 0 Control Register B
46
 
43
 
47
        // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
44
        // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
48
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
45
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
49
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
46
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
50
 
47
 
51
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
48
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
52
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
49
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
53
    TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
50
    TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
54
 
-
 
55
        // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
-
 
56
    OCR0A =  0;  // for PB3
-
 
57
    OCR0B = 120; // for PB4
-
 
58
 
51
 
59
        // init Timer/Counter 0 Register
52
        // init Timer/Counter 0 Register
60
    TCNT0 = 0;
53
    TCNT0 = 0;
61
 
54
 
62
        // Timer/Counter 0 Interrupt Mask Register
55
        // Timer/Counter 0 Interrupt Mask Register
63
        // enable timer overflow interrupt only
56
        // enable timer overflow interrupt only
64
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
57
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
65
        TIMSK0 |= (1<<TOIE0);
58
        TIMSK0 |= (1<<TOIE0);
66
 
59
 
67
 
60
 
68
        SystemTime.Year = 0;
61
        SystemTime.Year = 0;
69
        SystemTime.Month = 0;
62
        SystemTime.Month = 0;
70
        SystemTime.Day = 0;
63
        SystemTime.Day = 0;
71
        SystemTime.Hour = 0;
64
        SystemTime.Hour = 0;
72
        SystemTime.Min = 0;
65
        SystemTime.Min = 0;
73
        SystemTime.Sec = 0;
66
        SystemTime.Sec = 0;
74
        SystemTime.mSec = 0;
67
        SystemTime.mSec = 0;
75
        SystemTime.Valid = 0;
68
        SystemTime.Valid = 0;
76
 
69
 
77
        CountMilliseconds = 0;
70
        CountMilliseconds = 0;
78
 
71
 
79
 
72
 
80
        SREG = sreg;
73
        SREG = sreg;
81
}
74
}
82
 
75
 
83
 
76
 
84
 
77
 
85
/*****************************************************/
78
/*****************************************************/
86
/*          Interrupt Routine of Timer 0             */
79
/*          Interrupt Routine of Timer 0             */
87
/*****************************************************/
80
/*****************************************************/
88
ISR(TIMER0_OVF_vect)    // 9.765 kHz
81
ISR(TIMER0_OVF_vect)    // 9.765 kHz
89
{
82
{
90
    static uint8_t cnt = 0;
83
    static uint8_t cnt = 0;
91
    #ifdef USE_FOLLOWME
84
    #ifdef USE_FOLLOWME
92
    uint8_t Beeper_On = 0;
85
    uint8_t Beeper_On = 0;
93
        #endif
86
        #endif
94
 
87
 
95
        if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz)
88
        if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz)
96
        {
89
        {
97
                cnt = 9;
90
                cnt = 9;
98
                CountMilliseconds++; // increment millisecond counter
91
                CountMilliseconds++; // increment millisecond counter
99
        }
92
        }
100
 
93
 
101
         #ifdef USE_FOLLOWME
94
         #ifdef USE_FOLLOWME
102
        // beeper on if duration is not over
95
        // beeper on if duration is not over
103
        if(BeepTime)
96
        if(BeepTime)
104
        {
97
        {
105
                BeepTime--; // decrement BeepTime
98
                BeepTime--; // decrement BeepTime
106
                if(BeepTime & BeepModulation) Beeper_On = 1;
99
                if(BeepTime & BeepModulation) Beeper_On = 1;
107
                else Beeper_On = 0;
100
                else Beeper_On = 0;
108
        }
101
        }
109
        else // beeper off if duration is over
102
        else // beeper off if duration is over
110
        {
103
        {
111
                Beeper_On = 0;
104
                Beeper_On = 0;
112
                BeepModulation = 0xFFFF;
105
                BeepModulation = 0xFFFF;
113
        }
106
        }
114
 
107
 
115
        // if beeper is on
108
        // if beeper is on
116
        if(Beeper_On)
109
        if(Beeper_On)
117
        {
110
        {
118
                // set speaker port to high
111
                // set speaker port to high
119
                PORTC |= (1<<PORTC7); // Speaker at PC7
112
                PORTC |= (1<<PORTC7); // Speaker at PC7
120
        }
113
        }
121
        else // beeper is off
114
        else // beeper is off
122
        {
115
        {
123
                // set speaker port to low
116
                // set speaker port to low
124
                PORTC &= ~(1<<PORTC7);// Speaker at PC7
117
                PORTC &= ~(1<<PORTC7);// Speaker at PC7
125
        }
118
        }
126
        #endif
119
        #endif
127
}
120
}
128
 
121
 
129
 
122
 
130
// -----------------------------------------------------------------------
123
// -----------------------------------------------------------------------
131
uint16_t SetDelay (uint16_t t)
124
uint16_t SetDelay (uint16_t t)
132
{
125
{
133
  return(CountMilliseconds + t - 1);
126
  return(CountMilliseconds + t - 1);
134
}
127
}
135
 
128
 
136
// -----------------------------------------------------------------------
129
// -----------------------------------------------------------------------
137
int8_t CheckDelay(uint16_t t)
130
int8_t CheckDelay(uint16_t t)
138
{
131
{
139
  return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
132
  return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
140
}
133
}
141
 
134
 
142
// -----------------------------------------------------------------------
135
// -----------------------------------------------------------------------
143
void Delay_ms(uint16_t w)
136
void Delay_ms(uint16_t w)
144
{
137
{
145
 unsigned int t_stop;
138
 unsigned int t_stop;
146
 t_stop = SetDelay(w);
139
 t_stop = SetDelay(w);
147
 while (!CheckDelay(t_stop));
140
 while (!CheckDelay(t_stop));
148
}
141
}
149
 
142
 
150
 
143