Revision 812788aa
ID | 812788aa5bc592cf417de345fdd8a74933d7a0c8 |
Moved some sensors to different timers
Most notably, change the ROS millisecond counter to share the BOM 38 kHz
timer instead of having its own. The range sensor and BOM rx will now
share timer 5, but they both have to be fixed for the higher F_CPU.
scout_avr/src/bom.cpp | ||
---|---|---|
12 | 12 |
* 0: 1ms period |
13 | 13 |
* Signal: |
14 | 14 |
* 38kHz pulse for 320us, then off for rest of time (680us or 1680us) |
15 |
* BOM uses timer 4 for period timing, and timer 3 for 38kHz signal
|
|
15 |
* BOM uses timer 4 for period timing, and timer 0 for 38kHz signal
|
|
16 | 16 |
*/ |
17 | 17 |
|
18 |
#define TIME_KHZ(khz, prescale) (F_CPU / 1000 / (khz) / (prescale)) |
|
19 | 18 |
#define TIME_MICROS(us, prescale) (F_CPU / 1000000 * (us) / (prescale)) |
20 | 19 |
|
21 | 20 |
typedef uint16_t sharp_msg_t; |
... | ... | |
52 | 51 |
return robot_id; |
53 | 52 |
} |
54 | 53 |
|
55 |
ISR(TIMER3_COMPA_vect) {
|
|
54 |
void bom_isr() {
|
|
56 | 55 |
if (out_high) { |
57 | 56 |
BOM_EMIT ^= out_pin_mask; |
58 | 57 |
} |
... | ... | |
62 | 61 |
out_high = 0; |
63 | 62 |
out_pin_mask = 0; |
64 | 63 |
|
65 |
// timer 3 mode CTC (clear timer on compare), TOP = OCRA |
|
66 |
TCCR3A = 0; |
|
67 |
TCCR3B = _BV(WGM32); |
|
68 |
TCCR3C = 0; |
|
69 |
|
|
70 |
// toggling at 38kHz * 2 gives 38kHz wave |
|
71 |
OCR3A = TIME_KHZ(38 * 2, 1); |
|
72 |
|
|
73 |
// enable interrupt |
|
74 |
TIMSK3 = _BV(OCIE3A); |
|
75 |
|
|
76 |
// start timer 3 at F_CPU, no prescaling |
|
77 |
TCCR3B |= _BV(CS30); |
|
64 |
// timer configuration now done in Atmega128rfa1.cpp |
|
78 | 65 |
} |
79 | 66 |
|
80 | 67 |
static void start_38kHz_signal() { |
... | ... | |
92 | 79 |
// timer 4 mode CTC (clear timer on compare), TOP = OCRA |
93 | 80 |
TCCR4A = 0; |
94 | 81 |
TCCR4B = _BV(WGM42); |
95 |
TCCR3C = 0; |
|
96 | 82 |
|
97 | 83 |
// run interrupt immediately when timer started |
98 | 84 |
OCR4A = 0; |
... | ... | |
166 | 152 |
if (is_rising) { |
167 | 153 |
// TODO check 320us? or have 320us timeout on rising edge? |
168 | 154 |
} else { |
169 |
// uses timer 1, assuming prescale 1/8
|
|
170 |
// timer 1 is set up by range_init()
|
|
155 |
// uses timer 5, assuming prescale 1/8
|
|
156 |
// timer 5 is set up by range_init()
|
|
171 | 157 |
// this should be in units of microseconds |
172 |
int now = TCNT1 * (F_CPU / 1000000 / 8);
|
|
158 |
int now = TCNT5 / (F_CPU / 8 / 1000000);
|
|
173 | 159 |
|
174 | 160 |
if (rx->count) { |
175 | 161 |
int diff = now - rx->last_time; |
Also available in: Unified diff