Setting single bit on PORT toggles multiple bits on PIN

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

Hi,

 

This is regarding the mega2560 and I cannot figure out this strange behavior. If relevant, I'm using an Arduino Mega2560 and Atmel Studio 7.0. I am not using any Arduino libraries.

 

It's regarding this line:

 

PORTL |= (1 << PL3);

 

I'm debugging this with serial communication.

Immediately before this line, PORTL contains 00000000 and PINL contains 00000000.

Immediately after this line, PORTL contains 00001000 and PINL contains 00001110.

As I understand, PINL should be 00001000 after that line, not 00001110. Toggling a single PORT bit should change only a single PIN bit right?

 

I've also tried debugging this with Atmel ICE. Toggling a single PORTL bit through I/O View also sometimes toggles multiple PINL bits. I'm also not using any of the alternate functions of PORTL (no Timer 5 in the rest of my code, no Timer 4 Input Capture either). 

 

I don't want to show the rest of the code because it's really long. But what could cause this kind of behavior?

 

Thanks!

This topic has a solution.
Last Edited: Thu. May 24, 2018 - 06:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What do you have connected to those pins? Are they floating? Any shorts between pins?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

There is nothing connected to those pins. I just have the board with only a usb cable plugged to the Arduino's serial outlet.

 

 

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

So the PORTL (or any other port)  pins are floating they can pick up nearby charges and can assume any value as well as possibly suck a lot of current.

 

Put a load on those pins (10K? to ground) and see what happens.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Toggling a single PORT bit should change only a single PIN bit right?

That is not really a good question.   Pin is a read register* that always reads the state of the physical pin (which could be shorted to gnd or Vcc)...if IO direction is set as an output, then the value set in port register is the intended output value (which can also be verified by reading port).  So if the  IO is shorted reading PIN (actual/transient value) may disagree with reading PORT (assumed/intended value).

 

Normally if an IO is an output we never read PIN, just set PORT & assume the desired level is present.  So the PORT value (assumed value) is the working value.  PIN changes may lag due to heavy capacitive loads or just fast timing in general..  So relying on PIN  (for other than true inputs) can cause issues.  Some chips (such as early PICS) suffered from read-modify-write difficulties, since they lacked the equivalent PORT register.

 

 

*also, as an extra feature :Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn.

When in the dark remember-the future looks brighter than ever.

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

If nothing is connected to the pins then the value of the PIN register at any time is undetermined.

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

If nothing is connected to the pins then the value of the PIN register at any time is undetermined.

 Hmmm...what about the internal pullup? surprise  Perhaps that is considered being connected to something.   I'm often too determined to determine if something is undetermined. 

When in the dark remember-the future looks brighter than ever.

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

 Hmmm...what about the internal pullup? surprise  Perhaps that is considered being connected to something. 

It is, but only if the code enables the internal pull-up.

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

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

js wrote:

So the PORTL (or any other port)  pins are floating they can pick up nearby charges and can assume any value as well as possibly suck a lot of current.

 

Put a load on those pins (10K? to ground) and see what happens.

 

You nailed it. Connecting the input pin to something fixed everything and PINL2 no longer reads HIGH (1) where it shouldn't.

 

avrcandies wrote:

Toggling a single PORT bit should change only a single PIN bit right?

That is not really a good question.   Pin is a read register* that always reads the state of the physical pin (which could be shorted to gnd or Vcc)...if IO direction is set as an output, then the value set in port register is the intended output value (which can also be verified by reading port).  So if the  IO is shorted reading PIN (actual/transient value) may disagree with reading PORT (assumed/intended value).

 

Normally if an IO is an output we never read PIN, just set PORT & assume the desired level is present.  So the PORT value (assumed value) is the working value.  PIN changes may lag due to heavy capacitive loads or just fast timing in general..  So relying on PIN  (for other than true inputs) can cause issues.  Some chips (such as early PICS) suffered from read-modify-write difficulties, since they lacked the equivalent PORT register.

 

 

*also, as an extra feature :Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn.

 

You're completely right, it's a weird question. I should have mentioned the reason why I'm so fixated on PINL is because I need to check for an input on PINL2 after switching on PORTL3. 

 

avrcandies wrote:

If nothing is connected to the pins then the value of the PIN register at any time is undetermined.

 Hmmm...what about the internal pullup? surprise  Perhaps that is considered being connected to something.   I'm often too determined to determine if something is undetermined. 

 

I also tried enabling pullup on PINL2 and then toggling PORTL3 but that actually flipped on even more PINL bits where it shouldn't have (this is with floating pins).

 

So problem solved. Lesson learned for me: don't have floating pins and try to use PIN values. Thanks everyone! You guys are awesome!

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

Please mark the solution - see Tip #5

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...