execution program after button clik

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys,

I have question about reading a button and then execution program.

When i work witth I/O i need 3 registers. (DDR,PIN and PORT).

 

int main()

{

        DDRA=0b00000000; //set as Input

        PORTA=0b11111111; //pull up, logical 1 is on the port

 

        if(PINA == 0b00000000)

        {

            some code;

        }

        while(1)

}

 

This condition doesn't work.

When i pressed button i connection ground with pin and i have logical 0.

 

So if someone was so nice and show me how corretly read.

This topic has a solution.

New

Last Edited: Tue. Mar 12, 2019 - 09:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

Welcome to Freaks, well in general you need to state which microcontroller you are using, but broadly speaking, I think you should check this basic programming tutorial and read more about bit manipulation:

 

https://www.avrfreaks.net/forum/...

 

 

Last Edited: Tue. Mar 12, 2019 - 09:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

EvB 4.3 board with ATmega644P, thank you i will check it.

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How is it this way?

 

int main()
{
    DDRA=0b00000000; //set as Input
    PORTA=0b11111111; //pull up, logical 1 is on the port

    while(1)
    {
        if(PINA == 0b00000000)
        {
            some code;
        }
    }
}

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR_Castieloo wrote:
This condition doesn't work.
That condition requires all 8 bits of A to be pulled low at the same time! Seems unlikely? Perhaps try something more like:

int main()
{
    DDRA &= 011111011; //set as Input
    PORTA = 000000100; //pull up, logical 1 is on the port

    while(1) {
        if(!(PINA & 0b00000100))
        {
            some code;
        }
    }
}

In this example I chose to use just bit 2 of port A. I set bit 2 of DDRA low to make sure it's an input (though it defaults that way anyway). I used &= so as to not touch the other bits.

 

I then set bit 2 in the PORTA register so that one pin has a pull-up applied

 

I moved the test code into the while(1) loop so it's not just checked once at power on but is repeatedly checked all the time

 

I read PINA and then AND the result with 0b00000100. If the input signal is high (it will be as long as no button over-rides the pull-up) then PINA & 0b00000100 will be 0b00000100 which as a conditional value is "true". But I want to execute the code when that is "false" so I then use the ! (NOT) operator to invert true to false, false to true.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I tried it this way, but it didn't work. After pressing the button, the program should start writing the text to the display. When I leave the data dump on a display without a button it works, but when I add that I want the program to respond to the button nothing happens

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you sir

New

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Moe123 wrote:

 

 read more about bit manipulation:

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't just take my code "as is" but work through it and understand exactly WHY it works. To help I think you must read the link you were given in #2

 

I personally would suggest that using 0bnnnnnnn notation is about the worst possible way to do this. If I'd been writing this myself I would have used:

int main()
{
    DDRA &= ~(1 << PA2); //set as Input
    PORTA |= (1 << PA2); //pull up, logical 1 is on the port

    while(1) {
        if(!(PINA & (1 << PA2)))
        {
            some code;
        }
    }
}

(note the use of ~ (NOT) in that too!)

 

That is much easier to read (in my opinion) in that it is immediately obvious it is bit 2 that is being used. Some folks don't like having to keep typing (1 << n) so you may prefer (if using avr-gcc);

int main()
{
    DDRA &= ~_BV(2); //set as Input
    PORTA |= _BV(2); //pull up, logical 1 is on the port

    while(1) {
        if(!(PINA & _BV(2)))
        {
            some code;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I thanked you for explaining the issue and that you showed me where I was making a mistake . Again than you Sir.

New