10 years, 6 months ago.

How to change from output to interrupt input?

Hi, I have a sensor (an ultrasonic sensor to be exact) that runs off of a 1-wire input/output protocol. I was using DigitalInOut for it, and it worked perfectly, but due to the way the sensor worked, it could waste almost 20ms of processing time, and that is 20ms that I may need.

So, the way the protocol works is, I send a 2us long pulse to the sensor pin. This tells it to start a reading. Then, after a certain amount of time (I know the exact value), a return pulse starts, and lasts for somewhere between a few microseconds to about 20ms, after which, the pin drops low again.

To save time, I decided to make a timer, and configure the pin as an InterruptIn, with a function that triggers on the falling edge of the return pulse, instead of just waiting for however long it takes. But, for some reason "value" (see my attached code, but it's the value of the timer), never changes from 0. What is the problem? /media/uploads/Leonhart231/file.cpp

1 Answer

10 years, 6 months ago.

You can just copy paste code here, and put <<code>> and <</code>> around it.

Two things then: Does it enter your interrupt handler? You can simply for example light a LED when it comes there. That gives you more information. Second one, always if you have a global variable that is changed in an interrupt routine, it needs to be defined as volatile, otherwise the compiler might optimize a bit too much.

Accepted Answer

Ah, I'd been trying <code> and </code>, and failing. Thanks!

For anyone's future reference, it was declaring "value" as volatile that fixed it. Thank you very much for your help!

posted by Leon Hart 27 Oct 2013

They need to be on a seperate line from the code, and you need double '<<' and '>>' :)

posted by Erik - 27 Oct 2013