Shop Learn
SPI code conversion — Parallax Forums

SPI code conversion

Hello everyone, I am using a shift register (74HC595) and I have written some code that is working perfect on the BS2, but I cant seem to figure out how to do the same on the propeller in C. the BS2 code shifts out 8 bits each being its own variable so I can change any one of them at any time without changing the rest.

Out_595:
SHIFTOUT SerData, Clock, MSBFIRST, [op7\1, op6\1, op5\1, op4\1, op3\1, op2\1, op1\1, op0\1]
PULSOUT Latch, 5
RETURN

I am able to communicate with the 595 on the propeller perfectly with the code below. but I have been unable to make it so I can simply change the value of variables like the code above. any help is greatly appreciated thanks.

shift_out(21, 22, MSBFIRST, 8, 0b00000000);




Comments

  • DavidZemonDavidZemon Posts: 2,937
    edited 2016-07-27 14:56
    Using an array for this shift_out function would be highly inefficient. But possible. You'll need to write a wrapper function because, as you might have noticed, shift_out does not accept an array. So, I encourage you to simply use the format you posted: shift_out(21, 22, MSBFIRST, 8, 0b00000000);

    But here ya go:
    // C++ version
    unsigned int flatten (const bool bits[], const unsigned int numberOfBits) {
      unsigned int result = 0;
      for (unsigned int i = 0; i < numberOfBits; ++i) {
        result |= bits[i] << (numberOfBits - i);
      }
      return result;
    }
    
    // C version
    unsigned int flatten (const int bits[], const unsigned int numberOfBits) {
      unsigned int result = 0;
      for (unsigned int i = 0; i < numberOfBits; ++i) {
        if (bits[i]) {
          result |= 1 << (numberOfBits - i);
        }
      }
      return result;
    }
    
    shift_out(21, 22, MSBFIRST, 8, flatten({0, 0, 0, 0, 1, 1, 1, 1}, 8));
    
  • That's disappointing but after all my attempts i kinda figured it didn't work with a simple array, thank you for your help. each of those bits correspond to the on/off state of 8 different LED's and moters, turning them on or rotating forward/reverse. so my problem was if the first bit was on "1" and I needed to change the last bit then I would need separate 8 bit combination . and a separate 8 bits for every possible combination of 1's and 0's.
  • ElectrodudeElectrodude Posts: 1,440
    edited 2016-07-27 14:37
    Why the separate C and C++ versions? And is making a function to do that actually easier than just using regular old bitfield operations?

    Also, you forgot closing right parentheses after both of your for loops before the left curly brackets.
  • Why the separate C and C++ versions? And is making a function to do that actually easier than just using regular old bitfield operations?

    I like the C++ version because of the bool type. Makes the code a bit shorter. But he asked for C, so I thought it would be nice to include both.
    Also, you forgot closing right parentheses after both of your for loops before the left curly brackets.

    Thanks for noticing. I'll get that fixed.
  • bnikkel wrote: »
    That's disappointing but after all my attempts i kinda figured it didn't work with a simple array, thank you for your help. each of those bits correspond to the on/off state of 8 different LED's and moters, turning them on or rotating forward/reverse. so my problem was if the first bit was on "1" and I needed to change the last bit then I would need separate 8 bit combination . and a separate 8 bits for every possible combination of 1's and 0's.

    Certainly understandable. When we have folks talking about running JavaScript on the Propeller, and using 64-bit floats for everything, I would say a little array for some bits isn't too big of a deal :P. Simply be aware of the performance issues and, if you run into problems (speed or code size), you'll know one good place to look for optimization opportunities.
  • Thanks again for all your help David, greatly appreciated
  • No problem. Happy coding!
  • just wanted to update this. i revisited an old problem and have figured out a solution to it. being able to update variables independently and then shift_out to the 595. seems easy now but 5 years ago it was beyond me.

    `/*
    74HC595 com test
    */

    include "simpletools.h" // Include simpletools lib

    long newvalue;

    int a = 1;//1;
    int b = 0;//2;
    int c = 0;//4;
    int d = 0;//8;
    int e = 0;//16;
    int f = 0;//32;
    int g = 0;//64;
    int h = 0;//128;

    int i = 1;//256
    int j = 1;//512;
    int k = 0;//1024;
    int l = 0;//2048;
    int m = 0;//4096;
    int n = 0;//8192;
    int o = 0;//16384;
    int p = 1;//32768;

    int main() // Main function
    {

               newvalue = ((int)a << 15);   // 
               newvalue += ((int)b << 14);
               newvalue += ((int)c << 13);
               newvalue += ((int)d << 12);
               newvalue += ((int)e << 11);  
               newvalue += ((int)f << 10);
               newvalue += ((int)g << 9);                
               newvalue += ((int)h << 8);   
               newvalue += ((int)i << 7);   //   
               newvalue += ((int)j << 6);
               newvalue += ((int)k << 5);
               newvalue += ((int)l << 4);
               newvalue += ((int)m << 3);  
               newvalue += ((int)n << 2);
               newvalue += ((int)o << 1);                
               newvalue += (int)p;                  
    

    shift_out(0, 1, LSBFIRST, 16, newvalue); // Value for MCTL register
    pulse_out(6, 1);

     print("value  %b\n", newvalue);           // Display array element & value 
    

    pause(500);

    }`

Sign In or Register to comment.