I am discovering the world of embedder software with microcontrollers. My need is to check that an input of my system stays high for 2 secondes for example.

Since I am not very familiar with microcontrollers, I wonder what kind of peripherals can help me with this. With a list of peripherals, I could check in my microcontroller specs (a Freescale MPC5553) which peripherals I could use for that.

Since the Reference Manual has 1208 pages, I won't be able to fully understand all peripherals features. So I'll probably miss some possibilities.

At the moment, the ideas I had are:

  • Do everything by hand with a GPIO that is checked periodically
  • Use a timer which decrementation is enabled with external input (if that is even is possible!) and that triggers an interrupt on timeout.
  • Learn what is the Enhanced Time Processing Unit (eTPU) of this specific microcontroller.

Thank you for your help.

  • 1
    \$\begingroup\$ MCUs have timers you can use. Without any external inputs (well, except the main oscillator maybe..) \$\endgroup\$ – Eugene Sh. Nov 17 '15 at 15:48
  • \$\begingroup\$ Watchdog timer perhaps... \$\endgroup\$ – Spoon Nov 17 '15 at 20:46
  • \$\begingroup\$ How about instead asking about particular features that don't make sense to you, rather than us guessing what you've read in the datasheet and what you haven't? \$\endgroup\$ – whatsisname Nov 17 '15 at 23:44
  • \$\begingroup\$ @EugeneSh.: That is the first idea I got. But I wonder if a timer can be easily enabled "externally". Your answer tells me it's not that obvious... Thanks \$\endgroup\$ – Plouff Nov 18 '15 at 8:32
  • \$\begingroup\$ @Spoon: Same comment as above. I wonder if a watchdog can be enabled according to a GPIO state... \$\endgroup\$ – Plouff Nov 18 '15 at 8:33

This microcontroller in particular has a compare/capture module. This allows you to determine the duration of a given signal from the rising and falling edges.

  • \$\begingroup\$ That's a kind of peripherals I was not aware of. Thank you for pointing me this one. I'll have a look if the MCU I use has such a peripheral. Thanks again! \$\endgroup\$ – Plouff Nov 18 '15 at 8:24
  • \$\begingroup\$ I think this is called the Enhanced Modular Input/Output Subsystem (eMIOS) in the MCP5553. Might be the perfect fit! I'll check this out! \$\endgroup\$ – Plouff Nov 18 '15 at 8:39

This is a very general answer, but I think it applies to your situation. What you are describing is pretty similar to de-bouncing contacts, right? Of course you didn't mention whether you care about the input needing to stay LOW for 2 seconds too, but I'll get to that in a moment. The nice thing about dealing with micro-controllers is that a lot of tasks like you are describing can be handled purely in software.

Lets assume the simple case of a single threaded environment, meaning all your tasks get called at some point in a big loop. This is generally called a 'round robin" approach. Well I'm sure your controller has a method of checking a system millisecond timer. So within one of your "round robin" tasks, lets call it checkInput(), you need to set up some "static" variables (meaning they retain their value when the function returns and is called again). One of those variables can be called "lastState", and can be set to 0 initially. You also want another variable called "lastTime", which for simplicity can also be initially set to zero.

So when your program calls checkContacts, you want to do something like this...

  1. Read the current state of the GPIO input, and compare it to "lastState". If the current state is equal to "lastState", you "return" the value of lastState from the function without doing anything.
  2. Assuming the current state has changed, you next want to read the system millisecond timer into a temp variable, and subtract the value stored in lastTime. You said you wanted two seconds, so if the difference is found to be less than 2000 (2000mS == 2 seconds), again you return 'lastState' and do nothing.
  3. if indeed the difference has met or exceeded 2000, then you store the current value of the GPIO input in "lastState, AND store the current millisecond timer value in LastTime. Again, return the value of "lastState.

So now, checkInput() always returns the time-qualified input state. You'll just want to make sure it gets called often enough for whatever level of accuracy you deem acceptable.

There are several good things about this arrangement. Besides it requiring no additional hardware, your 2000mS timer is easy to alter, and you can easily fine tune it to operate differently depending on the transition. For example, you said you want the input to only register "HIGH" when its been that way for 2 seconds, but you can easily have a different time for the reverse case (maybe you don't want any repeats unless the input has been low for 20 seconds!). Another nice thing is that the GPIO pin doesn't always need to be an input. For example, I have a system where each button has a LED indicating the last button pushed, and that LED is powered by the same GPIO lines I use to sense button presses. All I have to do is set the GPIO pins as "inputs" when I enter my function, check my states, and then restore the GPIOs to being outputs before I return. Bottom line, a software only solution will offer you maximum flexibility, and lowest parts count.

  • \$\begingroup\$ What you describe is the first idea I came up with. I agree it allows the best flexibility. Unfortunately, my goal is to use as many HW peripherals as I can. Indeed, 1/ I want to discover the kind of peripheral available in MCUs, 2/ I need to save as much processing as possible for the "core" application. So I will keep on looking for a HW peripheral that could to the job for me :). Thank you very much for your help! \$\endgroup\$ – Plouff Nov 18 '15 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.