Go Down

Topic: SPCR values into string (Read 1 time) previous topic - next topic

HansLoepfe

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.
Dig-IT-all

HansLoepfe

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 [font=Courier]Serial.print(SPCR,BIN);[/font]
How can I avoid leading zero's?
How can I read each bit separately?

Help and advice is appreciated.
Thanks.
Dig-IT-all

AWOL

Code: [Select]

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


?
"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.

HansLoepfe

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).
Dig-IT-all

AWOL

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

maybe look up the stringify operator, ##.
"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.

HansLoepfe

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?
Dig-IT-all

AWOL

#6
Jul 30, 2009, 01:37 pm Last Edit: Jul 30, 2009, 01:39 pm by AWOL Reason: 1
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?  :D
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.
"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.

HansLoepfe

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

Dig-IT-all

AWOL

I was thinking more like:  (uncompiled, untested)
Code: [Select]

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)
"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.

HansLoepfe

Thanks a lot works great.

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

Code: [Select]


  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");

Dig-IT-all

Go Up