

amptest
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
Diff Revisions 2 vs 3 for /trunk/amptimer.cpp
@@ -10,26 +10,26 @@ | |||
10 | 10 | ||
11 | 11 | struct SSweep | |
12 | 12 | { | |
13 | - | bool enable; | |
13 | + | bool enable; | |
14 | 14 | ||
15 | - | double startFreq; // Hz | |
16 | - | double endFreq; // Hz | |
17 | - | double sweepRate; // Hz/sec | |
18 | - | ||
19 | - | double currFreq; // current frequency | |
20 | - | bool dir; // current direction. false - mean from start to end | |
21 | - | ||
22 | - | SSweep() | |
23 | - | { | |
24 | - | enable = false; | |
25 | - | ||
26 | - | startFreq = 0.5; | |
27 | - | endFreq = 10; | |
28 | - | sweepRate = 0.5; | |
29 | - | ||
30 | - | currFreq = startFreq; | |
31 | - | dir = false; | |
32 | - | } | |
15 | + | double startFreq; // Hz | |
16 | + | double endFreq; // Hz | |
17 | + | double sweepRate; // Hz/sec | |
18 | + | ||
19 | + | double currFreq; // current frequency | |
20 | + | bool dir; // current direction. false - mean from start to end | |
21 | + | ||
22 | + | SSweep() | |
23 | + | { | |
24 | + | enable = false; | |
25 | + | ||
26 | + | startFreq = 0.5; | |
27 | + | endFreq = 10; | |
28 | + | sweepRate = 0.5; | |
29 | + | ||
30 | + | currFreq = startFreq; | |
31 | + | dir = false; | |
32 | + | } | |
33 | 33 | }; | |
34 | 34 | ||
35 | 35 | #define SWEEP_STEPS_MAX_COUNT 2000 | |
@@ -42,13 +42,13 @@ | |||
42 | 42 | ||
43 | 43 | ||
44 | 44 | #if !defined(__SAM3X8E__) | |
45 | - | int tick_mult = 1; | |
46 | - | int tick_count = 0; | |
47 | - | uint32_t tmr_tick_in_count = 20; | |
48 | - | ||
49 | - | int sweep_tick_mult = 1; | |
50 | - | int sweep_tick_count = 0; | |
51 | - | uint32_t sweep_tmr_tick_in_count = 20; | |
45 | + | int tick_mult = 1; | |
46 | + | int tick_count = 0; | |
47 | + | uint32_t tmr_tick_in_count = 20; | |
48 | + | ||
49 | + | int sweep_tick_mult = 1; | |
50 | + | int sweep_tick_count = 0; | |
51 | + | uint32_t sweep_tmr_tick_in_count = 20; | |
52 | 52 | #endif | |
53 | 53 | ||
54 | 54 | ||
@@ -59,24 +59,24 @@ | |||
59 | 59 | #else | |
60 | 60 | ISR(TIMER1_OVF_vect) | |
61 | 61 | { | |
62 | - | TCNT1 = tmr_tick_in_count; | |
62 | + | TCNT1 = tmr_tick_in_count; | |
63 | 63 | ||
64 | - | if (tick_mult > 1) | |
65 | - | { | |
66 | - | tick_count++; | |
64 | + | if (tick_mult > 1) | |
65 | + | { | |
66 | + | tick_count++; | |
67 | 67 | ||
68 | - | if (tick_count < tick_mult) | |
69 | - | return; | |
70 | - | } | |
68 | + | if (tick_count < tick_mult) | |
69 | + | return; | |
70 | + | } | |
71 | 71 | ||
72 | - | tick_count = 0; | |
72 | + | tick_count = 0; | |
73 | 73 | ||
74 | 74 | #endif | |
75 | 75 | ||
76 | 76 | ||
77 | - | uint32_t val = getNextDAC() >> 4; // conv resolution from 16 to 12 bit | |
77 | + | uint32_t val = getNextDAC() >> 4; // conv resolution from 16 to 12 bit | |
78 | 78 | ||
79 | - | dac_write(val); | |
79 | + | dac_write(val); | |
80 | 80 | ||
81 | 81 | ||
82 | 82 | } | |
@@ -84,42 +84,42 @@ | |||
84 | 84 | #if defined(__SAM3X8E__) | |
85 | 85 | void Timer2Interrupt() | |
86 | 86 | { | |
87 | - | if (!m_sweep.enable) | |
88 | - | return; | |
87 | + | if (!m_sweep.enable) | |
88 | + | return; | |
89 | 89 | ||
90 | 90 | #else | |
91 | 91 | ISR(TIMER5_OVF_vect) | |
92 | 92 | { | |
93 | - | TCNT5 = sweep_tmr_tick_in_count; | |
93 | + | TCNT5 = sweep_tmr_tick_in_count; | |
94 | 94 | ||
95 | - | if (sweep_tick_mult > 1) | |
96 | - | { | |
97 | - | sweep_tick_count++; | |
95 | + | if (sweep_tick_mult > 1) | |
96 | + | { | |
97 | + | sweep_tick_count++; | |
98 | 98 | ||
99 | - | if (sweep_tick_count < sweep_tick_mult) | |
100 | - | return; | |
101 | - | } | |
99 | + | if (sweep_tick_count < sweep_tick_mult) | |
100 | + | return; | |
101 | + | } | |
102 | 102 | ||
103 | - | sweep_tick_count = 0; | |
103 | + | sweep_tick_count = 0; | |
104 | 104 | ||
105 | 105 | #endif | |
106 | 106 | ||
107 | - | double diff = m_sweep.sweepRate / (double)SWEEP_STEPS_MAX_COUNT; | |
107 | + | double diff = m_sweep.sweepRate / (double)SWEEP_STEPS_MAX_COUNT; | |
108 | 108 | ||
109 | - | if (m_sweep.endFreq < m_sweep.startFreq) diff *= -1.0; | |
109 | + | if (m_sweep.endFreq < m_sweep.startFreq) diff *= -1.0; | |
110 | 110 | ||
111 | - | double nextFreq = m_sweep.dir ? m_sweep.currFreq - diff : m_sweep.currFreq + diff; | |
111 | + | double nextFreq = m_sweep.dir ? m_sweep.currFreq - diff : m_sweep.currFreq + diff; | |
112 | 112 | ||
113 | - | double maxfreq = max(m_sweep.endFreq, m_sweep.startFreq); | |
114 | - | double minfreq = min(m_sweep.endFreq, m_sweep.startFreq); | |
113 | + | double maxfreq = max(m_sweep.endFreq, m_sweep.startFreq); | |
114 | + | double minfreq = min(m_sweep.endFreq, m_sweep.startFreq); | |
115 | 115 | ||
116 | - | if (nextFreq > maxfreq || nextFreq < minfreq) | |
117 | - | { | |
118 | - | nextFreq = m_sweep.currFreq; | |
119 | - | m_sweep.dir = !m_sweep.dir; | |
120 | - | } | |
116 | + | if (nextFreq > maxfreq || nextFreq < minfreq) | |
117 | + | { | |
118 | + | nextFreq = m_sweep.currFreq; | |
119 | + | m_sweep.dir = !m_sweep.dir; | |
120 | + | } | |
121 | 121 | ||
122 | - | m_sweep.currFreq = nextFreq; | |
122 | + | m_sweep.currFreq = nextFreq; | |
123 | 123 | ||
124 | 124 | ||
125 | 125 | ||
@@ -130,7 +130,7 @@ | |||
130 | 130 | ||
131 | 131 | ||
132 | 132 | ||
133 | - | setFreq(nextFreq); | |
133 | + | setFreq(nextFreq); | |
134 | 134 | ||
135 | 135 | ||
136 | 136 | } | |
@@ -139,11 +139,11 @@ | |||
139 | 139 | void timer_init() | |
140 | 140 | { | |
141 | 141 | #if defined(__SAM3X8E__) | |
142 | - | Timer1.attachInterrupt(Timer1Interrupt); | |
143 | - | Timer2.attachInterrupt(Timer2Interrupt); | |
142 | + | Timer1.attachInterrupt(Timer1Interrupt); | |
143 | + | Timer2.attachInterrupt(Timer2Interrupt); | |
144 | 144 | #else | |
145 | - | TCCR1B = 0x00; | |
146 | - | TCCR5B = 0x00; | |
145 | + | TCCR1B = 0x00; | |
146 | + | TCCR5B = 0x00; | |
147 | 147 | #endif | |
148 | 148 | } | |
149 | 149 | ||
@@ -151,45 +151,45 @@ | |||
151 | 151 | ||
152 | 152 | double getFreq() | |
153 | 153 | { | |
154 | - | return m_freq; | |
154 | + | return m_freq; | |
155 | 155 | } | |
156 | 156 | ||
157 | 157 | ||
158 | - | #define MIN_FREQ 0.1 | |
158 | + | #define MIN_FREQ 0.01 | |
159 | 159 | #define MAX_FREQ 200.0 | |
160 | 160 | #define MIN_SWEEP_RATE 0.01 | |
161 | 161 | #define MAX_SWEEP_RATE 20.0 | |
162 | 162 | ||
163 | 163 | ||
164 | - | void setFreq(double freq) // [0.1 - 200.0] | |
164 | + | void setFreq(double freq) // [0.01 - 200.0] | |
165 | 165 | { | |
166 | - | if (freq < MIN_FREQ) freq = MIN_FREQ; | |
167 | - | if (freq > MAX_FREQ) freq = MAX_FREQ; | |
166 | + | if (freq < MIN_FREQ) freq = MIN_FREQ; | |
167 | + | if (freq > MAX_FREQ) freq = MAX_FREQ; | |
168 | 168 | ||
169 | - | m_freq = freq; | |
169 | + | m_freq = freq; | |
170 | 170 | ||
171 | 171 | ||
172 | 172 | #if defined(__SAM3X8E__) | |
173 | 173 | ||
174 | - | Timer1.stop(); | |
175 | - | Timer1.setFrequency(freq * getTabSize()); | |
176 | - | Timer1.start(); | |
174 | + | Timer1.stop(); | |
175 | + | Timer1.setFrequency(freq * getTabSize()); | |
176 | + | Timer1.start(); | |
177 | 177 | ||
178 | 178 | #else | |
179 | 179 | ||
180 | - | int mult = 1; | |
180 | + | int mult = 1; | |
181 | 181 | ||
182 | - | uint32_t pulses = (uint32_t)(F_CPU / (freq * getTabSize())); | |
182 | + | uint32_t pulses = (uint32_t)(F_CPU / (freq * getTabSize())); | |
183 | 183 | ||
184 | - | while (pulses > 40000) | |
185 | - | { | |
186 | - | pulses /= 2; | |
187 | - | mult *= 2; | |
188 | - | } | |
184 | + | while (pulses > 40000) | |
185 | + | { | |
186 | + | pulses /= 2; | |
187 | + | mult *= 2; | |
188 | + | } | |
189 | 189 | ||
190 | - | tmr_tick_in_count = 65535 - pulses; | |
190 | + | tmr_tick_in_count = 65535 - pulses; | |
191 | 191 | ||
192 | - | tick_mult = mult; | |
192 | + | tick_mult = mult; | |
193 | 193 | ||
194 | 194 | #endif // __SAM3X8E__ | |
195 | 195 | ||
@@ -199,47 +199,47 @@ | |||
199 | 199 | ||
200 | 200 | void setSweepEnable(bool bEnable) | |
201 | 201 | { | |
202 | - | m_sweep.enable = bEnable; | |
202 | + | m_sweep.enable = bEnable; | |
203 | 203 | } | |
204 | 204 | ||
205 | 205 | void setSweep(double start, double end, double rate) | |
206 | 206 | { | |
207 | - | if (start < MIN_FREQ) start = MIN_FREQ; | |
208 | - | if (start > MAX_FREQ) start = MAX_FREQ; | |
209 | - | if (end < MIN_FREQ) end = MIN_FREQ; | |
210 | - | if (end > MAX_FREQ) end = MAX_FREQ; | |
211 | - | if (rate < MIN_SWEEP_RATE) rate = MIN_SWEEP_RATE; | |
212 | - | if (rate > MAX_SWEEP_RATE) rate = MAX_SWEEP_RATE; | |
213 | - | ||
214 | - | m_sweep.startFreq = start; | |
215 | - | m_sweep.endFreq = end; | |
216 | - | m_sweep.sweepRate = rate; | |
207 | + | if (start < MIN_FREQ) start = MIN_FREQ; | |
208 | + | if (start > MAX_FREQ) start = MAX_FREQ; | |
209 | + | if (end < MIN_FREQ) end = MIN_FREQ; | |
210 | + | if (end > MAX_FREQ) end = MAX_FREQ; | |
211 | + | if (rate < MIN_SWEEP_RATE) rate = MIN_SWEEP_RATE; | |
212 | + | if (rate > MAX_SWEEP_RATE) rate = MAX_SWEEP_RATE; | |
213 | + | ||
214 | + | m_sweep.startFreq = start; | |
215 | + | m_sweep.endFreq = end; | |
216 | + | m_sweep.sweepRate = rate; | |
217 | 217 | ||
218 | - | m_sweep.currFreq = m_sweep.startFreq; | |
219 | - | m_sweep.dir = false; // false - mean from start to end; true - from end to start | |
218 | + | m_sweep.currFreq = m_sweep.startFreq; | |
219 | + | m_sweep.dir = false; // false - mean from start to end; true - from end to start | |
220 | 220 | ||
221 | 221 | ||
222 | 222 | #if defined(__SAM3X8E__) | |
223 | 223 | ||
224 | - | Timer2.stop(); | |
225 | - | Timer2.setFrequency(rate * SWEEP_STEPS_MAX_COUNT); | |
226 | - | Timer2.start(); | |
224 | + | Timer2.stop(); | |
225 | + | Timer2.setFrequency(rate * SWEEP_STEPS_MAX_COUNT); | |
226 | + | Timer2.start(); | |
227 | 227 | ||
228 | 228 | #else | |
229 | 229 | ||
230 | - | int mult = 1; | |
230 | + | int mult = 1; | |
231 | 231 | ||
232 | - | uint32_t pulses = (uint32_t)(F_CPU / (rate * SWEEP_STEPS_MAX_COUNT));// | |
232 | + | uint32_t pulses = (uint32_t)(F_CPU / (rate * SWEEP_STEPS_MAX_COUNT));// | |
233 | 233 | ||
234 | - | while (pulses > 40000) | |
235 | - | { | |
236 | - | pulses /= 2; | |
237 | - | mult *= 2; | |
238 | - | } | |
234 | + | while (pulses > 40000) | |
235 | + | { | |
236 | + | pulses /= 2; | |
237 | + | mult *= 2; | |
238 | + | } | |
239 | 239 | ||
240 | - | sweep_tmr_tick_in_count = 65535 - pulses; | |
240 | + | sweep_tmr_tick_in_count = 65535 - pulses; | |
241 | 241 | ||
242 | - | sweep_tick_mult = mult; | |
242 | + | sweep_tick_mult = mult; | |
243 | 243 | ||
244 | 244 | #endif // __SAM3X8E__ | |
245 | 245 | } | |
@@ -247,41 +247,41 @@ | |||
247 | 247 | ||
248 | 248 | void setSweep(const char* szSweep) | |
249 | 249 | { | |
250 | - | if (strstr(szSweep, "?") != NULL) | |
251 | - | { | |
252 | - | return; | |
253 | - | } | |
254 | - | else if (strstr(szSweep, "no") != NULL) | |
255 | - | { | |
256 | - | setSweepEnable(false); | |
257 | - | return; | |
258 | - | } | |
259 | - | ||
260 | - | double startFreq = atof(szSweep); | |
261 | - | double endFreq = 0; | |
262 | - | double sweepRate = 0; | |
263 | - | ||
264 | - | char* szNext = strstr(szSweep, ";"); | |
265 | - | ||
266 | - | if (szNext != NULL) | |
267 | - | { | |
268 | - | szNext += 1; | |
269 | - | ||
270 | - | endFreq = atof(szNext); | |
271 | - | ||
272 | - | szNext = strstr(szNext, ";"); | |
273 | - | ||
274 | - | if (szNext != NULL) | |
275 | - | { | |
276 | - | szNext += 1; | |
277 | - | ||
278 | - | sweepRate = atof(szNext); | |
279 | - | ||
280 | - | setSweep(startFreq, endFreq, sweepRate); | |
281 | - | ||
282 | - | setSweepEnable(true); | |
283 | - | } | |
284 | - | } | |
250 | + | if (strstr(szSweep, "?") != NULL) | |
251 | + | { | |
252 | + | return; | |
253 | + | } | |
254 | + | else if (strstr(szSweep, "no") != NULL) | |
255 | + | { | |
256 | + | setSweepEnable(false); | |
257 | + | return; | |
258 | + | } | |
259 | + | ||
260 | + | double startFreq = atof(szSweep); | |
261 | + | double endFreq = 0; | |
262 | + | double sweepRate = 0; | |
263 | + | ||
264 | + | char* szNext = strstr(szSweep, ";"); | |
265 | + | ||
266 | + | if (szNext != NULL) | |
267 | + | { | |
268 | + | szNext += 1; | |
269 | + | ||
270 | + | endFreq = atof(szNext); | |
271 | + | ||
272 | + | szNext = strstr(szNext, ";"); | |
273 | + | ||
274 | + | if (szNext != NULL) | |
275 | + | { | |
276 | + | szNext += 1; | |
277 | + | ||
278 | + | sweepRate = atof(szNext); | |
279 | + | ||
280 | + | setSweep(startFreq, endFreq, sweepRate); | |
281 | + | ||
282 | + | setSweepEnable(true); | |
283 | + | } | |
284 | + | } | |
285 | 285 | ||
286 | 286 | } | |
287 | 287 | ||
@@ -290,11 +290,11 @@ | |||
290 | 290 | ||
291 | 291 | const char* getSweepParams() | |
292 | 292 | { | |
293 | - | if (m_sweep.enable) | |
294 | - | sprintf(sweepparams, "%0.3f;%0.3f;%0.3f;", m_sweep.startFreq, m_sweep.endFreq, m_sweep.sweepRate); | |
295 | - | else | |
296 | - | strcpy(sweepparams, "no"); | |
293 | + | if (m_sweep.enable) | |
294 | + | sprintf(sweepparams, "%0.3f;%0.3f;%0.3f;", m_sweep.startFreq, m_sweep.endFreq, m_sweep.sweepRate); | |
295 | + | else | |
296 | + | strcpy(sweepparams, "no"); | |
297 | 297 | ||
298 | - | return sweepparams; | |
298 | + | return sweepparams; | |
299 | 299 | } | |
300 | 300 |