EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Polling a Port Pin for a specific amount of time

Started by as1 April 1, 2008
Hello all,

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

I need to poll a pin for 5 seconds, and take certain actions when it
changes from high to low, but then go back to polling. Interrupts don't
seem like an ideal solution since it will interfere with the polling
process. How should I go about doing this? I read up on how to use the
clock frequency (24MHz for the Cypress EZ-USB NX2LP) to calculate how long
a machine cycles takes, and then I could use that to determine how many
cycles equate to 5 seconds but how would I account for the delay when
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 to
continuously poll the pin. Is that accurate? What about using a timer
without invoking an interrupt? 

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

Thanks in advance for any help.



On Apr 1, 3:39 pm, "as1" <picg...@yahoo.com> wrote:

> I need to poll a pin for 5 seconds, and take certain actions when it > changes from high to low, but then go back to polling.
Probably your best bet would be to program a hardware timer to use as a clock or count down timer,.
> Interrupts don't > seem like an ideal solution since it will interfere with the polling > process.
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.
> How should I go about doing this? I read up on how to use the > clock frequency (24MHz for the Cypress EZ-USB NX2LP) to calculate how long > a machine cycles takes, and then I could use that to determine how many > cycles equate to 5 seconds but how would I account for the delay when > processing the "if pin is low" condition?
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.
> Oh, I am using C for this project.
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.
On Apr 1, 12:39 pm, "as1" <picg...@yahoo.com> wrote:
> Hello all, > > I hope someone can help me as I am tackling my first ever work project > with 8051 and my previous experience is .....absolutely nothing, zero. > > I need to poll a pin for 5 seconds, and take certain actions when it > changes from high to low, but then go back to polling. Interrupts don't > seem like an ideal solution since it will interfere with the polling > process. How should I go about doing this? I read up on how to use the > clock frequency (24MHz for the Cypress EZ-USB NX2LP) to calculate how long > a machine cycles takes, and then I could use that to determine how many > cycles equate to 5 seconds but how would I account for the delay when > 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 to > continuously poll the pin. Is that accurate? What about using a timer > without invoking an interrupt? > > Oh, I am using C for this project. I hope I was able to convey my > questions adequately. > > Thanks in advance for any help.
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.
On Tue, 01 Apr 2008 14:39:09 -0500, "as1" <picghaw@yahoo.com> wrote:

>Hello all, > >I hope someone can help me as I am tackling my first ever work project >with 8051 and my previous experience is .....absolutely nothing, zero. > >I need to poll a pin for 5 seconds,
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?
>and take certain actions when it >changes from high to low,
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?
>but then go back to polling. Interrupts don't >seem like an ideal solution since it will interfere with the polling >process.
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
>On Tue, 01 Apr 2008 14:39:09 -0500, "as1" <picghaw@yahoo.com> wrote: > >>Hello all, >> >>I hope someone can help me as I am tackling my first ever work project >>with 8051 and my previous experience is .....absolutely nothing, zero. >> >>I need to poll a pin for 5 seconds, > >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? > >>and take certain actions when it >>changes from high to low, > >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? > >>but then go back to polling. Interrupts don't >>seem like an ideal solution since it will interfere with the polling >>process. > >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 >
Thank you all for your comments. I am going to try using a timer as you all suggested.
On Apr 1, 8:39=EF=BF=BDpm, "as1" <picg...@yahoo.com> wrote:
> Hello all, > > I hope someone can help me as I am tackling my first ever work project > with 8051 and my previous experience is .....absolutely nothing, zero. > > I need to poll a pin for 5 seconds, and take certain actions when it > changes from high to low, but then go back to polling. Interrupts don't > seem like an ideal solution since it will interfere with the polling > process. How should I go about doing this? I read up on how to use the > clock frequency (24MHz for the Cypress EZ-USB NX2LP) to calculate how long=
> a machine cycles takes, and then I could use that to determine how many > cycles equate to 5 seconds but how would I account for the delay when > processing the "if pin is low" condition? > > Rough Pseudocode : > > For 5 seconds > { > =EF=BF=BD while (port pin is high) > =EF=BF=BD =EF=BF=BD track machine cycle count to make sure 5 secs has not =
passed.
> =EF=BF=BD set a flag to indicate Pin has gone low > =EF=BF=BD continue checking Port Pin > > } > > I don't think using a "time-delay" routine would work as I need to > continuously poll the pin. Is that accurate? What about using a timer > without invoking an interrupt? > > Oh, I am using C for this project. I hope I was able to convey my > questions adequately. > > Thanks in advance for any help.
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.

The 2024 Embedded Online Conference