error of serial output at arduino due oscilloscope project

Hello.

I’m planning to make oscilloscope with arduino due board

Here is my cord.

int incomingByte = 0;
float times;
const uint32_t MaskPin25 = (1 << 0);
const uint32_t MaskPin27 = (1 << 2);
const uint32_t MaskPins2527 = MaskPin25 | MaskPin27;
void setup() {
   Serial.begin(115200);
 //  PMC->PMC_PCER0 = PMC_PCER0_PID13;
  pinMode(LED_BUILTIN, OUTPUT);
  //PMC->PMC_PCER0 |= PMC_PCER0_PID14;                  // on read mode
  PMC->PMC_PCER1 |= PMC_PCER1_PID36;                   // PWM power ON

  PIOC->PIO_PDR |= PIO_PDR_P3;                         // Set PWM pin to a peripheral
  PIOC->PIO_ABSR |= PIO_PC3B_PWMH0;                    // Set PWM pin peripheral type B for PWMH0 (Arduino pin 35)
 // PIOC->PIO_ABSR |= PIO_PC3B_TIOA0;

  PWM->PWM_CLK = PWM_CLK_PREB(0) | PWM_CLK_DIVB(2);    // select Frequency for clock B: Mck/2 = 42 MHz
  PWM->PWM_CH_NUM[0].PWM_CMR = PWM_CMR_CPRE_CLKB;      // The period is left aligned, clock source as CLKB on channel 0
  PWM->PWM_CH_NUM[0].PWM_CPRD = 65535;                 // Set the PWM frequency 42 MHz/PWM_CPRD = 641 Hz
  PWM->PWM_CH_NUM[0].PWM_CDTY = 32768;                 // Set the PWM duty cycle = (CPRD/CDTY) * 100 %

  PWM->PWM_IER1 = PWM_IER1_CHID0;                      // Interrupt on PWM Channel 0 counter
  NVIC_EnableIRQ(PWM_IRQn);                            // Enable interrupt

  PWM->PWM_ENA = PWM_ENA_CHID0;                        // Enable PWM channel 0

  pinMode(51, INPUT);
  pinMode(50, INPUT);
  pinMode(49, INPUT);
  pinMode(48, INPUT);
  pinMode(47, INPUT);
  pinMode(46, INPUT);
  pinMode(45, INPUT);
  pinMode(44, INPUT);
  // this is adc pin
}

void loop() {
  // update PWM frequency with  PWM->PWM_CH_NUM[0].PWM_CPRDUPD = ....;
  // update PWM duty cycle with PWM->PWM_CH_NUM[0].PWM_CDTYUPD = ....;

  if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();
                Serial.print("I received: ");
                Serial.println(incomingByte);
                if (incomingByte==48){
                PWM->PWM_CH_NUM[0].PWM_CDTY = 1;
                }
                // say what you got:
                
       }
int val[50][9];
int aaa;
  //   uint32_t status = PIOD->PIO_PDSR;
  //   boolean statusPin[9];
  //    statusPin[1] = status & (1 << 19);
  //    statusPin[2] = status & (1 << 18);
  //    statusPin[3] = status & (1 << 17);
  //    statusPin[4] = status & (1 << 16);
  //    statusPin[5] = status & (1 << 15);
  //    statusPin[6] = status & (1 << 14);
  //    statusPin[7] = status & (1 << 13);
  //    statusPin[8] = status & (1 << 12);
  //   Serial.print(statusPin[1]);Serial.print(statusPin[2]);Serial.print(statusPin[3]);Serial.print(statusPin[4]);Serial.print(statusPin[5]);Serial.print(statusPin[6]);Serial.print(statusPin[7]);Serial.print(statusPin[8]);
     
     Serial.println();
      times = micros();
     Serial.print(times);
     Serial.println();
  //   g_APinDescription[pin].pPort -> PIO_PDSR & g_APinDescription[pin].ulPin
 Serial.print(g_APinDescription[51].pPort -> PIO_PDSR & g_APinDescription[51].ulPin);
     Serial.println(); 
      for (int j=1; j <= 50; j++){
       val[j][1] = digitalRead(44);
       val[j][2] = digitalRead(45);
       val[j][3] = digitalRead(46);
       val[j][4] = digitalRead(47);
       val[j][5] = digitalRead(48);
       val[j][6] = digitalRead(49);
       val[j][7] = digitalRead(50);
       val[j][8] = digitalRead(51);
       delayMicroseconds(200);
      }
Serial.print(times);
     Serial.println();
       delay(500);
   // float cal = 0;
  //  float cola = 0;
 //   float colaa = 0;
//    float call[50] = 0;
float cal[50];
//        float cal[50] = 0; 
        for (int lp=1; lp <= 50; lp++){
         
       for (int i=1; i <= 8; i++){
        cal[lp] = cal[lp] + val[lp][i]*pow(2,(8-i));
        //cola = cola + statusPin[i]*pow(2,(8-i));
      //analogWrite(PWMpin, i);
     // delay(10);
       }
      // call[lp] = 1.5+cal*2/256;
       Serial.print(1.5+cal[lp]*2/256);
     Serial.println();
     cal[lp]=0;
        }
  
  // call = 1.5+cal*2/256;
  // colaa = 1.5+cal*2/256;
  
   //  Serial.print(call);
     Serial.println();
  //   Serial.print(colaa);
  //   Serial.println();
     
     Serial.println();
//     Serial.print(y0);
     //Serial.println(" b1");
//     Serial.print(z0);
     //Serial.println(" c1");
}

void PWM_Handler() {
  static uint32_t Count;

  PWM->PWM_ISR1;      // Clear status register
  if (Count++ == 641)
  {
    PIOB->PIO_ODSR ^= PIO_ODSR_P27;
    Count = 0;
  }
}

And Serial result

2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49



2155725.00
4096
2155725.00
2.48
⸮⸮j2.4
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49



2155725.00
4096
2155725.00
2.48
⸮⸮j2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.⸮
2039.00
4096
2039.00
2.52
2.52
2.50
ovf
2.52
-12.14
ovf
2.52
-795.48
2.52
ovf
2.52
2.52
2.52
nan
2.52
ovf
ovf
2.52
2.52
2.75
2.52
ovf
2.52
ovf
2.52
2.52
2.52
2.52
ovf
2.52
ovf
2.52
2.52
31.90
ovf
2.52
ovf
ovf
2.52
ovf
2.52
ovf
2.52
ovf
ovf
2.41
2.52
2.52
ovf



539480.00
4096
539480.00
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29
2.29



1078471.00
4096
1078471.00
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48
2.48



1617641.00
4096
1617641.00
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46
2.46



2156640.00
4096
2156640.00
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49
2.49

I didn’t understand why timer is not changing that betweening number 4096.
Also, the serise of result is not change that like 2.49.
It change that the paragraph changing moment like

2.46
2.46
2.46



2156640.00
4096
2156640.00
2.49
2.49
2.49

2.46 is changed to 2.49.

But 2.49 serise is not changed.

It must be changed in the for algorithm that contain delaymicroseconds.
So it will have variable value but not…

How could I fix this problem??

Arrays in C/C++ start at 0 and go up to MAX_ELEMENT-1. You are overwriting your array bounds