Shop OBEX P1 Docs P2 Docs Learn Events
SPI bit adressing question — Parallax Forums

SPI bit adressing question

I'm trying to learn how to control the individual bits in the 24 bit stream.

I am using this chip

http://cache.nxp.com/files/analog/doc/data_sheet/MC33996.pdf?pspll=1

Basically is and 16 output on/off switch controlled by SPI.

So my question is how do in configure the output message to control The individual bits to turn them on and off?

Hope i explained that correctly.

Thanks John

Comments

  • JasonDorieJasonDorie Posts: 1,930
    edited 2016-11-11 22:06
    The short answer is this:
      value := 0
    
      'turn on a bit
      value |= (1 << BitNumber)
    
      'turn off a bit
      value &= !(1 << BitNumber)
    

    The longer answer is that you'll need to build up the 24 bit value as either 3 bytes, or a single 32 bit value, then send it to the device via SPI. Doing that means setting the "Chip Select" line to the device, then for each bit, you set the MOSI pin to the bit you're sending, then toggle the SCLK pin to tell the device to read the bit. According to the spec you send the most significant bit (bit 23) first, and the least significant bit (bit 0) last. The code would look something like this:
      'set the right Prop pins as outputs
      dira[CSPin] := 1
      dira[MOSIPin] := 1
      dira[SCLKPin] := 1
    
      'Set bits 16 to 23 to zero (command to turn on/off)
      'Set the "switch" bits to enable the first 8, disable the next 8 outputs
      value := (0<<16) | ($00FF)
    
      outa[CSPin] := 0   'enable data transfer to the chip by setting chip select pin low
    
      repeat bit from 0 to 23
        'set the bit to output
        outa[MOSIPin] := (value >> (23-bit)) & 1
    
        'toggle the clock pin
        outa[SCLKPin] := 1
        outa[SCLKPin] := 0
    
      outa[CSPin] := 1   'apply the register change you just sent by setting CS high
    

    That's the rough idea - I have no idea if this will compile, but it should get you started.

    (edited to correct syntax for bitwise NOT operation in Spin)
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2016-11-11 19:16
    You are just simulating outputs so just assign a long that represents that output even though only 24-bits are active. Then just do your normal equivalents of ORs/ANDNs/XORs/NOTS/SHIFTS or bit operations using a simple function but decide whether you want to update the outputs immediately or on a timed basis. The immediate is probably your best bet so after any operation you would load the long and transmit 24-bits of it serially clocked using the chip enable. You can do this totally in Spin which although slow is probably good enough.

    BTW, the Spin operator to use to clear the bits involves a NOT operation before ANDing, so that is actually a ! and not a ~
  • Thanks
Sign In or Register to comment.