Switzerland
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« on: July 28, 2009, 08:27:14 pm » |
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
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #1 on: July 30, 2009, 05:02:35 am » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: July 30, 2009, 05:08:55 am » |
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.
|
|
|
|
Switzerland
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #3 on: July 30, 2009, 05:19:13 am » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: July 30, 2009, 05:42:19 am » |
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.
|
|
|
|
Switzerland
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #5 on: July 30, 2009, 06:33:09 am » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: July 30, 2009, 06:37:28 am » |
Your code is for SPE and SPIE. I am also seeking the values for all other bits. 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?  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.
|
|
|
|
Switzerland
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #7 on: July 30, 2009, 06:56:59 am » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: July 30, 2009, 07:08:22 am » |
I was thinking more like: (uncompiled, untested) 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.
|
|
|
|
Switzerland
Offline
Newbie
Karma: 0
Posts: 35
Arduino rocks
|
 |
« Reply #9 on: July 30, 2009, 08:13:07 am » |
Thanks a lot works great. This is what I did in the meantime producing the same result: 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
|
|
|
|
|