Datasheet and register question


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Hi Christopher,

    OK, hang on a minute.... I may have spotted something here.
    Lets take a look at the SPI section of the MCP2515 datasheet again, section 12.5
    The Write instruction is started by lowering the CS pin. The Write instruction is then sent to the MCP2515 followed by the adress and then at least one byte of data.
    Now let's take a look at your configuration section again, with my comments in it.
    Code:
    LOW CS			' The Write instruction is started by lowering the CS pin                         
    SSPBUF = mcpwrt		' The Write instruction then sent to the MCP2515
    
    PIR1.3 = 0		' Wait for it to tranfer              
    WHILE PIR1.3=0 :WEND
    
    MCPREG =   CANCTRL	' followed by the adress
    DATAOUT =  $08		' and at least one byte of data.			
    
    GOSUB SEND_CAN_DATA
    So far so good, as far as I can see. Now lets continue where we left off in the datasheet:
    It is possible to write sequential registers by continuing to clock in data bytes, as long as CS is held low.
    Keyword here: sequential. Now back to your code again:
    Code:
    SSPBUF = cnf3                   ' CNF3 REGISTER
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    
    SSPBUF = $07                    ' PART OF BAUD RATE
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    
    SSPBUF = $BA                    ' PART OF BAUD RATE
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    
    SSPBUF = $03                    ' PART OF BAUDRATE
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    
    MCPREG =   CANCTRL              'PUT THE MCP2515 BACK INTO NORMAL MODE
    DATAOUT =  $0
    GOSUB SEND_CAN_DATA
    
    high cs
    If I understand this correctly the intention of your code is to write the value $07 to register CNF3 (at adress $28) but since you haven't deasserted the CS line the next byte transfered to the MCP2515 will automatically get written to next adress (right after CANCTRL in this case). So what actually happens is that the value $28 (which is the adress of CNF3) gets written to the register after CANCTRL (CANCTRL is at $0F so $28 will end up at adress $10) and then value $07 gets written to the register after that ($11).

    If you intend to write to registers which aren't sequential in the MCP2515 I think you need must deassert the CS line and start all over again: CS low, write instruction, adress, data.


    I'd try with the slowest SPI clock possible to begin with. Ie SSPM3:SSPM0 = 0010

    /Henrik.

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Success! .........sort of

    I've been over the sequential write portion of code more times than I can count and it may have something to do with the problem. I was just now able to succeed sending data over the CAN! While the information isn't correct, some of the numbers I'm putting in are coming up on the computer so that's a good thing.

    I think the sequential write/bit modify stuff has something to do with it. Where I'm expecting data (i.e. - the data for byte one (D0) in the data field), I'm seeing the address. Perhaps it is expecting just to run through the data and not have to address each variable when I load the buffer. That may be why I was overloading the number of bytes and crashed the Microchip interface. Instead of seeing 8 bytes of data, it was seeing 16.

    First I'm going to SAVE this code! Then I'll start to modify the code a little bit and work my way up to two-way communication with all of the little additions needed to have an error free CAN node. If it took me this long to get something going, I can't imagine how long it will take to write the full code

    But, at least I'm past step one, which hopefully was the hardest. Again, thank you so much for your help. I'll be back sooner rather than later to ask a million more questions.

    Chris

  3. #3
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Success! .........sort of

    Microchip does it like this:
    Code:
    SPILoadTX(CAN_LD_TXB0_ID, 13, DataArray); 
      
      //Configure TX BUFFER 1
      DataArray[0] =  0x22;
      DataArray[1] =  0x00;
      DataArray[2] =  0x64;
      DataArray[3] =  0x68;
      DataArray[4] =  0x02;
      DataArray[5] =  0x00;
      DataArray[6] =  0x11;
      DataArray[7] =  0x22;
      DataArray[8] =  0x33;
      DataArray[9] =  0x44;
      DataArray[10] = 0x55;
      DataArray[11] = 0x66;
      DataArray[12] = 0x77;

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Hi,
    For transfering the actual data to be sent and setting up the packet lenght etc I'd probably try something like this:
    Code:
    CAN_DATA VAR BYTE[8]
    CAN_LENGTH VAR BYTE
    CAN_ID_H VAR BYTE
    CAN_ID_L VAR BYTE
    i VAR BYTE
    
    ' Prepare a message
    ArrayWrite CAN_DATA[$11, $22, $33, $44, $55, $66, $77, $88]  ' Actual data, 8 bytes
    CAN_LENGTH = 8   ' 8 because that's how many bytes we want to transfer
    CAN_ID_H = 0
    CAN_ID_L = 0
    
    GOSUB Transfer_CAN_Data
    
    END
    
    '----Subrotuine to set the packet length, Standard identifiers, load the data to be sent from array and tell MCP2515 to send it when it can (CAN).
    Tranfer_CAN_Data:
    
    	LOW CS					' Select device
    
    	SSPBUF = MCPWRT : PAUSE 1		' Send write command
    	SSPBUF = TXB0SIDL : PAUSE 1		' Select TXB0SIDL register.
    	SSPBUF = CAN_ID_L : PAUSE 1		' Send low byte of standard identifier	
    
    	' Next register is te TXB0SIDH so we can continue spitting out bytes.
    	SSPBUF = CAN_ID_H : PAUSE 1		' Send high byte of standard identifier
    
    	HIGH CS					' Deselect device to end write operation
    	PAUSE 10
    	LOW CS					' Select device
    
    	' Now set the Transmit bufferdata length register
    	SSPBUF = MCPWRT : PAUSE 1		' Send write command
    	SSPBUF = TXB0DLC : PAUSE 1		' Point at register
    	SSPBUF = CAN_LENGTH : PAUSE 1		' Send length of packet
    
    
    
    	' The first databyte register in the TX buffer is located right after the message
    	' length register so we can use the sequential write feature and just keep going
    	' with the actual data.	
    
    	FOR i = 0 to CAN_LENGHT - 1		' Iterate thru the array and transfer
    		SSPBUF = CAN_LENGTH[i]		' one byte at the time
    		PAUSE 1				' Wait for it
    	NEXT
    
    	HIGH CS					' Deselect device to end write operation
    	PAUSE 10
    	LOW CS					' Select device
    
    	' Now the the message lenth is set and the data to be sent is transfered.
    
    	SSPBUF = MCPWRT : PAUSE 1		' Send write command
    	SSPBUF = TXB0CTRL : PAUSE 1		' Transmitt buffer control register
    	SSPBUF = %00001011 : PAUSE 1		' Request message to be sent with highest priority
    
    	' According to the datasheet this does NOT inititare the actual transfer. It politely
    	' tells the MCP2515 that the databuffer is ready and can be sent whent the bus is available.
    
    RETURN
    Now, I don't know about the Standard Identifier, if they need to be set on a message per message basis or if they are like a node adress or something. In the code above they gets set each time which might be unneccessary. I'm also using PAUSE to wait for the MSSP module, looking at the interrupt flag or buffer free flag as you've been doing is obviously better but it cluttered up the overall structure so I used PAUSE for clarity.

    Not saying it'll work but that's what I'd try.

    /Henrik.

  5. #5
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    I still have version 2.5. I think you need 2.6 for arraywrite, correct? I have a program that will only work with 2.5 so I haven't moved past that point yet but it looks like I may need to. Is there a similar command that will work?

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Just load the array manually
    Code:
    CAN_DATA[0] = $00
    CAN_DATA[1] = $11
    CAN_DATA[2] = $22
    ' and so on

  7. #7
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Something changed and I can't figure out what I did. My waveforms appear to match the Microchip program, except for one. The SDO from the 18F4550 is at 5V instead of 0V. I can't find any reference to change that. The only thing I've found is commands to change the clock signal but nothing for the SDO. Any ideas?

    I'm very close now.

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