Shop OBEX P1 Docs P2 Docs Learn Events
SPI Tutorial_I'm Missing Something — Parallax Forums

SPI Tutorial_I'm Missing Something

briank77479briank77479 Posts: 36
edited 2014-06-19 11:44 in Learn with BlocklyProp
I'm going through the SPI tutorial and typed in the code for the Bit Mask exercise to read the Z-Axis, see below. I thought I checked to make sure I typed in the code correctly, but evidently I didn't. When I run the code all I get is a blank screen; no errors though.
I've run other code to ensure the device is connected correctly and checked that the IDE was configured to my Activityboard. All that appears correct.
I'm sure I'm overlooking something simple but my eyes have glazed over. Would very much appreciate another pair of eyes looking at the code I typed to see what I missed.

Thank you Parallax for these "C" tutorials. They are extremely helpful!!!

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-06-16 11:33
    Brain,

    I haven't used C on the Propeller much myself so I doubt I'll be much help but IMO you'll be more likely to get help if you provide a link to the tutorial you're asking about. I myself am often willing to take a look at code but I'm too lazy to search for a tutorial.
  • GenetixGenetix Posts: 1,749
    edited 2014-06-16 11:54
    You changed the pin numbers from 6-8 to 8-10. Double-check your wiring.
  • jazzedjazzed Posts: 11,803
    edited 2014-06-16 12:09
  • briank77479briank77479 Posts: 36
    edited 2014-06-17 10:22
    This is one of those scratch the head and wonder issues. I retyped the code, checked the IDE configuration, and ran it.
    Worked without issues. Funny thing is I don't see what I did differently to get it to work.
    Completed the tutorial successfully.

    Tkank you for all the input.
    Brian
  • GenetixGenetix Posts: 1,749
    edited 2014-06-17 12:14
    I noticed that your code didn't have the comments, so do you understand what this program does?

    The only gripe I have with the tutorial is the explanation of manipulating bits isn't clearly explained.

    A bit can be set, or changed to 1, by ORing it with a 1.
    Example:
    Set bit 2 of an 8-bit value ---> OR with 0000-0100
    result = data | 0x04;

    A bit can be reset, or changed to 0, by ANDing it with 0.
    Example:
    Reset bit 4 of an 8-bit value ---> AND with 1110-1111
    result = data & 0xEF;

    Bits can also be masked out, or all the un-needed bit can be gotten rid of, by ANDing with 1.
    Example:
    Mask bit 3 of an 8-bit value ---> AND with 0000-1000
    result = data & 0x08;
  • Ray0665Ray0665 Posts: 231
    edited 2014-06-18 05:56
    Bits can also be inverted by using XOR

    eg inverting bit 2
    011 XOR with 010 yields 001
    001 XOR with 010 yields 011
  • c07Brian.Kesterc07Brian.Kester Posts: 36
    edited 2014-06-18 07:52
    Ray0665 wrote: »
    Bits can also be inverted by using XOR
    I just want to make sure I'm understanding what you're saying if you do an "A XOR B," it will invert every bit on A corresponding to a 1 bit on B?
  • Ray0665Ray0665 Posts: 231
    edited 2014-06-18 09:55
    AND - OR - XOR and NOT are bit wise operations so you are dealing only with corresponding bits regardless of word size
    that being said you should look at the truth table for each operation to understand the operation

    the truth table for XOR can be found here
    http://en.wikipedia.org/wiki/Exclusive_or

    and yes as you said " if you do an "A XOR B," it will invert every bit on A corresponding to a 1 bit on B"

  • kwinnkwinn Posts: 8,697
    edited 2014-06-18 16:51
    One of the interesting and occasionally useful things you can do with XOR is to swap two bytes/words/longs/arrays without using temporary storage.

    A xor B
    B xor A
    A xor B

    The value in variable A will now be in B, and value in B is now in A.
  • Ray0665Ray0665 Posts: 231
    edited 2014-06-19 11:44
    And while we are diverting into uses for the XOR and friends
    When interfacing with hardware I generally include an AND/OR and XOR around I/O operations.
    The AND allows masking unwanted bits, An OR allows forcing bits on and the XOR allows polarity changes.
    I do this because we all know how "tight and accurate" (sarcasm) interface specs are. So rather than having to
    rework logic because of a mismatch I can correct it at the I/O point.
Sign In or Register to comment.