ATmega32 PINA value.

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

Hello everybody

I'm using the following code:

#include <io.h>
void main(void)
{
DDRA=0x00;
PORTA=0x00;
DDRB=0xFF;
while (1)
    {
    // Please write your application code here
    PORTB=PINA;
    }
}

and the circuit shown in the attached '11.png' image, which depicts an open circuit on all PORTA pins.

According to the diagram shown in the attached '12.png' image, should PINA has the value "0" or unknown? I could understand that the answer must be unknown, but simulation results shows "0", I can't understand why it is "0".

And the value of PORTB will be "0"?

Attachment(s): 

Last Edited: Thu. Mar 28, 2019 - 11:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Surely the key thing is this:

 

 

Your circuit shows nothing connected to any of the A pins and in the code, after ensuring the port is input you set PORTA to 0 so that switches off any pull-ups. So the input value is completely undefined as the pins are "floating". They might float up to 1 or down to 0. So the value read will likely be random.

 

This is why you would usually use pull up or pull down resistors on any AVR input that does not have a signal driving the input. As it happens the AVR has built pull-ups available. So if you wrote 0xFF to PORTA you would switch them on then the pins would be guaranteed to read 1 so the whole port would be 0xFF.

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

Thanks Clawson,

I know the use of pull up resistor, I will ask the question in another way, when I use "if statement" I will need as an example:

if (PINA.0==0)
//
else if (PINA.0==1)
//
else // used for undefined state

and if PINA is undefined, why PORTB=PINA is giving "0b00000000"?

Thanks in advance.

Last Edited: Thu. Mar 28, 2019 - 11:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if (PINA.0==0)
//
else if (PINA.0==1)
//
else // used for undefined state

makes no sense whatsoever. A bit variable can only hold 0 or 1. There is no chance it can have any other state. So if the whole of PINA is reading 0b00000000 then this will unequivocally execute the 

if (PINA.0==0)
//
else if (PINA.0==1)
//
else // used for undefined state - can never possibly happen

branch.

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

Your simulator can not simulate a floating input, so given nothing is driving the port pin high, it tells you it is low!

Real H/W will be very different, as on real H/W pins do float and will have a random value that will change over time as the pin floats between high/low threshold value!

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

ki0bk wrote:
will have a random value
True that but still only 0 or 1, nothing else.

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

Thank you very much  guys.