title: How to implement Sine PWM using Tiva TM4C123G?
body: <p>I am trying to implement the SPWM (Sine PWM) using the TM4C123G Microcontroller.</p>
  
  <p>I tried to do the same thing using another microcontroller, the STM32F100RB and it kinda worked. Here is the link for my repository in case someone want to take a look.
  <a href="https://github.com/felipheggaliza/TCC-Brushless-Gimbal" rel="nofollow">https://github.com/felipheggaliza/TCC-Brushless-Gimbal</a></p>
  
  <p>The problem I had with STM32F100RB is that the maximum frequency is just 24MHz. It worked well when driving one BLDC, but in my application I need to drive 3 BLDC motors plus IMU reading and Control. So then, I really want to implement this code using the TM4C123G because I think it will work better. The only thing that is annoying me is how to implement the phase shift in the TM4X123G.</p>
  
  <p>I also found the same technique implemented in Arduino. It is useful to understand how SPWM works: <a href="http://www.berryjam.eu/2015/04/driving-bldc-gimbals-at-super-slow-speeds-with-arduino/" rel="nofollow">http://www.berryjam.eu/2015/04/driving-bldc-gimbals-at-super-slow-speeds-with-arduino/</a></p>
  
  <p>Does someone have knowledge on this? Any piece of code doing the phase shift or direct driving a BLDC motor probably will help me a lot.</p>
  
  <p>Here is a sample of the code I am willing to implement using the TM4C123G. Could someone give me any directive?</p>
  
  <pre><code>//
  // Slow and precise BLDC motor driver using SPWM and SVPWM modulation
  // Part of code used from http://elabz.com/
  // (c) 2015 Ignas Gramba www.berryjam.eu
  //
  
  
  const int EN1 = 5;
  const int EN2 = 6;
  const int EN3 = 7;
  
  const int IN1 = 9;
  const int IN2 = 10;
  const int IN3 = 11;
  
  const int pwmSin[] = {128, 132, 136, 140, 143, 147, 151, 155, 159, 162, 166, 170, 174, 178, 181, 185, 189, 192, 196, 200, 203, 207, 211, 214, 218, 221, 225, 228, 232, 235, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 249, 250, 250, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 253, 252, 252, 251, 250, 250, 249, 248, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 249, 250, 250, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 253, 252, 252, 251, 250, 250, 249, 248, 248, 247, 246, 245, 244, 243, 242, 241, 240, 239, 238, 235, 232, 228, 225, 221, 218, 214, 211, 207, 203, 200, 196, 192, 189, 185, 181, 178, 174, 170, 166, 162, 159, 155, 151, 147, 143, 140, 136, 132, 128, 124, 120, 116, 113, 109, 105, 101, 97, 94, 90, 86, 82, 78, 75, 71, 67, 64, 60, 56, 53, 49, 45, 42, 38, 35, 31, 28, 24, 21, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 8, 7, 6, 6, 5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 24, 28, 31, 35, 38, 42, 45, 49, 53, 56, 60, 64, 67, 71, 75, 78, 82, 86, 90, 94, 97, 101, 105, 109, 113, 116, 120, 124};
  
  int currentStepA;
  int currentStepB;
  int currentStepC;
  int sineArraySize;
  int increment = 0;
  boolean direct = 1; // direction true=forward, false=backward
  
  //////////////////////////////////////////////////////////////////////////////
  
  void setup() {
  
    setPwmFrequency(IN1); // Increase PWM frequency to 32 kHz  (make unaudible)
    setPwmFrequency(IN2);
    setPwmFrequency(IN3);
  
    pinMode(IN1, OUTPUT); 
    pinMode(IN2, OUTPUT); 
    pinMode(IN3, OUTPUT); 
  
    pinMode(EN1, OUTPUT); 
    pinMode(EN2, OUTPUT); 
    pinMode(EN3, OUTPUT); 
  
  
    digitalWrite(EN1, HIGH);
    digitalWrite(EN2, HIGH);
    digitalWrite(EN3, HIGH);
  
  
    sineArraySize = sizeof(pwmSin)/sizeof(int); // Find lookup table size
    int phaseShift = sineArraySize / 3;         // Find phase shift and initial A, B C phase values
    currentStepA = 0;
    currentStepB = currentStepA + phaseShift;
    currentStepC = currentStepB + phaseShift;
  
    sineArraySize--; // Convert from array Size to last PWM array number
  }
  
  //////////////////////////////////////////////////////////////////////////////
  
  void loop() {
  
    analogWrite(IN1, pwmSin[currentStepA]);
    analogWrite(IN2, pwmSin[currentStepB]);
    analogWrite(IN3, pwmSin[currentStepC]);  
  
    if (direct==true) increment = 1;
    else increment = -1;     
  
    currentStepA = currentStepA + increment;
    currentStepB = currentStepB + increment;
    currentStepC = currentStepC + increment;
  
    //Check for lookup table overflow and return to opposite end if necessary
    if(currentStepA &gt; sineArraySize)  currentStepA = 0;
    if(currentStepA &lt; 0)  currentStepA = sineArraySize;
  
    if(currentStepB &gt; sineArraySize)  currentStepB = 0;
    if(currentStepB &lt; 0)  currentStepB = sineArraySize;
  
    if(currentStepC &gt; sineArraySize)  currentStepC = 0;
    if(currentStepC &lt; 0) currentStepC = sineArraySize; 
  
    /// Control speed by this delay
    delay(10);
  
  }
  
  
  void setPwmFrequency(int pin) {
    if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
      if(pin == 5 || pin == 6) {
        TCCR0B = TCCR0B &amp; 0b11111000 | 0x01;
      } else {
        TCCR1B = TCCR1B &amp; 0b11111000 | 0x01;
      }
    }
    else if(pin == 3 || pin == 11) {
      TCCR2B = TCCR2B &amp; 0b11111000 | 0x01;
    }
  }
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>Feliphe</p>