Changing mc33926 motor driver library to use Timer 2 instead of Timer 1

I am trying to use the mc33926 library to drive 2 motors. Right now the library is not working with my arduino because my project includes radio modules that use Timer 1 on the Arduino (Timer 1 is used in the mc33926 library). This is creating conflict between the 2 libraries.

Would it be possible to modify the MC33926 library to use Timer 2 instead? If so is there any instruction available to do so? Also could this change introduce additional problems?

-Thank you

Hello.

Yes, you could modify our MC33926 library to use timer2, but we do not have any specific instructions on how to do that. However, you might find useful this general advice:

The values will also likely be different for timer2. Also, we do not expect switching to a different timer to produce any inherent problems.

-Jon

Thank you Jon,

I believe that I will have to change the following sections of the library to get it working:

I think those pins need to be changed to 3 and 11 on the Arduino UNO for timer 2.

This statement sets up timer one. I believe TCCR1A and TCCR1B are registers that configure timer 1 but I have been unable to find the configuration registers for timer 2. If anyone is familiar the registers for timer 2 or could point me to the correct portion of the data sheet that would be great.

TCNT2, OCR2A and OCR2B look like they may be useful but I’m not sure.

Here is a link to the data sheet I am using:
http://www.atmel.com/images/atmel-8271-8-bit-avr-microcontroller-atmega48a-48pa-88a-88pa-168a-168pa-328-328p_datasheet_complete.pdf

-Eric

In the pdf you linked to, Section 18 “8-bit Timer/Counter2 with PWM and Asynchronous Operation” (page 141) is devoted to the use of timer2.

-Jon

Here’s what I changed. I was able to figure out that TCCR2A and TCCR2B control timer 2. Also the ICR1 register is not available for timer 2 so I was not able to set a user-defined “top” and just had to go with 0xFF (255)

However, after the modifications were done via a fork of the original from github, I tried to include the library to my arduino and got an error. I then tried with the original pololu library and got another error. Any ideas on why not even the original library could be uploaded? I am haven’t added my own library to arduino before so any advice on that would be appreciated.

Thanks,
-Eric

I don’t see anything obviously wrong with how you have configured your timer. Can you post a screenshot of the error you are getting?

-Jon

I was able to get around my issue of including the library by pasting the cpp and .h files straight into the program folder on my arduino. There were no compile errors. The following screenshot gives the error I was receiving

However when I ran the following example program, the fault function returned a fault and subsequently entered the infinite loop.

This is how the fault function is defined in the original library.

Does the fact that I am getting this indicate a wiring error? Could it also thbe that I changed the pwm pins to 3 and 11 as per timer 2 as shown below?

I think because I changed these pins I will need to reconfigure the shield from the default (I may have forgot to mention I am using a shield. The following link details the defaults.

It sounds like you did not install the libraries correctly, but moving them into the same folder as your sketch worked for you. If you want to get the library installed normally, let me know, and I can help you with that.

Yes, since you remapped the PWM pins in the MC33926 library, you will have to modify the MC33926 shield to connect your new PWM pins to the proper inputs on the drivers. The “Remapping the Arduino Connections” section you linked to has instructions on how to do that.

I do not think that a wiring error would produce a fault like what you are seeing. The MC33926 reports a fault when it is experiencing an over-temperature condition or short-circuit across its outputs. (You can learn more about the fault inside the datasheet for the MC33926, which we link to under the “Resources” tab of its product page.)

Does the fault still occur if the motor is disconnected? If so, and if you have a multimeter, can you check for a short across the motor outputs while the shield is powered? Can you also remove your shield from your Arduino and post pictures that clearly show both sides of it?

-Jon

After remapping the board I was able to get it to drive motors properly. It was more difficult to sever the connections than the description made it seem like it would be. I believe that may have been the issue that was causing the faults.

Below is what the shield currently looks like.

I still was not able to get the library to include as a .zip file but that is not important to me any longer. Below is a link to the modified library I created to use timer 2 in case anyone is interested in how to configure the registers.

If you can find my error that is preventing it from downloading let me know.

Thanks again.

The dual-mc33926-motor-shield-master zipped library folder is not intended to be used as-is, though we might update it in the future to work better with the Arduino IDE library manager. We have a short set of instructions that describe how to install that library under the Software section of the shield’s GitHub page. If that is not what you are doing, can you try that method and see if you can get the library to be recognized? If it is not being recognized, can you post pictures that clearly show your Arduino/libraries directory? (For reference, the location of mine is “C:\Users\jon\Documents\Arduino\libraries”.)

I am glad you are able to drive motors from your modified library; thanks for sharing it.

-Jon

Thanks for showing me those steps. I am no longer having the problem.

-Eric

1 Like