Go Down

Topic: Conflict with PWM on pins 34-35 AND GPIO input (Read 1 time) previous topic - next topic

geraldli

Hello,

I can't read any GPIO pin with this code adapted from
PWM channel output
Thanks for your HELP !


PMC->PMC_PCER1 |= PMC_PCER1_PID36;                  // Enable PWM (Power On)

 PIOC->PIO_PDR |= PIO_PDR_P3 | PIO_PDR_P2;           // DUE Pin 34-35 to PWM Peripheral
 PIOC->PIO_ABSR |= PIO_ABSR_P3 | PIO_ABSR_P2;        // Setting pins to Peripheral B

 PWM->PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(84);  // Set PWM clock = 1MHz (84MHz/84)
 PWM->PWM_CH_NUM[0].PWM_CMR = PWM_CMR_CPRE_CLKA;     // Period is left aligned, clock source is CLKA on Channel 0

 PWM->PWM_SCM |= PWM_SCM_SYNC0;                      // Synchronizing of Channels 0
 PWM->PWM_SCM |= PWM_SCM_UPDM_MODE1;                 // Manual Write of duty-cycle automatic trigger of the update

 NVIC_SetPriority(PWM_IRQn, 0);                      // Set the Nested Vector Interrupt Controller (NVIC) priority for the PWM controller to 0 (highest)
 NVIC_EnableIRQ(PWM_IRQn);                           // Connect PWM Controller to Nested Vector Interrupt Controller (NVIC)

 PWM->PWM_IER1 = PWM_IER1_CHID0;                     // Enable interrupt on PWM channel 0 triggered at end of PWM period

 PWM->PWM_CH_NUM[0].PWM_CPRD = 208;                  // Channel 0 Period f = 1MHz/(2*CPRD) --> T=208 micros
 PWM->PWM_CH_NUM[0].PWM_CDTY = 0;                    // Channel 0 duty-cycle at 0%
 PWM->PWM_CH_NUM[1].PWM_CDTY = 0;                    // Channel 1 duty-cycle at 0%

 PWM->PWM_ENA = PWM_ENA_CHID0;                       // Enable synchronous PWM on Channel 0
 PWM->PWM_SCUC = PWM_SCUC_UPDULOCK;      // Set the update unlock bit to trigger an update at the end of the next PWM period  IMPORTANT

adapted from
https://forum.arduino.cc/index.php?
topic=590202.25;wap


ard_newbie


What is the waveform you are trying to output ?

geraldli

Attached file = pin 34

void PWM_Handler() {                      // PWM ISR each 208 micros = T

  if (PWM->PWM_ISR1 & PWM_ISR1_CHID0) {   // update condition     

    switch (SM_PWM) {
      case 0  : PWM->PWM_CH_NUM[0].PWM_CDTYUPD = 0; T_PWM++; if (T_PWM==20) {SM_PWM=1; Bi_PWM=0;}  // 4,1 ms between doppel packet : measure with MS2
      break;
      case 1  : if (Pa[N_PWM][Bi_PWM]) {PWM->PWM_CH_NUM[0].PWM_CDTYUPD = 182;} else {PWM->PWM_CH_NUM[0].PWM_CDTYUPD = 26;}  // HIGH / LOW impulse
                Bi_PWM++;
                if (Bi_PWM==18) { // Packet sended
                  Do_PWM++; Bi_PWM=0;
                  if (Do_PWM==1) {SM_PWM=2; T_PWM=0;}  // doppel packet
                  else {SM_PWM=0; T_PWM=0; Do_PWM=0; N_PWM++; if (N_PWM==De_Max-1) {N_PWM=0;}}  // cycle Loc           
                }
      break;
      case 2  : PWM->PWM_CH_NUM[0].PWM_CDTYUPD = 0; T_PWM++; if (T_PWM==7) {SM_PWM=1; Bi_PWM=0;}  // 1,5 ms inside doppel packet from MS2 
      break;                                                                                      // doppel packet --> §3.2 https://www.nxp.com/docs/en/data-sheet/MC145026.pdf
    }
    PWM->PWM_SCUC = PWM_SCUC_UPDULOCK;  // IMPORTANT
  }
}

geraldli

Solution : use GPIO pins at the GOOD time
See attached file
Green = PWM output (to drive my locs Märklin)
Blue = Input/Output (for S88)

Go Up