A Timer Question, please help

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

A Timer Question, please help

2,484 Views
pIx
Contributor I
When I designed my hardware, I made a gigantic mistake and routed all the Port-T Pins to receive data from an external ADC. Now I want to use the Timer (bummer, i know...)

The documentation says that if I disable the Timer, it will revert back to I/O ports. Here are the doubts I have:

1. Will it automatically revert to the settings I initially configured the I/O port with after I enable, use and then disable the timer?

2. Please Verify: I am using the Timer as Output Capture and all I have to do to disable the timer and give back control to the Port to function as I/O is set the bit "TEN" of "TSCR1" = 0. Is that correct?

3. Can I disable the Timer by making "TEN" of "TSCR1" = 0 from within the Timer Interrupt routine? I tried that, but it was still constantly interrupting the timer until I disabled the Timer Interrupt.

Thank you,
Sujith

Message Edited by pIx on 2006-09-20 07:32 AM

Labels (1)
0 Kudos
4 Replies

555 Views
imajeff
Contributor III
Sujith,

Regarding question (3.) I think you might only be trying to stop the output compare, and normally you would not need to stop TCNT as it is a freerunning system timer. Of course you can still stop it for whatever reason, but just stopping with TEN leaves it on the time that triggered the OC interrupt.

- To clear the interrupt flag, write '1' to the appropriate bit in TFLG1:
 TFLG1 = (1<<4);          // clears OC interrupt flg for TC4


- To stop future interrupt events on TC4, clear the interrupt enable bit in TIE:
 TIE &= ~(1<<4);          // disables interrupts on TC4


- To re-enable interrupt events on TC4, clear the interrupt flag first because there is likely an event pending that should be ignored:
 TC4 = TCNT + 0x100       // set a new time for event 256 ticks from now
 TFLG1 = (1<<4);          // clear previously ignored flag, no matter if was already 0
 TIE = (1<<4);            // enable interrupt

Message Edited by imajeff on 2006-09-21 11:47 AM

0 Kudos

555 Views
pIx
Contributor I
Thank you very much for the replies, I now have it almost working the way I want it.

DanielIM: I am using HCS12 and not the SX12.

imajeff: you had written:
TC4 = TCNT + 0x100 // set a new time for event 256 ticks from now

The doubt I have is, what if TCNT is lets say 200 ticks away from an overflow and being reset, what exactly will happen if I try to put TCNT + 0x100 into it?

If it is a preblem, how do I reset TCNT. I tried setting "TCRE" of "TSCR2", but that works only for TC7 and the data sheet says writing into TCNT does not make sense :smileyhappy:....

Thank you once again for taking the time to help me out.
~Sujith

Message Edited by pIx on 2006-09-2201:00 PM

0 Kudos

555 Views
imajeff
Contributor III

pIx wrote:
imajeff: you had written:
TC4 = TCNT + 0x100 // set a new time for event 256 ticks from now

The doubt I have is, what if TCNT is lets say 200 ticks away from an overflow and being reset, what exactly will happen if I try to put TCNT + 0x100 into it?

I almost didn't notice this question, but it's an important one to answer!

No problem with the overflow. You see, overflowing TCNT is practically no different from overflowing in addition. Therefore, they both come out to the same result.

example:
TCNT is at 0xffe0
I want an event 0x100 ticks later, so I set TC0 = TCNT + 0x100 ((word)ffe0 + 100 = 00e0)
When TCNT overflows and hits 0x00e0 (256 ticks later), it triggers as it should.

I can think of two limitations though:
  1. Since it takes time to calculate and write to TC0, you could miss the event if you were adding say only 1uS to the timer.
  2. You could not add more that 0xffff unless you knew that TCNT would pass the first occurrance of that value before writing.
0 Kudos

555 Views
DanielM
Contributor III
1) Yes, as soon as the timer frees-up the pin it will revert to whatever state it was before (DDR and DATA register values will be applied).

2) There is no such thing as Output Capture. It can be input capture or output compare.

Input capture: you always have control of the pin through GPIO registers. You can drive it if you want...

Output compare: If Omx/OLx are 0/0 and corresponding OC7M bit is 0, you should be able to drive the pin as GPIO even with the counter still running.

Note: There is a new register on S12XE to make the situation even clearer and easier to understand. What device are you using?

3) Yes, you can do that, but clear the interrupt flag first or else you will keep getting the interrupt (as you have already seen). You cannot clear the interrut flag when the timer is disabled as the power-saving clock gate will stop all clock to the module when it is disabled.

Daniel
0 Kudos