I would like to use the COMP capabilities.
The input is from NRF_SAADC_INPUT_AIN1.
The input voltage is 0.1-0.8V.
I would like to get interrupt every time we change from VUP to VLOW and vice versa
Below is our code to initialize the comparator and the handler routin.
We change the voltage but did not get the expected results.
CODE:
static void EncoderEventHandler(nrf_comp_event_t event)
/*
* This function is called from interrupt context so it is very important
* to return quickly. Don't put busy loops or any other CPU intensive actions here.
* It is also not allowed to call soft device functions from it (if COMP IRQ
* priority is set to APP_IRQ_PRIORITY_HIGH).
*/
{
uint32_t value;
// 0 If the input voltage is below the threshold (VIN+ < VIN-).
// 1 If the input voltage is above the threshold (VIN+ > VIN-).
//LOGPRINT(LOG_PRINT_DEBUG, "event ID-%d",event);
value = nrf_drv_comp_sample();
if (event == NRF_COMP_EVENT_DOWN)
{
g_bEncodermove = true;
LOGPRINT(LOG_PRINT_DEBUG, "Event Down - %d",value);
}
else if (event == NRF_COMP_EVENT_UP)
{
g_bEncodermove = true;
LOGPRINT(LOG_PRINT_DEBUG, "Event Up- %d", value);
}
else if (event == NRF_COMP_EVENT_READY)
{
g_bEncodermove = true;
LOGPRINT(LOG_PRINT_DEBUG, "Event Ready");
}
}
void EncoderInit(void)
/*
* Initialize the comparator, and set the High and Low thresholds
*/
{
#define LowA2d 75
#define HighA2d 300
float Ref_voltage = 1.8;
// float ThresholdLow = (d_A2dToVolt((float)LowA2d, GENERAL_SCALING_COMPENSATION);
// float ThresholdHi = d_A2dToVolt((float)HighA2d, GENERAL_SCALING_COMPENSATION);
float ThresholdLow = 0.2; //(Ref_voltage / 1024) * LowA2d;
float ThresholdHi = 0.6; //(Ref_voltage / 1024) * HighA2d;
uint32_t err_code;
nrf_drv_comp_config_t comp_config = NRF_DRV_COMP_DEFAULT_CONFIG(Motor_Encoder);
// Configure threshold voltages.
comp_config.threshold.th_down = VOLTAGE_THRESHOLD_TO_INT(ThresholdLow, Ref_voltage);
comp_config.threshold.th_up = VOLTAGE_THRESHOLD_TO_INT(ThresholdHi, Ref_voltage);
comp_config.isource = NRF_COMP_ISOURCE_Off;
comp_config.main_mode = NRF_COMP_MAIN_MODE_SE;
//comp_config.isource = NRF_COMP_ISOURCE_Ien2uA5;
LOGPRINT(LOG_PRINT_DEBUG, "low-%d, hi-%d\r\n",comp_config.threshold.th_down,comp_config.threshold.th_up);
err_code = nrf_drv_comp_init(&comp_config, EncoderEventHandler);
nrf_drv_comp_start(NRF_DRV_COMP_EVT_EN_DOWN_MASK | NRF_DRV_COMP_EVT_EN_UP_MASK /*| NRF_DRV_COMP_EVT_EN_READY_MASK*/, 0);
}