MC9S12XEP100 Cycle counter

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

MC9S12XEP100 Cycle counter

跳至解决方案
2,038 次查看
spietari
Contributor I

Hello,

 

I'd like to implement a ten-millisecond counter on my MCU so that the cycle count representing ten milliseconds could be arbitrary. I need this to account for inaccuracies in the internal clock frequency.

 

For example my bus frequency is 36.864.000Hz and each 10ms period would be represented by 368.640 cycles. I get a reference time from a GPS module and I'd like to change this cycle count accordingly.

 

My problem is I don't know how to build a timer mechanism that would fire every n cycles, where n can be over 65536. I've tried to use PIT but it needs the timeout to be n * m cycles so a cycle count that is e.g. a prime number cannot be used and the resolution in general would be the minimum of n and m.

 

Using ECT this could be achieved by dynamically adjusting the compare register with the modulus of the desired cycle count but it seems inaccurate and inelegant.

 

Kind regards,

Seppo

标签 (1)
0 项奖励
回复
1 解答
1,132 次查看
kef
Specialist I

Is ECT not accurate? How? Do you mean you are going to prescale ECT clock? Just use least 1:1 prescaler and you won't lose accuracy or ECT resolution. Arbitrary timeout periods can be both generated and measured using ECT. Resolution and accuracy is as good as bus clock.

 

isr_10ms()

{

static short ect_overflow_counter;

 

   if(ect_overflow_counter)

   {

      ect_overflow_counter--;

   }

   else

   {

      ect_overflow_counter = 368640L / 65536;

      TCx += 368640L % 65536;

 

      increment_10ms_counter();

   }

   clear_TCx_flag();

}

 

在原帖中查看解决方案

0 项奖励
回复
2 回复数
1,133 次查看
kef
Specialist I

Is ECT not accurate? How? Do you mean you are going to prescale ECT clock? Just use least 1:1 prescaler and you won't lose accuracy or ECT resolution. Arbitrary timeout periods can be both generated and measured using ECT. Resolution and accuracy is as good as bus clock.

 

isr_10ms()

{

static short ect_overflow_counter;

 

   if(ect_overflow_counter)

   {

      ect_overflow_counter--;

   }

   else

   {

      ect_overflow_counter = 368640L / 65536;

      TCx += 368640L % 65536;

 

      increment_10ms_counter();

   }

   clear_TCx_flag();

}

 

0 项奖励
回复
1,132 次查看
spietari
Contributor I

Perfect.

 

Thank you!

 

 

0 项奖励
回复