Inverted PWM signal to driver

Good afternoon

I need to apply PWM to the input of the IR2130 driver.
But it needs inverted signals according to the datasheet. What is the best way to do this?
IR2130

void hall_BC() {
  AllTransistorsOff();
  analogWrite(AMinusPin, PWM); 
  analogWrite(CPlusPin, PWM); 
  //Serial.println("A-, C+");  
   }


Think about it…

PWM is a proportional duty cycle between 0 and 100% (0-255)

So you need it to be inverted between 100 and 0 (255 and 0)

So the math needed to invert that scale is completely trivial.

If you’re really stuck, I’m sure someone will help.

It also may need a gap between the signals. Search the forum for using "Frequency and Phase correct PWM" mode.

Or get a MOSFET driver that includes settable gaps between the hi/lo enable signals and only requires a single PWM signal.

Thank you.
I plan to implement deadtime



volatile int PWM = 0;  // PWM signal for Hall sensor ISRs
const byte deadtime = 10; //exclude short circuit of half-bridge transistors
unsigned long timing; // Variable to store the reference point

const byte HallAPin = 0;  // yellow to D0 /  Note: Would conflict with Serial1 on this Leonardo
const byte HallBPin = 1;  // green to D1 /  Note: Would conflict with Serial1 on this Leonardo
const byte HallCPin = 2; // blue to D2

const byte APlusPin = 3;    // (A+)=pin3, transistor М1
const byte BMinusPin = 11;   // (B-)=pin11, transistor М6
const byte CPlusPin = 10;    // (C+)=pin10, transistor М5
const byte AMinusPin = 9;   // (A-)=pin9, transistor М4
const byte BPlusPin = 6;   // (B+)=pin6, transistor М3
const byte CMinusPin = 5;  // (C-)=pin5, transistor М2

void AllTransistorsOff() {
  digitalWrite(APlusPin, LOW);   
  digitalWrite(BMinusPin, LOW);  
  digitalWrite(CPlusPin, LOW);   
  digitalWrite(AMinusPin, LOW);  
  digitalWrite(BPlusPin, LOW);   
  digitalWrite(CMinusPin, LOW);  
}

void setup() {
  Serial.begin(115200);
  delay(200);
  // Wait for USB connection or 5 seconds
  while (!Serial && millis() < 5000) {}

  AllTransistorsOff();
   
  pinMode(AMinusPin, OUTPUT);  
  pinMode(CPlusPin, OUTPUT);   
  pinMode(BMinusPin, OUTPUT);  
  pinMode(APlusPin, OUTPUT);   
  pinMode(CMinusPin, OUTPUT);  
  pinMode(BPlusPin, OUTPUT);   

digitalWrite(LED_BUILTIN, LOW);
  pinMode(LED_BUILTIN, OUTPUT);

  // Leonardo reads hall sensors of the BLDC motor
  pinMode(HallAPin, INPUT_PULLUP);  //  hallA - pin D0 yellow
  pinMode(HallBPin, INPUT_PULLUP);  //  hallB- pin D1 green
  pinMode(HallCPin, INPUT_PULLUP);  // hallC - pin D2 blue

  attachInterrupt(digitalPinToInterrupt(HallAPin), HallAPinISR, CHANGE);  // hallA - pin0 - yellow
  attachInterrupt(digitalPinToInterrupt(HallBPin), HallBPinISR, CHANGE);  // hallB - pin1 - green
  attachInterrupt(digitalPinToInterrupt(HallCPin), HallCPinISR, CHANGE);  // hallC  - pin2- blue
}

void loop() {
  int power = analogRead(A0) / 4;  // read the potentiometer that adjust PWM
  noInterrupts();  // Disable interrupt while volatile variable are updated
  PWM = power;
  interrupts();
if (millis() - timing > deadtime){ 
  timing = millis(); 
 }
}

// pin0 / hallA interaption
void HallAPinISR() {
  if (digitalRead(0) == HIGH) {
    digitalWrite(LED_BUILTIN, HIGH);
    hallA_C();  // code change from 001 to 101 / A+, B-
  } else {
    digitalWrite(LED_BUILTIN, LOW);
    hall_B_();  // code change from 110 to 010 / A-, B+
  }
 }

// pin1 / hallB interaption
void HallBPinISR() {
  if (digitalRead(1) == HIGH)
    hallAB_();  // code change from 100 to 110 / B+, C-
  else
    hall__C();  // code change from 011 to 001 /  B-, C+
}

// pin2 / hallC interaption
void HallCPinISR() {
  if (digitalRead(2) == HIGH)
    hall_BC();  // code change from 010 to 011 / A-, B+
  else
    hallA__();  // code change from 101 to 100 /  A+, C-
}


// A-, C+
void hall_BC() {
  AllTransistorsOff();
  analogWrite(AMinusPin, PWM); 
  analogWrite(CPlusPin, PWM); 
  //Serial.println("A-, C+");  
   }

// B-, C+
void hall__C() {
  AllTransistorsOff();
  analogWrite(BMinusPin, PWM);  
  analogWrite(CPlusPin, PWM); 
  Serial.println("B-, C+");  
}

// A+, B-
void hallA_C() {
  AllTransistorsOff();
  analogWrite(APlusPin, PWM);  
  analogWrite(BMinusPin, PWM);  
  Serial.println("A+, B-");
}


// A+, C-
void hallA__() {
  AllTransistorsOff();
  analogWrite(APlusPin, PWM);   
  analogWrite(CMinusPin, PWM);  
 Serial.println("A=, C-");
}

// B+, C-
void hallAB_() {
  AllTransistorsOff();
  analogWrite(BPlusPin, PWM);   
  analogWrite(CMinusPin, PWM); 
  Serial.println("B+, C-"); 
}


// A-, B+
void hall_B_() {
  AllTransistorsOff();
  analogWrite(AMinusPin, PWM);  
  analogWrite(BPlusPin, PWM);   
  Serial.println("A-, B+");
}

What do you recommend? You can use in the function void hall_BC() that appears while interrupted
to use delayMicroseconds(5);
I need some delay before PWM will be sent.

void hall_BC() {
  AllTransistorsOff();
delayMicroseconds(5);  
  analogWrite(AMinusPin, PWM); 
  analogWrite(CPlusPin, PWM); 
   }

The signals are active LOW, i.e. OFF by HIGH.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.