//ADC3 initianilize void Init_ADC3(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; /* Enable ADC1 clock ********************************************************/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC3, &ADC_InitStructure); ADC_EOCOnEachRegularChannelCmd(ADC3, ENABLE); ADC_Cmd(ADC3, ENABLE); ADC_SoftwareStartConv(ADC3); }
void ADC1_Config(void) { ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //This is irrelevant because we only are sampling one ADC pin (1 conversion only) ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; ADC_CommonInit(&ADC_CommonInitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_3Cycles); ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); //The ADC is powered on by setting the ADON bit in the ADC_CR2 register. //When the ADON bit is set for the first time, it wakes up the ADC from the Power-down mode. }
void initADC() { initADCGPIO(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_InitTypeDef init; init.ADC_Resolution = ADC_Resolution_12b; init.ADC_ScanConvMode = DISABLE; init.ADC_ContinuousConvMode = DISABLE; init.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None; init.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; init.ADC_DataAlign = ADC_DataAlign_Right; init.ADC_NbrOfConversion = 0; ADC_Init(ADC1, &init); ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_15Cycles); ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); }
void init_ADC(void) { RCC_APB2PeriphClockCmd(RCC_APB2ENR_ADC1EN, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOAEN, ENABLE); GPIO_InitTypeDef gpini[1] = {{0}}; gpini->GPIO_Pin = GPIO_Pin_0; gpini->GPIO_Mode = GPIO_Mode_AIN; gpini->GPIO_Speed = GPIO_Speed_2MHz; gpini->GPIO_OType = GPIO_OType_PP; gpini->GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, gpini); ADC_InitTypeDef ini[1] = {{0}}; ADC_StructInit(ini); // must be 30MHz max ini->ADC_Resolution = ADC_Resolution_12b; ini->ADC_ScanConvMode = DISABLE; ini->ADC_ContinuousConvMode = ENABLE; ini->ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ini->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T8_TRGO; // Don't care ini->ADC_DataAlign = ADC_DataAlign_Left; ini->ADC_NbrOfConversion = 1; ADC_CommonInitTypeDef inic[1] = {{0}}; ADC_CommonStructInit(inic); inic->ADC_Mode = ADC_Mode_Independent; inic->ADC_Prescaler = ADC_Prescaler_Div8; inic->ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; inic->ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; // don't care ADC_DeInit(); ADC_CommonInit(inic); ADC_Init(ADC1, ini); ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles); ADC_ContinuousModeCmd(ADC1, ENABLE); ADC_EOCOnEachRegularChannelCmd(ADC1, DISABLE); ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); }
/*configura joystick*/ void joystickInit(){ GPIO_InitTypeDef initPinADC; //configura analogicos xy do joystick ADC_CommonInitTypeDef commonADC; ADC_InitTypeDef initADC; GPIO_InitTypeDef config; //configura pino botao digital joystick initPinADC.GPIO_Pin = JOYSTICK_X|JOYSTICK_Y; initPinADC.GPIO_Mode = GPIO_Mode_AN; initPinADC.GPIO_Speed = GPIO_Speed_50MHz; initPinADC.GPIO_OType = GPIO_OType_PP; initPinADC.GPIO_PuPd = GPIO_PuPd_NOPULL; commonADC.ADC_Mode = ADC_Mode_Independent; commonADC.ADC_Prescaler = ADC_Prescaler_Div2; commonADC.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; commonADC.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; initADC.ADC_Resolution = ADC_Resolution_12b; initADC.ADC_ScanConvMode = DISABLE; initADC.ADC_ContinuousConvMode = ENABLE; initADC.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; initADC.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; initADC.ADC_DataAlign = ADC_DataAlign_Right; initADC.ADC_NbrOfConversion = 1; config.GPIO_Pin = JSBUTTON; config.GPIO_Mode = GPIO_Mode_IN; config.GPIO_Speed = GPIO_Speed_50MHz; config.GPIO_OType = GPIO_OType_PP; config.GPIO_PuPd = GPIO_PuPd_UP; //habilita resistor PULL-UP RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); GPIO_Init(JOYSTICK_PORT, &initPinADC); //analog joystick GPIO_Init(JSBUTTON_PORT, &config); //digital joystick ADC_CommonInit(&commonADC); ADC_StructInit(&initADC); ADC_Init(ADC1, &initADC); ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); }
void CIO::startInt() { if ((ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) != RESET)) io.interrupt(); // ADC1 PA0 analog input // ADC2 PA1 analog input // DAC1 PA4 analog output // Init the ADC GPIO_InitTypeDef GPIO_InitStruct; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; GPIO_StructInit(&GPIO_InitStruct); ADC_CommonStructInit(&ADC_CommonInitStructure); ADC_StructInit(&ADC_InitStructure); // Enable ADC clock RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); #if defined(SEND_RSSI_DATA) RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE); #else RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); #endif // For ADC1 on PA0, ADC2 on PA1 #if defined(SEND_RSSI_DATA) GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; #else GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; #endif GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStruct); // Init ADCs in dual mode, div clock by two ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); // Init ADC1 and ADC2: 12bit, single-conversion ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = 0; ADC_InitStructure.ADC_ExternalTrigConv = 0; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE); ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles); // Enable ADC1 ADC_Cmd(ADC1, ENABLE); #if defined(SEND_RSSI_DATA) ADC_Init(ADC2, &ADC_InitStructure); ADC_EOCOnEachRegularChannelCmd(ADC2, ENABLE); ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_3Cycles); // Enable ADC2 ADC_Cmd(ADC2, ENABLE); #endif // Init the DAC DAC_InitTypeDef DAC_InitStructure; GPIO_StructInit(&GPIO_InitStruct); DAC_StructInit(&DAC_InitStructure); // GPIOA & D clock enable RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // DAC Periph clock enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); // GPIO CONFIGURATION of DAC Pins GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStruct); DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1, ENABLE); // Init the timer RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseInitTypeDef timerInitStructure; TIM_TimeBaseStructInit (&timerInitStructure); timerInitStructure.TIM_Prescaler = 1749; // 24 kHz timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up; timerInitStructure.TIM_Period = 1; timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; timerInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &timerInitStructure); TIM_Cmd(TIM2, ENABLE); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); NVIC_InitTypeDef nvicStructure; nvicStructure.NVIC_IRQChannel = TIM2_IRQn; nvicStructure.NVIC_IRQChannelPreemptionPriority = 0; nvicStructure.NVIC_IRQChannelSubPriority = 1; nvicStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvicStructure); GPIO_ResetBits(PORT_COSLED, PIN_COSLED); GPIO_SetBits(PORT_LED, PIN_LED); }
/* functions */ void adc_init() { GPIO_InitTypeDef gpio; ADC_CommonInitTypeDef adc_com; ADC_InitTypeDef adc_init; NVIC_InitTypeDef nvic; int i; // make sure the HSI is turned on - STM32L1xx uses HSI only for ADC RCC_HSICmd(ENABLE); // enable ADC clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // set up pins as analog inputs // NOTE: clocks were likely enabled earlier. fix if they weren't. gpio.GPIO_Mode = GPIO_Mode_AN; gpio.GPIO_Speed = GPIO_Speed_2MHz; gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; for (i = 0; i < 3; i++) { gpio.GPIO_Pin = adc_gpio[i].pin; GPIO_Init(adc_gpio[i].port, &gpio); } // configure ADC initial values ADC_CommonStructInit(&adc_com); ADC_StructInit(&adc_init); adc_init.ADC_ExternalTrigConv = 0; // set up channels to read ADC_RegularChannelConfig(ADC1, ADC_CHAN_MIC_SIG, ADC_READ_MIC_SIG + 1, ADC_SampleTime_4Cycles); ADC_RegularChannelConfig(ADC1, ADC_CHAN_MIC_PEAK, ADC_READ_MIC_PEAK + 1, ADC_SampleTime_4Cycles); ADC_RegularChannelConfig(ADC1, ADC_CHAN_BATT_VOLTAGE, ADC_READ_BATT_VOLTAGE + 1, ADC_SampleTime_4Cycles); adc_init.ADC_ScanConvMode = ENABLE; adc_init.ADC_NbrOfConversion = 3; // enable end of conversion on each channel read ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE); // freeze ADC until data has been read ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); // initialize the ADC ADC_CommonInit(&adc_com); ADC_Init(ADC1, &adc_init); // it doesn't look like STM32L100 has auto-calibrate feature, // but if it did, we'd set it up here. baw. // set up ADC power saving ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE); // enable interrupt ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); // set interrupt priority nvic.NVIC_IRQChannel = ADC1_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 3; nvic.NVIC_IRQChannelSubPriority = 0; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); // finally, turn on the ADC ADC_Cmd(ADC1, ENABLE); }