Hey @Navymud,
This is very strange as these pins cannot belong to the same timer. So I’ve updated the code in the dev branch to allow for more timer high-low pair combinations. It would be really cool if you could try it out.
Now allowed pairs will be:
Timer | PWM pin pairs |
---|---|
TIM0 | 4 , 13 |
TIM1 | 11 , 12 |
TIM2 | 10 , 9 |
TIM3 | 5 , 2 |
TIM4 | 6 , 7 |
TIM5 | 45 , 46 |
It would be awesome if you could let us know if some other pin combination will work for you.
If you are not sure how to use the dev branch, you can also only copy paste the changes that I’ve made. Ony thing that you need to do is go to your SimpleFOC library code and navigate to src\drivers\hardware_specific\atmega2560_mcu.cpp
and replace the function _configureComplementaryPair
with this code
// function configuring pair of high-low side pwm channels, 32khz frequency and center aligned pwm
// supports Arudino/ATmega2560
// https://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf
// https://docs.arduino.cc/hacking/hardware/PinMapping2560
int _configureComplementaryPair(int pinH, int pinL) {
if( (pinH == 4 && pinL == 13 ) || (pinH == 13 && pinL == 4 ) ){
// configure the pwm phase-corrected mode
TCCR0A = ((TCCR0A & 0b11111100) | 0x01);
// configure complementary pwm on low side
if(pinH == 13 ) TCCR0A = 0b10110000 | (TCCR0A & 0b00001111) ;
else TCCR0A = 0b11100000 | (TCCR0A & 0b00001111) ;
// set prescaler to 1 - 32kHz freq
TCCR0B = ((TCCR0B & 0b11110000) | 0x01);
}else if( (pinH == 11 && pinL == 12 ) || (pinH == 12 && pinL == 11 ) ){
// set prescaler to 1 - 32kHz freq
TCCR1B = ((TCCR1B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 11 ) TCCR1A = 0b10110000 | (TCCR1A & 0b00001111) ;
else TCCR1A = 0b11100000 | (TCCR1A & 0b00001111) ;
}else if((pinH == 10 && pinL == 9 ) || (pinH == 9 && pinL == 10 ) ){
// set prescaler to 1 - 32kHz freq
TCCR2B = ((TCCR2B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 10 ) TCCR2A = 0b10110000 | (TCCR2A & 0b00001111) ;
else TCCR2A = 0b11100000 | (TCCR2A & 0b00001111) ;
}else if((pinH == 5 && pinL == 2 ) || (pinH == 2 && pinL == 5 ) ){
// set prescaler to 1 - 32kHz freq
TCCR3B = ((TCCR3B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 5 ) TCCR3A = 0b10110000 | (TCCR3A & 0b00001111) ;
else TCCR3A = 0b11100000 | (TCCR3A & 0b00001111) ;
}else if((pinH == 6 && pinL == 7 ) || (pinH == 7 && pinL == 6 ) ){
// set prescaler to 1 - 32kHz freq
TCCR4B = ((TCCR4B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 6 ) TCCR4A = 0b10110000 | (TCCR4A & 0b00001111) ;
else TCCR4A = 0b11100000 | (TCCR4A & 0b00001111) ;
}else if((pinH == 46 && pinL == 45 ) || (pinH == 45 && pinL == 46 ) ){
// set prescaler to 1 - 32kHz freq
TCCR5B = ((TCCR5B & 0b11111000) | 0x01);
// configure complementary pwm on low side
if(pinH == 46 ) TCCR5A = 0b10110000 | (TCCR5A & 0b00001111) ;
else TCCR5A = 0b11100000 | (TCCR5A & 0b00001111) ;
}else{
return -1;
}
return 0;
}