News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Canbus mcp2515

Started by Samir sayyed, Jun 18, 2021, 07:40 PM

Previous topic - Next topic

Samir sayyed

Any success story of canbus I am use 18f452 and mcp2515 but not proper intlize please share mcp2515.inc file
 

RGV250

Hi,
I did look at CAN SPI a few years ago but have not followed it up, here is what I did then which might be of help.
CAN_SPI_28_05_2017.zip
Basically found it a bit limited and native CAN was better.

Regards,
Bob

Samir sayyed


RGV250

Hi,
I have just checked and on my board the MCP uses a 16mhz xtal and the BAUD is 125khz.

Bob

Samir sayyed


RGV250

My EasyPIC board is playing up so I have tried it on Amicus18. It uses the same INC file in post 1 above.
You can see the results on the serial communicator, it is very basic but it does work.

Bob

'****************************************************************
'*  Name    : CAN_SPI                                           *
'*  Author  : Bobby Garrett                                     *
'*  Notice  : Copyright (c) 2021                                *
'*          : All Rights Reserved                               *
'*  Date    : 21/06/2021                                        *
'*  Version : 1.2                                               *
'*  Notes   : Uses Amicus18 board                               *
'*          :                                                   *
'****************************************************************

       Include "AMICUS18.INC"

       All_Digital = True          'Set all I/O to digital
        
        Clear                       'Clear all RAM
        DelayMS 200                 'Let PIC start up 

'****************************************************************         
'* This include is where all the CAN "Symbols" are              *
'****************************************************************
        Include "C:\BG_WORK\PIC_PROJECTS\CAN_SPI\CAN_SPI_MCP2515.INC"     

        Symbol CS = PORTC.0             ' Chip select pin                (Pin 16) Pin numbers are for PDIP package
        Symbol CAN_RESET = PORTC.2      ' Reset pin on MCP               (Pin 17)
        Symbol SPI_CLK = PORTC.3        ' SPI clock pin                  (Pin 13)
        Symbol SPI_DI = PORTC.4         ' SPI data in pin (SO On MCP)    (Pin 15)
        Symbol SPI_DO = PORTC.5         ' SPI data out pin (SI On MCP)   (Pin 14)     

        TRISC.0 = 0         
        TRISC.2 = 0
        TRISC.3 = 0       
        TRISC.4 = 1 
        TRISC.5 = 0  
       
        Dim DATA_IN As Byte                  
        Dim X As Byte
       
        GoSub MCP2515_INIT  'Initialise the CAN settings
     
'****************************************************************
'* Main program LOOP                                            *
'****************************************************************       
MAIN_LOOP:
        For X = 0 To 255

        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE_TX,0,0,0,0,1,X]
        CS = 1
             
        Nop  

' Initiate the transmission, TX buffer 0       
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,TXB0CTRL,%00001000]
        CS = 1
         
        Nop 
       
        HSerOut ["SENT  ", Dec X, " "]  
         
' Read RX buffer 0       
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_READ_RX]
        SHIn SPI_DI, SPI_CLK, MsbPre, [DATA_IN]        
        CS = 1    
           
        HSerOut [" RECEIVED  ", Dec DATA_IN,10,13]    
              
        DelayMS 1000  
            
        Next X
      
        GoTo MAIN_LOOP
   
MCP2515_INIT:

        CAN_RESET = 0
        DelayMS 10
        CAN_RESET = 1       
'Should not be needed as the reset command puts it in config mode
'Set operating mode to config
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,CANCTRL,MODE_CONFIG]
        CS = 1

        Nop       
       
'Set for ALL valid messages
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,RXB0CTRL,%01100000]
        CS = 1
             
        Nop      
              
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,RXB1CTRL,%01100000]
        CS = 1
         
        Nop

'Set NODE (0) / MESSAGE (0)     
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,RXB0SIDL,%00000000]
        CS = 1
           
        Nop
       
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,RXB0SIDH,%00000000]
        CS = 1
          
        Nop
       
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,RXB1SIDL,%00000000]
        CS = 1 

        Nop
       
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,RXB1SIDH,%00000000]
        CS = 1
                       
        Nop
       
'MASKS / FILTERS GO HERE
        'So far it should be set to receive all messages
        'so we do not need to set these

'Set CAN BAUD mode
'125khz / 16mhz xtal ($03, $9A, $07 from Kvaser bit timing calculator)
'https://www.kvaser.com/support/calculators/bit-timing-calculator/
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,CNF1,%00000011]
        CS = 1
  
        Nop
       
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,CNF2,%10011010]
        CS = 1

        Nop
       
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,CNF3,%00000111]
        CS = 1

        Nop
       
'Select operating mode to Loopback
        CS = 0
        SHOut SPI_DO, SPI_CLK, MsbFirst, [SPI_WRITE,CANCTRL,MODE_LOOPBACK]
        CS = 1

        Return
       
'****************************************************************
' END OF MCP2515 INITIALISATION                                 *
'****************************************************************   


Samir sayyed

Thanks bob your first mcp2515.inc file is very helpful
  My canbus first project is successfully Run  thanks bob very very thanks 😊😊😊😊

RGV250

Hi,
How far have you got with SPI CAN as I have not done a lot, only with CAN PIC's. Could you post your code so I might pick something up?

Bob

Samir sayyed

Hi bob send your mail I'd  I send my full project
  Thanks regard
  & I am proton compiler genuine USB key licence user how to update latest version

RGV250

#9
Hi,
I will send email, for the upgrade see here, you can either upgrade which you keep the dongle or for not a lot more you get a personal copy which you can use on any machine without the dongle.
https://sites.google.com/view/rosetta-tech/home/how-to-purchase-the-positron-compilers

I forgot to add, 3.7.5.5 is the last version before the new paid for update and you need that to upgrade but the full version does not need it.

Bob

Samir sayyed

Hi please check mail & set bousrate 500kbps

Anton1988

Good evening Samir.
Would you mind posting or sending some of your code?