Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
/**
  * @brief  Calculate the actual Voltage
  * @note   
  * @retval The value of the VoltageCal data.
  //STM32F042Cx  为12 bit 精度ADC
  */
float VoltageCal(void)
{	
	//uint32_t Voltage;
	#define Vref_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7ba))
	float Voltage;
	float Voltage2;
	float Voltage3;
	//启动转换
	ADC_StartOfConversion(ADC1);
	//wait for conversion complete
	while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)){;}
	//read ADC value
	Voltage = (float)ADC_GetConversionValue(ADC1);    
	#if  1
	//wait for conversion complete
	while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)){;}
	//read ADC value
	Voltage2 = (float)ADC_GetConversionValue(ADC1);    
	Voltage3 =   ((*Vref_CAL_ADDR )* 3.3) /Voltage2;
	Voltage = ( Voltage * Voltage3) /0xFFF;
	#else
	Voltage = ( Voltage * 3.3) /0xFFF;
	#endif	
    return Voltage;
}
Ejemplo n.º 3
0
/**
  * @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  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;
}
Ejemplo n.º 5
0
void adc_setup(){
    static int setup_complete = 0;
    ADC_InitTypeDef adc_init;
    GPIO_InitTypeDef gpio_init;
    
    if(setup_complete){
        return;
    }

    // Enable clock for ADC & GPIOC
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

    // Initialize GPIOC
    GPIO_StructInit(&gpio_init);
    gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | \
                         GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5; 
    gpio_init.GPIO_Mode = GPIO_Mode_AN;
    gpio_init.GPIO_Speed = GPIO_Speed_2MHz; 
    GPIO_Init(GPIOC, &gpio_init);
    
    // Initialize ADC1
    ADC_DeInit(ADC1);
    ADC_StructInit(&adc_init);
    ADC_Init(ADC1, &adc_init);

    //ADC_GetCalibrationFactor(ADC1);
    ADC_TempSensorVrefintCmd(ENABLE);
    ADC_Cmd(ADC1, ENABLE);
    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_RCNR));
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS));

    setup_complete++;
}
Ejemplo n.º 6
0
/*---------------------------------------------------------------------------*/
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;
}
Ejemplo n.º 7
0
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

}
Ejemplo n.º 8
0
static void
rng_seed()
{
    ADC_InitTypeDef ADC_InitStructure;
    ot_u16 ADCdata;
    int n;

    /* Enable The HSI (16Mhz) */
    RCC_HSICmd(ENABLE); // ADC can only use HSI?


    ADC_StructInit(&ADC_InitStructure);
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;
    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);

    ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_4Cycles);

    /* Enable ADC1 */
    ADC_Cmd(ADC1, ENABLE);

    /* Wait until ADC1 ON status */
    while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
    {
        asm("nop");
    }

    /* Start ADC1 Software Conversion */
    ADC_SoftwareStartConv(ADC1);

    r = 0;
    for (n = 0; n < 30; n++) {
        /* Wait until end of conversion */
        while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
            asm("nop");

        /* Read ADC conversion result */
        ADCdata = ADC_GetConversionValue(ADC1);
        r += ADCdata & 0x07;    // take lower noise bits
    }

    //debug_printf("r: %x\r\n", r);

    ADC_Cmd(ADC1, DISABLE);
    RCC_HSICmd(DISABLE); // assuming HSI not used
}
Ejemplo n.º 9
0
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);
}
Ejemplo n.º 10
0
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;
}
Ejemplo n.º 11
0
/**
 * @brief To get adc value of A0 ~ A3 from a WIZnet module.
 * @param index The sequence for A0 ~ A3 registration
 * @return adc value (uint16_t)
 */
uint16_t get_ADC_val(uint8_t index)
{
	uint16 adc_value = 0;

#if 0
	// for Test
	switch(index)
	{
		case A0: // WIZ550web BaseBoard: Potentiometer
			if(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == SET) adc_value = ADC_GetConversionValue(ADC2);
			break;
		case A1: // WIZ550web BaseBoard: Temperature Sensor
			adc_value = ADC1ConvertedValue; // TemperatureC = (((ADC_value * 3300) / 1023) - 500) / 10;
			break;
		case A2:
			adc_value = 1000;
			break;
		case A3:
			adc_value = 2000;
			break;
		default:
			adc_value = 0;
			break;
	}
#else
	adc_value = ADC_DualConvertedValueTab[index];
#endif

	return adc_value;
}
Ejemplo n.º 12
0
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;
}
Ejemplo n.º 13
0
unsigned short ReadADC1 (unsigned int channel)
{
	uint32_t tmpreg = 0;
	//GPIOA_PIN4_ON;
	// Set channel and sample time
	//ADC_ChannelConfig(ADC1, channel, ADC_SampleTime_7_5Cycles);	//pifia la medicion 2800 o 3400 en ves de 4095
	//ADC_ChannelConfig(ADC1, channel, ADC_SampleTime_239_5Cycles);
	//ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_239_5Cycles);

	//ADC_ChannelConfig INTERNALS
	/* Configure the ADC Channel */
	ADC1->CHSELR = channel;

	/* Clear the Sampling time Selection bits */
	tmpreg &= ~ADC_SMPR1_SMPR;

	/* Set the ADC Sampling Time register */
	tmpreg |= (uint32_t)ADC_SampleTime_239_5Cycles;

	/* Configure the ADC Sample time register */
	ADC1->SMPR = tmpreg ;


	// Start the conversion
	ADC_StartOfConversion(ADC1);
	// Wait until conversion completion
	while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
	// Get the conversion value
	//GPIOA_PIN4_OFF;	//tarda 20us en convertir
	return ADC_GetConversionValue(ADC1);
}
Ejemplo n.º 14
0
uint16_t read_adc(uint8_t channel)
{
	 uint16_t value=0;

	 ADC_RegularChannelConfig(ADC3, channel, 1, ADC_SampleTime_7Cycles5);
	 ADC_SoftwareStartConvCmd(ADC3, ENABLE);

	 while(ADC_GetFlagStatus(ADC3,ADC_FLAG_EOC)==RESET);

	 value=ADC_GetConversionValue(ADC3);//*3300/0xfff;
	 switch(channel)
 	{
	 	case 4:
	 		DEBUG("BATS_AB_MON_I val %d\r\n",value);
			break;
		case 6:
	 		DEBUG("BATS_A_V_MON val %d\r\n",value);
			break;
		case 7:
	 		DEBUG("BATS_B_V_MON val %d\r\n",value);
			break;
		default:
			break;
 	}
	 return value;
}
Ejemplo n.º 15
0
/**
  * @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);
}
Ejemplo n.º 16
0
uint16_t ADC1_Read(uint8_t Channel)                                     // convert and read given channel
{
  ADC_RegularChannelConfig(ADC1, Channel, 1, ADC_SampleTime_7Cycles5);
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);                               // Start the conversion
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);                // Wait until conversion complete
  return ADC_GetConversionValue(ADC1);                                  // Get the conversion value
}
Ejemplo n.º 17
0
Archivo: ADC.c Proyecto: cmtm/microP
// 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;
}
Ejemplo n.º 18
0
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);
}
Ejemplo n.º 19
0
/*
 * NOTE: No protection, should only be called from ISR
 */
static uint16_t read_channel(oscilloscope_input_t ch)
{
  uint8_t real_ch;
  switch(ch)
  {
    case input_channel0:
      real_ch = ADC_Channel_7;
      break;

    case input_channel1:
      real_ch = ADC_Channel_8;
      break;

    default:
      ipc_watchdog_signal_error(0);
      return UINT16_MAX;
  }
  
  ADC_RegularChannelConfig(ADC1, real_ch, 1, ADC_SampleTime_239Cycles5);
  ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

  /* spin until we have data */
  while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));			

  return ADC_GetConversionValue(ADC1);
}	
Ejemplo n.º 20
0
static inline uint16_t adc_read(analogin_t *obj) {
  // Get ADC registers structure address
  ADC_TypeDef *adc = (ADC_TypeDef *)(obj->adc);
  
  // Configure ADC channel
  switch (obj->pin) {
      case PA_0:
          ADC_RegularChannelConfig(adc, ADC_Channel_0, 1, ADC_SampleTime_7Cycles5);
          break;
      case PA_1:
          ADC_RegularChannelConfig(adc, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5);
          break;
      case PA_4:
          ADC_RegularChannelConfig(adc, ADC_Channel_4, 1, ADC_SampleTime_7Cycles5);
          break;
      case PB_0:
          ADC_RegularChannelConfig(adc, ADC_Channel_8, 1, ADC_SampleTime_7Cycles5);
          break;
      case PC_1:
          ADC_RegularChannelConfig(adc, ADC_Channel_11, 1, ADC_SampleTime_7Cycles5);
          break;
      case PC_0:
          ADC_RegularChannelConfig(adc, ADC_Channel_10, 1, ADC_SampleTime_7Cycles5);
          break;
      default:
          return 0;
  }

  ADC_SoftwareStartConvCmd(adc, ENABLE); // Start conversion
  
  while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion
  
  return(ADC_GetConversionValue(adc)); // Get conversion value
}
Ejemplo n.º 21
0
void adc_convert(void){
		uint16_t ADC1ConvertedValue;
	
	  /* Test EOC flag */
		while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
			;
 
	    /* Get ADC1 converted data */
		ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
		//update_rawADC(USART2, ADC1ConvertedValue);		//ADC value 0-4096
		//update_degF(USART2, ADC1ConvertedValue/1.64);	//degF 0-2500F
		//update_degC(USART2, ADC1ConvertedValue/3);		//degC 0-1370C
		
		//USART_PUT_TEMPF(USART2, ADC1ConvertedValue/1.64);
		//USART_PUT_TEMPC(USART2, ADC1ConvertedValue/3);
		
		if (ADC1ConvertedValue > 4000)
		{
			GPIOC->BSRR = GPIO_Pin_8;
			GPIOC->BSRR = GPIO_Pin_9;
		}
		else if (ADC1ConvertedValue > 2000)
		{
			GPIOC->BSRR = GPIO_Pin_8;
			GPIOC->BRR = GPIO_Pin_9;
		}
		else
		{
			GPIOC->BRR = GPIO_Pin_8;
			GPIOC->BRR = GPIO_Pin_9;
		}
}
Ejemplo n.º 22
0
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();
}
Ejemplo n.º 23
0
static void ADC_Configuration(void)
{
	ADC_InitTypeDef ADC_InitStructure;
	  /* Check that HSI oscillator is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
  /* ADC1 configuration */
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
	ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = ADC_CHANNEL_NUM;
  ADC_Init(ADC1, &ADC_InitStructure);

	// ADC_TempSensorVrefintCmd(ENABLE);
  /* ADC1 regular channel18 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_18, 1, ADC_SampleTime_4Cycles);
  //ADC_RegularChannelConfig(ADC1, ADC_Channel_Vrefint, 2, ADC_SampleTime_4Cycles);

  /* Enable the request after last transfer for DMA Circular mode */
  //ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
  
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  /* Wait until the ADC1 is ready */
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
  {
		
		
  }
}
Ejemplo n.º 24
0
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); 
}
Ejemplo n.º 25
0
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;
}
Ejemplo n.º 26
0
void U_ADC::ADCInit(uint8_t ADC_Channel, uint8_t ADC_SampleTime) {
	ADC_InitTypeDef ADC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

	ADC_DeInit(ADC1);
	ADC_StructInit(&ADC_InitStructure);
	ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
	ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
	ADC_Init(ADC1, &ADC_InitStructure);

	RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4);

	ADC_ChannelConfig(ADC1, ADC_Channel, ADC_SampleTime);

	ADC_TempSensorCmd(ENABLE);

	ADC_GetCalibrationFactor(ADC1);

	ADC_Cmd(ADC1, ENABLE);

	while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN))
		;
}
/**
  * @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_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
     */ 

  /* ADC Configuration */
  ADC_Config();
  /* LCD Display init  */
  Display_Init();
    
  /* Infinite loop */
  while (1)
  {
    /* Test EOC flag */
    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
    
    /* Get ADC1 converted data */
    ADC1ConvertedValue =ADC_GetConversionValue(ADC1);
    
    /* Compute the voltage */
    ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;
    
    /* Display converted data on the LCD */
    Display();
  }
}
Ejemplo n.º 28
0
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++;
}
Ejemplo n.º 29
0
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);
}
Ejemplo n.º 30
0
/**
 * @brief Perform a single synchronous software triggered conversion on a
 * channel.
 * @param dev ADC device to use for reading.
 * @param channel channel to convert
 * @return conversion result
 */
uint16_t adc_read(const adc_dev *dev, uint8_t channel)
{
  adc_data_ready = false;
  
#ifdef INTERNAL_VREF
  uint16_t vrefint = vref_read();
  float vref_V = 1.204 * 4095.0 / (float)vrefint;
#endif

  adc_disable(dev);
 
  /* ADC regular channel14 configuration */
  ADC_RegularChannelConfig(dev->ADCx, channel, 1, ADC_SampleTime_28Cycles5);
  adc_enable(dev);
      
  /* Start ADC Software Conversion */
  ADC_SoftwareStartConvCmd(dev->ADCx, ENABLE);
 
  /* Wait until ADC Channel end of conversion */  
  while (ADC_GetFlagStatus(dev->ADCx, ADC_FLAG_EOC) == RESET);
  
  /* Read ADC conversion result */
  uint16_t value = ADC_GetConversionValue(dev->ADCx);

#ifdef INTERNAL_VREF
  value = (uint16_t)((float)value * vref_V / 3.3);
#endif

  return value;
}