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