The Button

at the soft side

We played with the LED, let's play with the button, it's pretty easy. You can keep our previous code and just change the main routine with the following one. Read it first and think, run it first in your mind and then in your micro. What do you expect to see after Reset?


int main(void)
{
    u8 btnState;

    InitPorts();
    LEDOFF;

    while (forever)
    {
        // button pin reading
        btnState = ~PINB & (1<<3);
       
        // action
        if (btnState)
        {
            LEDON; Delay(5000);
            LEDOFF; Delay(5000);
        }
    }
}
 

I told you, it is easy. The only difference is that you read PINB, instead of writting to PORTB. Understand the logic behind the button pin reading. Could this command reside outside the loop?

Last thing, change the above code to make the LED change state on button release. I challenge you.

 
 
victor@avrtutor.com