I've been working on a project (Arduino Uno: Atmega328) that implements a gyroscope (which is used with the LSM9DS0 spark-fun library) that is communicated with through I2C and uses a Servo library. Due to a timer conflict with the standard Servo library and another library I've been using, I am now using the ServoTimer2 library which obviously uses timer 2. This is when the problem started.

When I call the attach() function of the ServoTimer2 library, it allows the gyroscope to be read a few times and then the gyroscope feed cuts out. I am using the serial monitor (115200 baud) to read the gyroscope data. The Serial monitor is not the problem because it uses a UART interrupt for Serial.

To fix this error, I modified the ServoTimer2 library to run on Timer0 (Yes I know this handles the delay functions, but they are not needed in my case), which is now called ServoTimer0. This modified version of the library works perfect except for when I attempt to use it with the gyroscope. The same problem still arises though. I've traced what causes this failure down to one line of code that sets up the interrupts:

TIMSK0 =  _BV(OCIE0A) //This is in my own modified version of the library. 

That is what causes it to fail. I believe the AtMega328 can run I2C natively, and my thought is that this native running somehow uses these timers.

Any solutions? Thanks

  • The I2C (Wire) functionality does not user Timer0. TIMSK0 = _BV(OCIE0A) //This is in my own modified version of the library.: That line enables the output-compare interrupt. Why are you enabling that? Do you have a handler to handle that interrupt? That would look like this: ISR(TIMER0_COMPA_vect) { // some code } – Nick Gammon Jun 26 '15 at 5:39
  • Please post your code. The line you mention needs the TIMER0_COMPA_vect interrupt vector to be supplied or it will crash. – Nick Gammon Aug 21 '15 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.