So I recently got an Arbotix to replace my MiniRobocontroller as I was out of pins and needed the extra serial port. Now my code worked on the MiniRobocontroller. The Motors library was using Timer0, the Hservo library was using Timer1, an I had the following code using Timer2 to generate 1ms interrupts.
and my ISR:Code:// First disable the timer overflow interrupt while we're configuring TIMSK2 &= ~(1<<TOIE2); // Configure timer2 in normal mode (pure counting, no PWM etc.) TCCR2A &= ~((1<<WGM21) | (1<<WGM20)); TCCR2B &= ~(1<<WGM22); // Select clock source: internal I/O clock ASSR &= ~(1<<AS2); // Disable Compare Match A interrupt enable (only want overflow) TIMSK2 &= ~(1<<OCIE2A); // Now configure the prescaler to CPU clock divided by 128 TCCR2B |= (1<<CS22) | (1<<CS20); // Set bits TCCR2B &= ~(1<<CS21); // Clear bit /* We need to calculate a proper value to load the timer counter. * The following loads the value 131 into the Timer 2 counter register * The math behind this is: * (CPU frequency) / (prescaler value) = 125000 Hz = 8us. * (desired period) / 8us = 125. * MAX(uint8) + 1 - 125 = 131; */ // Save value globally for later reload in ISR tcnt2 = 131; // Finally load end enable the timer TCNT2 = tcnt2; TIMSK2 |= (1<<TOIE2);
Code:ISR(TIMER2_OVF_vect) { // Reload the timer TCNT2 = tcnt2; // increment the system_timer system_time = system_time++; }
My code doesn't make use of arduino's mills(), micro(), or delay() functions, but am I correct in thinking that using all three timers would have broken these?
So the Arbotix uses Timer2 for its motor driver library, which means I needed to switch my 1ms interrupt over to Timer0. I modified my code to look like this:
With this ISR:Code:// First disable the timer overflow interrupt while we're configuring TIMSK0 &= ~(1<<TOIE0); // Configure timer2 in normal mode (pure counting, no PWM etc.) TCCR0A &= ~((1<<WGM01) | (1<<WGM00)); TCCR0B &= ~(1<<WGM02); // Select clock source: internal I/O clock ASSR &= ~(1<<AS2); // Disable Compare Match A interrupt enable (only want overflow) TIMSK2 &= ~(1<<OCIE2A); // Now configure the prescaler to CPU clock divided by 128 TCCR0B |= (1<<CS22) | (1<<CS20); // Set bits TCCR0B &= ~(1<<CS21); // Clear bit /* We need to calculate a proper value to load the timer counter. * The following loads the value 131 into the Timer 2 counter register * The math behind this is: * (CPU frequency) / (prescaler value) = 125000 Hz = 8us. * (desired period) / 8us = 125. * MAX(uint8) + 1 - 125 = 131; */ // Save value globally for later reload in ISR tcnt0 = 131; // Finally load end enable the timer TCNT0 = tcnt0; TIMSK0 |= (1<<TOIE0);
When I try to compile I get this error:Code:ISR(TIMER0_OVF_vect) { // Reload the timer TCNT0 = tcnt0; // increment the system_timer system_time = system_time++; }
core.a(wiring.c.o): In function `__vector_18':
/Users/jeffreydamelio/Documents/Arduino/hardware/arbotix/cores/arbotix/wiring.c:45: multiple definition of `__vector_18'
Sparky_1_0_arduino22.cpp.o:Sparky_1_0_arduino22.cp p:207: first defined here
As near as I can tell, the file that modifies Millis(), micro(), etc. for use on the 644p is already trying to use this interrupt vector. Is this correct? What do I change to make this work? And thanks in advance to all the helpful people.



Reply With Quote




Bookmarks