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