Example #1
0
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
{
	/*ADC2 injected group
	conversion is finished*/
	/*count the amount of samples collected*/
	sampleCounter[1]--;
	/*collect samples into accumulator*/
	adcAccumulator[3] += HAL_ADCEx_InjectedGetValue(hadc, ADC_INJECTED_RANK_1);
	adcAccumulator[4] += HAL_ADCEx_InjectedGetValue(hadc, ADC_INJECTED_RANK_2);
	adcAccumulator[5] += HAL_ADCEx_InjectedGetValue(hadc, ADC_INJECTED_RANK_3);
	if(sampleCounter[1] == 0)
	{
		/*if required number of samples is collected
		calculate average data
		and turn off ADC */
		HAL_ADCEx_InjectedStop_IT(&hadc2);
		xCurr = adcAccumulator[3]/ADC2_SAMPL_NUMBER;
		yCurr = adcAccumulator[4]/ADC2_SAMPL_NUMBER;
		zCurr = adcAccumulator[5]/ADC2_SAMPL_NUMBER;
		
	}
	else
	{
		/*perform next conversion cycle otherwise*/
		HAL_ADCEx_InjectedStart_IT(&hadc2);
	}
}
/**
* @brief  This function handles ADC1_2 interrupt request.
* @param  None
* @retval None
*/
void ADC1_2_IRQHandler(void)
{
  /* Clear ADC interrupt flag */
  __HAL_ADC_CLEAR_FLAG(&AdcHandle, ADC_FLAG_JEOS);
  /* Get conversion value of Vin and Vout synchronized with Timer A CMP4 */
  VinConversion = HAL_ADCEx_InjectedGetValue(&AdcHandle, ADC_INJECTED_RANK_1);
  VoutConversion = HAL_ADCEx_InjectedGetValue(&AdcHandle, ADC_INJECTED_RANK_2);
}
Example #3
0
uint16_t getTipInstantTemperature() {
	uint16_t sum;
	sum = HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_1);
	sum += HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_2);
	sum += HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_3);
	sum += HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_4);
	return sum;

}
Example #4
0
s16 Motor_CurrentValues(u8 idx)
{
	s32 wAux;
    s16 I;

    wAux = HAL_ADCEx_InjectedGetValue(MotorParams[idx].CURR_ADC, ADC_INJECTED_RANK_1);
    wAux = (s32)(MotorParams[idx].h_phase_a_offset) - wAux;          // 偏置电压换算 
    //采样运放放大2倍
    wAux = wAux * ADC_FULL_V/ADC_FULL_VALUE * 1000/R_I_SENSE / 2; // 电流值计算 mA 
    
	//Saturation of Ia 
    if (wAux < S16_MIN)
    {
		I = S16_MIN;
    }  
    else if (wAux > S16_MAX)
	{ 
		I = S16_MAX;
	}
	else
	{
		I = wAux;
	}
  
	return I;
}
Example #5
0
void test_adc_hist_cb(ADC_HandleTypeDef* hadc) {
    //float unknown_ch_volts = read_ADC_volts(hadc, 1);

    uint32_t ADCValue = HAL_ADCEx_InjectedGetValue(hadc, 1);
    float val = (float)ADCValue;
    avg *= (1.0f - alpha);
    avg += alpha * val;

    float dval = val-avg;
    var *= (1.0f - alpha);
    var += alpha * (dval * dval);

    if (hist_countdown) {
        --hist_countdown;
    } else {
        int idval = (int)dval;
        int pos = (idval >= 0);
        if (!pos)
            idval = -idval;
        if (idval >= 20)
            idval = 19;
        if (pos)
            ++errhist[idval];
        else
            ++neg_errhist[idval];
    }
}
Example #6
0
/**
* @brief  电机驱动芯片工作电流采样初值计算
* @param  None
* @retval None
*/
void Motor_CurrentReadingCalibration()
{
    u32 sum[2] = {0, 0};
	u16 bIndex;
    
    //获得零电压偏移值
    for(bIndex=0; bIndex <16; bIndex++)
    {
        while(!__HAL_ADC_GET_FLAG(MotorParams[0].CURR_ADC, ADC_FLAG_JEOC)) { }
        __HAL_ADC_CLEAR_FLAG(MotorParams[0].CURR_ADC, ADC_FLAG_JEOC);
        sum[0] += HAL_ADCEx_InjectedGetValue(MotorParams[0].CURR_ADC, ADC_INJECTED_RANK_1);
        
        while(!__HAL_ADC_GET_FLAG(MotorParams[1].CURR_ADC, ADC_FLAG_JEOC)) { }
        __HAL_ADC_CLEAR_FLAG(MotorParams[1].CURR_ADC, ADC_FLAG_JEOC);
        sum[1] += HAL_ADCEx_InjectedGetValue(MotorParams[1].CURR_ADC, ADC_INJECTED_RANK_1);
    }
    
    MotorParams[0].h_phase_a_offset = sum[0] / bIndex;
    MotorParams[1].h_phase_a_offset = sum[1] / bIndex;
}
Example #7
0
void lineSensors_ADC_IT(ADC_HandleTypeDef *hadc)
{
	if (hadc == &hadc2)
	{
		lineSensors.right.adc_value 		= HAL_ADCEx_InjectedGetValue(&hadc2, ADC_INJECTED_RANK_1);
		lineSensors.left.adc_value 			= HAL_ADCEx_InjectedGetValue(&hadc2, ADC_INJECTED_RANK_2);
		lineSensors.right.average_value = (lineSensors.right.average_value + lineSensors.right.adc_value)/2;
		lineSensors.left.average_value = (lineSensors.left.average_value + lineSensors.left.adc_value)/2;
		lineSensors.active_ADC2	= FALSE;
	}
	else
	{
		lineSensors.left_ext.adc_value  	= HAL_ADCEx_InjectedGetValue(&hadc3, ADC_INJECTED_RANK_1);
		lineSensors.front.adc_value 		= HAL_ADCEx_InjectedGetValue(&hadc3, ADC_INJECTED_RANK_2);
		lineSensors.right_ext.adc_value 	= HAL_ADCEx_InjectedGetValue(&hadc3, ADC_INJECTED_RANK_3);
		lineSensors.front.average_value     = (lineSensors.front.average_value + lineSensors.front.adc_value)/2;
		lineSensors.active_ADC3	= FALSE;
	}
	if (lineSensors.active_ADC2 == FALSE && lineSensors.active_ADC3 == FALSE )
	{
		HAL_GPIO_WritePin(GPIOA, TX_LINESENSORS, RESET);
		lineSensors.emitter_state = FALSE;
	}
}
Example #8
0
/**
  * @brief  Conversion complete callback in non blocking mode 
  * @param  AdcHandle : AdcHandle handle
  * @note   This example shows a simple way to report end of conversion, and 
  *         you can add your own implementation.    
  * @retval None
  */
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
  /* Get the converted value of injected channel */
  uhADCxConvertedInjValue = HAL_ADCEx_InjectedGetValue(AdcHandle, ADC_INJECTED_RANK_1);    
}
Example #9
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{

  /* STM32F3xx HAL library initialization:
       - Configure the Flash prefetch
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();

  /* Configure the system clock to have a system clock = 72 MHz */
  SystemClock_Config();

  /* Initialize STM32F3348-DISCO LEDs */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);
  BSP_LED_Init(LED6);

  /* Initialize User_Button on STM32F3348-DISCO */
  BSP_PB_Init(BUTTON_USER, BUTTON_MODE_GPIO);   

  /* Initialize ADC to be triggered by the HRTIMER */
  ADC_Config();
  
  /* Initialize HRTIM and related inputs */
  HRTIM_Config();
  /* Initialize HRTIM outputs (it has to be done after HRTIM init) */
  GPIO_HRTIM_outputs_Config();
  
  /* Infinite loop */
  while (1)
  {
    
    /* ---------------- */
    /* Fault management */
    /* ---------------- */
    while(__HAL_HRTIM_GET_FLAG(&hhrtim, HRTIM_FLAG_FLT1) == SET)
    {
      /* LED3 is flashing in case of fault */
      BSP_LED_On(LED3);
      HAL_Delay(20);
      BSP_LED_Off(LED3);
      HAL_Delay(80);
      
      /* Re-arm HRTIM TD1 output if "User" push button is pressed*/
      if((BSP_PB_GetState(BUTTON_USER) == SET))
      {
          __HAL_HRTIM_CLEAR_IT(&hhrtim, HRTIM_IT_FLT1);
          HAL_HRTIM_WaveformOutputStart(&hhrtim, HRTIM_OUTPUT_TD1);
      }
    }

    /* ---------------- */
    /* Normal operation */
    /* ---------------- */
    /* LED6 toggling to show MCU activity */
    BSP_LED_On(LED6);
    HAL_Delay(100);
    BSP_LED_Off(LED6);
    HAL_Delay(400);
    
    /* -------------------------------------------------------------------- */
    /* ADC monitoring (to be viewed with watch)                             */
    /* PA4 can be connected to the RC filter or any voltage to be monitored */
    /* -------------------------------------------------------------------- */
    ADCReadout = HAL_ADCEx_InjectedGetValue(&AdcHandle, ADC_INJECTED_RANK_1);
    
  }
}
Example #10
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{

    /* STM32F3xx HAL library initialization:
         - Configure the Flash prefetch
         - Configure the Systick to generate an interrupt each 1 msec
         - Set NVIC Group Priority to 4
         - Global MSP (MCU Support Package) initialization
       */
    HAL_Init();

    /* Configure the system clock to have a system clock = 72 MHz */
    SystemClock_Config();

    /* Initialize STM32F3348-DISCO LEDs */
    BSP_LED_Init(LED3);
    BSP_LED_Init(LED4);
    BSP_LED_Init(LED6);

    /* Initialize User_Button on STM32F3348-DISCO */
    BSP_PB_Init(BUTTON_USER, BUTTON_MODE_GPIO);

    /* Initialize ADC to be triggered by the HRTIMER */
    ADC_Config();

    /* Initialize HRTIM and related inputs */
#ifdef SNIPPET
    HRTIM_Config_NoHAL();
#else
    HRTIM_Config();
#endif

    /* Initialize BUCK outputs (it has to be done after HRTIM init) */
    GPIO_BUCK_outputs_Config();

    /* Turn ON T6 MOSFET on discovery board */
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);

    /* Infinite loop */
    while (1)
    {

        /* ---------------- */
        /* Fault management */
        /* ---------------- */
#ifdef SNIPPET
        /* If Fault occured */
        while(HRTIM1->sCommonRegs.ISR & HRTIM_ICR_FLT1C)
        {
            /* LED3 is flashing in case of fault */
            BSP_LED_On(LED3);
            HAL_Delay(20);
            BSP_LED_Off(LED3);
            HAL_Delay(80);

            /* Re-arm HRTIM TD1 output if "User" push button is pressed*/
            if((BSP_PB_GetState(BUTTON_USER) == SET))
            {
                /* Clear interrupt flag */
                HRTIM1->sCommonRegs.ICR = HRTIM_ICR_FLT1C;
                /* Re-enable TA1 and TA2 */
                HRTIM1->sCommonRegs.OENR = HRTIM_OENR_TA1OEN + HRTIM_OENR_TA2OEN;
            }
        }
#else
        while(__HAL_HRTIM_GET_FLAG(&hhrtim, HRTIM_FLAG_FLT1) == SET)
        {
            /* LED3 is flashing in case of fault */
            BSP_LED_On(LED3);
            HAL_Delay(20);
            BSP_LED_Off(LED3);
            HAL_Delay(80);

            /* Re-arm HRTIM TD1 output if "User" push button is pressed*/
            if((BSP_PB_GetState(BUTTON_USER) == SET))
            {
                __HAL_HRTIM_CLEAR_IT(&hhrtim, HRTIM_IT_FLT1);
                HAL_HRTIM_WaveformOutputStart(&hhrtim, HRTIM_OUTPUT_TA1 | HRTIM_OUTPUT_TA2);
            }
        }
#endif

        /* ---------------- */
        /* Normal operation */
        /* ---------------- */
        /* LED6 toggling to show MCU activity */
        BSP_LED_On(LED6);
        HAL_Delay(100);
        BSP_LED_Off(LED6);
        HAL_Delay(400);

        /* -----------------------------------------------------------------------*/
        /* Input and output voltages can be displayed real-time in a watch window */
        /* -----------------------------------------------------------------------*/
        Vin = (HAL_ADCEx_InjectedGetValue(&AdcHandle, ADC_INJECTED_RANK_1) * ADC_VREF)/0x1000;
        /* VIN bridge conversion is 4.97 (6.8K / 6.8K + 27K) */
        Vin = (497 * Vin )/100;

        Vout = (HAL_ADCEx_InjectedGetValue(&AdcHandle, ADC_INJECTED_RANK_2) * ADC_VREF)/0x1000;
        /* VOUT bridge conversion is 5.03 (3.3K / 3.3K + 13.3K) */
        Vout = (503 * Vout)/100;

    }
}
Example #11
0
/**
  * @brief  Injected conversion complete callback in non blocking mode 
  * @param  hadc: ADC handle
  * @retval None
  */
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  uhADCxConvertedValue_Injected = HAL_ADCEx_InjectedGetValue(hadc, ADC_INJECTED_RANK_1);
}