Polling a Port Pin for a specific amount of time

Hello all,

I hope someone can help me as I am tackling my first ever work projec with 8051 and my previous experience is .....absolutely nothing, zero.

I need to poll a pin for 5 seconds, and take certain actions when i changes from high to low, but then go back to polling. Interrupts don' seem like an ideal solution since it will interfere with the pollin process. How should I go about doing this? I read up on how to use th clock frequency (24MHz for the Cypress EZ-USB NX2LP) to calculate how lon a machine cycles takes, and then I could use that to determine how man cycles equate to 5 seconds but how would I account for the delay whe processing the "if pin is low" condition?

Rough Pseudocode :

For 5 seconds { while (port pin is high) track machine cycle count to make sure 5 secs has not passed. set a flag to indicate Pin has gone low continue checking Port Pin }

I don't think using a "time-delay" routine would work as I need t continuously poll the pin. Is that accurate? What about using a time without invoking an interrupt?

Oh, I am using C for this project. I hope I was able to convey m questions adequately.

Thanks in advance for any help.

Reply to
as1
Loading thread data ...

Probably your best bet would be to program a hardware timer to use as a clock or count down timer,.

It shouldn't interfere. Polling is by definition not continuous - it means that you go check every once in a while. And yes, the pin can change and change back in between checks, causing you to miss it. The alternative would be to make the pin trigger an interrupt, but then you aren't poling but using interrupts.

This would be wasteful, but you could pad out the other case with NOPs or useless math or something so that it takes the same number of cycles either way.

That may give you some trouble counting clock cycles, since you will have difficulty knowing exactly what instructions the compiler will generate. Where people do tend to do this with C, they are putting in some instructions to guarantee a minimum amount of time needed by hardware; especially once you start doing anything but the most trivial operations, the exct number of cycles in the compiler output is going to be hard to predict.

Reply to
cs_posting

cp_posting's idea for a timer is a good one.

Set up a timer to expire in 5 seconds. Now your pseudo-code looks like:

while (!timer_expired) if (pin == LOW) flag = PIN_WENT_LOW;

G.

Reply to
ghelbig

How precisely? I.e., 5 +/- n seconds where n is micro/milli/tenths/ or whole seconds. Do Bad Things happen if the interval is exceeded?

How much margin do you have; how soon after the edge must the certain actions commence? How long is the pin guaranteed to stay low? What happens if the pin has not changed by the end of the interval?

That depends somewhat on the answers to the above. Events that occur in the "seconds" epoch often imply that they're tied to user events, where the difference of a few milliseconds is negligible.

All other things being equal, I'd set up a timer to interrupt at a convenient interval and have that interrupt just increment a tick (so that it's fairly quick). Keep an eye on the pin value and the ticker and do the necessary when the pin changes or the ticker times out.

Also, you didn't mention it but it does matter whether the pin change is from other circuitry or, e.g., a user pushing a button. In the latter case, you'll also need to worry about debouncing the pin.

--
Rich Webb     Norfolk, VA
Reply to
Rich Webb

Thank you all for your comments. I am going to try using a timer as yo all suggested.

Reply to
as1

passed.

Interrupts are the obvious way, you just enable interrupt on the pin your monitoring for 5 secs, using a timer with it's interrupt. Any other way risks missing input pulses, adding extra latency and stopping your processor from doing anything else.

Reply to
cbarn24050

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.