This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

using if statement

Hi all...
i need some small help and also an idea on how to write the code for the following condition.
i want something like:
i want to give a delay of 5 seconds and just want to check if the any key is presses from keypad within this 5 seconds. if it is pressed then go for GREEN LED or else if any key is not pressed within this 5 seconds they go for RED LED.
can anybody please give me an idea on how can i do it....

  • I am using keil to program the 89S52 microcontroller..

  • Lots of possibilities.

    Start a timer.
    Poll keyboard in a loop until a key is found or the loop runs out of time.

    Or loop x times or until a key is pressed - and keep a small delay in the loop so you know how many iterations you can manage during 5 seconds.

    Or start a timer and use an interrupt to handle the key press.
    Wait until the interrupt handler has trigged and set a flag or the timer runs out of time.

    Note that 5 seconds is longer than you can normally handle with a single delay - but if you configure a timer for 10ms delay than 500 timer delays will represent 5 seconds. So you just poll the keyboard every 10 ms and if you have polled 500 times without a key press it's time for the red LED.

    But a question from me to you: Why are you asking on this forum? Couldn't you really figure out a solution yourself? Or at least figure out a solution but getting stuck with a much more specific question like how to "slow down" the 8051 timers to manage 5000ms delay? Spending some time thinking about solutions is one of the best ways to actually learn. Which will matter if you want your teacher to give you good grades. Which will matter if you later wants a good job and/or a good salary - real R&D work is all about finding reasonably good solutions to problems...