Arduino DUE PWM output

Excuse me! I would like to output PWM with this program, what program do I need to add? Also, I would like to introduce a carrier wave from the value read by the current sensor. Please help!

#define LED_PIN 13
#include<math.h>



float a[200];//u
float b[200];//v
float c[200];//w

int n=0;
int p=0;
int x = 0, y=0, z=0, w=0, t=0 ;
double average = 0 ;
double average1 = 0 ;


    
float value1 = 0;
float volt1 = 0;
float current1 = 0;
    
float value2 = 0;
float volt2 = 0;
float current2 = 0;

    
float value3 = 0;
float volt3 = 0;
float current3 = 0;


void setup(){
  
   Serial.begin(115200);
   tc_setup();
   adc_setup();
   pwm_setup();
   pinMode(LED_PIN,OUTPUT);
}




/*************  Timer Counter 0 Channel 2 to generate PWM pulses thru TIOA2  ************/
void tc_setup() {

  PMC->PMC_PCER0 |= PMC_PCER0_PID27;                      //TC2 power ON : Timer Counter 0 channel 2 IS TC2
  TC0->TC_CHANNEL[2].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC2 counter and enable
  
}

void adc_setup() {
   pmc_set_writeprotect(false);            // PMC controller write protection disable.
  
  PMC->PMC_PCER1 |= PMC_PCER1_PID37;                    // ADC power on
  ADC->ADC_CR = ADC_CR_SWRST;                           // Reset ADC
  ADC->ADC_MR =  ADC_MR_TRGEN_EN                       // Hardware trigger select
                  | ADC_MR_TRGSEL_ADC_TRIG5            // Trigger by PWM Event Line 1 
                  | ADC_MR_PRESCAL(1);                
 
  ADC->ADC_CHER = ADC_CHER_CH7 | ADC_CHER_CH6 | ADC_CHER_CH5;   // Enable Channel 7
  NVIC_EnableIRQ(ADC_IRQn);  // Enable ADC interrupt
  ADC->ADC_IER = ADC_IER_EOC7;                          // End Of Conversion interrupt enable for channel 7
                            
  

}



void pwm_setup(){
  REG_PMC_PCER1 |= PMC_PCER1_PID36; 
  
  uint16_t CounterPeriod;
  uint16_t DeadTime;

    

    REG_PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(1); 
 
    CounterPeriod = 16800;  //84MHz/5KHz = 16800
    DeadTime = 100;

   REG_PWM_CPRD0 = CounterPeriod; 
   REG_PWM_CPRD1 = CounterPeriod;
   REG_PWM_CPRD2 = CounterPeriod;
   REG_PWM_CPRD3 = CounterPeriod;

   //REG_PWM_CDTY1 = 21000;  //Channel Duty Cycle Register
   REG_PWM_CMR0 = PWM_CMR_CALG | PWM_CMR_CPRE_MCK;  //CALG senter Alg
   REG_PWM_CMR1 = PWM_CMR_DTE | PWM_CMR_CALG | PWM_CMR_CPRE_MCK;
   REG_PWM_CMR2 = PWM_CMR_DTE | PWM_CMR_CALG | PWM_CMR_CPRE_MCK;
   REG_PWM_CMR3 = PWM_CMR_DTE | PWM_CMR_CALG | PWM_CMR_CPRE_MCK;

   REG_PWM_DT1 = PWM_DT_DTH(DeadTime) | PWM_DT_DTL(DeadTime);
   REG_PWM_DT2 = PWM_DT_DTH(DeadTime) | PWM_DT_DTL(DeadTime);
   REG_PWM_DT3 = PWM_DT_DTH(DeadTime) | PWM_DT_DTL(DeadTime);
   
    
    REG_PIOC_PDR = PIO_PDR_P9 | PIO_PDR_P8 | PIO_PDR_P7 | PIO_PDR_P6 | PIO_PDR_P5 | PIO_PDR_P4;
    
    REG_PIOC_ABSR |= PIO_ABSR_P9 | PIO_ABSR_P8 | PIO_ABSR_P7 | PIO_ABSR_P6 | PIO_ABSR_P5 | PIO_ABSR_P4;
   
   
   PWM->PWM_CMP[0].PWM_CMPM = PWM_CMPM_CEN;                       
   PWM->PWM_ELMR[1] = PWM_ELMR_CSEL0;                             
   PWM->PWM_CMP[0].PWM_CMPV = PWM_CMPV_CV(CounterPeriod - 10);  
   
  NVIC_EnableIRQ(TC2_IRQn);  // TC2 NVIC enable
  NVIC_SetPriority(PWM_IRQn, 0); 
  NVIC_EnableIRQ(PWM_IRQn);               
  PWM->PWM_ENA |= PWM_ENA_CHID0 | PWM_ENA_CHID0; 
  PWM->PWM_IER1 |= PWM_IER1_CHID0; 
  REG_PWM_IER1 = PWM_IER1_CHID0; // Interrupt on PWM Channel 0 counter
    
}

void loop(){
 
     if(x==2199){ 
      for(int u=0 ;u>=0 && u<=199;u=u+1){
        Serial.print("U相電流 : ");
        Serial.println(a[u]);
      }
       
      for(int v=0 ;v>=0 && v<=199;v=v+1){
        Serial.print("V相電流 : ");
        Serial.println(b[v]);
       }
        for(int w=0 ;w>=0 && w<=199;w=w+1){
        Serial.print("W相電流 : ");  
        Serial.println(c[w]);
     }
  }
}

void PWM_Handler(){
  if (REG_PWM_ISR1 & PWM_ISR1_CHID0)    // Check if an update condition has occured
  {
     
    ADC_Handler();  
  }
}




void ADC_Handler(){

      
      value1 = ADC->ADC_CDR[7];
      value2 = ADC->ADC_CDR[6];
      value3 = ADC->ADC_CDR[5];
      

     //電流センサ計算部分//
    volt1 = ((value1-17)*(3.3/ 4095.0)) ;
    volt2 = ((value2-17)*(3.3/ 4095.0)) ;
    volt3 = ((value3-17)*(3.3/ 4095.0)) ;
    
    current1 = ((volt1 - 2.5)*40) ;
    current2 = ((volt2 - 2.5)*40) ;
    current3 = ((volt3 - 2.5)*40) ;
       
   
        
      //配列に値を格納//
    
      x++;
     if(2000<=x && x<=2199){
     
     //A/D変換の値を格納//
      a[y] = value1;
      b[z] = value2;
      c[w] = value3;
     
     //電流値を格納//
     /* a[y] = current1;
      b[z] = current2;
      c[w] = current3;*/
    
     
      y++;
      z++;
      w++;
     }

   
      
      
      
  
    //PIOB->PIO_ODSR ^= PIO_ODSR_P27; //Lチカ用
   
    if(n%2==0){
      digitalWrite(LED_PIN,HIGH);
      n++;
    }else{
      digitalWrite(LED_PIN,LOW);
      n++;
    }
    
    
      
    }
      

      
  
    
   

You present too much for anybody not familiar with your project :frowning:

Why do you need low level code instead of analogWrite()? Which dependencies do you want to implement between the PWM signals?

I use a current sensor to sense the three-phase AC current of the three-phase inverter, and use an Arduino DUE to ADC the current to calculate the current value. I want to use PI control to change the PWM duty of the output voltage of the inverter according to the current value. In order to do this, we need to use the Atmel SAM3X8E instead of analogWrite.

Where do you keep these index variables within the array bounds?

I'm not sure I understand your question. What do you mean?

The arrays have 250 elements. Shit happens if you try to access an element with a higher index.

Normally, the values stored in the array are not printed serially, but the acquired values are used for PWM control, so the processing is not that much.