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