Search code examples
carraysmicrochippic18

Sending an 8-byte array to SPI


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 !


Solution

  • 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;