/** * @brief Initializes the temperature sensor and its related ADC. * @param None * @retval the float value of temperature measured in Celsius. */ float temperature_MeasureValue(void) { /* Raw value of temperature sensor voltage converted from ADC1_IN16 */ uint16_t v_refint; /* Raw value of VREFINT converted from ADC1_INT17 */ uint16_t v_sensor; /* select ADC1_IN16 to sample sensor voltage value*/ ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_28Cycles); /* start one ADC conversion */ ADC_SoftwareStartConv(ADC1); /* wait unitl ECO bit is set, sample finished */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); ADC_ClearFlag(ADC1, ADC_FLAG_EOC); /* Read the value from ADC_DR*/ v_sensor = ADC_GetConversionValue(ADC1); /* select ADC1_IN16 to sample reference voltage value*/ ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 1, ADC_SampleTime_28Cycles); /* start one ADC conversion */ ADC_SoftwareStartConv(ADC1); /* wait unitl ECO bit is set, sample finished */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); ADC_ClearFlag(ADC1, ADC_FLAG_EOC); /* Read the value from ADC_DR*/ v_refint = ADC_GetConversionValue(ADC1); /* * measured_sensor_voltage = actual_reference_voltage * sampled_sensor_voltage / sampled_reference_voltage_value * temperature = (measured_sensor_voltage - sensor_voltage_at_25) / AVG_SLOPE + 25 */ return (VREFINT_VOLTAGE_V / v_refint * v_sensor - TEMPERATURE_V25) * 1000 / AVG_SLOPE + 25; }
/*---------------------------------------------------------------------------*/ int ubasic_get_adc(int ch) { int var = 0xff; switch(ch){ case 1: if (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) { var = 0xff; } else { var = ADC_GetConversionValue(ADC1) & 0x00ff; ADC_SoftwareStartConv(ADC1); } break; case 2: if (ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET) { var = 0xff; } else { var = ADC_GetConversionValue(ADC2) & 0x00ff; ADC_SoftwareStartConv(ADC2); } break; case 3: if (ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC) == RESET) { var = 0xff; } else { var = ADC_GetConversionValue(ADC3) & 0x00ff; ADC_SoftwareStartConv(ADC3); } break; default: var = 0xff; break; } return var; }
void _ADC_Init(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStruct; //Enabling ADC clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //ADC common init configuration for Multi mode ADC ADC_CommonInitStruct.ADC_Mode = ADC_DualMode_RegSimult; ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //ADC_DMAAccessMode_Disabled; ADC_DMAAccessMode_1; ADC_DMAAccessMode_2; ADC_DMAAccessMode_3 ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div2; //ADC_Prescaler_Div2; ADC_Prescaler_Div4; ADC_Prescaler_Div6; ADC_Prescaler_Div8 ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; //ADC_TwoSamplingDelay_5Cycles - i tak dalej po 1 do 20 cykli ADC_CommonInit(&ADC_CommonInitStruct); //ADC1 configuration ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; //Timer ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_Init(ADC1,&ADC_InitStructure); //ADC2 configuration ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; //Timer ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_Init(ADC2,&ADC_InitStructure); //Regular channels config ADC_RegularChannelConfig(ADC1,ADC_Channel_9,1,ADC_SampleTime_144Cycles); ADC_RegularChannelConfig(ADC2,ADC_Channel_2,1,ADC_SampleTime_144Cycles); //DMA for Multi mode ADC ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE); //Activating continuous mode ADC_ContinuousModeCmd(ADC1, ENABLE); ADC_ContinuousModeCmd(ADC2, ENABLE); //Enabling ADC ADC_Cmd(ADC1, ENABLE); ADC_Cmd(ADC2, ENABLE); ADC_SoftwareStartConv(ADC1); ADC_SoftwareStartConv(ADC2); }
uint16_t adc_read(uint8_t channel){ uint16_t vref; ADC_RegularChannelConfig(ADC1, ADC_Channel_Vrefint, 0, ADC_SampleTime_384Cycles); ADC_SoftwareStartConv(ADC1); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); vref=ADC_GetConversionValue(ADC1); ADC_RegularChannelConfig(ADC1, channel, 0, ADC_SampleTime_384Cycles); ADC_SoftwareStartConv(ADC1); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); return ADC_GetConversionValue(ADC1)*6840/vref; // magic number to get millivolts }
// fill this in float getTemp_celcius() { ADC_SoftwareStartConv(ADC1); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); ADC_ClearFlag(ADC1, ADC_FLAG_EOC); return ((ADC1->DR * 3000.0/4096.0) - 760)/2.5 + 25; }
int main(void) { //init: DAC_DeInit(); init_GPIOA(); init_GPIOC(); init_GPIOD(); init_DMA1(); init_DMA2(); init_TIM2(); init_TIM3(); init_TIM4(); init_TIM6(); init_ADC3(); init_DAC(); init_filter(&ap_1); init_filter(&ap_2); init_filter(&ap_3); init_filter(&ap_4); ap_filter_coefs(&ap_1); ap_filter_coefs(&ap_2); ap_filter_coefs(&ap_3); ap_filter_coefs(&ap_4); ADC_SoftwareStartConv(ADC3); while (1) { counter++; } }
/** * @brief Main program. * @param None * @retval None */ void main(void) { /* CLK configuration -------------------------------------------*/ CLK_Config(); /* ADC configuration -------------------------------------------*/ ADC_Config(); /* DMA configuration -------------------------------------------*/ DMA_Config(); /* TIM1 configuration -------------------------------------------*/ TIM1_Config(); /* Enable ADC1 DMA requests*/ ADC_DMACmd(ADC1, ENABLE); /* Enable TIM1 DMA requests*/ TIM1_DMACmd(TIM1_DMASource_Update, ENABLE); /* Start ADC1 Conversion using Software trigger*/ ADC_SoftwareStartConv(ADC1); while (1) {} }
char sampleADC(void) { char res = 0x0; CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_VrefintCmd(ENABLE); delay_10us(3); ADC_Cmd(ADC1, ENABLE); ADC_Init(ADC1, ADC_ConversionMode_Single, ADC_Resolution_6Bit, ADC_Prescaler_1); ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_9Cycles); ADC_ChannelCmd(ADC1, ADC_Channel_0, ENABLE); delay_10us(3); ADC_SoftwareStartConv(ADC1); while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0); res = (char)ADC_GetConversionValue(ADC1); ADC_VrefintCmd(DISABLE); ADC_DeInit(ADC1); /* disable SchmittTrigger for ADC_Channel_24, to save power */ //ADC_SchmittTriggerConfig(ADC1, ADC_Channel_24, DISABLE); CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, DISABLE); ADC_ChannelCmd(ADC1, ADC_Channel_0, DISABLE); return res; }
/** * @brief Configures the ADC1 channel5. * @param None * @retval None */ void ADC_Config(void) { /* Enable The HSI (16Mhz) */ RCC_HSICmd(ENABLE); /* Enable the GPIOF or GPIOA Clock */ RCC_AHBPeriphClockCmd(IDD_MEASUREMENT_GPIO_CLK, ENABLE); /* Configure PF.11 (ADC Channel11) or PA.05 (ADC Channe5) in analog mode */ GPIO_InitStructure.GPIO_Pin = IDD_MEASUREMENT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(IDD_MEASUREMENT_GPIO, &GPIO_InitStructure); /* Check that HSI oscillator is ready */ while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); /* ADC1 Configuration ------------------------------------------------------*/ /* Enable ADC1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); #ifdef USE_STM32L152D_EVAL /* Select ADC Bank channel */ ADC_BankSelection(ADC1, ADC_Bank_B); #endif ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel5 or channel1 configuration */ ADC_RegularChannelConfig(ADC1, IDD_MEASUREMENT_ADC_CHANNEL, 1, ADC_SampleTime_192Cycles); /* Define delay between ADC1 conversions */ ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); /* Enable ADC1 Power Down during Delay */ ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait until ADC1 ON status */ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET) { } /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); /* Wait until ADC Channel 5 or 1 end of conversion */ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) { } }
/*----------------------------------------------------------- * @brief Function Name : vhADC_initADC * @brief Description : Initializes ADC */ void vhADC_initADC(void){ /* ADC Common initialization */ ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div6; // 84MHz / prescaler(6) = 14MHz (max 30 OR 36... idk) ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); ADC_InitTypeDef ADC_InitStructure; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE; // Enable, because we want to measure more than 1 channel ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_RisingFalling; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 2; ADC_Init(ADC1, &ADC_InitStructure); /* ADCx regular channel configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_28Cycles); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 2, ADC_SampleTime_28Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); /* Enable ADCx DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADCx */ ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); }
void ADC_AcquireData() { /* Enable ADC clock */ //RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Enable ADC1 */ //ADC_Cmd(ADC1, ENABLE); /* Wait until the ADC1 is ready */ //while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET); /* Disable DMA mode for ADC1 */ ADC_DMACmd(ADC1, DISABLE); ADC_DMA_init(); /* Enable DMA mode for ADC1 */ ADC_DMACmd(ADC1, ENABLE); // /* Clear global flag for DMA transfert complete */ // clearADCDMA_TransferComplete(); /* Start ADC conversion */ ADC_SoftwareStartConv(ADC1); }
void InternalTempSensor::measureTemp(bool calibrate) { // ADC Conversion to read temperature sensor // Temperature (in °C) = ((Vsense – V25) / Avg_Slope) + 25 // Vense = Voltage Reading From Temperature Sensor // V25 = Voltage at 25°C, for STM32F407 = 0.76V // Avg_Slope = 2.5mV/°C // This data can be found in the STM32F407VF Data Sheet taskENTER_CRITICAL(); ADC_SoftwareStartConv(ADC1); //Start the conversion while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) {} ; //Processing the conversion temp = ADC_GetConversionValue(ADC1); //Return the converted data temp *= 3300; temp /= 0xfff; //Reading in mV temp /= 1000.0; //Reading in Volts temp -= 0.760; // Subtract the reference voltage at 25°C temp /= .0025; // Divide by slope 2.5mV temp += 25.0; // Add the 25°C if (calibrate) { temp -= calibration; } taskEXIT_CRITICAL(); }
/** * @brief To return the supply measurmeent * @caller several functions * @param None * @retval ADC value */ uint16_t ADC_Supply(void) { uint8_t i; uint16_t res; /* Initializes ADC */ ADC_Icc_Init(); ADC_TempSensorVrefintCmd(ENABLE); /* ADC1 regular channel 17 for VREF configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 1, ADC_SampleTime_192Cycles); /* initialize result */ res = 0; for(i=4; i>0; i--) { /* start ADC convertion by software */ ADC_SoftwareStartConv(ADC1); /* wait until end-of-covertion */ while( ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0 ); /* read ADC convertion result */ res += ADC_GetConversionValue(ADC1); } /* de-initialize ADC */ ADC_TempSensorVrefintCmd(DISABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, DISABLE); return (res>>2); }
//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); }
uint16_t AdcMcuRead( Adc_t *obj, uint8_t channel ) { uint16_t adcData = 0; /* Enable The HSI (16Mhz) */ RCC_HSICmd( ENABLE ); /* Check that HSI oscillator is ready */ while( RCC_GetFlagStatus( RCC_FLAG_HSIRDY ) == RESET ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, ENABLE ); // Temperature or Vref measurement if( ( channel == ADC_Channel_16 ) || ( channel == ADC_Channel_17 ) ) { // Yes, enable temperature sensor and internal reference voltage ADC_TempSensorVrefintCmd( ENABLE ); } // Configure selected channel ADC_RegularChannelConfig( ADC1, channel, 1, ADC_SampleTime_192Cycles ); /* Define delay between ADC1 conversions */ ADC_DelaySelectionConfig( ADC1, ADC_DelayLength_Freeze ); /* Enable ADC1 Power Down during Delay */ ADC_PowerDownCmd( ADC1, ADC_PowerDown_Idle_Delay, ENABLE ); /* Enable ADC1 */ ADC_Cmd( ADC1, ENABLE ); /* Wait until ADC1 ON status */ while( ADC_GetFlagStatus( ADC1, ADC_FLAG_ADONS ) == RESET ) { } /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv( ADC1 ); /* Wait until ADC Channel 5 or 1 end of conversion */ while( ADC_GetFlagStatus( ADC1, ADC_FLAG_EOC ) == RESET ) { } adcData = ADC_GetConversionValue( ADC1 ); ADC_Cmd( ADC1, DISABLE ); if( ( channel == ADC_Channel_16 ) || ( channel == ADC_Channel_17 ) ) { // De-initialize ADC ADC_TempSensorVrefintCmd( DISABLE ); } RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, DISABLE ); RCC_HSICmd( DISABLE ); return adcData; }
uint32_t adc_read_channel(int channel) { int timeout = 10000; if (channel > (ADC_NUM_CHANNELS-1)) { return 0; } /* ADC regular channel config ADC/Channel/SEQ Rank/Sample time */ ADC_RegularChannelConfig(ADCx, channel, 1, ADC_SampleTime_15Cycles); /* Start ADC single conversion */ ADC_SoftwareStartConv(ADCx); /* Wait for conversion to be complete*/ while(!ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) && --timeout >0) { } /* ADC conversion timed out */ if (timeout == 0) { return 0; } /* Return converted data */ return ADC_GetConversionValue(ADCx); }
void Sensor_Configuration(void) { ADC_CommonInitTypeDef ADC_CommonInitStructure; GPIO_InitTypeDef GPIO_InitStructure; SensorGPIO_Configuration(); DMA2_Configuration(); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; ADC_CommonInit(&ADC_CommonInitStructure); ADC1_Config(); ADC2_Config(); ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_Cmd(ADC2, ENABLE); ADC_DMACmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); }
void IRsensor1_Init(IRsensor * IRsensor){ if(IRsensor!=NULL) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE); IRsensor->state1=1; Init_ADC_Common(); Init_DMA(); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_3Cycles); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); } }
/** * @brief Configure ADC and Analog watchdog * @param None * @retval None */ static void ADC_Config(void) { /* Enable ADC1 clock */ CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE); /* Initialise and configure ADC1 */ ADC_Init(ADC1, ADC_ConversionMode_Continuous, ADC_Resolution_12Bit, ADC_Prescaler_2); ADC_SamplingTimeConfig(ADC1, ADC_Group_SlowChannels, ADC_SamplingTime_384Cycles); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 Channel 3 */ ADC_ChannelCmd(ADC1, ADC_Channel_3, ENABLE); /* Calculate Threshold data value*/ HighThresholdData = (uint16_t)(((uint32_t)HIGH_THRESHOLD_VOLTAGE * 1000) / (uint32_t)ADC_RATIO) ; LowThresholdData = (uint16_t)(((uint32_t)LOW_THRESHOLD_VOLTAGE * 1000) / (uint32_t)ADC_RATIO) ; /* Configure Analog Watchdog selected channel and Thresholds */ ADC_AnalogWatchdogConfig(ADC1, ADC_AnalogWatchdogSelection_Channel3, HighThresholdData, LowThresholdData); /* Enable Analog watchdog ADC1 Interrupt */ ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE); /* Enable Interrupts */ enableInterrupts(); /* Start ADC1 Conversion using Software trigger*/ ADC_SoftwareStartConv(ADC1); }
uint16_t adc_battery(void) { digitalWrite(ADC_BATTERY_ENABLE, HIGH); ADC_SoftwareStartConv(ADC1); while(ADC_GetSoftwareStartConvStatus(ADC1)); return ADC_GetConversionValue(ADC1); digitalWrite(ADC_BATTERY_ENABLE, LOW); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40xx.s/startup_stm32f427x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ #ifdef USE_LCD /* LCD Display init */ Display_Init(); #endif /* USE_LCD */ /* ADC1 Channel Vbat configuration */ ADC_Config(); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); while (1) { #ifdef USE_LCD /* Display ADC converted value on LCD */ Display(); #endif /* USE_LCD */ } }
uint16_t adc_lightsensor(void) { digitalWrite(ADC_LIGHTSENSOR_ENABLE, HIGH); ADC_SoftwareStartConv(ADC2); while(ADC_GetSoftwareStartConvStatus(ADC2)); return ADC_GetConversionValue(ADC2); digitalWrite(ADC_LIGHTSENSOR_ENABLE, LOW); }
float readTemp(){ float temperature; ADC_SoftwareStartConv(ADC1); // Start the conversion while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); // Wait for conversion to finish temperature = (float) ADC_GetConversionValue(ADC1); // Get ADC reading // Print ADC reading setbuf(stdout, NULL); printf("%f, " , temperature); // TODO: Convert ADC (digital) reading back to voltage value // Use the formula on page 20 of the lecture slides // Here, v_min = 0, v_max = 3.3, and n depends on the resolution // of the ADC (refer to the adc intialization in initTempSensor() function) // Assign the voltage value back to the temperature variable convADC(&temperature,12); setbuf(stdout, NULL); printf("%f, " , temperature); // TODO: Convert the digital value to a temperature and assign back // to the temperature value. // The formula for this conversion is given in the Technical Reference Manual // (v_sense is the voltage value we calculated in the previous step // and assigned back to temp) // Temperature (in °C) = {(V_SENSE - V_25) / Avg_Slope} + 25 convVolt(&temperature); setbuf(stdout, NULL); printf("%f\n" , temperature); return temperature; }
void CIO::interrupt() { uint8_t control = MARK_NONE; uint16_t sample = DC_OFFSET; uint16_t rawRSSI = 0U; m_txBuffer.get(sample, control); // Send the value to the DAC DAC_SetChannel1Data(DAC_Align_12b_R, sample); // Read value from ADC1 and ADC2 if ((ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)) { // shouldn't be still in reset at this point so null the sample value? sample = 0U; } else { sample = ADC_GetConversionValue(ADC1); #if defined(SEND_RSSI_DATA) rawRSSI = ADC_GetConversionValue(ADC2); #endif } // trigger next ADC1 ADC_ClearFlag(ADC1, ADC_FLAG_EOC); ADC_SoftwareStartConv(ADC1); m_rxBuffer.put(sample, control); m_rssiBuffer.put(rawRSSI); m_watchdog++; }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f429_439xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ #ifdef USE_LCD /* LCD Display init */ Display_Init(); #endif /* USE_LCD */ /* ADC3 configuration *******************************************************/ /* - Enable peripheral clocks */ /* - DMA2_Stream0 channel2 configuration */ /* - Configure ADC Channel13 pin as analog input */ /* - Configure ADC3 Channel13 */ ADC3_CH13_DMA_Config(); /* Start ADC3 Software Conversion */ ADC_SoftwareStartConv(ADC3); while (1) { /* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.0V)*/ uwADC3ConvertedVoltage = uhADC3ConvertedValue *3000/0xFFF; /* Display ADCs converted values on LCD */ #ifdef USE_LCD Display(); #endif /* USE_LCD */ } }
static void config_driver_adc_1(void) { ADC_InitTypeDef ADC_InitStructure; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 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_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; ADC_CommonInit(&ADC_CommonInitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_480Cycles); ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); }
float ad_readval() { ADC_SoftwareStartConv(ADC1); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)); int ival = ADC_GetConversionValue(ADC1); float fval = (float) (ival - 2048) / 2048.0; return fval; }
void acquireTemperatureData(void) { /* Enable ADC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Enable DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait until the ADC1 is ready */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET); /* re-initialize DMA -- is it needed ?*/ DMA_DeInit(DMA1_Channel1); DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_Cmd(DMA1_Channel1, ENABLE); /* Enable DMA channel 1 Transmit complete interrupt*/ DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); /* Disable DMA mode for ADC1 */ ADC_DMACmd(ADC1, DISABLE); /* Enable DMA mode for ADC1 */ ADC_DMACmd(ADC1, ENABLE); /* Clear global flag for DMA transfert complete */ clearADCDMA_TransferComplete(); /* Start ADC conversion */ ADC_SoftwareStartConv(ADC1); }
uint16_t TM_ADC_Read(ADC_TypeDef* ADCx, uint8_t channel) { ADC_RegularChannelConfig(ADCx, channel, 1, ADC_SampleTime_15Cycles); ADC_SoftwareStartConv(ADCx); while (ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET); return ADC_GetConversionValue(ADCx); }
u16 readADC(ADC_TypeDef* ADCx, u8 channel, uint8_t sampleTime) { ADC_RegularChannelConfig(ADCx, channel, 1, sampleTime); ADC_SoftwareStartConv(ADCx); while(ADC_GetFlagStatus(ADCx, ADC_FLAG_EOC) == RESET); return ADC_GetConversionValue(ADCx); }