Can someone please help me?
I want to do an event interrupt, but how can I do it to call ADC_Handler?
int i=0 ;
double average = 0 ;
double average1 = 0 ;
float value;
float volt;
float current;
void setup(){
Serial.begin(9600);
tc_setup();
pwm_setup();
adc_setup();
}
/************* 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->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 PWMイベントライン1をトリガに設定
| ADC_MR_PRESCAL(1); // ここは0でも1でもどっちでも
ADC->ADC_IER = ADC_IER_EOC7; // End Of Conversion interrupt enable for channel 7
NVIC_EnableIRQ(ADC_IRQn); // Enable ADC interrupt
ADC->ADC_CHER = ADC_CHER_CH7 | ADC_CHER_CH6 | ADC_CHER_CH5; // Enable Channel 7
}
void pwm_setup(){
uint16_t CounterPeriod;
uint16_t DeadTime;
//キャリア周波数10Hz、デッドタイム約1.2usの場合の設定
CounterPeriod = 4200 - 1;
DeadTime = 100;
REG_PWM_CPRD0 = CounterPeriod;
REG_PWM_CPRD1 = CounterPeriod;
REG_PWM_CDTY1 = 2100;
REG_PWM_CMR0 = PWM_CMR_CALG | PWM_CMR_CPRE_MCK;
REG_PWM_CMR1 = PWM_CMR_DTE | PWM_CMR_CALG | PWM_CMR_CPRE_MCK;
REG_PWM_DT1 = PWM_DT_DTH(DeadTime) | PWM_DT_DTL(DeadTime);
//PIOのディセーブルレジスタ
REG_PIOC_PDR = PIO_PDR_P9 | PIO_PDR_P8 | PIO_PDR_P7 | PIO_PDR_P6 | PIO_PDR_P5 | PIO_PDR_P4;
//ペリフェラルABセレクタ相補にしている
REG_PIOC_ABSR |= PIO_ABSR_P9 | PIO_ABSR_P8 | PIO_ABSR_P7 | PIO_ABSR_P6 | PIO_ABSR_P5 | PIO_ABSR_P4;
//AD変換のためのPWMイベントライン設定
PWM->PWM_CMP[0].PWM_CMPM = PWM_CMPM_CEN; // PWM0の比較を有効化
PWM->PWM_ELMR[1] = PWM_ELMR_CSEL0; // PWMイベントライン1にPWM0を設定
PWM->PWM_CMP[0].PWM_CMPV = PWM_CMPV_CV(CounterPeriod - 10); // PWM0のDUTYを設定
NVIC_EnableIRQ(TC2_IRQn); // TC2 NVIC enable
NVIC_SetPriority(PWM_IRQn, 0); // PWMコントローラのNVIC(Nested Vector Interrupt Controller)の優先度を0(最高)にする
NVIC_EnableIRQ(PWM_IRQn); // PWMコントローラをNested Vector Interrupt Controller (NVIC)に接続する。
PWM->PWM_ENA |= PWM_ENA_CHID1 | PWM_ENA_CHID0; // PWMチャンネルを有効にする
PWM->PWM_IER1 |= PWM_IER1_CHID0; // イベント割り込みを有効にする
REG_PWM_IER1 = PWM_IER1_CHID0; // Interrupt on PWM Channel 0 counter
REG_PMC_PCER1 |= PMC_PCER1_PID36; // PWMの有効化
REG_PWM_CLK = PWM_CLK_PREA(0) | PWM_CLK_DIVA(1); // PWMクロックレートを84MHzに設定 (84MHz/1)
PWM->PWM_CH_NUM[0].PWM_CDTY = 50; // PWMのデューティサイクルを50%にする
}
void loop(){
}
int x = 0, y=0;
void ADC_Handler(void){
if (ADC->ADC_ISR & ADC_ISR_EOC7)
{
value = ADC->ADC_CDR[7];
Serial.print(value);
}
}