go_away

Author Topic: sbi or cbi?  (Read 4574 times)

0 Members and 1 Guest are viewing this topic.

Offline vidamTopic starter

  • Supreme Robot
  • *****
  • Posts: 423
  • Helpful? 1
  • Robotronics.org
sbi or cbi?
« on: August 23, 2008, 08:00:09 PM »
Please tell me how these commands work?

   sbi(TCCR2A,PWM11);
        cbi(TCCR2A,PWM10);

sbi and cbi?

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
Re: sbi or cbi?
« Reply #1 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 for more info on this matter
« Last Edit: September 06, 2008, 12:08:26 AM by izua »
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,652
  • Helpful? 169
    • Society of Robots
Re: sbi or cbi?
« Reply #2 on: September 04, 2008, 07:24:05 PM »
Quote
the avrgcc user manual recommends against using them, favouring bit operators
how come?

Offline scottad73

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
Re: sbi or cbi?
« Reply #3 on: July 30, 2010, 09:58:40 AM »
the link that izua posted doesn't work.

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
Re: sbi or cbi?
« Reply #4 on: July 30, 2010, 10:26:12 AM »

Offline scottad73

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
Re: sbi or cbi?
« Reply #5 on: August 02, 2010, 10:31:37 AM »
Thanks for the link!

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: sbi or cbi?
« Reply #6 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

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
Re: sbi or cbi?
« Reply #7 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.
« Last Edit: August 02, 2010, 03:41:24 PM by madsci1016 »

Offline kupter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: sbi or cbi?
« Reply #8 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..

 


Get Your Ad Here