MC912DG128A - HC12 Timer

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

MC912DG128A - HC12 Timer

6,372 次查看
hc12prog
Contributor I
Hello,
 
i have a problem with the timer counter.
 
Code:
int variable1, variable2;
timerinit();while(1){variable1=TCNT;  //save the TimerCounterRegister TCNT in variable1variable2=TCNT;  //...}

The Timer has a resolution of 1µs. Now the problem: in the first cycle of the loop the difference between variable1 and variable2 is 6µs. from the second cycle the difference is only 1µs or 0µs. the reason of 0 is probably that the resolution is to big. But why 6µs in the first cycle? Do you have any idea? Thank you!



--
Alban Edit: Please always include FSL Part Number in Message Subject line.




Message Edited by Alban on 2007-09-22 09:06 PM
标签 (1)
0 项奖励
回复
11 回复数

2,260 次查看
kef
Specialist I
hc12prog,
 
are interrupts enabled? Some interrupt could fire between two TCNT readings.
 
0 项奖励
回复

2,260 次查看
hc12prog
Contributor I
Hello,
no, there are no interrupts. i show yo the complete source, but the source do not important things. I want to test some functions of the HC12 only. As Output at may terminal I see the curious phenomenon, that TCNT Register has the following values: a1=38; a2=44; a3=50;....;ax=122; ax+1=128; ax+2=129,ax+3=129;.... until TCNT=128 the variables change with add 6, but then with add1. Why? I find in the Manual the following text by the TCNT-Register discribtion, can this be the reason?
 
"The 16-bit main timer is an up counter. A full access for the counter register should take place in one clock cycle. A separate read/write for high byte and low byte will give a different result than accessing them as a word.

Read anytime.

Write has no meaning or effect in the normal mode; only writable in special modes (SMODN = 0). The period of the first count after a write to the TCNT registers may be a different size because the write is not synchronized with the prescaler clock."

 
Code:
#include "stdlib.h"#include "stdio.h"#include "912d128.h"#include "sci.h"volatile int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23;void main(){ // Timer Setup TSCR   = 0x80; // Timer enable TMSK2  = 0x03; // Prescaler = 8 (1tick all 1µs) SCI0Init(); while(1)  {    a1=TCNT;   a2=TCNT;   a3=TCNT;   a4=TCNT;   a5=TCNT;   a6=TCNT;   a7=TCNT;   a8=TCNT;   a9=TCNT;   a10=TCNT;   a11=TCNT;   a12=TCNT;   a13=TCNT;   a14=TCNT;   a15=TCNT;   a16=TCNT;   a17=TCNT;   a18=TCNT;   a19=TCNT;   a20=TCNT;   a21=TCNT;   a22=TCNT;   a23=TCNT;   SCI_OutString("x");   SCI_OutUDec(a1);   SCI_OutString("y");   SCI_OutUDec(a2);   SCI_OutString("z");   SCI_OutUDec(a3);   SCI_OutString("zz");   SCI_OutUDec(a4);   SCI_OutString("y");   SCI_OutUDec(a5);   SCI_OutString("z");   SCI_OutUDec(a6);   SCI_OutString("zz");   SCI_OutUDec(a7);   SCI_OutString("y");   SCI_OutUDec(a8);   SCI_OutString("z");   SCI_OutUDec(a9);   SCI_OutString("zz");   SCI_OutUDec(a10);   SCI_OutString("y");   SCI_OutUDec(a11);   SCI_OutString("z");   SCI_OutUDec(a12);   SCI_OutString("zz");   SCI_OutUDec(a13);   SCI_OutString("y");   SCI_OutUDec(a14);   SCI_OutString("z");   SCI_OutUDec(a15);   SCI_OutString("zz");   SCI_OutUDec(a16);    SCI_OutString("y");   SCI_OutUDec(a17);   SCI_OutString("z");   SCI_OutUDec(a18);   SCI_OutString("zz");   SCI_OutUDec(a19);   SCI_OutString("zz");   SCI_OutUDec(a20);    SCI_OutString("y");   SCI_OutUDec(a21);   SCI_OutString("z");   SCI_OutUDec(a22);   SCI_OutString("zz");   SCI_OutUDec(a23);  }}

 


 
0 项奖励
回复

2,260 次查看
kef
Specialist I
hc12prog,
 
Looks like you found timer bug. 912D60A and 9S12D64 ECT do behave like you noticed. I confirm that in normal mode:
 
1) enabling the timer and 2) setting timer prescaller to divide by 8, - new timer prescaler (divide by 8) doesn't apply until TCNT reaches 128.
 
Thanks



Message Edited by kef on 2007-09-24 02:24 PM

 

oops, thought it's a bug, but here is a notice in ECT docs, both 912D60A and S12 :

The newly selected prescale factor will not take effect until the next synchronized edge where all prescale counter stages equal zero.

So behaviour is expected



Message Edited by kef on 2007-09-24 02:34 PM

Message Edited by kef on 2007-09-24 02:34 PM
0 项奖励
回复

2,260 次查看
hc12prog
Contributor I
Thank you for the answer,
also it is no bug, right? and it is the right behavior for the HC12-Timer, which I discribe or is there another mistake?
0 项奖励
回复

2,260 次查看
kef
Specialist I
Yes, it's not a bug. According to the docs, timer prescaler changes not immediately after you write prescaler bits, but at TCNT=0bXXXXXXXXX0000000.
 
Maybe it's possible to set prescaler first, then enable the timer. Since after reset TCNT is 0, maybe prescaler could be enabled for first 127 ticks? I don't remember if it's possible to change prescaler while timer is disabled.
 
 
0 项奖励
回复

2,260 次查看
hc12prog
Contributor I
Okay, thank you. I will test it.
0 项奖励
回复

2,260 次查看
kef
Specialist I
I tried to setup timer prescaler first, then enable the timer. Doing this for S12 ECT makes new prescaler active even in first 128 timer ticks. S12 setup:
 
TSCR2 = 0x03; // Prescaler = 8 (1tick all 1µs)
TSCR1 = 0x80; // Timer enable
 
Doing the same with 912D60A ECT doesn't work.

TMSK2 = 0x03;  // write prescaler
TSCR = 0x80;    // enable timer

I mean that in D60A, the write of non default prescaler to TMSK2 is granted but doesn't apply until 128 timer ticks.

0 项奖励
回复

2,260 次查看
Lundin
Senior Contributor IV
In most of the cases it isn't safe to assume that the TCNT is running in sync with your code. The phenomenon you describe has nothing to do with 8-bit access though, since you are only doing 16-bit access.

What's causing the delay in this case is hard to tell. Perhaps you are running the program through a debugger/BDM? In that case the realtime will naturally be affected in unpredicted ways: the debugger might do all kind of weird stuff in the background that you don't see.
0 项奖励
回复

2,260 次查看
Alban
Senior Contributor II
Hello

HC12 what ?
Sure it is not S12 ?

Please always include FSL Part Number in Message Subject line.


Message Edited by Alban on 2007-09-22 07:26 PM
0 项奖励
回复

2,260 次查看
hc12prog
Contributor I
Hello,
 
what means FSL? i use the MC912DG128A
 
Can you explain the problem?
 

Thank you.

--
Alban Edit: Please always include FSL Part Number in Message Subject line.



Message Edited by Alban on 2007-09-22 09:07 PM
0 项奖励
回复

2,260 次查看
Alban
Senior Contributor II
Hi,

FSL means Freescale. This terms comes from the fact that people like Three Letters Accronysms.
FSL was also the share symbol at the New York Stock Exchange.
It is not anymore as FSL became private last year.

I haven't worked on HC12 enough to remember the TIM by heart.
Maybe someone else will be more helpful.

I edited messages so it shows the part number. This will help device experts to help you.
This will also avoid confusion as most of peole now think about S12 automatically (also called HCS12).

Cheers,
Alban.
0 项奖励
回复