I'm working on an R7FA2E2A32 device. I have an analog channel connected to an halved sine waveform (only the upper portion is present, every negative value is collapsed to zero) and need to receive, as precisely as possible, an interrupt when said waveform hits zero.
I have setup the window compare function for that purpose, but I am stumbling into some issues.
My problem is similar to this other question: the ADC is setup in continuous mode, so I receive an interrupt when the value falls below a threshold; unfortunately the interrupt keeps firing because the ADC immediately starts another conversion that is still below the threshold.
I have tried to flip the window on each trigger (wait for the value to fall below the threshold, then for the next one expect it to rise above it) while also disabling the interrupt for 100us every time. This does not appear to work and the interrupt is just triggers every 100us.
If I don't flip the window the interrupt triggers continuously as long as the the value is below the threshold.
If I set the ADC conversion mode to single and trigger it every 25 us instead it works as expected.
This is the code I'm using for the window interrupt:
void adc_sample_callback(adc_callback_args_t *p_arg) { switch (p_arg->event) { case ADC_EVENT_WINDOW_COMPARE_A: { // Disable interrupt; it is periodically re-enabled after 100us R_BSP_IrqDisable(ADC0_WINDOW_A_IRQn); volatile adc_channel_t channel = p_arg->channel; volatile uint8_t below = (g_adc0_ctrl.p_reg->ADCMPLR[0] & (1 << channel)) == 0; // ADC reached 0 if (below) { g_adc0_ctrl.p_reg->ADCMPLR[0] |= (uint16_t) ((1 << channel) & 0xFFFF); } // ADC rose up else { g_adc0_ctrl.p_reg->ADCMPLR[0] &= (uint16_t) (~(1U << channel) & 0xFFFF); } switch (channel) { case ADC_CHANNEL_9: // Do stuff break; default: break; } break; } default: break; } return; }
Is it possible to achieve this?
Hi Maldus512,
Thanks for reaching out Renesas Community.
For your request, I'd suggest you to disable the ADC in ADC compare interrupt and then restart ADC after a delay defined by you. Actually in official sample codes, it also add a delay to avoid continuous interrupt:
BR,
NP_Renesas