Need help with PWM registers/analogWrite

Im creating a motor controller for a 3 phase AC motor.
I’m having a lot of difficulty with generating the correct pwm signals. I need to generate a signal with 10kHz frequency, the period must be center aligned, and I need to update the duty cycle every 10kHz.
I tried this using analogWrite, but the pwm signales where shifted, and only left aligned. So I went on the great internet in search for some help and found some code of interest. The problem I have with this code is that when I try to update the duty cycle the pwm signal ends up as a giant noisy blur. It seems like it doesnt reset properly and write the new duty cycle. Any help would be much appreciated!

//Encoder library
#include <Encoder.h>

// Black magic, function for setting up timers
void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) {
  uint32_t rc = VARIANT_MCK/128/frequency; //128 because we selected TIMER_CLOCK4 above
  TC_SetRA(tc, channel, rc/2); //50% high, 50% low
  TC_SetRC(tc, channel, rc);
  TC_Start(tc, channel);


//assigning pins to variables
int aPin=59;
int bPin=58;
int cPin=57;
int vfcPin=55;
int ibuckPin=62;
int ipmsmPin=60;
int iscPin=61;
int pwmbuckPin=4;
int pwmtisPin=0;
int pwmaPin=6;
int pwmbPin=8;
int pwmcPin=9;
int pwmostPin=0;
int pwmpikPin=0;

//int enablebuckPin=3;//not assigned on the physical board yet
int encA=30;//30 on new board.
int encB=28;//28
int encZ=26;//26
//int pwmfcPin=2;
int lemref=66;

//declaring variables used for calculations and I/O
volatile int a=0;
volatile int b=0;
volatile int c=0;
volatile int oldPos= -999;
volatile int newPos= 0;
volatile int enablebuck=0;

volatile int da=0;
volatile int db=0;
volatile int dc=0;

volatile int dbuck=0;
volatile int ibuck=0;
volatile int iboost=0;
volatile int iq=0;
volatile int id=0;
volatile int ia,ib,ic;
volatile int vqc=0;
volatile int vdc=0;
volatile int pos=0;
int idref=0;
volatile int iqref = 0.5*10000;
volatile int k=0;
int i=0;
int kpdq=2;
int kidq=1000;//1250
int kpspeed=0;
int kispeed=0;
int Ts=1;//0,00004 s (1/25 kHz) scaled
volatile int vd,vq;
volatile int prevvd,prevvq,preverrd,preverrq,va,vb,vc,ysat,prevysat,ysatd,ysatq,prevysatd,prevysatq,vdaw,vqaw,errord,errorq,vdint,vqint,errordold,errorqold,errordaw,errorqaw,errordawold,errorqawold=0;
int kaw=200;

//Look up table for cosine/sine function
int table[]={10000,9998,9994,9986,9976,9962,9945,9925,9903,9877,9848,9816,9781,9744,9703,9659,9613,9563,9511,9455,9397,9336,9272,9205,9135,9063,8988,8910,8829,8746,8660,8572,8480,8387,8290,8192,8090,7986,7880,7771,7660,7547,7431,7314,7194,7071,6947,6820,6691,6561,6428,6293,6157,6018,5878,5736,5592,5446,5299,5150,5000,4848,4695,4540,4384,4226,4067,3907,3746,3584,3420,3256,3090,2924,2756,2588,2419,2250,2079,1908,1736,1564,1392,1219,1045,872,698,523,349,175,0};

//*Assigning pins as either input or output
//*starting timers
void setup(){
  //asssigning pinmodes 

  //More timer fun!
  ADC->ADC_WPMR=0x00; //disable write protection on MR
  ADC->ADC_MR |= 0x200080; //set ADC in free running mode and settling time is 9 times ADC clock
  ADC->ADC_CR=2; //enable ADC clock
  ADC->ADC_CHER=0x1FF; //enable A0->A7+A8

  startTimer(TC1, 0, TC3_IRQn, 10000);//timer for pwm write inverter and sync buck
  startTimer(TC1, 1, TC4_IRQn, 10000); //timer for reading analog inputs and calculating duty cycles
Encoder myEnc(encA,encB);

void loop(){
   newPos =;
   if (newPos != oldPos){
    pos=0.0439*2*newPos; //(360/2000) From pulses to degrees

void TC4_Handler()

  while((ADC->ADC_ISR & 0x1FF)!=0x1FF);//waiting for data on A0-A8
  a=ADC->ADC_CDR[2]; //read data on A5 pin
  b=ADC->ADC_CDR[3]; //read data on A4 pin
  c=ADC->ADC_CDR[4]; //read data on A3 pin

//scale measerued signals   

  //Park transformation, from adc to dq0 ref system
  //id and iq are scaled with 10000


  //scale down id and iq to be 10000 times actual value (instead of 10^8 timesactual value)

  //PI controller with anti windup

  else if(vd<-28000000){
    vd= -28000000;

  else if(vq<-28000000){
    vq= -28000000;
  //scaling vd and vq down to 100 times actual value
  //dq0 to abc (inverse park transformation)
  //va,vb,vc is calculated 10^6 times bigger than actual value  


  //convert voltage references to duty cycles

Rest of the code:

//reset the position every index puls
void reset(){

//lookup table for cosine function
//Works for all angles between -720 and 720 degrees.
int cosinus(int posit){
  if((posit<-720)&&(posit<= -1080))
  if((posit<-360)&&(posit<= -720))
  if((posit<0) && (posit<=-360))
  if((posit<=90) && (posit>=0))
        return table[i]; 
  else if((posit>90) && (posit<=180))
    return table[i]*(-1);
  else if((posit>180) && (posit<=270))
    return table[i]*(-1);
  else if((posit>270) && (posit<=360))
    return table[i];

void TC3_Handler()
  // You must do TC_GetStatus to "accept" interrupt
  // As parameters use the first two parameters used in startTimer (TC1, 0 in this case)
  TC_GetStatus(TC1, 0);

    //20kHz freq, gives 10kHz since the signals is center aligned
    PWMC_ConfigureClocks(20000*255, 0, VARIANT_MCK );
  // SETUP PINS 6 T/M 9
  uint32_t chan6 = g_APinDescription[6].ulPWMChannel;
  uint32_t chan7 = g_APinDescription[7].ulPWMChannel;
  uint32_t chan8 = g_APinDescription[8].ulPWMChannel;
  uint32_t chan9 = g_APinDescription[9].ulPWMChannel;
  int duty_100 = 255;
  int duty_75 = (((duty_100+1)/4)*3)-1;
  int duty_50 = ((duty_100+1)/2)-1;
  int duty_25 = ((duty_100+1)/4)-1;

  static uint8_t PWMEnabled = 0;
  static uint8_t ADCEnabled = 0;
  static uint8_t pin6Enabled = 0;
  static uint8_t pin7Enabled = 0;
  static uint8_t pin8Enabled = 0;
  static uint8_t pin9Enabled = 0;

    PIO_Configure(g_APinDescription[6].pPort, g_APinDescription[6].ulPinType, g_APinDescription[6].ulPin, g_APinDescription[6].ulPinConfiguration);
    PIO_Configure(g_APinDescription[7].pPort, g_APinDescription[7].ulPinType, g_APinDescription[7].ulPin, g_APinDescription[7].ulPinConfiguration);
    PIO_Configure(g_APinDescription[8].pPort, g_APinDescription[8].ulPinType, g_APinDescription[8].ulPin, g_APinDescription[8].ulPinConfiguration);  
    PIO_Configure(g_APinDescription[9].pPort, g_APinDescription[9].ulPinType, g_APinDescription[9].ulPin, g_APinDescription[9].ulPinConfiguration);
    PWMC_ConfigureChannel(PWM, chan6, PWM_CMR_CPRE_CLKA, (1 << 8), 0);
    PWMC_ConfigureChannel(PWM, chan7, PWM_CMR_CPRE_CLKA, (1 << 8), 0);
    PWMC_ConfigureChannel(PWM, chan8, PWM_CMR_CPRE_CLKA, (1 << 8), 0);            
    PWMC_ConfigureChannel(PWM, chan9, PWM_CMR_CPRE_CLKA, (1 << 8), 0); 
    PWMC_SetPeriod(PWM, chan6, duty_100);
    //PWMC_SetPeriod(PWM, chan7, duty_100);
    PWMC_SetPeriod(PWM, chan8, duty_100);
    PWMC_SetPeriod(PWM, chan9, duty_100);  // PWM_MAX_DUTY_CYCLE (100%)
    // SET DUTY CYCLES    
    PWMC_SetDutyCycle(PWM, chan6, da);   
    //PWMC_SetDutyCycle(PWM, chan7, 127);  
    PWMC_SetDutyCycle(PWM, chan8, db);   
    PWMC_SetDutyCycle(PWM, chan9, dc);  
    PWMC_ConfigureSyncChannel(PWM, ( PWM_SCM_SYNC4 |  PWM_SCM_SYNC5 | PWM_SCM_SYNC6 | PWM_SCM_SYNC7), PWM_SCM_UPDM_MODE2, 0, 0);  
    PWMC_SetSyncChannelUpdateUnlock(PWM);              // PWM_SCUC set UPDULOCK to 1, update next period
    PWMC_SetSyncChannelUpdatePeriod(PWM, duty_100);    //  update period of the synchronous channels PWM_SCUP
    PWMC_ConfigureChannel(PWM, 0, PWM_CMR_CPRE_CLKA, (1 << 8), 0);
    PWMC_SetPeriod(PWM, 0, duty_100);
    PWMC_SetDutyCycle(PWM, 0, duty_50);
    PWMC_EnableChannel(PWM, 0);

If anyone know how to center align and sync the analogWrite() function it would also work. When I tried analogWrite it was left aligned (the period) and the 3 pwm signales where shifted by some 7-8µS of each other.