How to use array variable for shiftout command


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: How to use array variable for shiftout command

    Hi hendric , thanks for the input ,
    temp_data_out is a throw away variable , i just used it to give this example

    the example you show, would only write one byte at a time for each spi write loop and the chip on the SPI requires that the format be - command byte , register byte , number of bytes ,

    The number of bytes changes depending on the command and register being used , which can be from min of 3 bytes upto 35 bytes per SPI write string


    I have to some sample code for achip that had a SPI built in to one that does not.


    shiftout command structure , requires that each byte , to be sent has a "," after each byte , so i need a way to join the bytes into a string for the shiftout command to work

    normaly i would code using a string varable to create the string or even just write the data as a string to start with where possible

    the string length would vary on the number of bytes to send

    since no string variable exists for PICbasic , i can not see how to structure the data as one string for the shiftout command to work when i have a varable amount of bytes to send as one write command

    love to see some code that can

    Cheers

    Sheldon

    spi_write:

    for x = 1 to number_bytes

    temp_data_out = (data_out(x) + "," + temp_data_out )

    next x

    CSN=0
    shiftout SI, SCK, MSBFIRST[ temp_data_out)
    CSN=1
    return







    byte0 s

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: How to use array variable for shiftout command

    Hi,
    I don't understand.... Your example will only send one byte (because the SHIFTOUT is outside of the FOR-NEXT loop), mine will send as many bytes as you set num_byte to (plus one). If you load your array with 12 values (0-11) and then set Num_byte to 11 before calling SPI_Write it will SHIFTOUT 12 bytes. It iterates thru the loop and copies the value from array pointed to by x into the temporary variable which it then uses to send that particular byte.

    Do you mean that you actually need to send a comma (ASCII 44) between each databyte? I don't think that's what you mean but IF so then perhaps something like this will work
    Code:
    SPI_write: 
       CS = 0                                                       ' Chip enable 
        For x = 0 to Num_byte                                         '   loop for # byte
          temp_data_out = data_out(x)  
          Shiftout SI, SCK, MSBFIRST, [temp_data_out]          ' Send byte
          
    
          ' Send a comma after the byte. EXCEPT for the last byte.
          If x < Num_byte THEN
             Shiftout SI, SCK, MSBFIRST, [","]           ' Or perhaps LSBFIRST here
          ENDIF    
    
    next x   
      CS = 1               ' Disable to execute command
    
    return
    Now, when Num_Byte is 5 and you GOSUB SPI_Write the CS line will go low, then it will send the first value in the array ( Data_out(0) ) followed by a comma. Then it will send Data_out(1) followed by a comma and so on. When it sends the last databyte ( Data_out(Num_byte) ) the x variable is no longer less than Num_Byte so this time it won't send the comma.

    But again, I have a feeling I'm missunderstanding...

    /Henrik.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts