How to use array variable for shiftout command


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838

    Default How to use array variable for shiftout command

    Hi guys ,
    can someone show me some code on how to set a different length of bytes in to the string for the Shiftout command

    the device on the SPI needs LSB to MSB , the number of bytes changes depending on the command sent and then the following amount of data bytes for that command

    I only want to have 1 write command routine for all SPI writes and reads

    was thinking something like this but cant seem to get the code right in my head to allow for the command to have changing number of bytes lengths in routine

    the sample code here will only just add values of data_out(x) which is not going to work

    need to have [data_out(0) , data_out(1) , data_out(2), etc ] format for the command to work correctly

    cheers


    Code:
    ' ------ Rx Address for Pipe 0 ------
    data_out[0]=Write_reg+Rx_addr_p0 'Rx address for pipe0
    data_out[1]= $4E ' "N" '5 byte address
    data_out[2]= $45 ' "E"
    data_out[3]= $54 ' "T"
    data_out[4]= $01 ' "1"
    data_out[5]= $04  '"4"
    num_byte=5
    gosub SPI_write
    
    
    SPI_write: 
       CSN = 0                                                           ' Chip enable 
        For  x = 0 to Num_byte                                     '   loop for # byte
           temp_data_out = data_out(x) + temp_data_out   
        next x   
      Shiftout SI, SCK, MSBFIRST, [temp_data_out]  ' Send write enable command
      CS = 1               ' Disable to execute command
    return

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


    Did you find this post helpful? Yes | No

    Default Re: How to use array variable for shiftout command

    Hi,
    You need to put the SHIFTOUT statement inside the FOR-NEXT loop. Also, I'm not sure about adding temp_data_out, is that something specific to your needs? Finally there's something weird going on with the chip select line, you pull CSN low but set CS high. I'd try something like:
    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
        next x   
    
      CS = 1               ' Disable to execute command
    return

  3. #3
    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

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    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.

  5. #5
    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

    thanks for the info , as i did not know how to include a comma in that way

    perhaps its me who is not too clear on the shiftout command and how it works , but the way i read the command from the help is that items within the brackets [ ], separated by a comma are the bytes to be sent synchronously ,

    eg - shiftout , datapin , clockpin ,mode, [ byte(0) , byte(1), byte(2)]

    the number of bytes can be as many as required , but each byte hex value MUST have a comma separating them ,
    the waveform produced would generate the clock and bits data values for the number of the bytes shown

    after each byte processed within the command , will hold the clock pin low period 2 times longer than each clock tick width , then process the next byte , till all bytes are sent

    the waveform would have no breaks in time for the total of each bytes sent per command , register byte , data bytes
    i am not sure if that is important to other chips on the SPI bus as i thought it may be for it is looking for command , register , number of bytes in sync

    as such you would need to get the data you want to send as one complete string and then execute the command once.

    now if i loop the command such as

    for x to number_bytes
    shiftout , datapin , clockpin ,mode, [ data_out(x)]

    next x


    the resulting waveform will have a longer time between each byte processed as only 1 byte is sent per the command

    it is in effect sending command byte , then next loop , register byte , then next loop data byte(0) , etc

    if that is not a problem for any device on a SPI then all i need do is loop untill the number of bytes to be sent


    but i think you will see the differences in the resulting waveform between the 2 methods

    admittedly as write the explanation, i also solve the problem sometimes as well ,

    so will loop and see how it goes i think

    cheers

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: How to use array variable for shiftout command

    Hi,
    Yes, there will be a slight delay between bytes when using the loop method but since SPI is a synchronous bus I don't think it'll be a problem in real life. With that said I HAVE run into a device which had a minimum SPI clock frequency. In that particular case SHIFTOUT was too slow all together but if it had been just on the limit then something like this loop method MIGHT have messed with it. You'd need to scope the output to determine the actual delay and compare that to the datasheet of the device you're interfacing. I'd venture a bet that it'll work just fine.

    OK, got it regarding the commas, you didn't mean to actually send them out, you're just talking about the syntax of the SHIFTOUT command, thanks.

    /Henrik.

  7. #7
    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

    this device on the spi on this devices has true bidirectional data going out on SI , when a write , but returns at the same time on SO ( going back in ) the status register is there a way to get this information while i do a write and use it
    i would think the shiftout command would have to have a parameter to get this if it could.

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: How to use array variable for shiftout command

    No, I'm afraid you can't do that with SHIFTOUT/SHIFTIN. If you're PIC has a MSSP module I suggest you take a look at that, it'll do what you want. If it doesn't have a MSSP module then you'd have to roll your own bit-banged routine to do the simultainous out/in. Which PIC are you using and what device is you're interfacing?

  9. #9
    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

    the pic is 12F683 , does not have a specific SPI interface , i was thinking of using pulsein command to capture the data returned on the SO pin during the SI ?

    at the moment it looks like to get a status reg read i need to send a nop , and the get the result which is not a big issue

  10. #10
    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

    there are 2 devices on the spi , which are eeprom , NRF24L01 , the NRF24L01 is the only device with true bidirectional interface

Members who have read this thread : 1

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