Subversion Repositories FlightCtrl

Rev

Rev 2015 | Rev 2039 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2015 Rev 2026
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
2
// + Copyright (c) 04.2007 Holger Buss
3
// + Nur f�r den privaten Gebrauch
3
// + Nur f�r den privaten Gebrauch
4
// + www.MikroKopter.com
4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Es gilt f�r das gesamte Projekt (Hardware, Software, Bin�rfiles, Sourcecode und Dokumentation),
6
// + Es gilt f�r das gesamte Projekt (Hardware, Software, Bin�rfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten (nicht-kommerziellen) Gebrauch zul�ssig ist.
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten (nicht-kommerziellen) Gebrauch zul�ssig ist.
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Best�ckung und Verkauf von Platinen oder Baus�tzen,
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Best�ckung und Verkauf von Platinen oder Baus�tzen,
11
// + Verkauf von Luftbildaufnahmen, usw.
11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder ver�ffentlicht,
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder ver�ffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright m�ssen dann beiliegen
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright m�ssen dann beiliegen
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
17
// + auf anderen Webseiten oder sonstigen Medien ver�ffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
17
// + auf anderen Webseiten oder sonstigen Medien ver�ffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt werden
18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Keine Gew�hr auf Fehlerfreiheit, Vollst�ndigkeit oder Funktion
20
// + Keine Gew�hr auf Fehlerfreiheit, Vollst�ndigkeit oder Funktion
21
// + Benutzung auf eigene Gefahr
21
// + Benutzung auf eigene Gefahr
22
// + Wir �bernehmen keinerlei Haftung f�r direkte oder indirekte Personen- oder Sachsch�den
22
// + Wir �bernehmen keinerlei Haftung f�r direkte oder indirekte Personen- oder Sachsch�den
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
25
// + mit unserer Zustimmung zul�ssig
25
// + mit unserer Zustimmung zul�ssig
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <inttypes.h>
51
#include <inttypes.h>
52
#include <avr/io.h>
52
#include <avr/io.h>
53
#include <avr/interrupt.h>
53
#include <avr/interrupt.h>
54
#include "eeprom.h"
54
#include "eeprom.h"
55
#include "analog.h"
55
#include "analog.h"
56
#include "controlMixer.h"
56
#include "controlMixer.h"
57
 
57
 
58
#include "timer0.h"
58
#include "timer0.h"
59
// for debugging!
59
// for debugging!
60
#include "uart0.h"
60
#include "uart0.h"
61
#include "output.h"
61
#include "output.h"
62
 
62
 
63
#ifdef USE_MK3MAG
63
#ifdef USE_MK3MAG
64
#include "mk3mag.h"
64
#include "mk3mag.h"
65
#endif
65
#endif
66
 
66
 
67
volatile uint16_t millisecondsCount = 0;
67
volatile uint16_t millisecondsCount = 0;
68
volatile uint8_t  runFlightControl = 0;
68
volatile uint8_t  runFlightControl = 0;
69
volatile uint16_t cntKompass = 0;
69
volatile uint16_t cntKompass = 0;
70
volatile uint16_t beepTime = 0;
70
volatile uint16_t beepTime = 0;
71
volatile uint16_t beepModulation = BEEP_MODULATION_NONE;
71
volatile uint16_t beepModulation = BEEP_MODULATION_NONE;
72
 
72
 
73
#ifdef USE_NAVICTRL
73
#ifdef USE_NAVICTRL
74
volatile uint8_t SendSPI = 0;
74
volatile uint8_t SendSPI = 0;
75
#endif
75
#endif
76
 
76
 
77
/*****************************************************
77
/*****************************************************
78
 * Initialize Timer 0                  
78
 * Initialize Timer 0                  
79
 *****************************************************/
79
 *****************************************************/
80
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
80
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
81
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
81
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
82
void timer0_init(void) {
82
void timer0_init(void) {
83
  uint8_t sreg = SREG;
83
  uint8_t sreg = SREG;
84
 
84
 
85
  // disable all interrupts before reconfiguration
85
  // disable all interrupts before reconfiguration
86
  cli();
86
  cli();
87
 
87
 
88
  // Configure speaker port as output.
88
  // Configure speaker port as output.
89
 
89
 
90
  if (boardRelease == 10) { // Speaker at PD2
90
  if (boardRelease == 10) { // Speaker at PD2
91
    DDRD |= (1 << DDD2);
91
    DDRD |= (1 << DDD2);
92
    PORTD &= ~(1 << PORTD2);
92
    PORTD &= ~(1 << PORTD2);
93
  } else { // Speaker at PC7
93
  } else { // Speaker at PC7
94
    DDRC |= (1 << DDC7);
94
    DDRC |= (1 << DDC7);
95
    PORTC &= ~(1 << PORTC7);
95
    PORTC &= ~(1 << PORTC7);
96
  }
96
  }
97
 
97
 
98
  // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor
98
  // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor
99
  DDRB |= (1 << DDB4) | (1 << DDB3);
99
  DDRB |= (1 << DDB4) | (1 << DDB3);
100
  PORTB &= ~((1 << PORTB4) | (1 << PORTB3));
100
  PORTB &= ~((1 << PORTB4) | (1 << PORTB3));
101
 
101
 
102
  // Timer/Counter 0 Control Register A
102
  // Timer/Counter 0 Control Register A
103
 
103
 
104
  // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
104
  // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
105
  // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
105
  // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
106
  // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
106
  // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
107
  TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0));
107
  TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0));
108
  TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
108
  TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
109
 
109
 
110
  // Timer/Counter 0 Control Register B
110
  // Timer/Counter 0 Control Register B
111
 
111
 
112
  // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
112
  // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
113
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
113
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
114
  // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
114
  // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
115
 
115
 
116
  // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
116
  // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
117
  TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02));
117
  TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02));
118
  TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00);
118
  TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00);
119
 
119
 
120
  // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
120
  // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
121
  OCR0A = 0; // for PB3
121
  OCR0A = 0; // for PB3
122
  OCR0B = 120; // for PB4
122
  OCR0B = 120; // for PB4
123
 
123
 
124
  // init Timer/Counter 0 Register
124
  // init Timer/Counter 0 Register
125
  TCNT0 = 0;
125
  TCNT0 = 0;
126
 
126
 
127
  // Timer/Counter 0 Interrupt Mask Register
127
  // Timer/Counter 0 Interrupt Mask Register
128
  // enable timer overflow interrupt only
128
  // enable timer overflow interrupt only
129
  TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A));
129
  TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A));
130
  TIMSK0 |= (1 << TOIE0);
130
  TIMSK0 |= (1 << TOIE0);
131
 
131
 
132
  SREG = sreg;
132
  SREG = sreg;
133
}
133
}
134
 
134
 
135
/*****************************************************/
135
/*****************************************************/
136
/*          Interrupt Routine of Timer 0             */
136
/*          Interrupt Routine of Timer 0             */
137
/*****************************************************/
137
/*****************************************************/
138
ISR(TIMER0_OVF_vect)
138
ISR(TIMER0_OVF_vect)
139
{ // 9765.625 Hz
139
{ // 9765.625 Hz
140
  static uint8_t cnt_1ms = 1, cnt = 0;
140
  static uint8_t cnt_1ms = 1, cnt = 0;
141
  uint8_t beeper_On = 0;
141
  uint8_t beeper_On = 0;
142
 
142
 
143
#ifdef USE_NAVICTRL
143
#ifdef USE_NAVICTRL
144
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
144
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
145
#endif
145
#endif
146
 
146
 
147
  if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz)
147
  if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz)
148
    cnt = 9;
148
    cnt = 9;
149
    cnt_1ms ^= 1;
149
    cnt_1ms ^= 1;
150
    if (!cnt_1ms) {
150
    if (!cnt_1ms) {
151
      if (runFlightControl == 1)
151
      if (runFlightControl == 1)
152
        debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER;
152
        debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER;
153
      else
153
      else
154
        debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER;
154
        debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER;
155
      runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
155
      runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
156
    }
156
    }
157
    millisecondsCount++; // increment millisecond counter
157
    millisecondsCount++; // increment millisecond counter
158
  }
158
  }
159
 
159
 
160
  // beeper on if duration is not over
160
  // beeper on if duration is not over
161
  if (beepTime) {
161
  if (beepTime) {
162
    beepTime--; // decrement BeepTime
162
    beepTime--; // decrement BeepTime
163
    if (beepTime & beepModulation)
163
    if (beepTime & beepModulation)
164
      // beeper_On = 1;
164
      // beeper_On = 1;
165
      beeper_On = 1; // shut up in dev!!!
165
      beeper_On = 1; // shut up in dev!!!
166
    else
166
    else
167
      beeper_On = 0;
167
      beeper_On = 0;
168
  } else { // beeper off if duration is over
168
  } else { // beeper off if duration is over
169
    beeper_On = 0;
169
    beeper_On = 0;
170
    beepModulation = BEEP_MODULATION_NONE;
170
    beepModulation = BEEP_MODULATION_NONE;
171
  }
171
  }
172
 
-
 
173
  // Beeper remains silent till the first time a control signal was received.
172
 
174
  if (beeper_On & controlMixer_didReceiveSignal) {
173
  if (beeper_On) {
175
    // set speaker port to high.
174
    // set speaker port to high.
176
    if (boardRelease == 10)
175
    if (boardRelease == 10)
177
      PORTD |= (1 << PORTD2); // Speaker at PD2
176
      PORTD |= (1 << PORTD2); // Speaker at PD2
178
    else
177
    else
179
      PORTC |= (1 << PORTC7); // Speaker at PC7
178
      PORTC |= (1 << PORTC7); // Speaker at PC7
180
  } else { // beeper is off
179
  } else { // beeper is off
181
    // set speaker port to low
180
    // set speaker port to low
182
    if (boardRelease == 10)
181
    if (boardRelease == 10)
183
      PORTD &= ~(1 << PORTD2);// Speaker at PD2
182
      PORTD &= ~(1 << PORTD2);// Speaker at PD2
184
    else
183
    else
185
      PORTC &= ~(1 << PORTC7);// Speaker at PC7
184
      PORTC &= ~(1 << PORTC7);// Speaker at PC7
186
  }
185
  }
187
 
186
 
188
#ifndef USE_NAVICTRL
187
#ifndef USE_NAVICTRL
189
  // update compass value if this option is enabled in the settings
188
  // update compass value if this option is enabled in the settings
190
  if (staticParams.bitConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) {
189
  if (staticParams.bitConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) {
191
#ifdef USE_MK3MAG
190
#ifdef USE_MK3MAG
192
    MK3MAG_Update(); // read out mk3mag pwm
191
    MK3MAG_Update(); // read out mk3mag pwm
193
#endif
192
#endif
194
  }
193
  }
195
#endif
194
#endif
196
}
195
}
197
 
196
 
198
// -----------------------------------------------------------------------
197
// -----------------------------------------------------------------------
199
uint16_t setDelay(uint16_t t) {
198
uint16_t setDelay(uint16_t t) {
200
  return (millisecondsCount + t - 1);
199
  return (millisecondsCount + t - 1);
201
}
200
}
202
 
201
 
203
// -----------------------------------------------------------------------
202
// -----------------------------------------------------------------------
204
int8_t checkDelay(uint16_t t) {
203
int8_t checkDelay(uint16_t t) {
205
  return (((t - millisecondsCount) & 0x8000) >> 8); // check sign bit
204
  return (((t - millisecondsCount) & 0x8000) >> 8); // check sign bit
206
}
205
}
207
 
206
 
208
// -----------------------------------------------------------------------
207
// -----------------------------------------------------------------------
209
void delay_ms(uint16_t w) {
208
void delay_ms(uint16_t w) {
210
  uint16_t t_stop = setDelay(w);
209
  uint16_t t_stop = setDelay(w);
211
  while (!checkDelay(t_stop))
210
  while (!checkDelay(t_stop))
212
    ;
211
    ;
213
}
212
}
214
 
213
 
215
// -----------------------------------------------------------------------
214
// -----------------------------------------------------------------------
216
void delay_ms_with_adc_measurement(uint16_t w, uint8_t stop) {
215
void delay_ms_with_adc_measurement(uint16_t w, uint8_t stop) {
217
  uint16_t t_stop;
216
  uint16_t t_stop;
218
  t_stop = setDelay(w);
217
  t_stop = setDelay(w);
219
  while (!checkDelay(t_stop)) {
218
  while (!checkDelay(t_stop)) {
220
        if (analogDataReady) {
219
        if (analogDataReady) {
221
          analog_update();
220
          analog_update();
222
          startAnalogConversionCycle();
221
          startAnalogConversionCycle();
223
        }
222
        }
224
  }
223
  }
225
  if (stop) {
224
  if (stop) {
226
  // Wait for new samples to get prepared but do not restart AD conversion after that!
225
  // Wait for new samples to get prepared but do not restart AD conversion after that!
227
  // Caller MUST to that.
226
  // Caller MUST to that.
228
        while (!analogDataReady);
227
        while (!analogDataReady);
229
  }
228
  }
230
}
229
}
231
 
230