代码如下:
static uint32_t SDADC1_Config(void)
{
SDADC_AINStructTypeDef SDADC_AINStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
uint32_t SDADCTimeout = 0;
/* MPX2102_SDADC APB2 interface clock enable */
RCC_APB2PeriphClockCmd(MPX2102_SDADC_CLK, ENABLE);
/* PWR APB1 interface clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Enable MPX2102_SDADC analog interface */
PWR_SDADCAnalogCmd(MPX2102_SDADC_PWR, ENABLE);
/* Set the SDADC divider: The SDADC should run @6MHz */
/* If Sysclk is 72MHz, SDADC divider should be 12 */
RCC_SDADCCLKConfig(RCC_SDADCCLK_SYSCLK_Div48);
/* GPIOE Peripheral clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
/* MPX2102_SDADC channel 8P (PE8) and channel 8N (PE9) */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Select External reference: The reference voltage selection is available
only in SDADC1 and therefore to select the VREF for SDADC2/SDADC3, SDADC1
clock must be already enabled */
SDADC_VREFSelect(SDADC_VREF_Ext);
//SDADC_VREFSelect(SDADC_VREF_VDDA);
//SDADC_VREFSelect(SDADC_VREF_VREFINT2);
/* Insert delay equal to ~5 ms */
delay(5); //5ms
/* Enable MPX2102_SDADC */
SDADC_Cmd(MPX2102_SDADC, ENABLE);
/* Enter initialization mode */
SDADC_InitModeCmd(MPX2102_SDADC, ENABLE);
SDADCTimeout = SDADC_INIT_TIMEOUT;
/* wait for INITRDY flag to be set */
//while((SDADC_GetFlagStatus(MPX2102_SDADC, SDADC_FLAG_INITRDY) == RESET) && (--SDADCTimeout != 0));
while(SDADC_GetFlagStatus(MPX2102_SDADC, SDADC_FLAG_INITRDY) == RESET);
if(SDADCTimeout == 0)
{
/* INITRDY flag is not set */
return 1;
}
/* Analog Input configuration conf0: use differential mode and gain = 4 */
//SDADC_AINStructure.SDADC_InputMode = SDADC_InputMode_Diff;
SDADC_AINStructure.SDADC_InputMode = SDADC_InputMode_SEZeroReference;
//SDADC_AINStructure.SDADC_InputMode = SDADC_InputMode_SEOffset;
SDADC_AINStructure.SDADC_Gain = SDADC_Gain_1;
SDADC_AINStructure.SDADC_CommonMode = SDADC_CommonMode_VSSA;
//SDADC_AINStructure.SDADC_CommonMode = SDADC_CommonMode_VDDA_2;
SDADC_AINStructure.SDADC_Offset = 0;
SDADC_AINInit(MPX2102_SDADC, SDADC_Conf_0, &SDADC_AINStructure);
/* select MPX2102_SDADC channel 8 to use conf0 */
SDADC_ChannelConfig(MPX2102_SDADC, SDADC_Channel_8, SDADC_Conf_0);
/* select channel 8 */
SDADC_InjectedChannelSelect(MPX2102_SDADC, SDADC_Channel_8);
SDADC_InjectedContinuousModeCmd(MPX2102_SDADC, ENABLE);
/* Select an external trigger */
//SDADC_ExternalTrigInjectedConvConfig(MPX2102_SDADC, SDADC_ExternalTrigInjecConv_T19_CC2);
/* Select rising edge */
//SDADC_ExternalTrigInjectedConvEdgeConfig(MPX2102_SDADC, SDADC_ExternalTrigInjecConvEdge_Rising);
/* Exit initialization mode */
SDADC_InitModeCmd(MPX2102_SDADC, DISABLE);
/* Enable end of injected conversion interrupt */
SDADC_ITConfig(MPX2102_SDADC, SDADC_IT_JEOC, ENABLE);
/* NVIC Configuration */
NVIC_InitStructure.NVIC_IRQChannel = SDADC1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* configure calibration to be performed on conf0 */
SDADC_CalibrationSequenceConfig(MPX2102_SDADC, SDADC_CalibrationSequence_1);
/* start MPX2102_SDADC Calibration */
SDADC_StartCalibration(MPX2102_SDADC);
/* Set calibration timeout: 5.12 ms at 6 MHz in a single calibration sequence */
SDADCTimeout = SDADC_CAL_TIMEOUT;
/* wait for MPX2102_SDADC Calibration process to end */
//while((SDADC_GetFlagStatus(MPX2102_SDADC, SDADC_FLAG_EOCAL) == RESET) && (--SDADCTimeout != 0));
while(SDADC_GetFlagStatus(MPX2102_SDADC, SDADC_FLAG_EOCAL) == RESET);
if(SDADCTimeout == 0)
{
/* EOCAL flag can not set */
return 2;
}
SDADC_SoftwareStartInjectedConv(MPX2102_SDADC);
return 0;
}
void SDADC1_IRQHandler(void)
{
uint32_t ChannelIndex;
if(SDADC_GetFlagStatus(SDADC1, SDADC_FLAG_JEOC) != RESET)
{
/* Get the converted value */
InjectedConvData = SDADC_GetInjectedConversionValue(SDADC1, &ChannelIndex);
ChannelIndex = ChannelIndex;
}
} |