Go Down

Topic: Control 40 LED using five TPIC6C595 (Read 212 times) previous topic - next topic

Gennady

Jul 21, 2016, 01:56 pm Last Edit: Jul 21, 2016, 07:12 pm by Gennady
Hi!
I need to control 40 LED using five TPIC6C595 in the chain.
Can I record via the SPI once all 40 bits using SPI.transfer (buffer, size)?

Code: [Select]


#include <SPI.h>
#define LE 10

byte lightLed[] = {
  B11110111,
  B11111011,
  B11111101,
  B11111110,
  B11101111,
};

void setup()
{
  pinMode(LE, OUTPUT);
  SPI.begin();
}


void loop()
{
  digitalWrite (LE, LOW);
  SPI.transfer (lightLed[], 5);
  digitalWrite (LE, HIGH);
}



And one more.  I can identify each element in the lightLed[] size is 40 bits (or 64 bits) and transfer by SPI.transfer all 40 (64) bits like one element (word)?

Array size  is 35 (number_of_row= 0.....34) and  each element by 40bit (or 64bit) .

SPI.transfer (lightLed[number_of_row]);

Thanks!


CrossRoads

Yes, send one burst of bytes via SPI .transfer() calls to the shift registers. I don't know about (buffer, size) but this works.
I  have done as many as 45 bytes.
Code: [Select]

digitalWrite (ssPin, LOW);
for (x=0; x<5; x=x+1){
SPI.transfer(dataArray[x]);
}
digitalWrite (ssPin, HIGH); // outputs change on this rising edge

Have the bytes in an array, and manipulate each byte as needed.
Examples:
dataArray[18] = dataArray[18] & 0b11111011; // clears bit 2 at that location
dataArray[16] = dataArray[16] | 0b01000000; // clears bit 6 at that location
dataArray[12] = Serial.read(); change to a value read from serial port
dataArray[10] = dataArray[14]// change value to the same as another byte
dataArray[12] = 0x27; // change to a fixed value
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Longest data element Arduino supports is a unsigned long, 32 bits. You have to pick off 8 bits at a time to send out via SPI (internal hardware limitation).  Easier to just have a 5 byte array.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

PaulRB

#3
Jul 21, 2016, 09:55 pm Last Edit: Jul 21, 2016, 10:59 pm by PaulRB
@CrossRoads, unsigned long long is supported which is 64 bits. Unfortunately, bitRead(), bitWrite() etc. don't work for 64 bit variables, but the technique you suggested should be OK.

To set bit p you can use
Code: [Select]
unsigned long long x = 0b00001111000011110000111100001111000111000011110000111100001111;
x |= 1ULL << p;

You can also use this to treat an unsigned long long as an array of bytes
Code: [Select]
union MatrixData {
  unsigned long long l;
  byte b[8];
};

Gennady

#4
Jul 22, 2016, 07:58 am Last Edit: Jul 22, 2016, 08:18 am by Gennady
@CrossRoads, unsigned long long is supported which is 64 bits. Unfortunately, bitRead(), bitWrite() etc. don't work for 64 bit variables, but the technique you suggested should be OK.

To set bit p you can use
Code: [Select]
unsigned long long x = 0b00001111000011110000111100001111000111000011110000111100001111;
x |= 1ULL << p;

You can also use this to treat an unsigned long long as an array of bytes
Code: [Select]
union MatrixData {
  unsigned long long l;
  byte b[8];
};

I tried, but only prints zeros:



0
я0
0
0
0
0
0
0
0
0
0

My test:

Code: [Select]

unsigned long long x;
byte b[8];

void setup() {
  Serial.begin(9600);

}

void loop()
{
  x = 0b1111111111111111111111111111111111111111111111111111111111111111;

    union MatrixData {  //cut 64 bit word into  8x8 bit  word and put in array b[]
    unsigned long long x;
    byte b[8];
  };
  for (int i = 0; i < 8; i = i + 1)
  {
    Serial.println (b[i], BIN);  //print 64 bit word
  }

}

I not found "union" in any Arduino reference.
Any mistake?
Thanks!

Gennady

Yes, send one burst of bytes via SPI .transfer() calls to the shift registers. I don't know about (buffer, size) but this works.
I  have done as many as 45 bytes.
Code: [Select]

digitalWrite (ssPin, LOW);
for (x=0; x<5; x=x+1){
SPI.transfer(dataArray[x]);
}
digitalWrite (ssPin, HIGH); // outputs change on this rising edge

Have the bytes in an array, and manipulate each byte as needed.
Examples:
dataArray[18] = dataArray[18] & 0b11111011; // clears bit 2 at that location
dataArray[16] = dataArray[16] | 0b01000000; // clears bit 6 at that location
dataArray[12] = Serial.read(); change to a value read from serial port
dataArray[10] = dataArray[14]// change value to the same as another byte
dataArray[12] = 0x27; // change to a fixed value

Hi!
Thanks for Your answer!

But I would like to be manipulated with a 64 bit (or 40 bit) word, and then cut it into 8 bit pieces (put in  byte dataArray [])  for send it through to SPI.transfer as You suggest. But as I have suggested above, could not

PaulRB

#6
Jul 22, 2016, 09:52 am Last Edit: Jul 22, 2016, 11:28 am by PaulRB
No, like this:
Code: [Select]

union MatrixData {  //cut 64 bit word into  8x8 bit  word and put in array b[]
  unsigned long long x;
  byte b[8];
};

MatrixData l;

void setup() {
  Serial.begin(9600);

}

void loop()
{
  l.x = 0b1111111111111111111111111111111111111111111111111111111111111111;

  for (int i = 0; i < 8; i = i + 1)
  {
    Serial.println (l.b[i], BIN);  //print 64 bit word
  }

}


You do not have to call the union type "MatrixData", you can call it something else that makes more sense for your sketch.

Read this about union types.

You are correct about SPI.transfer(buffer,size). Here is the page.

Gennady

#7
Jul 22, 2016, 12:27 pm Last Edit: Jul 22, 2016, 02:25 pm by Gennady
No, like this:
Code: [Select]

union MatrixData {  //cut 64 bit word into  8x8 bit  word and put in array b[]
  unsigned long long x;
  byte b[8];
};

MatrixData l;

void setup() {
  Serial.begin(9600);

}

void loop()
{
  l.x = 0b1111111111111111111111111111111111111111111111111111111111111111;

  for (int i = 0; i < 8; i = i + 1)
  {
    Serial.println (l.b[i], BIN);  //print 64 bit word
  }

}


You do not have to call the union type "MatrixData", you can call it something else that makes more sense for your sketch.

Read this about union types.

You are correct about SPI.transfer(buffer,size). Here is the page.
Very interesting information about union. Thanks!

But there is a little problem with printing

Code: [Select]


union MatrixData {  //cut 64 bit word into  8x8 bit  word and put in array b[]
  unsigned long long x;
  byte b[8];
};

MatrixData l;

void setup() {
  Serial.begin(9600);
}

void loop()
{
  l.x = 0b1111111111111111111111111111111111111111111111111111111111111111;
 
  for (int i = 0; i < 8; i = i + 1)
  {
    Serial.print (l.b[i], BIN);  //print 64 bit word
  }
Serial.println ("\n");
Serial.println ("STOP");
Serial.println ("*****************");
}
 



Prints all 64 "1". Its O.K.!

1111111111111111111111111111111111111111111111111111111111111111

STOP
*****************



But if put bitwise NOT operator:

l.x = 0b1111111111111111111111111111111111111111111111111111111111111111;
l.x = ~ l.x;

console print only 8 "0" (not 64  "0"):



00000000

STOP
*****************

Please tell me why?

Also about SPI.transfer(buffer, size) -it is not clear how to use it, for example for the transfer of five bytes  by one command SPI.transfer:

SPI.transfer(buffer, size)
Parameters
buffer: the array of data to be transferred

or something similar:
UDP.write(buffer, size);
buffer: an array to send as a series of bytes (byte or char)
size: the length of the buffer

Maybe

byte buffer[]={byte1, byte2, byte3, byte4, byte5 };
SPI.transfer(buffer[], 5);

PaulRB

When you use Serial.print(x, BIN), it suppress all leading zeroes, so 0b00000000 is printed as 0 and 0b00001010 is printed as 1010.

I agree that the instructions on how to use SPI.transfer(buffer,size) are not very clear, but I think your guess is correct.



Gennady

When you use Serial.print(x, BIN), it suppress all leading zeroes, so 0b00000000 is printed as 0 and 0b00001010 is printed as 1010.

I agree that the instructions on how to use SPI.transfer(buffer,size) are not very clear, but I think your guess is correct.



Thanks!
I will check on hardware.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy