sbis PIND, 2

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

Hi Guys, I have been looking around for a simple translation from 

this:

 

sbis   PIND, 2

 

or

 

sbic   PIND, 2           

 

to C code.

 

I can't seem to find it.

 

Can anyone help?

 

Thanks in advance.

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

if ((PIND & (1<<PD2)) == 0)
{
}

You can figure out the other one.

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

newbie123 wrote:
I can't seem to find it.

Well, a particular C compiler may or may not ever generate a particular machine instruction.

 

What have you done so far?  What does

newbie123 wrote:
sbis PIND, 2
mean?

...so bit 2 of PINC, right?

 

How about

if (PINC & (1<<PINC2))
    {
        ...
    }

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

The first one will skip the next instruction if PIND bit 2 is set the next will skip if the bit is clear.

 

something like

if (PIND & (1<<2))    // Test if bit set
    {
        your function    
    }

 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Too many cooks.......

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

And all implemented the sbic (since you are executing the code within braces on bit set)...smiley

David (aka frog_jr)

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

Look again!

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

Sorry Kartman, missed the == 0...blush

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
if ((PIND & (1<<PD2)) == 0)

Probably Mr. Johan would go with:

if (!(PIND & (1<<PD2)))

 

Last Edited: Sat. Feb 10, 2018 - 01:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if (!(PIND & (1<<PD2)))

or how 'bout:  if !(PIND & (1<<PD2)) 

 

looking for the equalities (==0 or ==1 ) is probably more natural to think about initially...of course, then the error becomes using  = instead of ==

 

 

When in the dark remember-the future looks brighter than ever.

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

if !(PIND & (1<<PD2)) 

You sure about that?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

It's all Greek...I mean C to me...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

You sure about that?

 

good call!

 

arrrg...sadly, C requires those extra parens 

 

 

When in the dark remember-the future looks brighter than ever.

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

Surely it's just...

 

Quote:

if (PIND.2) {

    ;

}

 

like this...

 

Quote:

0000 0011     if (PORTD.2) {

000064 9992      SBIC 0x12,2

0000 0012         a = 1;

000065 e011      LDI  R17,LOW(1)

0000 0013     }

"This forum helps those that help themselves."

"If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

 

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

Just so OP isn't confused, you only get to use PIND.2 if you own Codevision. While a very nice feature (previously seen in other micros like 8051 compilers) it breaks the rules of C as anything beyond '.' in that syntax should name a struct member and they follow the usual C naming convention that requires symbol names to start with a non-digit. The best you can achieve in standard C is a syntax like:

if (Pind.b2) ...

 

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

Probably Mr. Johan would go with:

if (!(PIND & (1<<PD2)))

...and the other variations.  Remember what I mentioned:

 

theusch wrote:
a particular C compiler may or may not ever generate a particular machine instruction.

 

Your toolchain may or may not generate SBIC with the ==0 or the !(...) or even the .b2 variations.  [even applies to the "usual" (PINx&(1<<n)) variant]

 

OP's query sounds a bit ingenuous at first reading, but indeed if you haven't been around AVR for a goodly stretch there is some magic in all but the .n syntax in the compilers recognizing the (1<<n) bit mask and turning it into the bit number needed for SBIS/SBIC.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Just to confirm that for ".b2" syntax avr-gcc will generate the " tight" opcodes. (I've got a .h generator that kind of relies on this)

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

clawson wrote:
Just to confirm that for ".b2" syntax avr-gcc will generate the " tight" opcodes. (I've got a .h generator that kind of relies on this)

Again, OP asked for a C fragment, right?  And I stand behind my ongoing "it depends on the toolchain and the version and the phase of the moon and the recognizable/transformable construct" and similar.

 

But OP has disappeared and hasn't come back to expand on the context of why this might be important.  Or even useful, or pertinent.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

clawson wrote:
Just to confirm that for ".b2" syntax avr-gcc will generate the " tight" opcodes.

Well, I had to try a couple experiments.  A recent CV version, and GCC from Studio 6.2.  I might well need to relent about "not all variants might generate SBIS/SBIC" as these modern versions picked up on the variations I threw at them.

 

But -- check out the GCC results for (PIND & 4) ...

                 	.CSEG
                 _main:
                 ;.FSTART _main
                 ;0000 0004 unsigned char scratch;
                 ;0000 0005 
                 ;0000 0006     scratch = 0;
                 ;	scratch -> R17
00003f e010      	LDI  R17,LOW(0)
                 ;0000 0007 
                 ;0000 0008    if (PIND & 4)    scratch++;
000040 994a      	SBIC 0x9,2
000041 5f1f      	SUBI R17,-1
                 ;0000 0009    if (PIND & (1<<2)) scratch++;
000042 994a      	SBIC 0x9,2
000043 5f1f      	SUBI R17,-1
                 ;0000 000A    if (PIND.2) scratch++;
000044 994a      	SBIC 0x9,2
000045 5f1f      	SUBI R17,-1
                 ;0000 000B 
                 ;0000 000C    if ((PIND & (1<<2)) != 0) scratch++;
000046 994a      	SBIC 0x9,2
000047 5f1f      	SUBI R17,-1
                 ;0000 000D    if (!(PIND & (1<<2))) scratch++;
000048 9b4a      	SBIS 0x9,2
000049 5f1f      	SUBI R17,-1
                 ;0000 000E 
                 ;0000 000F    PORTB = scratch;
00004a b915      	OUT  0x5,R17
                 ;0000 0010 
                 ;0000 0011  while (1);
0000009e <main>:

	unsigned char scratch;

	scratch = 0;
	
	if (PIND & 4)    scratch++;
  9e:	89 b1       	in	r24, 0x09	; 9
  a0:	82 fb       	bst	r24, 2
  a2:	88 27       	eor	r24, r24
  a4:	80 f9       	bld	r24, 0
	if (PIND & (1<<2)) scratch++;
  a6:	4a 99       	sbic	0x09, 2	; 9
  a8:	8f 5f       	subi	r24, 0xFF	; 255
//	if (PIND.2) scratch++;
	
	if ((PIND & (1<<2)) != 0) scratch++;
  aa:	4a 99       	sbic	0x09, 2	; 9
  ac:	8f 5f       	subi	r24, 0xFF	; 255
	if (!(PIND & (1<<2))) scratch++;
  ae:	4a 9b       	sbis	0x09, 2	; 9
  b0:	8f 5f       	subi	r24, 0xFF	; 255
	
	PORTB = scratch;
  b2:	85 b9       	out	0x05, r24	; 5
  b4:	ff cf       	rjmp	.-2      	; 0xb4 <main+0x16>

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.