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