Society of Robots - Robot Forum
Software => Software => Topic started by: vidam on August 23, 2008, 08:00:09 PM
-
Please tell me how these commands work?
sbi(TCCR2A,PWM11);
cbi(TCCR2A,PWM10);
sbi and cbi?
-
sbi sets a bit in a register, cbi clears a bit.
the avrgcc user manual recommends against using them, favouring bit operators
sbi (a,b) becomes a |= b
cbi (c,d) becomes c &=~d
check out my tutorial (http://www.izua.ro/contents/Bit_operators) for more info on this matter
-
the avrgcc user manual recommends against using them, favouring bit operators
how come?
-
the link that izua posted doesn't work.
-
Here is one
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=37871 (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=37871)
-
Thanks for the link!
-
the avrgcc user manual recommends against using them, favouring bit operators
how come?
I think the confusion comes about because if you, ages ago..., wrote:
PORTD |= (1<<4);
Then the C compiler would generate 3 instructions:
1. Read PORTD
2. Set the bit
3. Write the answer to PORTD
But the AVR is capable of doing it one instruction which is actually called 'sbi' or 'cbi' to Set or 'Clear' a bit.
Hence I believe the original sbi and cbi macros were written to generate the single instruction by temporarily flicking into assembly language.
However: the C compiler has now been optimised so that rather than generating the 3 instructions above it only generates the one. So now the cbi and sbi macros are sort of defunct. I say 'sort of' in that they used to make your program smaller/better but now they don't make any difference.
ie
sbi(PORTD, 4);
and
PORTD |= (1<<4);
will actually generate the same code.
So they no longer effect the program - but I believe that they are more 'human readable' - so I continue to use them
-
The avr-libc manual says
i.e.: sbi(PORTB, PB1); is now PORTB |= _BV(PB1);
This actually is more flexible than having sbi directly, as the optimizer will use a hardware sbi if appropriate, or a read/or/write operation if not appropriate. You do not need to keep track of which registers sbi/cbi will operate on.
so in theory the new way could be less code, if the optimizer feels like it. Unless i'm reading that wrong.
-
As I know SBI (set bit) use to give bit "1"
and CBi ( clear bit) use to set bit "0"
usually use for intterupt..