cancel
Showing results for 
Search instead for 
Did you mean: 

ADC sampling with TIM1 and saving to DMA

thannara123
Senior

Hello ,

I am generating PWM wave of 16khz ,by using TIM1 ,the Duty cycle updating from the DMA array in circular mode .

(to generate sinewave ).

And worked as expected . The TIM1 is configured the Trigger (TRGO) to update event.

Now i want to sample ADC with a 124 micro second (as per the PWM frequency)

The TIM1 update event  should trigger the ADC ,to sample  and need to save the data via DMA.

how to configure it 

I triedout some of configuration dosent working correctly 

please help me 

uint32_t ADC_RES[200];

HAL_ADC_Start_DMA(&hadc, (uint32_t*)&ADC_RES,200);

HAL_ADC_Start_DMA(&hadc, (uint32_t*)&ADC_RES,200);

 

I have configured as follows , But i didnt get 124 micro second sample why ?

 

 

static void MX_ADC_Init(void)

{

 

/* USER CODE BEGIN ADC_Init 0 */

 

/* USER CODE END ADC_Init 0 */

 

ADC_ChannelConfTypeDef sConfig = {0};

 

/* USER CODE BEGIN ADC_Init 1 */

 

/* USER CODE END ADC_Init 1 */

 

/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

*/

hadc.Instance = ADC1;

hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

hadc.Init.Resolution = ADC_RESOLUTION_12B;

hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;

hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

hadc.Init.LowPowerAutoWait = DISABLE;

hadc.Init.LowPowerAutoPowerOff = DISABLE;

hadc.Init.ContinuousConvMode = ENABLE;

hadc.Init.DiscontinuousConvMode = DISABLE;

hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_TRGO;

hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;

hadc.Init.DMAContinuousRequests = ENABLE;

hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;

if (HAL_ADC_Init(&hadc) != HAL_OK)

{

Error_Handler();

}

 

/** Configure for the selected ADC regular channel to be converted.

*/

sConfig.Channel = ADC_CHANNEL_0;

sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;

if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN ADC_Init 2 */

 

/* USER CODE END ADC_Init 2 */

 

}

Screenshot 2024-05-04 at 9.13.21 PM.png

 

 

Thanking you 

 

 

@AScha.3   

@waclawek.jan 

 

 

 

 

13 REPLIES 13

Why ?

 

+

look in src -> xxx_hal_msp.c

If you feel a post has answered your question, please click "Accept as Solution".
thannara123
Senior

I am a beginer,I didnt know where it is placed ,i didnt see it on main.c configuration section why i asked ,

I am searching for the solution for the same as said in the post .

Trying adc + Timer alone but not working something wrong with me 

Ok,

so better first look for some examples from STM :

https://github.com/STMicroelectronics

- there are many, just search and look and learn...

If you feel a post has answered your question, please click "Accept as Solution".
thannara123
Senior

thanks