i question about SPI function?

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

can any one please give a full description if how this line works:

while ((SPSR & (1<<SPIF))==0);

does it shift the number "00000001" with respect of the amount of SPIF bit?
how it ANDs SPSR (8 bits while only 3 are used) with the rest of argumant? and how it generates only one bit as out put??

this whole matter really confuses me up :(

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

Read a generic C book instead of asking about any next operator which is new to you.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

each C guidance has given a different explain about these operators, i am really confused.

if you know any online texts containing thorough and true description avaiable for this please put the link here.

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

Quote:
each C guidance has given a different explain about these operators
I just don't believe you. Read this:

http://publications.gbdirect.co....

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

ok i read the related part of the ebook here:
http://publications.gbdirect.co....

these i already knew, but yet again i have not got any thing clears up the working of that line:
while ((SPSR & (1<<SPIF))==0);

could you be nice enough to just explain this line?

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

Quote:
could you be nice enough to just explain this line?

Good Luck, MBedder is not known to just answer a question, nor be nice either :)

I would answer it, but I don't really understand 'c'. Sorry

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

jgmdesign wrote:
Quote:
could you be nice enough to just explain this line?

Good Luck, MBedder is not known to just answer a question, nor be nice either :)

I would answer it, but I don't really understand 'c'. Sorry

Jim

----------------

yes i already have got that! no C book has ever explained such arguments.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
while ((SPSR & (1<<SPIF))==0);

SPSR is a register - a hardware "variable" that maps to a portion of the AVR's on-chip SPI peripheral. When read, it will return 8 bits of data with the meanings given in the datasheet.

The "&" operator is a bit-wise AND. There's a tutorial in the Tutorials forums that explains bitwise operators, but the relevant portion of it here is that "X & Y" will return a set of bits which are set in both "X" and "Y", with all other bits being cleared to 0. This bitwise ANDing operation is commonly referred to as a "bitmask" operation.

The (1<<SPIF) section creates a "mask" (8-bit value in this case) with the SPIF bit set and all others cleared. The "<<" left shift operator moves the left operand value the right operand number of bits left. The SPIF constant is a bit number as given in the datasheet. By shifting the constant "1" to the left SPIF number of times, you end up with a binary value with the a single bit set at the position indicated by the value of SPIF. For example, if SPIF was equal to the value of 2, you would end up with the binary value of 0b00000100.

When you put these two concepts together, you end up with the code masking the current value of the SPSR register with a mask of the SPIF bit, resulting in the value 0 if the SPIF bit was not set in the SPSR register, or the SPIF mask if it was set. The "== 0" checks this and converts this into a binary 0 or 1, depending on the value of the SPIF bit.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

No one mentioned Bit Manipulation 101 (in the tutorial forum) so far?

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

ok, i think i have understood this, the number 00000001 is shifted left as long as its 1 matchs the SPIF's position in SPSR, and then gets ANDed with the SPIF itself (equivallantly positioned bit on SPSR to the position of the bit 1), then the AND is only comparing the bit 1 with the bit SPIF and as long as the output is 0 the program does not pass to the next line, it might be a codevision trick possibly because the SPSR register could not be addressed directly (?????), well then if the compiler knows the variable SPFR then why it has not been writen as:

while (SPIF==0);
?????????
this matter is turning to be somehow mind blowing :(

Attachment(s): 

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

You clearly haven't read the link I gave. Report back when you have.

Bottom line: because of SBR, SBI, CBI, CBR, SBIC, SBIS Atmel chose to define control bits as bit number rather than bit mask. The 1<< construct converts a bit number to a bit mask (it's far more difficult to convert a bit mask back into a bit number!). This conversion is done at compile/assembly time and leads to no runtime code overhead.

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

Looks like today is a real idiots' parade on AVR Freaks :(

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

alright, that says nothing new, the bit 1 is shifted to the position of the bit number and the out put is a bit mask, now how is that two bytes are being ANDed and the output is a bit which is needed to decide?

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

Quote:

now how is that two bytes are being ANDed and the output is a bit which is needed to decide?

Haven't the first idea what you are asking here?!? But surely this is obvious? You do understand the difference between a bit number (0,1,2,3,4,5,6,7) and a bit mask (0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80) and what must be done to convert a bit number to a bit mask? I take it you also understand the logical operators OR, AND and XOR? After that what more is there to not understand? It's hardly rocket science is it? In fact it's so basic that if you cannot understand it I wonder if embedded programming is the right hobby/vocation?

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

i believe you dont get what i mean, consider this:

0x03&0x06
what is the out put? yes it is
0x02
yes as you might have noticed it is still a byte, now the while sequence needs a single bit (0 or 1) to decide. not a byte.

well after al perhaps i am forgetting some concepts, you know i had no tutors and also have been away from the deal for a long time, i was studying meckanics because i am interested in making industrial robotic arms!

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

Hopefully none of us will ever have a close contact with any of your robots :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

Quote:

0x03&0x06
what is the out put? yes it is
0x02

Not sure what point you are making. That result is either 0 when the bit being tested isn't set or it's non-zero when the bit is set. So any of the following tests is perfectly valid (given that C conditionals treat 0=false and anything else as true):

if ((0x03 & 0x06) == 0) {
  // do this when tested bit is not set
}
if (0x03 & 0x06) {
  // do this when tested bit is set
}
if (!(0x03 & 0x06)) {
  // do this when tested bit is not set
}
if ((0x03 & 0x06) == 0x02) {
  // do this when tested bit is set
}

What would not be valid is:

if ((0x03 & 0x06) == 1) {
  // do this when tested bit is set
}

BTW 0x06 as a bit mask in this is a bad example. That is testing two bits. Usually that bit mask would be one of 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 or 0x80 so only a single bit is tested.

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

the point is that a byte cant be only 0 or 1, its a byte, what do you mean of "the tested bit"??

see if i write
while (0x03&0x06);

it is like to write:
while (0x02==1);

which does not mean any thing!

and its exactly what happens here:
while ((SPSR & (1<<SPIF))==0);

:(

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

Idiot parade is approaching the main street :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

ok i understood the mater, it is a bad thing to stay away from study and work!

thank you so much :)

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

Quote:

which does not mean any thing!

No. You are missing a point in the C language (that I mentioned above). The while() conditional can either be

while(true);

or

while(false);

false == 0

any other value is "true". So:

while (0x03 & 0x06);

is the same as:

while(true);

If that 0x03 were just 0x01 then it would be:

while(0x01 & 0x06);

which is the same as

while(false);

So now consider:

while ((SPSR & (1<<SPIF))==0); 

where SPIF=7. So either bit 7 in SPSR is set or it isn't. If the bit is set the condition says:

while ((0b1??????? & (0x80))==0); 

which is:

while ((0x80)==0); 

which is false so execution continues after this while() condition. On the other hand if SPIF is not set in SPSR then it says:

while ((0b0??????? & (0x80))==0); 

which is

while ((0x00)==0); 

which is true so it sticks in this while() condition until the bit does become set.

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

also i believe that russian guy has no information about the embedded programing and is here just to make some obsessive comments, it is a shame. i could not help but to say this.

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

thanks for your time and your help clawson.

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

Quote:
i believe that russian guy has no information about the embedded programing and is here just to make some obsessive comments, it is a shame.

No, I just call an idiot an idiot without looking at the nationality first. Presuming my English is even worse than your Russian, I will repeat my previous statements in a simplified form: YOU ARE AN IDIOT.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

To add a bit of clarity here, with respect to the subject line....

This operation is NOT unique to the SPSR. This construct is used with MANY registers, including PIN registers, USART status register, timer status registers, and more. Thus, it is important to NOT get locked in to thinking that this is unique to working with the SPSR.

Second important thing is that (1<<SPIF) is >>JUST<< a number! It is determined at the time the program is compiled rather than as the program executes. Thus, it is equivalent to

if (SPSR & a_number == 0)

The construct (1<<bitref) is simply a way to improve readability. Why? Few readers will even remember what bit position SPIF represents. They WILL remember its function and how it operates, but not its bit position, Further, the bit position might not be the same in all AVRs. So, this also improves portability.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net