Rev 966 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
838 | MikeW | 1 | #include "main.h" |
2 | volatile unsigned long cnt_ms = 0; |
||
3 | volatile unsigned int CountMilliseconds = 0; |
||
4 | volatile unsigned int Count8Khz = 0; |
||
5 | |||
6 | volatile static unsigned int tim_main; |
||
7 | volatile unsigned char UpdateMotor = 0; |
||
8 | volatile unsigned int cntKompass = 0; |
||
9 | volatile unsigned int beeptime = 0; |
||
10 | unsigned int BeepMuster = 0xffff; |
||
11 | int ServoValue = 0; |
||
12 | extern void MM3_Update(void); |
||
966 | MikeW | 13 | |
838 | MikeW | 14 | enum { |
15 | STOP = 0, |
||
966 | MikeW | 16 | CK = 1, |
17 | CK8 = 2, |
||
18 | CK64 = 3, |
||
19 | CK256 = 4, |
||
20 | CK1024 = 5, |
||
21 | T0_FALLING_EDGE = 6, |
||
22 | T0_RISING_EDGE = 7 |
||
838 | MikeW | 23 | }; |
24 | |||
25 | |||
26 | SIGNAL (SIG_OVERFLOW0) // 8kHz |
||
27 | { |
||
966 | MikeW | 28 | static unsigned char cnt_1ms = 1,cnt = 0; |
29 | unsigned char pieper_ein = 0; |
||
30 | |||
31 | Count8Khz++; |
||
32 | |||
33 | if(!cnt--) |
||
34 | { |
||
35 | cnt = 9; |
||
36 | cnt_1ms++; |
||
37 | cnt_1ms %= 2; |
||
38 | if(!cnt_1ms) |
||
838 | MikeW | 39 | { |
966 | MikeW | 40 | UpdateMotor = 1; |
41 | } |
||
42 | CountMilliseconds++; |
||
43 | cnt_ms++; |
||
44 | // update compass value if this option is enabled in the settings |
||
979 | MikeW | 45 | #ifdef USE_COMPASS |
966 | MikeW | 46 | MM3_Update(); // read out mm3 board |
979 | MikeW | 47 | #endif |
966 | MikeW | 48 | } |
49 | |||
50 | if(beeptime > 1) |
||
51 | { |
||
52 | beeptime--; |
||
53 | if(beeptime & BeepMuster) |
||
838 | MikeW | 54 | { |
966 | MikeW | 55 | pieper_ein = 1; |
838 | MikeW | 56 | } |
966 | MikeW | 57 | else pieper_ein = 0; |
58 | } |
||
59 | else |
||
60 | { |
||
61 | pieper_ein = 0; |
||
62 | BeepMuster = 0xffff; |
||
63 | } |
||
64 | |||
65 | if(pieper_ein) |
||
66 | { |
||
67 | PORTC |= (1<<7); // Speaker an PORTC.7 |
||
68 | } |
||
69 | else |
||
70 | { |
||
71 | PORTC &= ~(1<<7); |
||
72 | } |
||
73 | |||
74 | #if 0 |
||
75 | if(PINC & 0x10) |
||
76 | { |
||
77 | cntKompass++; |
||
78 | } |
||
79 | else |
||
80 | { |
||
81 | if((cntKompass) && (cntKompass < 4000)) |
||
82 | { |
||
83 | if(cntKompass < 10) |
||
84 | { |
||
85 | cntKompass = 10; |
||
86 | } |
||
87 | KompassValue = (((int) cntKompass-10) * 36) / 35; |
||
838 | MikeW | 88 | } |
966 | MikeW | 89 | cntKompass = 0; |
90 | } |
||
838 | MikeW | 91 | #endif |
92 | } |
||
93 | |||
94 | |||
95 | void Timer_Init(void) |
||
96 | { |
||
966 | MikeW | 97 | tim_main = SetDelay(10); |
98 | TCCR0B = CK8; |
||
99 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
||
100 | OCR0A = 0; |
||
101 | OCR0B = 120; |
||
102 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
||
103 | |||
104 | TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3; |
||
105 | TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22); |
||
106 | TIMSK2 |= _BV(OCIE2A); |
||
107 | |||
108 | TIMSK0 |= _BV(TOIE0); |
||
109 | OCR2A = 10; |
||
110 | TCNT2 = 0; |
||
838 | MikeW | 111 | } |
112 | |||
113 | // ----------------------------------------------------------------------- |
||
114 | |||
115 | unsigned int SetDelay (unsigned int t) |
||
116 | { |
||
966 | MikeW | 117 | // TIMSK0 &= ~_BV(TOIE0); |
838 | MikeW | 118 | return(CountMilliseconds + t + 1); |
966 | MikeW | 119 | // TIMSK0 |= _BV(TOIE0); |
838 | MikeW | 120 | } |
121 | |||
122 | // ----------------------------------------------------------------------- |
||
123 | char CheckDelay(unsigned int t) |
||
124 | { |
||
966 | MikeW | 125 | // TIMSK0 &= ~_BV(TOIE0); |
838 | MikeW | 126 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
966 | MikeW | 127 | // TIMSK0 |= _BV(TOIE0); |
838 | MikeW | 128 | } |
129 | |||
130 | // ----------------------------------------------------------------------- |
||
131 | void Delay_ms(unsigned int w) |
||
132 | { |
||
966 | MikeW | 133 | unsigned int akt; |
134 | akt = SetDelay(w); |
||
135 | while (!CheckDelay(akt)); |
||
838 | MikeW | 136 | } |
137 | |||
138 | void Delay_ms_Mess(unsigned int w) |
||
139 | { |
||
966 | MikeW | 140 | unsigned int akt; |
141 | akt = SetDelay(w); |
||
142 | while (!CheckDelay(akt)) ANALOG_ON; |
||
838 | MikeW | 143 | } |
144 | |||
145 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
146 | // Servo ansteuern |
||
147 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
148 | SIGNAL(SIG_OUTPUT_COMPARE2A) |
||
149 | { |
||
150 | static unsigned char timer = 10; |
||
151 | if(!timer--) |
||
966 | MikeW | 152 | { |
979 | MikeW | 153 | |
154 | TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3; |
||
155 | if(ServoValue < EE_Parameter.ServoNickMin) |
||
156 | { |
||
157 | ServoValue = EE_Parameter.ServoNickMin; |
||
158 | } |
||
159 | if(ServoValue > EE_Parameter.ServoNickMax) |
||
160 | { |
||
161 | ServoValue = EE_Parameter.ServoNickMax; |
||
162 | } |
||
966 | MikeW | 163 | |
979 | MikeW | 164 | OCR2A = ServoValue; |
966 | MikeW | 165 | timer = EE_Parameter.ServoNickRefresh; |
166 | } |
||
167 | else |
||
168 | { |
||
169 | TCCR2A =3; |
||
170 | PORTD&=~0x80; |
||
979 | MikeW | 171 | } |
838 | MikeW | 172 | } |