Society of Robots - Robot Forum

Software => Software => Topic started by: vidam on August 23, 2008, 08:00:09 PM

Title: sbi or cbi?
Post 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?
Title: Re: sbi or cbi?
Post by: izua on August 24, 2008, 06:49:41 AM
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
Title: Re: sbi or cbi?
Post by: Admin on September 04, 2008, 07:24:05 PM
Quote
the avrgcc user manual recommends against using them, favouring bit operators
how come?
Title: Re: sbi or cbi?
Post by: scottad73 on July 30, 2010, 09:58:40 AM
the link that izua posted doesn't work.
Title: Re: sbi or cbi?
Post by: Razor Concepts on July 30, 2010, 10:26:12 AM
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)
Title: Re: sbi or cbi?
Post by: scottad73 on August 02, 2010, 10:31:37 AM
Thanks for the link!
Title: Re: sbi or cbi?
Post by: Webbot on August 02, 2010, 03:11:57 PM
Quote
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

Title: Re: sbi or cbi?
Post by: madsci1016 on August 02, 2010, 03:34:27 PM
The avr-libc manual says

Quote
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.
Title: Re: sbi or cbi?
Post by: kupter on August 18, 2010, 10:34:12 AM
As I know SBI (set bit) use to give bit "1"
and CBi ( clear bit) use to set bit "0"
 usually use for intterupt..