Hello,
i have the dual-mc33926 Motor Shield I have some troubles to unterstand some pwm stuff. I’m using a PID controller to control the both motors, but i need a higher pwm resolution. I’m using a Arduino Mega with an Atmega2560.
Which parameters I have to change in the library?
In the code below, I’ve added some gcc tags like AVR_ATmega1280 and 2560, changed ICR3 to 1023 (10 Bits) and the speedlimits to 1023. Is it enought or do I forget something?
#if defined(__AVR_ATmega1280__)|| defined(__AVR_ATmega2560__)|| defined(__AVR_ATmega328P__)
// Timer 1 configuration
// prescaler: clockI/O / 1
// outputs enabled
// phase-correct PWM
// top of 400
//
// PWM frequency calculation
// 16MHz / 1 (prescaler) / 2 (phase-correct) / 400 (top) = 20kHz
TCCR3A = _BV(COM3A1)|_BV(COM3B1) | _BV(COM3C1);
TCCR3B = _BV(WGM33) | _BV(CS30);
ICR3 = 1023;
#endif
}
// Set speed for motor 1, speed is a number betwenn -400 and 400
void DualMC33926MotorShield::setM1Speed(int speed)
{
unsigned char reverse = 0;
if (speed < 0)
{
speed = -speed; // Make speed a positive quantity
reverse = 1; // Preserve the direction
}
if (speed > 1023) // Max PWM dutycycle
speed = 1023;
#if defined(__AVR_ATmega1280__)|| defined(__AVR_ATmega2560__)||defined(__AVR_ATmega328P__)
OCR1A = speed;
#else
analogWrite(_M1PWM,speed * 51 / 80); // default to using analogWrite, mapping 400 to 255
#endif
if (reverse)
digitalWrite(_M1DIR,HIGH);
else
digitalWrite(_M1DIR,LOW);
}