cancel
Showing results for 
Search instead for 
Did you mean: 

how to configure ADC injected channel with PWM ?

Sudhakar.R
Associate III

Hi

I am using STM32F103C8T6 Microcontroller and tring to configure ADC injected channel with Timer 2 PWM in Register level. but i don't know how to enable the ADC injected channel. help me

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
FBL
ST Employee

Hello @Sudhakar.R​ 

Change to Timer 3 and follow this configuration instead

0693W00000Y6uHGQAZ.pngPWM generation doesn't need any output since timer is triggering ADC internally.

Trigger output event set to Update Event so that event every time the timer counter reaches ARR. Also, you should adjust counter period depending on sampling frequency and the application requirements.

0693W00000Y6uHfQAJ.pngADC should be triggered by Timer 3 trigger output event (For that reason you need to change to TIM3)

You don't need Output Compare 1 mode in this application.

Hope this helps

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

7 REPLIES 7
FBL
ST Employee

Hello @Sudhakar.R​ 

Maybe you should start by exploring the provided example of PWMOutput.

Then, get inspired by the example provided of ADC injected conversion. You may refer to RM0008 section 11.3.9 Injected channel management Subsection Triggered Injection for the configuration.

To do so, you may start the configuration on CubeMX, to enable the ADC injected channel using TIM2 PWM. Here is an example :

0693W00000Y6oibQAB.png

0693W00000Y6okRQAR.pngThen conversion results could be read from in the ADC_JDRx registers.

Hope this helps.

When your question is answered, please close this topic by choosing Select as Best.

Firas

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Sudhakar.R
Associate III

Hello @F.Belaid

Thank you for your replay.

This information helps me more.

Sudhakar.R
Associate III

Hello @F.Belaid​ 

I am using one regular ADC channel and one Injected ADC channel with PWM using STM32F103C8T6. but I getting regular and injected ADC values only and PWM is not generate. below mention my code and output image.

This is my code,

#include<stm32f10x.h>

volatile uint32_t Data_1 = 0;

volatile uint32_t Data_2 = 0;

void Delay(unsigned int T)

{

while(T--);

}

void ADC_Config()

{

RCC->APB2ENR|=(1<<9)|(1<<3); //ADC 1 interface clock enable & IO port B clock enable

RCC->APB2ENR|=(1<<2); //IO port A clock enable

GPIOB->CRL|=0x00000000; //GPIOB 0_PIN SELECTED AS ANALOG INPUT

GPIOA->CRL|=0x0000000B; //GPIOA 0_PIN SELECTED AS ANALOG INPUT

ADC1->CR2|=0x00000001; // Enable ADC and to start conversion

RCC->CFGR|=0x00008000; //ADC prescaler /PCLK2 divided by 6(72MHZ/6=12MHZ)

ADC1->CR2|=(1<<15); //Conversion on external trigger conversion mode enable for injected channels 

ADC1->CR1&= ~ADC_CR1_JAUTO; //Automatic injected group conversion disable

ADC1->CR1|=(1<<8); //Scan mode enable

/* Select Timer 2 TRGO event for injected group */

ADC1->CR2&= ~ADC_CR2_JEXTSEL;

ADC1->CR2|= (1<<13); //010: Timer 2 TRGO event

ADC1->CR2|=0x00000002; //Continuous conversion mode & Data Alignment RIGHT 

Delay(1);

ADC1->CR2|=0x00000001; // Enable ADC and to start conversion

Delay(1);

ADC1->CR2|=0x00000004; // ENABLE CALIBRATION

while(ADC1->CR2 & 0x00000004);

/* Select Channel 7 regular */ 

ADC1->SMPR2|=((1<<21)|(1<<22)|(1<<23)); //SELECT SMP7 SAMPLE_TIME_5 AS 239.5 cycles

ADC1->SQR1|=0x00000000;  // SELECT THE 1_CONVERSION

ADC1->SQR3|=0x00000008;  // SELECT THE 8_CHANNEL

/* Select Channel injected */

ADC1->JSQR&= ~ADC_JSQR_JL;

ADC1->JSQR&= ~ADC_JSQR_JSQ4;

ADC1->JSQR|= ADC_JSQR_JSQ4_1;

/* Start Conversion of Regular Channels */

ADC1->CR2|= ADC_CR2_SWSTART;

/* Start Conversion of Injected Channels */

ADC1->CR2|= ADC_CR2_JSWSTART;

}

void Timer_Config()

{

RCC->APB1ENR=0X00000001; //TIMR2 CLOCK ENABLE

TIM2->CR1|=(1<<7); //1: TIMx_ARR register is buffered & Auto-reload preload enable

TIM2->CR1&= ~TIM_CR1_DIR; //Set Counter Direction as Up-Counter

TIM2->ARR=36-1;   //ARR VALUE IS 0-65535

TIM2->PSC=1000-1;  //PERSCALER VALUE IS 0-65535

TIM2->CCR1 =Data_2<<4; //18; //Duty Cycle

TIM2->CNT=0;  //COUNT START IN 0

/* Select update event as trigger output (TRGO) */

TIM2->CR2&= ~TIM_CR2_MMS;

TIM2->CR2|= TIM_CR2_MMS_1;

/* Output Compare 1 mode */

//TIM2->CCMR1&= ~TIM_CCMR1_OC1M; //Clear Output compare 1 mode

TIM2->CCMR1|= TIM_CCMR1_OC1CE; //Output compare 1 clear enable

TIM2->CCMR1|=(1<<3); //Output compare 1 preload enable

TIM2->CCMR1|= TIM_CCMR1_OC1M_0|TIM_CCMR1_OC1M_1; //Configure the pins as PWM

TIM2->CCER = 0x00000001; //Capture/Compare 1 output enable

TIM2->CR1|=0x00000001; //COUNTER ENABLE

TIM2->EGR = 0x00000001;

}

int main()

{

ADC_Config();

Timer_Config();

while(1)

{

GPIOA->ODR |= (1<<0);

/*Check ADC1 EOC */

if((ADC1->SR & ADC_SR_EOC) == ADC_SR_EOC)

{

/* Read analog data and clear EOC flag */

Data_1 = ADC1->DR;

/* Start conversion of regular Channels */

ADC1->CR2|= ADC_CR2_SWSTART;

}

ADC1->SR|= ADC_SR_JSTRT;

/* Check ADC1 JEOC */

if((ADC1->SR & ADC_SR_JEOC) == ADC_SR_JEOC)

{

/* Read analog data and clear EOC flag */

Data_2 = ADC1->JDR1;

/* Clear JEOC flag */

ADC1->SR &= ~(ADC_SR_JEOC);

/* Start Conversion of Injected Channels */

ADC1->CR2|= ADC_CR2_JSWSTART;

}

}

}

i don't know where i mistake. if you know help me.

This is my output image,

0693W00000Y6sQVQAZ.png

Sudhakar.R
Associate III

Hi @F.Belaid​ 

Please response me.

FBL
ST Employee

Hello @Sudhakar.R​ 

Change to Timer 3 and follow this configuration instead

0693W00000Y6uHGQAZ.pngPWM generation doesn't need any output since timer is triggering ADC internally.

Trigger output event set to Update Event so that event every time the timer counter reaches ARR. Also, you should adjust counter period depending on sampling frequency and the application requirements.

0693W00000Y6uHfQAJ.pngADC should be triggered by Timer 3 trigger output event (For that reason you need to change to TIM3)

You don't need Output Compare 1 mode in this application.

Hope this helps

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

FBL
ST Employee

Hello again @Sudhakar.R​,

If you still facing issues, this article is dedicated to STM32H7 product but it may help you.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Sudhakar.R
Associate III

Hi @F.Belaid​ 

Thank you for your guidance. I got output. This is very useful for me.