cancel
Showing results for 
Search instead for 
Did you mean: 

How to initialize and use TIM2 for output compare?

Vinod Kumar
Associate II

I am using the STM32F303CB controller.

System Clock - 72MHz

APB1ENR - 72MHz for Timer

I followed the below steps as per the reference manual.

Timer2 Initialization:

RCC->APB1ENR|= RCC_APB1ENR_TIM2EN;

   TIM2->ARR = 72000-1;

   TIM2->CCR1= 4000;

   TIM2->PSC = 1000-1;

   TIM2->DIER = TIM_DIER_CC1IE;

   TIM2->CCMR1 = TIM_CCMR1_OC1M_0|TIM_CCMR1_OC1M_1;

   TIM2->CCMR1 &= ~(TIM_CCMR1_OC1PE);

   TIM2->CCER |= TIM_CCER_CC1E;

   TIM2->CR1 |= TIM_CR1_CEN;

   while (!(TIM2->SR & (1<<0)));

   NVIC_EnableIRQ(TIM2_IRQn);

I have initialized the timer for 1 sec.

My application wants the output compare feature for a timer period.

For Example:

When the counter matches with CCR1(4000) interrupt should raise and CCR1 is updated with new value CCR1(15000) vice versa.

Did I made any mistake or need to do more steps.

Please guide me.

1 ACCEPTED SOLUTION

Accepted Solutions

You still can increase the prescaler.

JW

View solution in original post

8 REPLIES 8

IMO looks good. What is the observation? Read out the content of TIM registers and check.

JW

Vinod Kumar
Associate II

Hi waclawek.jan,

Good to see your reply.

Timer Interrupt Handler is raising for every 1 Sec. That I am not expected. I Want Interrupt Handler should raise only after matching CNT reg with CCR reg and CNT reg is incrementing as normally not with the time delay of 1 sec.

That's my observation.

> Timer Interrupt Handler is raising for every 1 Sec.

You've set PSC to 1000-1 and have TIM clock 72MHz, it means, that CNT is incremented 72MHz/1000=72000 times per second.

As ARR=72000-1, TIM_CNT counts at this 7kHz rate from 0 to 72000-1; this of course takes exactly 1s.

Within that 1s, there is one match between CNT and CCR1, whatever is the value of CCR1.

I don't quite understand, what is your expectation.

JW

Vinod Kumar
Associate II

Thanks for the exact explanation.

My Requirement is to Timer Interrupt should be raised only after 4000 seconds elapse as I stored in CCR1.

Then set CCR1 to 72000*4000. TIM2 is 32-bit, so this value fits.

You have to set ARR higher, but there's no reason not to set it to maximum, 0xFFFFFFFF (or simply not set it at all, as that's the reset value).

JW

Vinod Kumar
Associate II

Thank you.

I Initialized CCR1 with 72000*300=> (300/60=5).

Now Timer Interrupt is raising after 5 Minutes.

Exactly My expected output is:

  1. Timer Interrupt is raised for a particular time period.
  2. Initially, I will store one time period value. Then Interrupt raise after elapsing this time period.
  3. Timer Interrupt will raise after elapsing the first time period. In timer interrupt, I will store another time period.
  4. Now, time interrupt should raise after the new time period elapsed.

Basically timer interrupt should raise only when the time period elapsed. In my case, I will use two periods.

But the CCR1 is 32-bit we can able to store max 0xffffffff=>4,29,49,67,295.

So, we can achieve only the 59,652=>994 minutes=> 16 hours.

I like to have a time period of 24 hours.

Is there any possible way to achieve it?

You still can increase the prescaler.

JW

Vinod Kumar
Associate II

As you told I tried its working fine.

Thank you.

Is it possible to store the initial value to start from there in CNT reg?

I like to start the timer with some value regardless of zero.