1

I am trying to set my SPI communication with a PIC18F452 from Microchip in order to send an 8-byte array to the bus (= transfer of all the bytes at once). Here is an example of what my array looks like :

 array   0x0080 byte[8]
array[0] 0x0080  0x01 
array[1] 0x0081  0x01 
array[2] 0x0082  '\0'
array[3] 0x0083  0x01 
array[4] 0x0084  0x01 
array[5] 0x0085  0x01 
array[6] 0x0086  0x01 
array[7] 0x0087  '\0'

The values are coming from an input signal (audio file, clock, etc.) and received on PORTDbits.RD0 to fulfill the array. The values are always 1 or 0 and nothing else.

First, I have started by sending one byte at the time with putcSPI(). As I fulfill the array, I send one byte to the SPI and the results are a match.

Then, I tried to send all the bytes at once, using putsSPI() like follow :

/// SEND DATA TO SPI
SPI_INT = 0;     // Enable SS 
putsSPI(array);
SPI_INT = 1;     // Disable SS
delay_ms();      // Delay

But the transfer of the frame is stopped when I encounter a 0 (considered as end of array, so that's normal). And, my frame is divided into chunks.

e.g, for the array displayed above, I have on the SPI bus "1 1" and then, the next values are from the frames that follow

That being said, I thought of converting the data from binary to hexadecimal (or integer) and then send it to SPI. I have tried several methods of conversion found here, but until now none has been successful.

Is there a solution to send directly the full array to the bus, or does anyone have an idea on how to perform the conversion in this particular case ?

Many thanks in advance !

talonmies
  • 70,661
  • 34
  • 192
  • 269
Daymov
  • 45
  • 9
  • 1
    Why would you send 8 bytes that can only contain one of two values each? You can trivially pack the values into a single byte. – EOF May 30 '16 at 13:19
  • Problem solved with a totally different method. Will post the solution I have used later – Daymov Aug 03 '16 at 10:30
  • If `putsSPI` expects a null-terminated string, it will obviously only send characters up to first null-character. Why wouldn't you simply send one byte at a time? An as @EOF already wrote, 8 bits is a single byte, are you sure you don't need to send only a single byte to the recipient? – vgru Aug 03 '16 at 12:15
  • @Groo thanks for your answer. As I said in my previous comment, I have solved my problem by restarting from scratch with new ideas – Daymov Aug 03 '16 at 12:21
  • @Tealyf: I saw it, but it's rather different from the question. From an 8 byte array you went to 2 bytes (16 bits), which are "modified using a counter" (weren't they being received from an input signal?). The reason your code wasn't working was because `putsSPI` stops at `\0`, and `WriteSPI` just sends the byte forward, all the other declarations and bit manipulation are irrelevant. – vgru Aug 03 '16 at 12:34
  • Yes, it's because of the '\0' I had decided to look for help. I was aware my code couldn't work the way it was. But sometimes it's good to start all over. :) – Daymov Aug 03 '16 at 12:35

1 Answers1

0

Eventually and in order to achieve my goal, I have completely changed my method to solve this problem.

I still use SPI for the communication. But instead of an array, I save the value of my input signal (after all the operations I apply to it) in a value. This value is later separate in two parts that will be send one byte one to the bus.

// Definition
unsigned int value = 0;
unsigned char value_byte1 = 0; 
unsigned char value_byte2 = 0; 
unsigned int onTheBus_byte1 = 0; 
unsigned int onTheBus_byte2 = 0;

// In main function, the value is modified using a counter (value++;)

// Transfer to SPI
value_byte1 = value >> 8;
value_byte2 = value & 0xFF;

SPI_INT = 0;  // Enable SS 
onTheBus_byte1 = WriteSPI(value_byte1);
onTheBus_byte2 = WriteSPI(value_byte2);
SPI_INT = 1;  // Disable SS

So, if I get :

value = 1505 (= 0x05E1)

in the main loop, then

value_byte1 = 0x05
value_byte2 = 0xE1

(This code has been tested on Proteus, and I do see both bytes on the SPI debugger when the value is written on the bus)

In case I need to use the value, say on another pic, I assemble the two parts after I have read them :

value = (value_byte1 << 8) | value_byte2; 
Daymov
  • 45
  • 9