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

C51: Getting to work volatile variable

Hello,
I have beginners problem with getting to work volatile variable.
Trying to get reusable function for debouncing and detecting key_up state for all declared keys but can't get it to work as expected.
Situation is as follows:

declaration:

sbit key1=P3^7;
sbit key2=P3^6;
bit key_up(volatile bit key);

usage:

       if (!key_up(key2))
        {
            green=~green;
        }

        if (!key_up(key1))
        {
        ... do something...
        {

function:

bit key_up(volatile bit key)
{
    if (key==0)                 // check if key is pressed
    {
        delay1ms(20);           // debounce
        if (key==0)             // really pressed
        {
            while(key==0)       // hold until pressed
            {
                ;
            }
            // key is released
            delay1ms(20);       // debounce when key released
            if (key!=0)         // check if still released
            {
                key=1;          // reset key
                return 0;       // return key is up
            }
        }
    }
    return 1;   // key was not pressed at all
}

Program compiles with no errors but problem is that variable 'key' is not rechecked inside a function and I think it should be if it is declared 'volatile'.
Please help to get my function working.

Parents
  • This still don't work as expected.

    That's because you're expecting volatile to work some kind of magic that it never did, and hopefully never will.

    A volatile variable is still a variable, it's not a magic tunnel to the actually volatile object (a port pin) whose value you assigned into it at some point. For what you're trying to do here, volatile is of no use at all. Forget about volatile for now.

    You need to actually read the port bits explicitly, every time you want to know their status. Yes, that means you have to do your switch()/case block every simple time you do that, to decide which pin bit to read.

    So: break the reading of a single button port pin out of the function that handles debouncing, into a function of its own, that does nothing else. Call that function every time you need a pin state.

Reply
  • This still don't work as expected.

    That's because you're expecting volatile to work some kind of magic that it never did, and hopefully never will.

    A volatile variable is still a variable, it's not a magic tunnel to the actually volatile object (a port pin) whose value you assigned into it at some point. For what you're trying to do here, volatile is of no use at all. Forget about volatile for now.

    You need to actually read the port bits explicitly, every time you want to know their status. Yes, that means you have to do your switch()/case block every simple time you do that, to decide which pin bit to read.

    So: break the reading of a single button port pin out of the function that handles debouncing, into a function of its own, that does nothing else. Call that function every time you need a pin state.

Children
No data