Pages: [1]   Go Down
Author Topic: SPCR values into string  (Read 1292 times)
0 Members and 1 Guest are viewing this topic.
Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I get the SPCR values to be displayed on the screen just fine with the command below, but I couldn't copy them into a string variable (that should possibly also contain leading zero(s)).

   Serial.print(SPCR, BIN);

Kind of feel dumb to ask - your advice is certainly appreciated.
Logged

Dig-IT-all

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

May be it's better to formulate my question in a different way.

We know that all SPI settings are determined by the Arduino SPI Control Register (SPCR).

The question is:

How can I read out values for each of the specifc bits and store each of them into a different variable?

In SPI these bits are:
SPIE, SPE, DORD, MSTR, CPOL, CPHA,  SPR1, SPR0
I'd like the values to displayed on screen and then be put into string-variables named like:
v_SPIE, v_SPE, v_DORD, v_MSTR, v_CPOL, v_CPHA,  v-SPR1, v_SPR0.

Displaying the values of SPCR on screen is not a problem, except the leading zero's are omitted. The command is Serial.print(SPCR,BIN);
How can I avoid leading zero's?
How can I read each bit separately?

Help and advice is appreciated.
Thanks.
Logged

Dig-IT-all

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26313
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (SPCR & 0x80) Serial.print ("SPIE");
if (SPCR & 0x40) Serial.print ("SPE");

?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks AWOL,
but that 's conditional (if), I don't know the values, I need to read whatever is there (1) and then transform to a string (2).
Logged

Dig-IT-all

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26313
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sorry, I don't understand the question.
If you don't provide the names, who does?

maybe look up the stringify operator, ##.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code is for SPE and SPIE.
I am also seeking the values for all other bits.
I mean the values 0x80 or 0x40 are conditional, aren't they?
But then that's true too, because they're either 1 or 0 for each bit.
So I guess I have to query for the two possible states for each bit, right?
Logged

Dig-IT-all

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26313
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Your code is for SPE and SPIE.
I am also seeking the values for all other bits.
Quote
So I guess I have to query for the two possible states for each bit, right?

That's right - you didn't expect me to write it all for you, did you?  smiley-grin
That was just an example.

Another more general way of doing it would be to put the strings into an array of eight, then you could use the bit number as a subscript, and either print or store the strings as you want.
« Last Edit: July 30, 2009, 06:39:42 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it, thanks.

That array sounds tempting.

So the array of 8 would look like this:

0:00010000
1:00100000
2:01000000
3:10000000
4:00001000
5:00000100
6:00000010
7:00000001

Logged

Dig-IT-all

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26313
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was thinking more like:  (uncompiled, untested)
Code:
const char* SPCR_names [] = {"SPR0", "SPR1", "CPHA", "CPOL",
                                               "MSTR", "DORD", "SPE", "SPIE"};

void printSPCR ()
{
   byte SPCRVal = SPCR;  //snapshot
   for (int i = 7; i >= 0; --i) {
       if ((SPCRVal >> i) & 1) {
          Serial.print (SPCR_names [i]);
          // maybe print a space or comma here?
       }
   }
}


(for extra credits, use PROGMEM for the strings)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot works great.

This is what I did in the meantime producing the same result:

Code:

   Serial.println("Current SPI SPCR Register Bit Values:");
   Serial.println("Check this:");  
   if (SPCR & 0x80) Serial.println ("SPIE = 1");
   if (SPCR & 0x40) Serial.println ("SPE = 1");
   if (SPCR & 0x20) Serial.println ("DORD = 1");
   if (SPCR & 0x10) Serial.println ("MSTR = 1");
   if (SPCR & 0x8) Serial.println ("CPOL = 1");  
   if (SPCR & 0x4) Serial.println ("CPHA = 1");
   if (SPCR & 0x2) Serial.println ("SPR1 = 1");  
   if (SPCR & 0x1) Serial.println ("SPR0 = 1");

Logged

Dig-IT-all

Pages: [1]   Go Up
Jump to: