what happened if write an input pin?

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

 

Hi, I am a MCU newbie, 

 

I learned from the official Atmel Studio Youtube channel how to "toggle" an output pin.

 

here is the code:

 

 

but if the PORTB5 has been defined as an output, how can the PINB |= (1<<PINB5) work to toggle it?

 

Last Edited: Mon. Oct 5, 2020 - 01:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

PINB is the "input register"; the output register would still be PORTB.
Newer AVRs have a feature that writing the input register causes a pin toggle.

For example, ATmega328 datasheet;

 

  1. 14.2.2  Toggling the Pin

    Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port.

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

doing a |= doesn't seem correct. Should be PINB = (1 << PORTB5); methinks

Otherwise with the |=, then current pin state is read. Ored with the bit you want then written to PINB, thus any port bits that were high will toggle.

 

Did the Atmel youtube vid tell you to do the |= ?

 

Last Edited: Mon. Oct 5, 2020 - 07:45 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please see Tip #1 in my signature, below, for how to properly post source code:

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

doing a |= doesn't seem correct.

You need to do "|= " to get the compiler to generate an SBI instruction (when it can.)

(I believe that there have been previous discussions about the unfortunate semantic incorrectness of this :-( )

Doing PINB = 0x55 will toggle 4 bits, though.

 

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

thanks alot for the amazing answer

 

but I think this new AVR "feathure" really confuse the newbie like me...

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

westfw wrote:
Newer AVRs have a feature that writing the input register causes a pin toggle.
"Newer" being the operative word there. This actually appeared when ATmega48/88/1268 first appeared in about 2005 and subsequent AVRs have the feature but this does mean some of the "old classics" like mega8,16,32,64,128 do NOT have this feature. So this really is going to depend on the age of your AVR design.

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


siwei.xia79@gmail.com wrote:
this new AVR "feathure" really confuse the newbie like me...

It seems quite clearly stated in the datasheet; eg,

 

 

Perhaps if you could describe where you find this confusing, we could better explain it?

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


well, as a "normal" person, writing a bit to a "ready-only" register is exactly the confusing linguistic...

 

p.s. is this feature only valid by the GPIO?  what happen to all the other "ready-only" registers?

 

 

 

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

Don't think of these Special Function Registers (SFRs) as just memory; they aren't - they are direct connections to hardware.

 

It is quite common for reading & writing at a particular SFR address to have different functions.

 

See: https://www.avrfreaks.net/commen...

 

EDIT

 

Related: https://www.avrfreaks.net/commen...

 

Note that this has nothing to do with Atmel Studio - the way the chip works is not affected by what IDE you use.

 

#SfrNotMemory

 

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...
Last Edited: Mon. Oct 5, 2020 - 09:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

REALLY new AVRs (atmega4809, attiny402) have separate “toggle” registers.

 

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

westfw wrote:
REALLY new AVRs (atmega4809, attiny402) have separate “toggle” registers.
Well actually that sates all the way back to the original Atxmega128A1 in 2006. It had TGL right back then. Everything that has subsequently appeared right up to AVR-0, AVR-1, AVR-Dx are just Xemage derivatives of that.

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

westfw wrote:
REALLY new AVRs (atmega4809, attiny402) have separate “toggle” registers.

https://www.avrfreaks.net/commen...

 

Do they retain the facility to toggle by writing to the PIN register, or is that gone?

 

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

awneil wrote:
Do they retain the facility to toggle by writing to the PIN register, or is that gone?
Someone asked that just recently. I think it was determined that PIN writes no longer work but then I think I read something else that said they did (think VPORTs when TGL may not be mapped but PIN is). So I think one would have to find that by inspection for a particular Xmega and port (ie whether PORT or VPORT).

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

siwei.xia79@gmail.com wrote:
but I think this new AVR "feathure" really confuse the newbie like me...

If you find it confusing, then don't use it, just do a read-modify-write to the port register, the new feature provides a faster, and more compact way of doing the same thing.

If you find that confusing, wait until you try sending and receiving using the USART's UDR register, where there is a read-only reg, and a write-only reg at the SAME address!

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

awneil wrote:

Please see Tip #1 in my signature, below, for how to properly post source code:

 

 

Meh,

While the screenshot is a little fuzzy, it is readable, and not complicated compared to some of the stuff thats posted.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"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"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

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

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

and, to be fair, the OP has taken the effort to crop it down to a sensible size.

 

But, in general, it's not a good way to post code because it means we can't copy/paste it - or parts of it - to comment, or to test it. And it doesn't work for larger amounts of code.

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

He said it was an excerpt from an Atmel Youtube video?!? Short of transcribing the text how could he paste anything but a picture extracted from one frame of the video?

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

Oh, I see - didn't get that at all.

 

EDIT

 

So even Atmel Microchip leave in the bit saying "insert your code here" after they have done the code!

 

frown

 

EDIT 2

 

And, if it was in a video, didn't that video explain about writing to PIN ?

 

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...
Last Edited: Mon. Oct 5, 2020 - 02:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


 

Its all right here in the logic of a typical port as shown in the datasheet:

The Blue circled circuit shows when the WPx goes high (write to pin reg) the bit mux switches from the bus input(0) to the inverter input(1) connected to the output D flipflop, this causes the FF to toggle state.

 

That part of the circuit is missing from older AVR's.

 

 

Jim

 

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Mon. Oct 5, 2020 - 03:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

westfw wrote:

doing a |= doesn't seem correct.

You need to do "|= " to get the compiler to generate an SBI instruction (when it can.)

(I believe that there have been previous discussions about the unfortunate semantic incorrectness of this :-( )

Doing PINB = 0x55 will toggle 4 bits, though.

In this case, not that it matters much,

an out instruction would be faster, at least on processors with a 2-cycle sbi.

 

Note that said discussions included what C allowed and what the compiler should do.

All agreed that the compiler issuing semantically different code at different optimization levels was a bad thing.

I suggested that the compiler should issue an informational message whenever |= or &= is used on a PINx register.

 

 

Iluvatar is the better part of Valar.

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

The (gcc) compiler treats  "|=" differently depending if the register is in I/O space or not .  

EDIT: code removed -- I remember SBI instructions be generated before.

Last Edited: Mon. Oct 5, 2020 - 03:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

siwei.xia79@gmail.com wrote:
well, as a "normal" person, writing a bit to a "ready-only" register is exactly the confusing linguistic...

As a normal person then, why would you write a 1 to clear an interrupt flag?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

>Do they retain the facility to toggle by writing to the PIN register, or is that gone?

 

You can. For the avr0/1, you have 4 ways to toggle-

 

https://godbolt.org/z/Gq9GdE