How should I write the ISR routine if i want to use the Switch Case to change the duty cycle? I tried doing it but it doesn't work.
byte count = 0;
void setup()
{
Serial.begin(57600);
PMC->PMC_PCER1 |= PMC_PCER1_PID36; //Enable PWM (Power On)
PWM->PWM_DIS = PWM_DIS_CHID0; //Disable PWM on Channel 0
PIOC->PIO_PDR |= PIO_PDR_P3 | PIO_PDR_P5; // Setting pins 3,5, (DUE Pins 35, 37, 39) to PWM Peripheral, not GPIO
PIOC->PIO_ABSR |= PIO_PC3B_PWMH0 | PIO_PC5B_PWMH1; // Setting pins to Peripheral B
PIOC->PIO_PDR |= PIO_PDR_P2 | PIO_PDR_P4; // Setting pins 2,4, (DUE Pins 34, 36, 38) to PWM Peripheral, not GPIO
PIOC->PIO_ABSR |= PIO_PC2B_PWML0 | PIO_PC4B_PWML1; // Setting pins to Peripheral B
PWM->PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(42); //Set PWM clocke rate to 2MHz (84MHz/42)
PWM->PWM_CH_NUM[0].PWM_CMR = PWM_CMR_CPRE_CLKA; // Period is left aligned,clock source is CLKA on Channel 0
REG_PWM_SCM |= PWM_SCM_SYNC0 | PWM_SCM_SYNC1; // Synchronizing of Channels 0, 1 and 2
REG_PWM_SCM |= PWM_SCM_UPDM_MODE1; // Manual Write of duty-cycle automatic trigger of the update
PWM->PWM_SCUP = PWM_SCUP_UPR(0); // Defining update period (UPR + 1)
PWM->PWM_IER2 = PWM_IER2_WRDY; // Enable Interrupt when WRDY flag is set
//PWM_SCM_PTRM == 0;; // The WRDY flag in PWM_ISR2 and the PDC transfer request are set to 1 as soon as the update period is elapsed.
REG_PWM_CPRD0 = 1000000; //Channel 0 Period f = 2MHz/(2*CPRD)
//REG_PWM_CDTY0 = 0; //Channel 0 Duty Cycle x% = (CDTY/ CPRD)*100%
REG_PWM_CPRD1 = 1000000;
//REG_PWM_CDTY1 = 1000000;
PWM->PWM_ENA = PWM_ENA_CHID0; // Enable PWM on Channel 0
update_Duty_Cycle();
//NVIC_EnableIRQ(PWM_IRQn); // enable PWM interrupts
}
void loop()
{
// put your main code here, to run repeatedly:
// Serial.print("count = ");
// Serial.print(count);
// Serial.print(" ");
}
void PWM_Interrupt_Handler()
{
PWM->PWM_ISR2; // Read ISR2 and clear status register
//update_Duty_Cycle();
}
void update_Duty_Cycle()
{
if(count > 3){
count = 0;}
switch(count)
{
case 0:
REG_PWM_CDTYUPD0 = 1000000;
REG_PWM_CDTYUPD1 = 0;
break;
case 1:
REG_PWM_CDTYUPD1 = 1000000;
break;
case 2:
REG_PWM_CDTYUPD0 = 0;
break;
case 3:
REG_PWM_CDTYUPD1 = 0;
break;
default:
break;
}
//SquareWave(count);
//count++;
}
/*
void SquareWave(byte Sector)
{
switch(Sector)
{
case 0:
REG_PWM_CDTYUPD0 = 1000000;
REG_PWM_CDTYUPD1 = 0;
break;
case 1:
REG_PWM_CDTYUPD1 = 1000000;
break;
case 2:
REG_PWM_CDTYUPD0 = 0;
break;
case 3:
REG_PWM_CDTYUPD1 = 0;
break;
default:
break;
}
}
*/
This is what I tried to do, I know some of the configuration is wrong but this is essentially what I want to do