Periodic Interrupt Timer

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

Periodic Interrupt Timer

Jump to solution
3,964 Views
sreeharshak
Contributor I

Hello everyone,

 

I'm working on MC9S12XDT512 controller and i'm in particular dealing with timers. My requirement is that i need to measure how long my signal from the proximity sensor is ON and how long it is OFF. I've tried with the 16 bit TCNT register but i'm getting only the time when i my sensor detects an obstacle. Does using a periodic interrupt timer help? How do i use it?

 

Thank You.

Labels (1)
0 Kudos
1 Solution
1,063 Views
RadekS
NXP Employee
NXP Employee

Hi,

Timer overflow interrupt routine is called when main 16-bit free-running timer (TCNT) overflows from 0xFFFF to 0x0000.

For example: rising_edge = 0xF000; overflow = 3; falling_edge = 0x2000; then:

number_of_cycles = (0xFFFF * (unsigned long int)overflow) + falling_edge - rising_edge

number_of_cycles = (0xFFFF * 3) + 0x2000 - 0xF000 =  0x22FFD;

Back to your first question: Can we use PIT for such detection?

Idea 1: IC timer function (previous proposal)

Idea 2: We can connect signal from proximity sensor to port with pin interrupt (Ports P, H, and J). In interrupt routine we can start some output compare function for measuring 2s and set some flag. Timer interrupt will signalize 2s quiet on line and we should clear our flag. When pin interrupt occurs before 2s time interval (our flag is still set) finishes, we should reset OC timer for measuring 2s again. Note: pin interrupt could be set to rising or falling edge, unfortunately not simultaneously. Detected edge could be set by PPSP (PPSH, PPSJ) register.

Disadvantage: PIT is not suited for such general OC function (periodic interrupt) and 2s could be too long interval for ECT (we should work with overflow …).

Idea 3: We can connect signal from proximity sensor to port with pin interrupt as in case 2. We can configure PIT for generating interrupt with period for example 100ms. In PIT interrupt routine we will increment counter and when counter>=20 we should generate some action. In pin interrupt routine we will simply clear this counter (plus change polarity of detected edge).

Disadvantage: we measure 2s with maximum error = 100ms (periodic interruptionand edge detection are independent). So, question is how accuracy of this 2s interval you need.

Note: for this purpose you can use also RTI (real time interrupt).

I found some similar example code for this case (Idea 3). I hope that help you.


View solution in original post

0 Kudos
7 Replies
1,063 Views
GordonD
Contributor IV

Sree,

To answer your question, I really need some additional details.

Is the signal from the proximity sensor a relatively clean digital signal?

What are the minimum and maximum on and off times you are trying to measure?

Best Regards,

Gordon

0 Kudos
1,063 Views
sreeharshak
Contributor I

Hi :-)

Yes, it is a clean digital signal. I get a logic 1 when there is no obstacle and i get a logic 0 when there is an obstacle. I am using this to detect if the wheel of my bike is stationary or not. So, i need to perform an action once i know that my wheel is stationary for 2 or more seconds. I will be continuously monitoring the proximity sensor.

0 Kudos
1,063 Views
RadekS
NXP Employee
NXP Employee

PIT is suited for output compare function only (generate period). I think that it will be difficult to use PIT for measuring of pulse with.

If I understood correctly, your problem with normal timer (ECT) is 2s interval = too long for TCNT timer. Correct?

In attachment you can find simple example code for measuring of pulse width by ECT module. This example code uses overflow interrupt for measuring longer pulses (longer than TCNT period). I suppose that you can simply modify this code for measuring both (0V and 5V) pulses.

You can also simply testing overflow variable for 2s limit in overflow interrupt routine.

Note: There is necessary take into account error of one TCNT period. If you want get better accuracy (of 2s limit), you should calculate with last value of TCx timer/resets TCx timer value….


1,063 Views
sreeharshak
Contributor I

Hi Radek :-)

I've gone through your program. I have a few doubts. Can you tell me when exactly is the program executing the timer overflow interrupt? We first are going through the input capture interrupt and at exactly what point do we branch to the timer overflow interrupt?

I executed your program and it runs fine, I get my logic 1 and logic 0 detected if signal exists for greater than 2 seconds but this executes only once. I need to reset to detect my signal again.

Can u please help me in my doubts in the first paragraph? Once i'm through that part, i feel i can manage the rest.

Thanking You,

Regards,

Harsha.

0 Kudos
1,064 Views
RadekS
NXP Employee
NXP Employee

Hi,

Timer overflow interrupt routine is called when main 16-bit free-running timer (TCNT) overflows from 0xFFFF to 0x0000.

For example: rising_edge = 0xF000; overflow = 3; falling_edge = 0x2000; then:

number_of_cycles = (0xFFFF * (unsigned long int)overflow) + falling_edge - rising_edge

number_of_cycles = (0xFFFF * 3) + 0x2000 - 0xF000 =  0x22FFD;

Back to your first question: Can we use PIT for such detection?

Idea 1: IC timer function (previous proposal)

Idea 2: We can connect signal from proximity sensor to port with pin interrupt (Ports P, H, and J). In interrupt routine we can start some output compare function for measuring 2s and set some flag. Timer interrupt will signalize 2s quiet on line and we should clear our flag. When pin interrupt occurs before 2s time interval (our flag is still set) finishes, we should reset OC timer for measuring 2s again. Note: pin interrupt could be set to rising or falling edge, unfortunately not simultaneously. Detected edge could be set by PPSP (PPSH, PPSJ) register.

Disadvantage: PIT is not suited for such general OC function (periodic interrupt) and 2s could be too long interval for ECT (we should work with overflow …).

Idea 3: We can connect signal from proximity sensor to port with pin interrupt as in case 2. We can configure PIT for generating interrupt with period for example 100ms. In PIT interrupt routine we will increment counter and when counter>=20 we should generate some action. In pin interrupt routine we will simply clear this counter (plus change polarity of detected edge).

Disadvantage: we measure 2s with maximum error = 100ms (periodic interruptionand edge detection are independent). So, question is how accuracy of this 2s interval you need.

Note: for this purpose you can use also RTI (real time interrupt).

I found some similar example code for this case (Idea 3). I hope that help you.


0 Kudos
1,063 Views
sreeharshak
Contributor I

Thank you so much for such a detail explanation :-) i've done a few modifications and i'm now getting the response the way i wanted.

Thank you for all the help :-) learnt a lot from your response.

0 Kudos
1,063 Views
sreeharshak
Contributor I

Hi Radek :-)

You got my question right. Thanks for the help:-)

I'll try this method and will get back to you.

0 Kudos