cancel
Showing results for 
Search instead for 
Did you mean: 

Can I interrupt on an external pin being low for a certain period of time?

DiBosco
Senior
Posted on July 14, 2014 at 12:43

Folks,

Having started at the data sheet for over half a day I don't think I can do this, but feel it should be possible, so am hoping someone can clarify!

I'm using an STM32F107 and I would like to connect an external pin to the processor and measure if it stays low for a certain period of time. If it does, it should interrupt the processor. The timers are incredibly complex (and the manual does not make them easy to grasp), I think they almost do what I want, but not quite. 

For example, reset capture mode resets a timer when the input goes high, but it does not stop it when the input is high. If it did, it would count when the signal was low, reset and stay off when high. If I programmed the correct compare value into one of the CCR registers, if the counter got to a certain value, before my input went high, I could interrupt and know the external line had been low for the requisite time. However, as far as I can see, the counter continues when the input is high, What I need is for the external signal to reset the counter AND stop it counting at the same time. I could happily connect the same signal to two pins if that would work.

Can I actually do what I need to do please? And if so, how?!

Many thanks! :~)
7 REPLIES 7
zzdz2
Associate II
Posted on July 14, 2014 at 14:25

Maybe you can combine interrupts to achieve this.

I think you could use EXTI irq, when input is rising you disable the timer, when falling you setup it and after a delay the timer will generate its irq.

Posted on July 14, 2014 at 14:40

Or slave a timer off an external input so it gates the counter, and set the counter prescaler/period to timeout and interrupt.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
DiBosco
Senior
Posted on July 14, 2014 at 15:33

Chaps,

If I understand your suggestions correctly, with knick's, every time I had a negative going interrupt I'd have to go and [re]start the timer. I don't want that as the processor would be overloaded servicing the external interrupt routine! :D

Clive, from what I understand about the gated mode, although the counter would stop when TIx is low, when it went high again, it would start counting again, from where it was previously frozen. If I could get it to [automatically] reset the counter to zero when the pin went high, that would be perfect. I don't *think* I can do that, but I'd be delighted to be told otherwise!

I should have been more specific about not wanting an interrupt unless and until the pin had been held down for the requisite period of time, sorry.

Thanks again. :~)

Posted on July 14, 2014 at 17:23

Wouldn't PWM Input reset the counter? It would use CCR1/CCR2 to latch counter measurements of the pulse. Set CCR3 or CCR4 to a threshold count in Compare mode, and interrupt when compare hits.

Might get the odd interrupt to manage, but should be less than looking at everything.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
DiBosco
Senior
Posted on July 14, 2014 at 19:22

I don't think so, Clive. As far as I can see, that wouldn't stop and reset the counter. Maybe I misunderstand what you mean.

I've spent a few hours trying to get the ETR pin to reset the counter and go about this whole thing in a slightly different manner, but I cannot get it to do anything. Have I misunderstood the datasheet? It, to me, infers that you can choose a whole host of pins to reset the counter in reset mode, but I cannot do anything to make that happen. Do I actually have to use CH1 to achieve that? :(

Pretty much the only pin I can actually use is TIM4_ETR as something to reset the counter. What I was having a go at doing was having the ETR reset the counter, CCR1 interrupting after 100us and then have CCR2 interrupt after 200us. If the line was idle for that long I would assume there was no activity on the pin, then switch to using an edge interrupt on an external pin. Once I had that edge interrupt I'd start the time going again. The vast majority of the time the line would change state long before 100us was over, so I'd not get any interrupts if the ETR pin was resetting the counter.

I'm now worried that the ETR pin does not control the counter reset after spending all this time on it. :(

zzdz2
Associate II
Posted on July 15, 2014 at 09:39

I have another idea.

If you can't use interrupts then maybe combining two timers would work.

One timer in gated mode to generate reset events when input is high and second timer in reset mode.

If they can be connected that way.

DiBosco
Senior
Posted on July 15, 2014 at 10:19

I've managed to fudge it. It's not exactly as I want it, but I think it will work for me:

Set counter up to reset on negative going edge on ETR input, (turns out you can do it!) -

TIM4->SMCR = 0x8074;

Setup CCR1 to interrupt if the line has been unchanged for 80us and unchanged for CCR2 for 200us.

If interrupt goes off and it's CCR1, get ready to processor input. If it's a CCR2 interrupt, the line has been idle too long and nothing is happening. Go to idle mode and wait for an external interrupt before starting to look at the timings again.

The waveforms look rock solid on a scope if I put a toggle in on the CCR1 interrupt, so it seems to work pretty well.

As with a lot of STM32 stuff, the user guide is very poorly written, but when you get it working, it really does work beautifully. It looks to me like the manuals were written by a non-English speaker and very poorly translated and proof read. ST really should be doing better and look how Atmel do manuals.

Many thanks for the suggestions. :~)