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