adc.c 4.93 KB
#include "adc.h"

void adc_batt_init()
{
	adc_batt_peripheralInit();
}

uint32_t adc_batt_process()
{
	uint16_t adcRegisterValue;
	uint32_t analogValue;

	adcRegisterValue = adc_batt_read();
	analogValue = ADCbatt_VREF_MINUS + (((adcRegisterValue - ADCbatt_MIN_VALUE)*(ADCbatt_VREF_PLUS - ADCbatt_VREF_MINUS))/(ADCbatt_MAX_VALUE - ADCbatt_MIN_VALUE));
	printf("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCbatt_GPIO_STR);
	return analogValue;
}

void adc_batt_peripheralInit()
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	ADC_InitTypeDef  ADC_InitStructure;
	RCC_APB2PeriphClockCmd(ADCbatt_GPIO_RCC, ENABLE);
	GPIO_InitStructure.GPIO_Pin = ADCbatt_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(ADCbatt_GPIO_PORT, &GPIO_InitStructure);

	RCC_ADCCLKConfig(ADCbatt_DIVIDER);
	RCC_APB2PeriphClockCmd(ADCbatt_RCC, ENABLE);
	ADC_DeInit(ADCbatt);

	/* ADC1 and ADC2 operate independently */
	ADC_InitStructure.ADC_Mode 					= ADC_Mode_Independent; 
	/* Disable the scan conversion so we do one at a time */
	ADC_InitStructure.ADC_ScanConvMode 			= DISABLE;
	/* Don't do contimuous conversions - do them on demand */
	ADC_InitStructure.ADC_ContinuousConvMode 	= DISABLE;
	/* Start conversin by software, not an external trigger */
	ADC_InitStructure.ADC_ExternalTrigConv 		= ADC_ExternalTrigConv_None;
	/* Conversions are 12 bit - put them in the lower 12 bits of the result */
	ADC_InitStructure.ADC_DataAlign 			= ADC_DataAlign_Right;
	/* Say how many channels would be used by the sequencer */
	ADC_InitStructure.ADC_NbrOfChannel 			= 1;

	ADC_Init(ADCbatt, &ADC_InitStructure);
	ADC_Cmd(ADCbatt, ENABLE);

	/* Enable ADCbatt reset calibaration register */
	ADC_ResetCalibration(ADCbatt);
	/* Check the end of ADCbatt reset calibration register */
	while(ADC_GetResetCalibrationStatus(ADCbatt));
	/* Start ADCbatt calibaration */
	ADC_StartCalibration(ADCbatt);
	/* Check the end of ADC1 calibration */
	while(ADC_GetCalibrationStatus(ADCbatt));
}

uint16_t adc_batt_read()
{
  ADC_RegularChannelConfig(ADCbatt, ADCbatt_CHANNEL, 1, ADCbatt_SAMPLETIME);
  /* Start the conversion */
  ADC_SoftwareStartConvCmd(ADCbatt, ENABLE);
  /* Wait until conversion completion */
  while(ADC_GetFlagStatus(ADCbatt, ADC_FLAG_EOC) == RESET);
  /* Get the conversion value */
  return ADC_GetConversionValue(ADCbatt);
}

void adc_sound_init()
{
	adc_sound_peripheralInit();
}

uint32_t adc_sound_process()
{
	uint16_t adcRegisterValue;
	uint32_t analogValue;

	adcRegisterValue = adc_sound_read();
	analogValue = ADCsound_VREF_MINUS + (((adcRegisterValue - ADCsound_MIN_VALUE)*(ADCsound_VREF_PLUS - ADCsound_VREF_MINUS))/(ADCsound_MAX_VALUE - ADCsound_MIN_VALUE));
	printf("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCsound_GPIO_STR);
	return analogValue;
}

void adc_sound_peripheralInit()
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	ADC_InitTypeDef  ADC_InitStructure;
	RCC_APB2PeriphClockCmd(ADCsound_GPIO_RCC, ENABLE);
	GPIO_InitStructure.GPIO_Pin = ADCsound_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(ADCsound_GPIO_PORT, &GPIO_InitStructure);

	RCC_ADCCLKConfig(ADCsound_DIVIDER);
	RCC_APB2PeriphClockCmd(ADCsound_RCC, ENABLE);
	ADC_DeInit(ADCsound);

	
	/* ADC1 and ADC2 operate independently */
	ADC_InitStructure.ADC_Mode 					= ADC_Mode_Independent; 
	/* Disable the scan conversion so we do one at a time */
	ADC_InitStructure.ADC_ScanConvMode 			= DISABLE;
	/* Don't do contimuous conversions - do them on demand */
	ADC_InitStructure.ADC_ContinuousConvMode 	= DISABLE;
	/* Start conversin by software, not an external trigger */
	ADC_InitStructure.ADC_ExternalTrigConv 		= ADC_ExternalTrigConv_None;
	/* Conversions are 12 bit - put them in the lower 12 bits of the result */
	ADC_InitStructure.ADC_DataAlign 			= ADC_DataAlign_Right;
	/* Say how many channels would be used by the sequencer */
	ADC_InitStructure.ADC_NbrOfChannel 			= 1;

	ADC_Init(ADCsound, &ADC_InitStructure);
	ADC_Cmd(ADCsound, ENABLE);

	/* Enable ADCsound reset calibaration register */
	ADC_ResetCalibration(ADCsound);
	/* Check the end of ADCsound reset calibration register */
	while(ADC_GetResetCalibrationStatus(ADCsound));
	/* Start ADCsound calibaration */
	ADC_StartCalibration(ADCsound);
	/* Check the end of ADC1 calibration */
	while(ADC_GetCalibrationStatus(ADCsound));
}

uint16_t adc_sound_read()
{
  ADC_RegularChannelConfig(ADCsound, ADCsound_CHANNEL, 1, ADCsound_SAMPLETIME);
  /* Start the conversion */
  ADC_SoftwareStartConvCmd(ADCsound, ENABLE);
  /* Wait until conversion completion */
  while(ADC_GetFlagStatus(ADCsound, ADC_FLAG_EOC) == RESET);
  /* Get the conversion value */
  return ADC_GetConversionValue(ADCsound);
}