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?
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?
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;
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.
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 |= ?
Please see Tip #1 in my signature, below, for how to properly post source code:
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.
thanks alot for the amazing answer
but I think this new AVR "feathure" really confuse the newbie like me...
Newer AVRs have a feature that writing the input register causes a pin toggle.
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?
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?
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
REALLY new AVRs (atmega4809, attiny402) have separate “toggle” registers.
REALLY new AVRs (atmega4809, attiny402) have separate “toggle” registers.
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?
Do they retain the facility to toggle by writing to the PIN register, or is that gone?
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
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
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.
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?
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!
EDIT 2
And, if it was in a video, didn't that video explain about writing to PIN ?
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
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.
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.
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.
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?
>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-