Also, I found microchip.com describes sbi command as:
Sets a specified bit in an I/O register.
So, I am confused that why this sketch will generate square wave that is why there are times when pin 13 output LOW while this command in loop() tell pin 13 to be HIGH all the time? Also, another question is from the oscilloscope I noticed that the duty of the PWM seems to be 50%, is that really this case in theory?
PINB is the input register for port B. Normally you wouldn't write to it you would read from it. But the input registers on the AVR chips have a special feature. It is documented in the datasheet but I cant look up the exact reference page. If you write to the PIN register for any port it toggles the appropriate pin. It's a hardware feature.
Delta_G:
PINB is the input register for port B. Normally you wouldn't write to it you would read from it. But the input registers on the AVR chips have a special feature. It is documented in the datasheet but I cant look up the exact reference page. If you write to the PIN register for any port it toggles the appropriate pin. It's a hardware feature.
In my understanding, you mean sbi command will change the state of a pin but not set the state to be HIGH by "it toggles the appropriate pin". Is this correct? (If so, the square wave output makes sense.) But why both the book and microchip.com use the verb "set" to describe this command? Does "set" means "toggle" in this context?
No, the SBI instruction does indeed set a bit high. But, when the destination is the PIN register, which is normally used to read the port, each time the SBI instruction writes a one, the output state of that port bit toggles.
the input registers on the AVR chips have a special feature.
"Some" AVR chips...
Data sheet Section 14.1 ("I/O Ports"/"Overview"):
Three I/O memory address locations are allocated for each port, one each for the Data Register – PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx. The Port Input Pins I/O location is read only, while the Data Register and the Data Direction Register are read/write. However, writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register. In addition, the Pull-up Disable...
Page_David:
In my understanding, you mean sbi command will change the state of a pin but not set the state to be HIGH by "it toggles the appropriate pin". Is this correct? (If so, the square wave output makes sense.) But why both the book and microchip.com use the verb "set" to describe this command? Does "set" means "toggle" in this context?
sbi does write a 1 to that bit. In this case, writing a 1 tot that bit makes the pin toggle. If you use sbi on the PORT register instead of the PIN register then it will work like you expect and only ever set the pin HIGH. Because when you set a bit in the PORT register it sets the pin HIGH, but when you set a bit in the PIN register it toggles the pin. It's a hardware feature.