Datasheet and register question


Closed Thread
Results 1 to 22 of 22

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Hi Christopher,
    This may be moot if you've already decided to switch devices but it still seems to me like if you're missing one crucial point. You said that you can't get your variable set up correctly which variables are you talking about here and how do you set them? I don't mean to which values you set them but how do you actually assign your values - whatever value it might be - TO the variable?

    The reason I ask is because I still think you may be missing the point that in order for the MCP2515 to work you need to set IT up - ie assign values to the regsiters that are IN the MCP2515. The MCP2515 is an intelligent device, it's not like a MAX232 which has nothing to set up. When you say variables it sounds as if you're still trying to assign values to variables that you create in the PIC and that won't work.

    Are you using SHIFTOUT to write the values or do you use the MSSP module?

    If it was me I start by making a routine which write values to the MCP2515. Then I'd make a routine which reads values from the MCP2515 to verify that the write routine is working properly.

    Can you post some code of your current attempts?

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Quote Originally Posted by comwarrior
    The 4550 is quite commonly supported... not only that but the datasheets seem (to me) to be written properly...

    I've not played with CAN, however i (along with a lot of other people on here) know the 4550 very well...
    Thank you for your substantive post. It has helped me greatly.

    Are you using SHIFTOUT to write the values or do you use the MSSP module?

    If it was me I start by making a routine which write values to the MCP2515. Then I'd make a routine which reads values from the MCP2515 to verify that the write routine is working properly.

    Can you post some code of your current attempts?
    I'm using the MSSP. I think my commands are getting to the MCP but I'm guessing my problem lies in one of these places:

    CNF1, CNF2, CNF3, SSPCON, SSPSTAT OR BAUDCON. It could be anywhere else too. Basically, the entire program is suspect. Thanks for the help Henrik. Maybe you can spot some issues with what I wrote:

    Code:
    INCLUDE "modedefs.bas"
    '          
    DEFINE OSC 20 
    
    
    '==============================================================================
    '                 SETTINGS
    ' ==============================================================================
               '76543210          '76543210
     TRISA   = %11111111: PORTA = %00000000 
     TRISB   = %00010001: PORTB = %00010000   
     TRISC   = %00000000: PORTC = %00000001
     TRISD   = %11111111: PORTD = %00000000
     TRISE   = %00010000: PORTE = %00010111
     ADCON0  = %00000000
     ADCON1  = %00000000
     ADRESH  = %00000000 
     ADRESL  = %00000000
     CMCON   = %00000000
     SSPSTAT = %11000100      
     SSPCON1 = %10110001 
     BAUDCON = %00001111   
        
    
    '===============================================================================
    '                   Register Mapping                          
    '================================================================================
    
     CANSTAT         CON    $0E
     CANCTRL         CON    $0F
     BFPCTRL         CON    $0C
     TEC             CON    $1C
     REC             CON    $1D
     CNF3            CON    $28
     CNF2            CON    $29
     CNF1            CON    $2A
     CANINTE         CON    $2B
     CANINTF         CON    $2C
     EFLG            CON    $2D
     TXRTSCTRL       CON    $0D
    
    ''  Recieve Filters ''
     RXF0SIDH        CON    $00
     RXF0SIDL        CON    $01
     RXF0EID8        CON    $02
     RXF0EID0        CON    $03
     RXF1SIDH        CON    $04
     RXF1SIDL        CON    $05
     RXF1EID8        CON    $06
     RXF1EID0        CON    $07
     RXF2SIDH        CON    $08
     RXF2SIDL        CON    $09
     RXF2EID8        CON    $0A
     RXF2EID0        CON    $0B
     RXF3SIDH        CON    $10
     RXF3SIDL        CON    $11
     RXF3EID8        CON    $12
     RXF3EID0        CON    $13
     RXF4SIDH        CON    $14
     RXF4SIDL        CON    $15
     RXF4EID8        CON    $16
     RXF4EID0        CON    $17
     RXF5SIDH        CON    $18
     RXF5SIDL        CON    $19
     RXF5EID8        CON    $1A
     RXF5EID0        CON    $1B
    
    '' Receive Masks ''
     RXM0SIDH        CON    $20
     RXM0SIDL        CON    $21
     RXM0EID8        CON    $22
     RXM0EID0        CON    $23
     RXM1SIDH        CON    $24
     RXM1SIDL        CON    $25
     RXM1EID8        CON    $26
     RXM1EID0        CON    $27
    
    '' Tx Buffer 0 ''
     TXB0CTRL        CON    $30
     TXB0SIDH        CON    $31
     TXB0SIDL        CON    $32
     TXB0EID8        CON    $33
     TXB0EID0        CON    $34
     TXB0DLC         CON    $35
     TXB0D0          CON    $36
     TXB0D1          CON    $37
     TXB0D2          CON    $38
     TXB0D3          CON    $39
     TXB0D4          CON    $3A
     TXB0D5          CON    $3B
     TXB0D6          CON    $3C
     TXB0D7          CON    $3D
                             
    '' Tx Buffer 1 ''
     TXB1CTRL        CON    $40
     TXB1SIDH        CON    $41
     TXB1SIDL        CON    $42
     TXB1EID8        CON    $43
     TXB1EID0        CON    $44
     TXB1DLC         CON    $45
     TXB1D0          CON    $46
     TXB1D1          CON    $47
     TXB1D2          CON    $48
     TXB1D3          CON    $49
     TXB1D4          CON    $4A
     TXB1D5          CON    $4B
     TXB1D6          CON    $4C
     TXB1D7          CON    $4D
    
    '' Tx Buffer 2 ''
     TXB2CTRL        CON    $50
     TXB2SIDH        CON    $51
     TXB2SIDL        CON    $52
     TXB2EID8        CON    $53
     TXB2EID0        CON    $54
     TXB2DLC         CON    $55
     TXB2D0          CON    $56
     TXB2D1          CON    $57
     TXB2D2          CON    $58
     TXB2D3          CON    $59
     TXB2D4          CON    $5A
     TXB2D5          CON    $5B
     TXB2D6          CON    $5C
     TXB2D7          CON    $5D
                             
    '' Rx Buffer 0 ''
     RXB0CTRL        CON    $60
     RXB0SIDH        CON    $61
     RXB0SIDL        CON    $62
     RXB0EID8        CON    $63
     RXB0EID0        CON    $64
     RXB0DLC         CON    $65
     RXB0D0          CON    $66
     RXB0D1          CON    $67
     RXB0D2          CON    $68
     RXB0D3          CON    $69
     RXB0D4          CON    $6A
     RXB0D5          CON    $6B
     RXB0D6          CON    $6C
     RXB0D7          CON    $6D
                             
    '' Rx Buffer 1 ''
     RXB1CTRL        CON    $70
     RXB1SIDH        CON    $71
     RXB1SIDL        CON    $72
     RXB1EID8        CON    $73
     RXB1EID0        CON    $74
     RXB1DLC         CON    $75
     RXB1D0          CON    $76
     RXB1D1          CON    $77
     RXB1D2          CON    $78
     RXB1D3          CON    $79
     RXB1D4          CON    $7A
     RXB1D5          CON    $7B
     RXB1D6          CON    $7C
     RXB1D7          CON    $7D
    
    
    '===============================================================================
    '                  Register Bit Masks                             
    '===============================================================================
    
     DLC_0          CON   $00
     DLC_1          CON   $01
     DLC_2          CON   $02
     DLC_3          CON   $03
     DLC_4          CON   $04
     DLC_5          CON   $05
     DLC_6          CON   $06
     DLC_7          CON   $07    
     DLC_8          CON   $08
    
    
    
    '===============================================================================
    ' '                  CAN SPI commands                               
    '===============================================================================
    '
    ' CAN_RESET         $C0
    ' CAN_READ          $03
    MCPWRT              CON       $02
    ' CAN_RTS           $80
    ' CAN_RTS_TXB0      $81
    ' CAN_RTS_TXB1      $82
    ' CAN_RTS_TXB2      $84
    ' CAN_RD_STATUS     $A0
    ' CAN_BIT_MODIFY    $05  
    ' CAN_RX_STATUS     $B0
    ' CAN_RD_RX_BUFF    $90
    ' CAN_LOAD_TX       $40  
    
    '===============================================================================
    '                         ALIAS & MODIFIERS
    '==============================================================================
    CS          VAR PORTB.4             ' CS LINE
    SCLK        VAR PORTB.1             ' CLOCK LINE
    SDI         VAR PORTB.0             ' DATA INPUT LINE
    SDO         VAR PORTC.7             ' DATA OUTPUT LINE
    SENDLED     VAR PORTA.5             ' LED TO INDICATE THE PROGRAM IS IN THE SENDING ROUTINE
    MAINLED     VAR PORTE.2             ' LED TO INDICATE THE PROGRAM IS IN THE MAIN ROUTINE
    INT         VAR PORTB.5             ' INTERRUPT OUTPUT FROM 18F4550
    MCPRST      VAR PORTC.0             ' RESET LINE FOR MCP2515
    SW_LOAD     VAR PORTA.4             ' BUTTON CONNECTED TO 18F4550 TO START SENDING DATA (WHEN IMPLEMENTED)
    
    '===============================================================================
    '                       SOFTWARE VARIABLES
    '===============================================================================
    
    DATAOUT VAR BYTE           'FIRST BYTE OF DATA TO BE SENT
    MCPREG VAR BYTE            'ADDRESS OF FIRST BYTE OF DATA TO BE SENT
    
    '==============================================================================
    '                   RESET MCP2515
    '==============================================================================
    
    LOW MCPRST                 ' COMMAND TO RESET THE MCP2515 (SOFTWARE)
    PAUSE 1
    HIGH MCPRST
    PAUSE 20
    
    '==============================================================================
    '                   INITIALIZE MCP2515
    '==============================================================================
    
    
    LOW CS
    SSPBUF = MCPWRT                 ' COMMAND TO WRITE TO THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND 
    
    MCPREG = CANCTRL                ' PUT THE MCP2515 INTO CONFIGURATION MODE 
    DATAOUT =  %10001010
    GOSUB SENDCANDATA  
    
    MCPREG =   CNF3                 'CNF3 - CONFIGURATION REGISTER 3 
    DATAOUT =  $07
    GOSUB SENDCANDATA
    
    MCPREG =   CNF2                 'CNF2 - CONFIGURATION REGISTER 2 
    DATAOUT =  $BA
    GOSUB SENDCANDATA
    
    MCPREG =   CNF1                 'CNF1 - CONFIGURATION REGISTER 1 
    DATAOUT =  $03
    GOSUB SENDCANDATA
        
    MCPREG =   TXB0SIDL             'TRANSMIT BUFFER 0 FOR SIDL
    DATAOUT =  %10000000    
    GOSUB SENDCANDATA 
    
    MCPREG =   TXB0SIDH             'TRANSMIT BUFFER 0 FOR SIDH
    DATAOUT =  %00000001
    GOSUB SENDCANDATA
        
    MCPREG =   CANCTRL              'PUT THE MCP2515 BACK INTO NORMAL MODE
    DATAOUT =  %00001010
    GOSUB SENDCANDATA
    HIGH CS
    
    
    
    '==============================================================================
    '                   MAIN PROGRAM FOR MCP2515
    '==============================================================================
    
    PROGRAMSTART:
    HIGH SENDLED 
    PAUSE 50
    
     LOW CS
    
    SSPBUF = $02                ' COMMAND TO WRITE TO THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    
    MCPREG =   CANINTE         ' DISABLE ALL INTERRUPTS
    DATAOUT =  %00000000
    GOSUB SENDCANDATA
                                
    MCPREG =   TXB0CTRL         ' FLAG THE TRANSMIT BUFFER - - 11=HIGH, 10=MEDIUM, 01=LOW, 00=LOWEST 
    DATAOUT =  %00001011
    GOSUB SENDCANDATA
    
    MCPREG =   TXB0SIDL         ' TRANSMIT BUFFER 0 FOR SIDH 
    DATAOUT =  %00110010    
    GOSUB SENDCANDATA 
    
    MCPREG =   TXB0SIDH        ' TRANSMIT BUFFER 0 FOR SIDH  
    DATAOUT =  %00110001
    GOSUB SENDCANDATA
     
    MCPREG =   TXB0DLC         ' NUMBER OF BYTES TO BE TRANSMITTED IN CAN FRAME  
    DATAOUT =  %00000010
    GOSUB SENDCANDATA
    
    MCPREG =   TXB0D0          ' DATA PACKET FOR BYTE 1 
    DATAOUT =  %00000001
    GOSUB SENDCANDATA
        
    MCPREG =   TXB0D1          'DATA PACKET FOR BYTE 2
    DATAOUT =  %00000100
    GOSUB SENDCANDATA
    
    
    HIGH CS
    
    LOW sendled
    pause 50
    
    
    
    goto PROGRAMSTART
    '===============================================================================
    '                           SEND CAN DATA SECTION
    '===============================================================================
    SENDCANDATA:
    
    SSPBUF = MCPREG          ' ADDRESS TO LOAD THE DATA TO THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 : WEND 
    SSPBUF = DATAOUT          'DATA BEING LOADED TO THE MCP2515 ON MCPREG
    PIR1.3 = 0              
    WHILE PIR1.3=0 : WEND
        
     RETURN

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Hi Christopher,
    OK, you're making progress, don't give up!

    You have ADCON1 = 0 which will have all 13 analog pins set as analog, you probably want to turn the analog stuff OFF by setting ADCON1 = 15. For example RB0 and RB1 are AN12 and AN10 respectively but they are also the SDI and SCK pins for the MSSP module - that may or may not be part of the problem.

    You have CMOCON = 0 which will leave both analog comparators ON, to turn them OFF on the 4550 you need to do CMCON = 7 - may or may not be part of the problem depending on exactly which pins they are on and what you're using, or trying to use, them for - I haven't looked into it.

    The MCP2515 SPI timing diagrams show the SPI clock idling LOW. You have SSPCON1.4 set to 1 which will make the clock idle HIGH, ie wrong polarity.

    Well, that's three things I see that may have something to do with it. Try adressing those, then try capturing the signals with your scope and see if they look anything like the timing diagrams in the MCP2515 datasheet.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Jag är trött.....snälla hjälp mig!

    It's 1am here so hopefully this post is clear enough and someone can help me! I've been adjusting settings and trying new things and it still doesn't work. Here is what it boils down to. This is a picture of the working code from Microchip (if I knew C I'd be done already).

    Channel 1 (Yellow) SDO from 18F4550
    Channel 2 (Blue) CS from 18F4550
    Channel 3 (Purple) TXCAN from MCP2515
    Channel 4 (Green) CLK from 18F4550?

    Name:  Microchip code.jpg
Views: 1990
Size:  92.8 KB
    This is what I'm looking to achieve.

    This is what my code looks like:
    Name:  My code.jpg
Views: 1398
Size:  71.4 KB
    At least it's not that much different and I'm in the ballpark. What I can see is the signals are similar but way more compressed. It seems like a timing issue but I really don't know.

    I adjusted the oscilloscope to the same seconds/division as the Microchip example and it's very compressed. It looks like this:Name:  My code with the same division as Microchip.jpg
Views: 1267
Size:  105.1 KB

    I tried adjusting the HSPLL to HS and that seemed to make things a little better but didn't do much as far as a working program. The clock still looks strange when I compare it with the Microchip code.
    Name:  My code with the same division as Microchip but with HS instead of HSPLL.jpg
Views: 1258
Size:  93.4 KB

    The other things I see wrong:

    1. The code I'm using is sending one data packet, along with SIDH and SIDL. I setup Microchip's program to do the same thing. I can't figure out why but my code is sending a lot more than a few bytes of data. In fact, it overloads the demo program that Microchip supplies and shuts it down.

    2. Microchip's code sends the CAN data after the CS line goes high. For some reason, my program sends it halfway through the data transfer and my SDO line and the CAN output line are sending data at the same time. I did it once (send data after the CS line went high) but I can't remember what I did.

    3. I'm confused with the CKE/CKP bits and not sure what I need to set them as. I see this "mode 0,0" advertised in the MCP2515 datasheet but it literally means nothing to me and I'm sure it's pretty important.

    4. Did I properly set my BAUDCON?

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Hi Christopher,
    Being tired and trying to solve problems usually don't go hand in hand....

    1) I don't know why your code is sending more data than the Microchip example because A) I don't know which Microchip example your talking about and B) I don't see your code so it's impossible to say. Besides I'm not very good at reading C either.

    2) Your program probably transfers the CAN data to the MCP2515. It then starts to send it down the CAN-bus WHILE your program continues to transfer some other data to the MCP2515. The MCP example, since it apparently transfers LESS data the MCP2515 than your code, de-asserts the CS line right after transfering the actual data to be sent. (All guesses of course.)

    3) CKP is Clock Polarity. 0 means clock idles low, 1 means clock idles high. CKE is the "phase" of the data being output by the PIC on the SDO-pin. Here's a quote form the MCP2515 datasheet
    Commands and data are sent to the device via the SI pin, with data being clocked in on the rising edge of SCK. Data is driven out by the MCP2515 (on the SO line) on the falling edge of SCK.
    This means that the PIC must have the state of SDO pin valid by the time the rising edge of the clock occurs. The datasheet for the 18F4550 explains: When CKE bit is set, the SDO data is valid before there is a clock edge on SCK. So, my vote is for CKP=0, CKE=1 which should have the data output covered.

    The SMP bit in the PIC controls when the SDI-input is sampled relative to the clock. Since the MCP2515 outputs data on the falling edge of CLK-input we need to sample at, or after, that event. Looking at figure 19-3 and following the CKP0=0,CKE=0 trace down in the diagram we can see that in order to sample at the falling edge of the clock we need to have SMP=1.

    It is a bit confusing and I can honestly say that I'm not 100% confident I've got this right but that's my interpretation of the two the datasheets and it is what I'd try as starting point.

    4) BAUDCON in the 18F4550 is used to control the baudrate of the EUSART and has nothing to do with the MSSP module. The SPI clock rate of the MSSP module is a function of the device clock (Fosc) and the divide ratio set by the lower two bits in the SSPCON1 register.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    I made my last post at 1:23am, it's now 5:11am and I've been up for 20 minutes getting ready for work. My math tells me that's about 3.5 hours or so.....

    Yes, I see I forgot to attach my code. I knew I forgot something! I also attached the Microchip C code as well.

    Code:
     
    
    ==============================================================================
    '                       DEFINES & INCLUDES
    '===============================================================================
    INCLUDE "modedefs.bas"
    '          
    DEFINE OSC 20 
    
    ' ==============================================================================
    '                        CONFIGS
    '===============================================================================
    
    @ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
    @ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_OFF_2L & _BORV_3_2L & _VREGEN_ON_2L
    @ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32768_2H 
    @ __CONFIG _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
    @ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_ON_4L & _XINST_OFF_4L
    
    '==============================================================================
    '                 SETTINGS
    ' ==============================================================================
               '76543210          '76543210
     TRISA   = %00010110: PORTA = %00000000 
     TRISB   = %00010001: PORTB = %00010000   
     TRISC   = %00000000: PORTC = %00000001
     TRISD   = %00011100: PORTD = %00000000
     TRISE   = %00010000: PORTE = %00010111
     ADCON0  = %00111100
     ADCON1  = 15
     ADRESH  = %00000000 
     ADRESL  = %00000000
     CMCON   = 7
     SSPSTAT = %01100100      
     SSPCON1 = %00100001 
     BAUDCON = %00001111     
        
    
    '===============================================================================
    '                   Register Mapping                          
    '================================================================================
    
     CANSTAT         CON    $0E
     CANCTRL         CON    $0F
     BFPCTRL         CON    $0C
     TEC             CON    $1C
     REC             CON    $1D
     CNF3            CON    $28
     CNF2            CON    $29
     CNF1            CON    $2A
     CANINTE         CON    $2B
     CANINTF         CON    $2C
     EFLG            CON    $2D
     TXRTSCTRL       CON    $0D
    
    ''  Recieve Filters ''
     RXF0SIDH        CON    $00
     RXF0SIDL        CON    $01
     RXF0EID8        CON    $02
     RXF0EID0        CON    $03
     RXF1SIDH        CON    $04
     RXF1SIDL        CON    $05
     RXF1EID8        CON    $06
     RXF1EID0        CON    $07
     RXF2SIDH        CON    $08
     RXF2SIDL        CON    $09
     RXF2EID8        CON    $0A
     RXF2EID0        CON    $0B
     RXF3SIDH        CON    $10
     RXF3SIDL        CON    $11
     RXF3EID8        CON    $12
     RXF3EID0        CON    $13
     RXF4SIDH        CON    $14
     RXF4SIDL        CON    $15
     RXF4EID8        CON    $16
     RXF4EID0        CON    $17
     RXF5SIDH        CON    $18
     RXF5SIDL        CON    $19
     RXF5EID8        CON    $1A
     RXF5EID0        CON    $1B
    
    '' Receive Masks ''
     RXM0SIDH        CON    $20
     RXM0SIDL        CON    $21
     RXM0EID8        CON    $22
     RXM0EID0        CON    $23
     RXM1SIDH        CON    $24
     RXM1SIDL        CON    $25
     RXM1EID8        CON    $26
     RXM1EID0        CON    $27
    
    '' Tx Buffer 0 ''
     TXB0CTRL        CON    $30
     TXB0SIDH        CON    $31
     TXB0SIDL        CON    $32
     TXB0EID8        CON    $33
     TXB0EID0        CON    $34
     TXB0DLC         CON    $35
     TXB0D0          CON    $36
     TXB0D1          CON    $37
     TXB0D2          CON    $38
     TXB0D3          CON    $39
     TXB0D4          CON    $3A
     TXB0D5          CON    $3B
     TXB0D6          CON    $3C
     TXB0D7          CON    $3D
                             
    '' Tx Buffer 1 ''
     TXB1CTRL        CON    $40
     TXB1SIDH        CON    $41
     TXB1SIDL        CON    $42
     TXB1EID8        CON    $43
     TXB1EID0        CON    $44
     TXB1DLC         CON    $45
     TXB1D0          CON    $46
     TXB1D1          CON    $47
     TXB1D2          CON    $48
     TXB1D3          CON    $49
     TXB1D4          CON    $4A
     TXB1D5          CON    $4B
     TXB1D6          CON    $4C
     TXB1D7          CON    $4D
    
    '' Tx Buffer 2 ''
     TXB2CTRL        CON    $50
     TXB2SIDH        CON    $51
     TXB2SIDL        CON    $52
     TXB2EID8        CON    $53
     TXB2EID0        CON    $54
     TXB2DLC         CON    $55
     TXB2D0          CON    $56
     TXB2D1          CON    $57
     TXB2D2          CON    $58
     TXB2D3          CON    $59
     TXB2D4          CON    $5A
     TXB2D5          CON    $5B
     TXB2D6          CON    $5C
     TXB2D7          CON    $5D
                             
    '' Rx Buffer 0 ''
     RXB0CTRL        CON    $60
     RXB0SIDH        CON    $61
     RXB0SIDL        CON    $62
     RXB0EID8        CON    $63
     RXB0EID0        CON    $64
     RXB0DLC         CON    $65
     RXB0D0          CON    $66
     RXB0D1          CON    $67
     RXB0D2          CON    $68
     RXB0D3          CON    $69
     RXB0D4          CON    $6A
     RXB0D5          CON    $6B
     RXB0D6          CON    $6C
     RXB0D7          CON    $6D
                             
    '' Rx Buffer 1 ''
     RXB1CTRL        CON    $70
     RXB1SIDH        CON    $71
     RXB1SIDL        CON    $72
     RXB1EID8        CON    $73
     RXB1EID0        CON    $74
     RXB1DLC         CON    $75
     RXB1D0          CON    $76
     RXB1D1          CON    $77
     RXB1D2          CON    $78
     RXB1D3          CON    $79
     RXB1D4          CON    $7A
     RXB1D5          CON    $7B
     RXB1D6          CON    $7C
     RXB1D7          CON    $7D
    
    
    '===============================================================================
    '                  Register Bit Masks                             
    '===============================================================================
    
     DLC_0          CON   $00
     DLC_1          CON   $01
     DLC_2          CON   $02
     DLC_3          CON   $03
     DLC_4          CON   $04
     DLC_5          CON   $05
     DLC_6          CON   $06
     DLC_7          CON   $07    
     DLC_8          CON   $08
    
    
    
    '===============================================================================
    ' '                  CAN SPI commands                               
    '===============================================================================
    '
    CAN_RST             CON       $C0
    ' CAN_READ          $03
    MCPWRT              CON       $02
    ' CAN_RTS           $80
    ' CAN_RTS_TXB0      $81
    ' CAN_RTS_TXB1      $82
    ' CAN_RTS_TXB2      $84
    ' CAN_RD_STATUS     $A0
    ' CAN_BIT_MODIFY    $05  
    ' CAN_RX_STATUS     $B0
    ' CAN_RD_RX_BUFF    $90
    ' CAN_LOAD_TX       $40  
    
    '===============================================================================
    '                         ALIAS & MODIFIERS
    '==============================================================================
    CS          VAR PORTB.4             ' CS LINE
    SCLK        VAR PORTB.1             ' CLOCK LINE
    SDI         VAR PORTB.0             ' DATA INPUT LINE
    SDO         VAR PORTC.7             ' DATA OUTPUT LINE
    SENDLED     VAR PORTA.5             ' LED TO INDICATE THE PROGRAM IS IN THE SENDING ROUTINE
    MAINLED     VAR PORTE.2             ' LED TO INDICATE THE PROGRAM IS IN THE MAIN ROUTINE
    INT         VAR PORTB.5             ' INTERRUPT OUTPUT FROM 18F4550
    MCPRST      VAR PORTC.0             ' RESET LINE FOR MCP2515
    SW_LOAD     VAR PORTA.4             ' BUTTON CONNECTED TO 18F4550 TO START SENDING DATA (WHEN IMPLEMENTED)
    RX_LED      VAR PORTD.1             ' USB RECEIVE LED
    TX_LED      VAR PORTD.0             ' USB TRANSMIT LED
    
    '===============================================================================
    '                       SOFTWARE VARIABLES
    '===============================================================================
    
    DATAOUT VAR BYTE           'FIRST BYTE OF DATA TO BE SENT
    MCPREG VAR BYTE            'ADDRESS OF FIRST BYTE OF DATA TO BE SENT
    
    
    TX_LED=1
    RX_LED=1
    '==============================================================================
    '                   RESET CAN
    '==============================================================================
    
    
    
    
    '==============================================================================
    '                   RESET CAN
    '==============================================================================
    
    '==============================================================================
    '                   INITIALIZE MCP2515
    '==============================================================================
    
    
    LOW CS
    SSPBUF = MCPRST               ' COMMAND TO RESET THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    HIGH CS
                     
    PAUSE 1000
    LOW CS                         
    
    SSPBUF = mcpwrt                 ' COMMAND TO WRITE TO THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    
    MCPREG =   CANCTRL              'PUT THE MCP2515 INTO CONFIGURATON MODE
    DATAOUT =  $08
    GOSUB SEND_CAN_DATA                    
    
    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
    
    
                                                                                                                  
    
    
    
    
    '==============================================================================
    '                   MAIN PROGRAM FOR MCP2515
    '==============================================================================
    
    START:
    HIGH SENDLED 
    PAUSE 4000
    
    LOW CS
       
    SSPBUF = mcpwrt                 ' COMMAND TO WRITE TO THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND              
     
    MCPREG =   CANINTE              ' DISABLE ALL INTERRUPTS
    DATAOUT =  %00000000
    GOSUB SEND_CAN_DATA            
    
    high cs                         ' ON THE MICROCHIP EXAMPLE, THEY PULLED THE CS LINE LOW, SENT
                                    ' SOME DATA, THEN HIGH AND LOW AGAIN TO RESEND MORE DATA. I 
                                    ' I STILL CAN'T FIGURE OUT WHY. IT'S PROBABLY SOMETHING 
                                    ' SIMPLE I AM MISSING BUT I TRIED TO DUPLICATE IT.
    LOW CS
    
    SSPBUF = mcpwrt                ' COMMAND TO WRITE TO THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    
    
    MCPREG =   TXB0CTRL         ' FLAG THE TRANSMIT BUFFER - - 11=HIGH, 10=MEDIUM, 01=LOW, 00=LOWEST 
    DATAOUT =  %00001011
    GOSUB SEND_CAN_DATA
    
    MCPREG =   TXB0SIDL         ' TRANSMIT BUFFER 0 FOR SIDH 
    DATAOUT =  %0000000    
    GOSUB SEND_CAN_DATA 
    
    MCPREG =   TXB0SIDH        ' TRANSMIT BUFFER 0 FOR SIDH  
    DATAOUT =  %00001000
    GOSUB SEND_CAN_DATA
     
    MCPREG =   TXB0DLC         ' NUMBER OF BYTES TO BE TRANSMITTED IN CAN FRAME  
    DATAOUT =  %00000001
    GOSUB SEND_CAN_DATA
    
    MCPREG =   TXB0D0          ' DATA PACKET FOR BYTE 1 
    DATAOUT =  %00000111
    GOSUB SEND_CAN_DATA
        
    'MCPREG =   TXB0D1          'DATA PACKET FOR BYTE 2
    'DATAOUT =  %00001111
    'GOSUB SEND_CAN_DATA 
    
    HIGH CS
    
    
    LOW sendled
    pause 4000 
    
    goto START
    
    
    '===============================================================================
    '                           SEND CAN DATA SECTION
    '===============================================================================
    SEND_CAN_DATA:
    
    
     
    
    SSPBUF = MCPREG          ' ADDRESS TO LOAD THE DATA TO THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 : WEND 
    
    SSPBUF = DATAOUT          'DATA BEING LOADED TO THE MCP2515 ON MCPREG
    PIR1.3 = 0              
    WHILE PIR1.3=0 : WEND
    
        
    RETURN
    Code:
    /*********************************************************************
     *
     *                Microchip USB C18 Firmware Version 1.0
     *
     *********************************************************************
     * FileName:        main.c
     * Dependencies:    See INCLUDES section below
     * Processor:       PIC18
     * Compiler:        C18 2.30.01+
     * Company:         Microchip Technology, Inc.
     *
     * Software License Agreement
     *
     * The software supplied herewith by Microchip Technology Incorporated
     * (the “Company”) for its PICmicro® Microcontroller is intended and
     * supplied to you, the Company’s customer, for use solely and
     * exclusively on Microchip PICmicro Microcontroller products. The
     * software is owned by the Company and/or its supplier, and is
     * protected under applicable copyright laws. All rights are reserved.
     * Any use in violation of the foregoing restrictions may subject the
     * user to criminal sanctions under applicable laws, as well as to
     * civil liability for the breach of the terms and conditions of this
     * license.
     *
     * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
     * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
     * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
     * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
     * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
     *
     * Author               Date        Comment
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Rawin Rojvanit       11/19/04    Original.
     ********************************************************************/
    	
    		#pragma config PLLDIV   = 5         // (20 MHz crystal on PICDEM FS USB board)
            #pragma config CPUDIV   = OSC1_PLL2   
            #pragma config USBDIV   = 2         // Clock source from 96MHz PLL/2
            #pragma config FOSC     = HSPLL_HS
            #pragma config FCMEN    = OFF
            #pragma config IESO     = OFF 
            #pragma config PWRT     = OFF 
            #pragma config BOR      = OFF
            #pragma config BORV     = 3
            #pragma config VREGEN   = ON      //USB Voltage Regulator
            #pragma config WDT      = OFF
            #pragma config WDTPS    = 32768
            #pragma config MCLRE    = ON
            #pragma config LPT1OSC  = OFF
            #pragma config PBADEN   = OFF
          #pragma config CCP2MX   = ON
            #pragma config STVREN   = ON
            #pragma config LVP      = OFF
            #pragma config ICPRT    = ON       // Dedicated In-Circuit Debug/Programming
            #pragma config XINST    = OFF       // Extended Instruction Set
            #pragma config CP0      = OFF
            #pragma config CP1      = OFF
          #pragma config CP2      = OFF
          #pragma config CP3      = OFF
            #pragma config CPB      = OFF
          #pragma config CPD      = OFF
            #pragma config WRT0     = OFF
            #pragma config WRT1     = OFF
          #pragma config WRT2     = OFF
          #pragma config WRT3     = OFF
            #pragma config WRTB     = OFF       // Boot Block Write Protection
            #pragma config WRTC     = OFF
          #pragma config WRTD     = OFF
            #pragma config EBTR0    = OFF
            #pragma config EBTR1    = OFF
          #pragma config EBTR2    = OFF
          #pragma config EBTR3    = OFF
            #pragma config EBTRB    = OFF
    	
    
    
    /** I N C L U D E S **********************************************************/
    #include <p18cxxx.h>
    #include "system\typedefs.h"                        // Required
    #include "system\usb\usb.h"                         // Required
    #include "io_cfg.h"                                 // Required
    
    #include "system\usb\usb_compile_time_validation.h" // Optional
    #include "user\BusMon.h"                        // Modifiable
    
    
    /** V A R I A B L E S ********************************************************/
    #pragma udata
    
    /** P R I V A T E  P R O T O T Y P E S ***************************************/
    static void InitializeSystem(void);
    void USBTasks(void);
    
    /** V E C T O R  R E M A P P I N G *******************************************/
    
    /*
    extern void _startup (void);        // See c018i.c in your C18 compiler dir
    #pragma code _RESET_INTERRUPT_VECTOR = 0x002000
    void _reset (void)
    {
        _asm goto _startup _endasm
    }
    */
    
    //#pragma code _HIGH_INTERRUPT_VECTOR = 0x002008
    //void _high_ISR (void)
    //{
    //    ;
    //}
    //
    //#pragma code LOW_INTERRUPT_VECTOR = 0x002018
    //void _low_ISR (void)
    //{
    //  _asm
    //    goto InterruptHandler
    //  _endasm
    //}
    
    #pragma code                                                                                                                
    
    /******************************************************************************
     * Function:        void main(void)
     *
     * PreCondition:    None
     *
     * Input:           None
     *
     * Output:          None
     *
     * Side Effects:    None
     *
     * Overview:        Main program entry point.
     *
     * Note:            None
     *****************************************************************************/
    void main(void)
    {
      InitializeSystem();
      UserInit();         // See BusMon.c & .h
      while(1)
      {    
       if(usb_power_check)  //Only do USB USB if not powered by CAN connector
          USBTasks();         // USB Tasks
    //  INTCONbits.GIE = 1;
        ProcessIO();        // See user\BusMon.c & .h
      }
    }//end main
    
    /******************************************************************************
     * Function:        static void InitializeSystem(void)
     *
     * PreCondition:    None
     *
     * Input:           None
     *
     * Output:          None
     *
     * Side Effects:    None
     *
     * Overview:        InitializeSystem is a centralize initialization routine.
     *                  All required USB initialization routines are called from
     *                  here.
     *
     *                  User application initialization routine should also be
     *                  called from here.                  
     *
     * Note:            None
     *****************************************************************************/
    static void InitializeSystem(void)
    {
        ADCON1 |= 0x0F;                 // Default all pins to digital
        
        #if defined(USE_USB_BUS_SENSE_IO)
        tris_usb_bus_sense = INPUT_PIN; // See io_cfg.h
        #endif
        
        #if defined(USE_SELF_POWER_SENSE_IO)
        tris_self_power = INPUT_PIN;
        #endif
        
        if(usb_power_check)  //Only initialize USB if not powered by CAN connector
          mInitializeUSBDriver();         // See usbdrv.h
        
    }//end InitializeSystem
    
    /******************************************************************************
     * Function:        void USBTasks(void)
     *
     * PreCondition:    InitializeSystem has been called.
     *
     * Input:           None
     *
     * Output:          None
     *
     * Side Effects:    None
     *
     * Overview:        Service loop for USB tasks.
     *
     * Note:            None
     *****************************************************************************/
    void USBTasks(void)
    {
        /*
         * Servicing Hardware
         */
        USBCheckBusStatus();                    // Must use polling method
        if(UCFGbits.UTEYE!=1)
            USBDriverService();                 // Interrupt or polling method
    
    }// end USBTasks
    
    
    ////*****************************************************************************
    //// High priority interrupt vector
    //#pragma code InterruptVectorHigh = 0x08
    //void InterruptVectorHigh(void)
    //{
    //_asm
    //bra InterruptHandler // jump to interrupt routine
    //_endasm
    //}
    //#pragma code InterruptVectorHigh = 0x08
    //void InterruptVectorLow(void)
    //{
    //_asm
    //bra InterruptHandler // jump to interrupt routine
    //_endasm
    //}
    //
    //
    //#pragma code
    //#pragma interrupt InterruptHandler
    //void InterruptHandler(void)
    //{
    //}
    
    /** EOF main.c ***************************************************************/

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


    Did you find this post helpful? Yes | No

    Default Re: Datasheet and register question

    Hi Christopher,
    That Microchip code doesn't make any sense at all in this context. I see a lot of USB references but nothing about the MSSP module or the MCP2515.

    But I took another look at your code.... At the very beginning, in the INITIALIZE MCP2515 section you have this piece of code:
    Code:
    LOW CS
    SSPBUF = MCPRST               ' COMMAND TO RESET THE MCP2515
    PIR1.3 = 0              
    WHILE PIR1.3=0 :WEND
    HIGH CS
    I then went to see exactly what MCPRST was and the only thing I found in your code was an alias to PortC.0 so what's happening in that piece of code is that the PIC reads PortC.0 and then the result of that read operation (1 or 0) to SSPBUF. To reset the device you either pull the hardware reset line LOW or send $C0 over SPI.

    Properly resetting the device automatically puts in the configuration mode so therotically you shouldn't need to put it in that mode directly after a (proper) reset. Then your comments says that you're writing to "part of baudrate" or something but $07 is the EXTENDED IDENTIFIER LOW register receiver 1, $03 is EXTENDED IDENTIFIER LOW register for receiver 0 and I can't even find what $BA is.

    OK, moving to MAIN then....
    You send the command to write then you send the value 0 to CANINTE at adress $02, that all seems OK to me if disabling interrupts is what you want to do.

    Next you set the TXB0CTRL register for a high priority message and request it to send. Then you write the TRANSMIT BUFFER STANDARD IDENTIFIER register. I have no idea what these does so I'm going to asume it's correct. Then, you tell it you want to send a single byte by writing the value 1 to TXB0DLC and then you write the actual data.

    Personally I would've thought you should first load the data to be sent THEN tell the device to send it. Ie write TXB0CTRL after you've loaded the data but the way you have it might be correct.

    OK, that's it for now. It feels like your pretty close.

    /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