2012-03-28 07:12 AM
I'm having some difficulty with getting the analog watchdog to work a second time. I am able to use it the first time correctly, but I think I might be missing something either when I disable it, or re-enable it because as soon as I start up the AWD the second time, the interrupt fires immediately even though the current ADC value no where near the AWD range I am specifying.
Startup code (Most of the time I do a single ADC read so we default to that):
/* Initialize ADC defaults */
adc_init_def.ADC_Mode = ADC_Mode_Independent;
adc_init_def.ADC_ScanConvMode = DISABLE;
adc_init_def.ADC_ContinuousConvMode = DISABLE;
adc_init_def.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None
adc_init_def.ADC_DataAlign = ADC_DataAlign_Right;
adc_init_def.ADC_NbrOfChannel = 1;
/* Reset the ADC */
ADC_DeInit(adc);
/* Init the ADC */
ADC_Init(adc, &adc_init_def);
/* Enable the ADC */
ADC_Cmd(adc, ENABLE);
/* Calibrate the ADC */
ADC_ResetCalibration(adc);
while
(ADC_GetResetCalibrationStatus(adc)) { }
ADC_StartCalibration(adc);
while
(ADC_GetCalibrationStatus(adc)) { }
When I call the AWD function to set it up, I do the following:
/* Set the mode to continous */
adc_init_def.ADC_ContinuousConvMode = ENABLE;
/* Initialize the ADC again to enable continuous conversion mode */
ADC_DeInit(adc);
ADC_Init(adc, &adc_init_def);
/* Configure the channel */
ADC_RegularChannelConfig(adc, adc_channel, 1, ADC_SampleTime_41Cycles5);
/* Set the watchdog thresholds */
ADC_ClearFlag(adc, ADC_FLAG_AWD);
ADC_AnalogWatchdogThresholdsConfig(adc, current_pos, future_pos);
/* Set the configuration for the watchdog channel */
ADC_AnalogWatchdogSingleChannelConfig(adc, adc_channel);
ADC_AnalogWatchdogCmd(adc, ADC_AnalogWatchdog_SingleRegEnable);
/* Enable the analog watchdog interrupt then enable the interrupts */
ADC_ITConfig(adc, ADC_IT_AWD, ENABLE);
/* Turn on the ADC if its not already on */
ADC_Cmd(adc, ENABLE);
/* Calibrate the ADC */
ADC_ResetCalibration(adc);
while
(ADC_GetResetCalibrationStatus(adc));
ADC_StartCalibration(adc);
while
(ADC_GetCalibrationStatus(adc));
And I handle the AWD inside the ISR as follows:
/* Analog watchdog interrupt */
if
(ADC_GetITStatus(adc, ADC_IT_AWD) != RESET)
{
/* Clear the analog watchdog interrupt flag */
ADC_ClearFlag(adc, ADC_FLAG_AWD);
ADC_ClearITPendingBit(adc, ADC_IT_AWD);
ADC_ITConfig(adc, ADC_IT_AWD, DISABLE);
/* Call the ISR function */
awd_isr->ISR();
/* Disable continous conversion mode, and the watchdog status/owner */
adc_init_def.ADC_ContinuousConvMode = DISABLE;
/* Re-initialize the ADC with continous conversion mode disabled */
ADC_DeInit(adc);
ADC_Init(adc, &adc_init_def);
}
2012-03-28 07:17 AM
Whoops, last line of the AWD setup should have:
1.
ADC_SoftwareStartConvCmd(adc, ENABLE);
2012-03-28 10:18 AM
Scratch that. Turns out my problem was related to power being removed from the potentiometer before I was reading it the second time.