Exemple #1
0
Fichier : main.c Projet : kqzca/prj
int readAdcChannel(uint32_t channel)
{
	int rtn = -1;
	
  HAL_ADC_Init(&hadc);
  ADC_ChannelConfTypeDef sConfig;
	
	static uint32_t channels[3] = {ADC_CHANNEL_7, ADC_CHANNEL_9, ADC_CHANNEL_9};
	for (int idx = 0; idx < 3; idx++)
	{
		sConfig.Channel = channels[idx];
		if (channel == sConfig.Channel)
		{
			sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
		}
		else
		{
			sConfig.Rank = ADC_RANK_NONE;
		}
		HAL_ADC_ConfigChannel(&hadc, &sConfig);
	}
	
	int adcRead[3] = {-1, -1, -1};
	for (int i=0; i<3; i++)
	{
		if (HAL_ADC_Start(&hadc) == HAL_OK)
		{
			if (HAL_ADC_PollForConversion(&hadc, 1000) == HAL_OK)
			{
				adcRead[i] = (int)HAL_ADC_GetValue(&hadc);
				HAL_ADC_Stop(&hadc);
			}
			else
			{
				HAL_ADC_Stop(&hadc);
				break;
			}
		}
		else
		{
			break;
		}
	}
	
	if ((adcRead[0] >= 0) && (adcRead[1] >= 0) && (adcRead[2] >= 0))
	{
		rtn = (adcRead[0] + adcRead[1] + adcRead[2])/3;
	}
	return rtn;
}
Exemple #2
0
// --- Read one sample from the photo diode ---
uint16_t samplePD(void)
{
		uint16_t value = 0;
		
		HAL_ADC_Start(&hadc);

		while(HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY) != HAL_OK) {}
		
		if (pdMode == 0)					// Analog
				value = HAL_ADC_GetValue(&hadc);
		else if (pdMode == 1)			// Digital
		{
				value = HAL_ADC_GetValue(&hadc);
				if (value >= onelevel)
						value = 1;
				else if (value <= zerolevel)
						value = 0;
				else
						value = round(rand()%1);
		}
			
		HAL_ADC_Stop(&hadc);
			
		return value;
}
Exemple #3
0
uint32_t adc_read(uint32_t u32_adc_chan)
{
	ADC_ChannelConfTypeDef sConfigADC;
	uint32_t u32_adc_result = 0;
	uint8_t j = 0;
	uint8_t u8_num_conv = 10;

	/* Configure channel */
	sConfigADC.Rank = ADC_RANK_CHANNEL_NUMBER;
	sConfigADC.Channel = u32_adc_chan;
	hstat = HAL_ADC_ConfigChannel(&hadc, &sConfigADC);

	/* Perform conversion */
	hstat = HAL_ADC_Start(&hadc);
	while(j<u8_num_conv)
	{
		hstat = HAL_ADC_PollForConversion(&hadc, 10);
		u32_adc_result = u32_adc_result + HAL_ADC_GetValue(&hadc);
		j++;
	}
	u32_adc_result = u32_adc_result / u8_num_conv;
	hstat = HAL_ADC_Stop(&hadc);

	/* Disable channel */
	sConfigADC.Rank = ADC_RANK_NONE;
	hstat = HAL_ADC_ConfigChannel(&hadc, &sConfigADC);

	return u32_adc_result;
}
Exemple #4
0
//------------------------------------------------------
uint8_t read_batt()
{ uint32_t temp[3];
	//HAL_ADC_Start_IT(&hadc1);
	for(uint8_t i=0;i<3;i++)
	{
		HAL_ADC_Start(&hadc1);
		if (HAL_ADC_PollForConversion(&hadc1, 100) != HAL_OK)
			return 0;
		if ((HAL_ADC_GetState(&hadc1) & HAL_ADC_STATE_EOC_REG) == HAL_ADC_STATE_EOC_REG)
		{
			temp[i] = HAL_ADC_GetValue(&hadc1);
				if(temp[i]<batt_min)
					temp[i]=batt_min;
				if(temp[i]>batt_max)
					temp[i]=batt_max;
		}
		HAL_ADC_Stop(&hadc1);
	}
	temp[0]=(temp[0]+temp[1]+temp[2])/3;
	batt_status=(temp[0]-batt_min)*100/(batt_max-batt_min);
	SEGGER_RTT_printf(0,"power:%d\r\n",batt_status);			

	return 1;

}
Exemple #5
0
STATIC uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) {
    HAL_ADC_Start(adcHandle);
    adc_wait_for_eoc_or_timeout(EOC_TIMEOUT);
    uint32_t value = ADCx->DR;
    HAL_ADC_Stop(adcHandle);
    return value;
}
Exemple #6
0
uint16_t temperature(){
		float									temp,Vdd_voltage,temp30,sense,x=0.8058608;
		int32_t                			f**k,vrefint_data; 												//VREFINT measured value
		int16_t											vrefint_cal_temp,vrefint_cal_int; 				//VREFINT calibration value
		ADC_ChannelConfTypeDef        sConfig;

		vrefint_cal_temp= *((int32_t*)0x1FFFF7B8);						
		vrefint_cal_int= *((int32_t*)0x1FFFF7BA);	
		ADC->CCR |= ADC_CCR_TSEN;
		
		HAL_Delay(50);
	//MX_ADC_Init();
	//HAL_ADC_Init(&hadc);
		HAL_ADCEx_Calibration_Start(&hadc);
	
		sConfig.Channel      = ADC_CHANNEL_VREFINT;
		sConfig.Rank         = ADC_RANK_CHANNEL_NUMBER;
		sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5 ;		
		hadc.Instance->CHSELR = (uint32_t)(ADC_CHSELR_CHSEL17);
		HAL_ADC_ConfigChannel(&hadc,&sConfig);
		
		HAL_ADC_Start(&hadc);
		HAL_ADC_PollForConversion(&hadc,1000);
		vrefint_data = (int32_t)HAL_ADC_GetValue(&hadc);								//vint beolvasott digitális érték
		HAL_ADC_Stop(&hadc);

		sConfig.Channel      =  ADC_CHANNEL_TEMPSENSOR;
		hadc.Instance->CHSELR = (uint32_t)(ADC_CHSELR_CHSEL16);		
		HAL_ADC_ConfigChannel(&hadc,&sConfig);	
		
		HAL_ADC_Start(&hadc);
		HAL_ADC_PollForConversion(&hadc,1000);
		f**k = (int32_t)HAL_ADC_GetValue(&hadc);										//temerature sensor
		HAL_ADC_Stop(&hadc);

		sense=(float)((float)vrefint_cal_int*(float)f**k);
		sense=(float)(sense/(float)vrefint_data);
		sense=(float)(sense*x);
		temp30=(3300*vrefint_cal_temp)/4095;
		temp=temp30-sense+30;
		PutString("Temperature:");
		PutNumber((uint32_t)temp);
		PutString("\n");

	//	HAL_ADC_DeInit(&hadc);
		return (uint16_t)temp;
}
Exemple #7
0
	uint16_t voltage_read(uint32_t channel, float voltage_div ){
		//TIM_CHANNEL_2 -> Battery voltage, div=2.0
		
		//ADC_voltage = (Vcal*vrefint_cal* ADC_data) / (vrefint_data*FULL_SCALE)
		
		float												voltage,x=0.80586;												//x=3300/4095
		uint32_t                			aResult;						//a mért csdatorna értéke
		uint16_t											vrefint_cal; 				//VREFINT calibration value,gyári kalibrációs érték(3,3V on)  vrefint
		uint32_t 											vrefint_data; 			//VREFINT measured value, tényleges vrefint
		ADC->CCR |= ADC_CCR_VREFEN;
		vrefint_cal= *((uint16_t*)VREFINT_CAL_ADDR);
		ADC_ChannelConfTypeDef        sConfig;

		//MX_ADC_Init();
		//HAL_ADC_Init(&hadc);

		sConfig.Channel      = ADC_CHANNEL_VREFINT;
		sConfig.Rank         = ADC_RANK_CHANNEL_NUMBER;
		sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;		
		hadc.Instance->CHSELR = (uint32_t)(ADC_CHSELR_CHSEL17);	
		HAL_ADC_ConfigChannel(&hadc,&sConfig);
	
		HAL_ADCEx_Calibration_Start(&hadc);
		
		HAL_ADC_Start(&hadc);
		HAL_ADC_PollForConversion(&hadc,1000);
		vrefint_data = HAL_ADC_GetValue(&hadc);								//vrefint beolvasott digitális érték
		HAL_ADC_Stop(&hadc);

		hadc.Instance->CHSELR = (uint32_t)(ADC_CHSELR_CHSEL2);	
		sConfig.Channel      = channel;								
		HAL_ADC_ConfigChannel(&hadc,&sConfig);		
		
		HAL_ADC_Start(&hadc);	
		HAL_ADC_PollForConversion(&hadc,100);
		aResult = HAL_ADC_GetValue(&hadc);										//aksifesz beolvasás
		HAL_ADC_Stop(&hadc);
		
		voltage = (((float)vrefint_cal/(float)vrefint_data))* (float)aResult*x;		//feszültség kiszámolása mV-ban
		voltage *=voltage_div;

		PutString("voltage=");
		PutNumber((uint32_t)voltage);	
		PutString("mV\n");
		return (uint16_t)voltage;
	}
Exemple #8
0
uint16_t battery_voltage_read(){												//PA2 lábon, chanel_2
		float												voltage,x=0.80586;
		uint32_t                			aResult;
		uint16_t											vrefint_cal; 				//VREFINT calibration value
		uint32_t 											vrefint_data; 			//VREFINT measured value
		ADC->CCR |= ADC_CCR_VREFEN;
		vrefint_cal= *((uint16_t*)VREFINT_CAL_ADDR);
		ADC_ChannelConfTypeDef        sConfig;

		//MX_ADC_Init();
		//HAL_ADC_Init(&hadc);

		sConfig.Channel      = ADC_CHANNEL_VREFINT;
		sConfig.Rank         = ADC_RANK_CHANNEL_NUMBER;
		sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;		
		hadc.Instance->CHSELR = (uint32_t)(ADC_CHSELR_CHSEL17);	
		HAL_ADC_ConfigChannel(&hadc,&sConfig);
	
		HAL_ADCEx_Calibration_Start(&hadc);
		
		HAL_ADC_Start(&hadc);
		HAL_ADC_PollForConversion(&hadc,1000);
		vrefint_data = HAL_ADC_GetValue(&hadc);								//vrefint beolvasott digitális érték
		HAL_ADC_Stop(&hadc);

		hadc.Instance->CHSELR = (uint32_t)(ADC_CHSELR_CHSEL2);	
		sConfig.Channel      = ADC_CHANNEL_2;									//aksifesz bemenet 
		HAL_ADC_ConfigChannel(&hadc,&sConfig);		
		
		HAL_ADC_Start(&hadc);	
		HAL_ADC_PollForConversion(&hadc,100);
		aResult = HAL_ADC_GetValue(&hadc);										//aksifesz beolvasás
		HAL_ADC_Stop(&hadc);
		
		voltage = (((float)vrefint_cal/(float)vrefint_data))* (float)aResult*x;		//feszültség kiszámolása mV-ban
		voltage *=2;

		PutString("battery voltage=");
		PutNumber((uint32_t)voltage);	
		PutString("mV");
		PutString("\n");

	//	HAL_ADC_DeInit(&hadc);
		return (uint32_t)voltage;
	}
Exemple #9
0
int main(void)
{

  HAL_Init();

  SystemClock_Config();


  MX_GPIO_Init();
  MX_ADC1_Init();
  MX_DMA2D_Init();
  MX_FMC_Init();
  MX_I2C3_Init();
  MX_LTDC_Init();
  MX_RNG_Init();
  MX_SPI5_Init();

	BSP_LCD_Init();
	BSP_LCD_LayerDefaultInit(LCD_BACKGROUND_LAYER, LCD_FRAME_BUFFER);
	BSP_LCD_LayerDefaultInit(LCD_FOREGROUND_LAYER, LCD_FRAME_BUFFER);
	BSP_LCD_SelectLayer(LCD_FOREGROUND_LAYER);
	BSP_LCD_Clear(LCD_COLOR_BLACK);
	BSP_LCD_DisplayOn();
	uint8_t Button_Value;
	GPIO_InitTypeDef GPIO_InitStruct;
  GPIO_InitStruct.Pin |= GPIO_PIN_0;
  GPIO_InitStruct.Mode = 0x00;
  GPIO_InitStruct.Pull = 0x02;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	while (1)
		{
		
		for(uint16_t i = 0; i < SIZE; i+=2) {
			HAL_ADC_Start(&hadc1);
			HAL_ADC_PollForConversion(&hadc1,1000); 
			samples[i] = HAL_ADC_GetValue(&hadc1);
			samples[i+1] = 0;
			HAL_ADC_Stop(&hadc1);
		}
		
		Button_Value = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
		if( Button_Value )
		{		
			fft();
			
		}
		else
		{
		
			display_samples(samples);

		}
	}
}
Exemple #10
0
STATIC uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) {
    uint32_t rawValue = 0;

    HAL_ADC_Start(adcHandle);
    if (HAL_ADC_PollForConversion(adcHandle, 10) == HAL_OK && HAL_ADC_GetState(adcHandle) == HAL_ADC_STATE_EOC_REG) {
        rawValue = HAL_ADC_GetValue(adcHandle);
    }
    HAL_ADC_Stop(adcHandle);

    return rawValue;
}
Exemple #11
0
/* --- Read one sample from the photo diode --- */
uint16_t SamplePD(void)
{
	uint16_t value = 0;
	
	HAL_ADC_Start(&hadc);

	while(HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY) != HAL_OK) {}
	
	value = HAL_ADC_GetValue(&hadc);
		
	HAL_ADC_Stop(&hadc);
		
	return value;
}
Exemple #12
0
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//
// single sample
//
uint16 uni_adc_singleSample(void)
{
	uint16 val16 = 0;
  ADC_ChannelConfTypeDef sConfig;
  //sConfig.Channel = ADC_CHANNEL_VREFINT;
  sConfig.Channel = ADC_CHANNEL_1;
	sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
	sConfig.Rank = 1;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
	if(HAL_ADC_Start(&hadc1) != HAL_OK) return 0;
	HAL_ADC_PollForConversion(&hadc1, 10); // timeout
	val16 = HAL_ADC_GetValue(&hadc1);
	HAL_ADC_Stop(&hadc1);
	return val16;
}
Exemple #13
0
/** \brief
 *
 * \param dev_num bsp_dev_adc_t: ADC dev num.
 * \param rx_data uint16_t*: The received byte.
 * \param nb_data uint8_t: Number of byte to received.
 * \return bsp_status_t: Status of the transfer.
 *
 */
bsp_status_t bsp_adc_read_u16(bsp_dev_adc_t dev_num, uint16_t* rx_data, uint8_t nb_data)
{
	(void)dev_num;
	int i;
	HAL_StatusTypeDef status;
	ADC_HandleTypeDef* hadc;

	status = BSP_ERROR;
	hadc = &adc_handle[dev_num];

	for(i=0; i<nb_data; i++) {
		HAL_ADC_Start(hadc);
		status = HAL_ADC_PollForConversion(hadc, ADCx_TIMEOUT_MAX);

		if(status != HAL_OK)
			return status;

		rx_data[i] = HAL_ADC_GetValue(hadc);
		HAL_ADC_Stop(hadc);
	}
	return status;
}
Exemple #14
0
uint16_t adc_read(analogin_t *obj)
{
    ADC_ChannelConfTypeDef sConfig = {0};

    // Configure ADC channel
    sConfig.Rank         = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5;
    sConfig.SingleDiff   = ADC_SINGLE_ENDED;
    sConfig.OffsetNumber = ADC_OFFSET_NONE;
    sConfig.Offset       = 0;

    switch (obj->channel) {
        case 1:
            sConfig.Channel = ADC_CHANNEL_1;
            break;
        case 2:
            sConfig.Channel = ADC_CHANNEL_2;
            break;
        case 3:
            sConfig.Channel = ADC_CHANNEL_3;
            break;
        case 4:
            sConfig.Channel = ADC_CHANNEL_4;
            break;
        case 5:
            sConfig.Channel = ADC_CHANNEL_5;
            break;
        case 6:
            sConfig.Channel = ADC_CHANNEL_6;
            break;
        case 7:
            sConfig.Channel = ADC_CHANNEL_7;
            break;
        case 8:
            sConfig.Channel = ADC_CHANNEL_8;
            break;
        case 9:
            sConfig.Channel = ADC_CHANNEL_9;
            break;
        case 10:
            sConfig.Channel = ADC_CHANNEL_10;
            break;
        case 11:
            sConfig.Channel = ADC_CHANNEL_11;
            break;
        case 12:
            sConfig.Channel = ADC_CHANNEL_12;
            break;
        case 13:
            sConfig.Channel = ADC_CHANNEL_13;
            break;
        case 14:
            sConfig.Channel = ADC_CHANNEL_14;
            break;
        case 15:
            if ((ADCName)obj->handle.Instance == ADC_1) {
                sConfig.Channel = ADC_CHANNEL_VOPAMP1;
                sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
            } else {
                sConfig.Channel = ADC_CHANNEL_15;
            }
            break;
        case 16:
            if ((ADCName)obj->handle.Instance == ADC_1) {
                sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
                sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
            } else {
                sConfig.Channel = ADC_CHANNEL_16;
            }
            break;
        case 17:
            sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
            if ((ADCName)obj->handle.Instance == ADC_1) {
                sConfig.Channel = ADC_CHANNEL_VBAT;
            }
#if defined(ADC2)
            if ((ADCName)obj->handle.Instance == ADC_2) {
                sConfig.Channel = ADC_CHANNEL_VOPAMP2;
            }
#endif
#if defined(ADC3)
            if ((ADCName)obj->handle.Instance == ADC_3) {
                sConfig.Channel = ADC_CHANNEL_VOPAMP3;
            }
#endif
#if defined(ADC4)
            if ((ADCName)obj->handle.Instance == ADC_4) {
                sConfig.Channel = ADC_CHANNEL_VOPAMP4;
            }
#endif
            break;
        case 18:
            sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
            sConfig.Channel = ADC_CHANNEL_VREFINT;
            break;
        default:
            return 0;
    }

    if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
        debug("HAL_ADC_ConfigChannel issue\n");;
    }

    if (HAL_ADC_Start(&obj->handle) != HAL_OK) {
        debug("HAL_ADC_Start issue\n");;
    }

    uint16_t MeasuredValue = 0;

    if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
        MeasuredValue = (uint16_t)HAL_ADC_GetValue(&obj->handle);
    } else {
        debug("HAL_ADC_PollForConversion issue\n");
    }

    if (HAL_ADC_Stop(&obj->handle) != HAL_OK) {
        debug("HAL_ADC_Stop issue\n");;
    }

    return MeasuredValue;
}
Exemple #15
0
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_I2C1_Init();
  MX_RTC_Init();
  MX_USART1_UART_Init();
  MX_SPI2_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();

  /* USER CODE BEGIN 2 */

  HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED);

    /* Enable Ultra low power mode */
    HAL_PWREx_EnableUltraLowPower();
    __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);

    /* Buffer used for transmission on USART1 */
    char tx1_buffer[120];


    RFM95_init(&hspi2);

    uint8_t payload_buff[14];
    PAYLOAD_Garden payload_garden;
    payload_garden.MessageType = 50;
    payload_garden.MessageId = 0;


    // Start in sensing mode.
    state = MAIN_STATE_SENSE;

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
    while (1) {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

        /* Do some work */
        if (state == MAIN_STATE_SENSE) {
            HAL_ADC_Start(&hadc);
            // junk readings
            payload_garden.Temperature = TEMPERATURE_external();
            payload_garden.CpuTemperature = TEMPERATURE_cpu();

            HAL_GPIO_WritePin(GPIOA, LED_Pin, GPIO_PIN_SET);

            payload_garden.MessageId++;
            payload_garden.VCC = BATTERY_vcc();
            payload_garden.ChargeMv = BATTERY_ChargeMv();
            payload_garden.ChargeMa = BATTERY_ChargeMa();



            /* Get the light reading while the adc gets ready */
            payload_garden.Light = LIGHT_lux();

            payload_garden.Temperature = TEMPERATURE_external();
            payload_garden.CpuTemperature = TEMPERATURE_cpu();
            HAL_ADC_Stop(&hadc);

            sprintf(tx1_buffer, "id:%d, vcc:%d, mv:%d, ma:%d, C:%d, cpuC:%d, lux:%d\n",
                    payload_garden.MessageId,
                    payload_garden.VCC,
                    payload_garden.ChargeMv,
                    payload_garden.ChargeMa,
                    payload_garden.Temperature,
                    payload_garden.CpuTemperature,
                    payload_garden.Light);
            HAL_UART_Transmit(&huart1, (uint8_t*) tx1_buffer, strlen(tx1_buffer), 1000);

            PAYLOAD_Garden_serialize(payload_garden, payload_buff);
            RFM95_send(&hspi2, payload_buff, 14);

            state = MAIN_STATE_TX;
        }

        /* Do nothing while the transmission is in progress */
        else if (state == MAIN_STATE_TX) {
            if (dio0_action == 1) {
                RFM95_setMode(&hspi2, RFM95_MODE_SLEEP);
                state = MAIN_STATE_SLEEP;
            }

            //TMP while interrupts are investigated
            //HAL_Delay(30);
            //state = MAIN_STATE_SLEEP;
        }

        /* Now that all the work is done, sleep until its time to do it all again */
        else if (state == MAIN_STATE_SLEEP) {
            HAL_GPIO_WritePin(GPIOA, LED_Pin, GPIO_PIN_RESET);

            //TMP while RFM is diabled
            //HAL_Delay(1000);

            /* Turn off the pin interrupts */
            HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);

            HAL_SuspendTick();

            /* Enter Stop Mode */
            HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 60,
            RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
            HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
            HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
            HAL_ResumeTick();

            /* Turn on the pin interrupts */
            HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

            state = MAIN_STATE_SENSE;
        }

    }
  /* USER CODE END 3 */

}
Exemple #16
-41
/**
  * @brief  This function will set the ADC to the required value
  * @param  pin : the pin to use
  * @retval the value of the adc
  */
uint16_t adc_read_value(PinName pin)
{
  ADC_HandleTypeDef AdcHandle = {};
  ADC_ChannelConfTypeDef  AdcChannelConf = {};
  __IO uint16_t uhADCxConvertedValue = 0;

  AdcHandle.Instance = pinmap_peripheral(pin, PinMap_ADC);

  if (AdcHandle.Instance == NP) return 0;

#ifndef STM32F1xx
  AdcHandle.Init.ClockPrescaler        = ADC_CLOCK_DIV;          /* Asynchronous clock mode, input ADC clock divided */
  AdcHandle.Init.Resolution            = ADC_RESOLUTION_12B;            /* 12-bit resolution for converted data */
  AdcHandle.Init.EOCSelection          = ADC_EOC_SINGLE_CONV;           /* EOC flag picked-up to indicate conversion end */
  AdcHandle.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
  AdcHandle.Init.DMAContinuousRequests = DISABLE;                       /* DMA one-shot mode selected (not applied to this example) */
#endif
  AdcHandle.Init.DataAlign             = ADC_DATAALIGN_RIGHT;           /* Right-alignment for converted data */
  AdcHandle.Init.ScanConvMode          = DISABLE;                       /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
  AdcHandle.Init.ContinuousConvMode    = DISABLE;                       /* Continuous mode disabled to have only 1 conversion at each conversion trig */
  AdcHandle.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */
  AdcHandle.Init.ExternalTrigConv      = ADC_SOFTWARE_START;            /* Software start to trig the 1st conversion manually, without external event */
  AdcHandle.State = HAL_ADC_STATE_RESET;
#if defined (STM32F0xx) || defined (STM32L0xx)
  AdcHandle.Init.LowPowerAutoWait      = DISABLE;                       /* Auto-delayed conversion feature disabled */
  AdcHandle.Init.LowPowerAutoPowerOff  = DISABLE;                       /* ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered */
  AdcHandle.Init.Overrun               = ADC_OVR_DATA_OVERWRITTEN;      /* DR register is overwritten with the last conversion result in case of overrun */
#ifdef STM32F0xx
  AdcHandle.Init.SamplingTimeCommon    = SAMPLINGTIME;
#else // STM32L0
  //LowPowerFrequencyMode to enable if clk freq < 2.8Mhz
  AdcHandle.Init.SamplingTime          = SAMPLINGTIME;
#endif
#else
#ifdef STM32F3xx
  AdcHandle.Init.LowPowerAutoWait      = DISABLE;                       /* Auto-delayed conversion feature disabled */
#endif
  AdcHandle.Init.NbrOfConversion       = 1;                             /* Specifies the number of ranks that will be converted within the regular group sequencer. */
  AdcHandle.Init.NbrOfDiscConversion   = 0;                             /* Parameter discarded because sequencer is disabled */
#endif

  g_current_pin = pin; /* Needed for HAL_ADC_MspInit*/

  if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
    return 0;
  }

  AdcChannelConf.Channel      = get_adc_channel(pin);             /* Specifies the channel to configure into ADC */
#ifdef STM32L4xx
  if (!IS_ADC_CHANNEL(&AdcHandle, AdcChannelConf.Channel)) return 0;
#else
  if (!IS_ADC_CHANNEL(AdcChannelConf.Channel)) return 0;
#endif
  AdcChannelConf.Rank         = ADC_REGULAR_RANK_1;               /* Specifies the rank in the regular group sequencer */
#ifndef STM32L0xx
  AdcChannelConf.SamplingTime = SAMPLINGTIME;                     /* Sampling time value to be set for the selected channel */
#endif
#if defined (STM32F3xx) || defined (STM32L4xx)
  AdcChannelConf.SingleDiff   = ADC_SINGLE_ENDED;                 /* Single-ended input channel */
  AdcChannelConf.OffsetNumber = ADC_OFFSET_NONE;                  /* No offset subtraction */
  AdcChannelConf.Offset = 0;                                      /* Parameter discarded because offset correction is disabled */
#endif
  /*##-2- Configure ADC regular channel ######################################*/
  if (HAL_ADC_ConfigChannel(&AdcHandle, &AdcChannelConf) != HAL_OK)
  {
    /* Channel Configuration Error */
    return 0;
  }

#if defined (STM32F0xx) || defined (STM32F1xx) || defined (STM32F3xx) || defined (STM32L0xx) || defined (STM32L4xx)
  /*##-2.1- Calibrate ADC then Start the conversion process ####################*/
#if defined (STM32F0xx) || defined (STM32F1xx)
  if (HAL_ADCEx_Calibration_Start(&AdcHandle) !=  HAL_OK)
#else
  if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) !=  HAL_OK)
#endif
  {
    /* ADC Calibration Error */
    return 0;
  }
#endif

  /*##-3- Start the conversion process ####################*/
  if (HAL_ADC_Start(&AdcHandle) != HAL_OK)
  {
    /* Start Conversation Error */
    return 0;
  }

  /*##-4- Wait for the end of conversion #####################################*/
  /*  For simplicity reasons, this example is just waiting till the end of the
      conversion, but application may perform other tasks while conversion
      operation is ongoing. */
  if (HAL_ADC_PollForConversion(&AdcHandle, 10) != HAL_OK)
  {
    /* End Of Conversion flag not set on time */
    return 0;
  }

  /* Check if the continous conversion of regular channel is finished */
  if ((HAL_ADC_GetState(&AdcHandle) & HAL_ADC_STATE_REG_EOC) == HAL_ADC_STATE_REG_EOC)
  {
    /*##-5- Get the converted value of regular channel  ########################*/
    uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle);
  }

  if (HAL_ADC_Stop(&AdcHandle) != HAL_OK)
  {
    /* Stop Conversation Error */
    return 0;
  }

  if(HAL_ADC_DeInit(&AdcHandle) != HAL_OK) {
    return 0;
  }

  return uhADCxConvertedValue;
}