BV usage

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to find a way to just change one bit. So lets say I have PORTB as my output and I want to just change bit 3 on portb how do i use the BV command? Lets say portb.3 = 0, and I want to say do something like portb.3 = 1. This is how its done in codevision, now how do i do it using winavr? thansk guys. I could also use a good example, or text on the usage of bv if anyone knows of one.

Nothin's ever easy. But the hard part makes it all worth while.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I told minutes ago:
Have a look inside the manual. If you installed WinAVR the normal way its right on your desktop. Look at 'Special function registers'

And have a look at the thread:
https://www.avrfreaks.net/phpBB2/...

regards
Ulrich

best regards

Ulrich
-------------------------->>> connecting to car mains means connecting to the power supply from hell....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i looked through that whole thread and there's nothing in there that talks about BV. Talks about other ways to deal with single bits but not bv usage. Where's the manual? i didn't see a liunk to it. thanks.

Nothin's ever easy. But the hard part makes it all worth while.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
The easiest way to set or clear a bit in any of the IO registers of AVR is to use cbi and sbi calls. Here is the sample code

cbi (PORTA,3) clear PORTA bit 3 (0)
sbi (PORTA,3) set PORTA bit 3 (1)

Hope this helps

Parthasaradhi Nayani

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh wow, hey thanks a mil for that. I've looked high and low for that and couldn't figure it out.. thanks dude

Nothin's ever easy. But the hard part makes it all worth while.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The sbi and cbi macros are deprecated.

Please use the standard C constructs:

REG |= (1 << bitnr);
REG &= ~(1 << bitnr);

instead. (If you look carefully, you'll notice that the cbi and sbi
macros expand to exactly this.)

If you want, you can use

_BV(bitnr)

instead of

(1 << bitnr)

but that's an avr-libc extension.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

If you wish to get CodeVision approach like PORT.3, look at:

https://www.avrfreaks.net/phpBB2/...

Regards,
Alex

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Jorg,
I found the CBI and SBI macros to be very helpful. Is there any special reason to deprecate these macros?

Thanks.

Parthasaradhi Nayani

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yep: they make people blindly assume they would issue the
corresponding SBI/CBI instructions automatically -- which they don't,
they're actually nothing more than the quoted C constructs, wrapped in
a macro.

Not having them perhaps makes developers think a bit more about their
code. ;-) Sure, there might be situations where you want to have them,
but I've seen enough pointless code like

sbi(FOO, 2);
sbi(FOO, 3);
cbi(FOO, 5);

where, with a little bit of thinking,

FOO = _BV(2) | _BV(3);

would have been much more appropriate.

Note that, given an optimization level of at least 1, and provided the
arguments fit (i. e. the IO port is in the allowable range, and the
bit number is a compile-time constant), the quoted C constructs will
actually be translated into SBI/CBI instructions, regardless of
whether you hide them in a macro or not. ;-)

The main issue is that the sbi()/cbi() macros used to translate
into exactly the respective instructions via inline asm (including all
problems, i. e. only a limited range of operands allowed) in the past.
We want to make people aware that this is no longer the case.

Finally, all of Atmel's code examples use the direct assignment form
for IO ports, so we just follow that habit.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.