cancel
Showing results for 
Search instead for 
Did you mean: 

Timer count of 1 ms

dbgarasiya
Senior II

Hello

I want to configure timer which increment count by every millisecond

what are value of prescaller or other parameter should configure in it ?

i am attaching you my clock configuration with this

RCC->APB1ENR |= (1<<1); // Enable clock for TIM3

TIM3->PSC = 65535;  // Int clk divided by 1000(999+1)   // 2160000    // 65535

TIM3->EGR = TIM_EGR_UG; // Forcing to generate a update

TIM3->ARR = 9999; // Counts from 0 to 7999

TIM3->CR1 = TIM_CR1_CEN; // timer start TIM_CR1_OPM

I want that TIM3->CNT should increment by every millisecond

1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal

The internal prescaler goes only up to 65535, so you have to use an external one. Configure TIM3 as a slave, external clock mode 1, and another timer as a master, providing the clock to TIM3 instead of the APB1 clock.

Check the description of the TIM3 slave mode control register (SMCR) first. There is a table at the bottom, listing the available masters for each timer, pick one which is not used elsewhere in your project. Set the TS bits in TIM3->SMCR accordingly, along with the SMS bits (0111, note that the bitfield is not contiguous).

Set up the master timer, enable in RCC first, select master mode selection update

TIMx->CR2 |= TIM_CR2_MMS_1;

and set some limit in ARR. The product of (TIM_master->ARR + 1)*(TIM3->PSC+1)*1000 should give the APB clock frequency of the master.

Set the TIM3 prescaler etc., then start both timers.

View solution in original post

6 REPLIES 6

As you've probably found out, the prescaler is only 16-bit, so you simply can't divide a 108MHz input clock into 1kHz.

You could divide the whole APB1 down to a lower input clock, but that's probably not what you want as that influences all other peripherals on APB1 thus the total system performance.

Simply learn to live with what you have available. Other option would be to use two timers in master-slave arrangement, using one timer as a prescaler to the other.

JW

dbgarasiya
Senior II

Thanks for reply , I found out the solution

i divide whole APB1 and get timer clock as 54MHZ and then i solved out the solution

Thanks a lot,

berendi
Principal

The internal prescaler goes only up to 65535, so you have to use an external one. Configure TIM3 as a slave, external clock mode 1, and another timer as a master, providing the clock to TIM3 instead of the APB1 clock.

Check the description of the TIM3 slave mode control register (SMCR) first. There is a table at the bottom, listing the available masters for each timer, pick one which is not used elsewhere in your project. Set the TS bits in TIM3->SMCR accordingly, along with the SMS bits (0111, note that the bitfield is not contiguous).

Set up the master timer, enable in RCC first, select master mode selection update

TIMx->CR2 |= TIM_CR2_MMS_1;

and set some limit in ARR. The product of (TIM_master->ARR + 1)*(TIM3->PSC+1)*1000 should give the APB clock frequency of the master.

Set the TIM3 prescaler etc., then start both timers.

dbgarasiya
Senior II

Thanks for support,

dbgarasiya
Senior II

Hello ,

When i configure two timers in my project. and start both timer at same time

counter of both timer becomes same after first time (attached screenshot with post )

i have write code for counter 1

RCC->APB2ENR |= (1<<0);    // for timer 1

TIM1->PSC = 54000;

TIM1->EGR = TIM_EGR_UG;

TIM1->CR1 = TIM_CR1_CEN; // start timer 1

read count here // read counter 1

TIM1->CNT = 0; // reset counter 1

i have write code for counter 3

RCC->APB1ENR |= (1<<1);    // for timer 3

TIM3->PSC = 54000;

TIM3->EGR = TIM_EGR_UG;

TIM3->CR1 = TIM_CR1_CEN; // start timer 3

read count here // read timer count 3

TIM3->CNT = 0; // reset timer 3

i have attached my output of terminal and clock configuration with this post

 0690X00000BvldXQAR.png

dbgarasiya
Senior II

I solved out the problem

Thanks to all for support