CANbus


Closed Thread
Results 1 to 24 of 24

Thread: CANbus

Hybrid View

  1. #1
    Join Date
    Jul 2009
    Posts
    10

    Default

    Hi Ingo,

    Update for you,

    Received two 18F248 today. snatched half an hour to quickly try out, but no bus activity at all. getting the same results as for 18F4580 otherwise.

    Also tried using your configuration ( modified to 16Mhz OSC)


    FYI, here is my transmit code for 18F4580



    define OSC 16

    TRISB = %00000010 'Set portb.2 output (cantx) ,b.2 input (canrx) 18F248/4580
    '************************************************* ***************
    ' Include "CanRxDeclarePic18.bas"
    '************************************************* ***************
    TXB0DLC = $00
    TXB0D0 = $00
    TXB0D1 = $00
    TXB0D2 = $00
    TXB0D3 = $00
    TXB0D4 = $00
    TXB0D5 = $00
    TXB0D6 = $00
    TXB0D7 = $00

    '************************************************* ***************************
    ' Include "CanVarsPic18.bas"
    '************************************************* ***************************

    DataBytesTx VAR BYTE 'number of bytes to transmit from SPI / MCP2510
    DataBytesTx = 0
    CanDataTx VAR BYTE[8] '0 - 7 data bytes to send (xmit)
    k var byte

    for k = 0 to 7
    CanDataTx[k] = 0
    next


    TREQ var bit
    TREQ =0


    CanStatusReg var byte
    CanControlReg var byte
    RxBuffer0Control var byte
    ExtCanControl var byte
    BaudRateGenControl1 var byte
    BaudRateGenControl2 var byte
    BaudRateGenControl3 var byte
    CanIOControl var byte
    PeripheralInterruptReg3 var byte
    PeripheralInterruptEnable var byte
    CnfgReg1 Var byte
    '************************************************* ***************
    ' CanConfig
    '************************************************* ***************

    CANCON.7 = 1 'Request configuration mode (looped on CANSTAT)
    CanStatusReg = CANSTAT

    'Set Data Rate based on 16Mhz OSC See page 332
    '125 kbs:
    BRGCON1 = $07 'brp = 7(16 mhz), jump 1 TQ (SWJ): 8Tq = 1x10.6/8 = 125Kbs data rate
    BRGCON2 = $90 'freely programmable , sample once, 3TQ seg1,propogation time 1Tq
    BRGCON3 = $02 '3TQ seg2
    BaudRateGenControl1 = BRGCON1
    BaudRateGenControl2 = BRGCON2
    BaudRateGenControl3 = BRGCON3

    'CIOCON see page 314

    CIOCON = $30 ' bit 5 set, CANtx drives vdd when recessive, bit 4 set,enable CAN capture
    CanIOControl=CIOCON
    TXB0SIDH = $10 'b7 - b0 = SID10- SID3
    TXB0SIDL = $10 'b7 = SID2, b6 = SID1, b5 = SID0, b3=EXIDE = 0 for SID, 1 for EID



    CANCON = $08 'request normal mode set tx buffer 0
    CanControlReg = CANCON
    CanStatusReg = CANSTAT



    '************************************************* ***************


    k=1

    '------------------------------------------------------------------------------

    ' Program Loop Program Loop

    '------------------------------------------------------------------------------


    Main:
    PeripheralInterruptEnable =PIE3
    if k >=256 then k=0
    '*********Transmit Data **************************



    ' CANCON = %00000000 'clear WIN bits
    ' CanControlReg = CANCON
    ' CANCON = %00001000 'select TXbuffer0 WIN bits b3 hi, b2-b1 low
    CANCON.3=1 ' set win bit to txbuffer 0
    CanControlReg = CANCON
    CanStatusReg = CANSTAT
    PeripheralInterruptReg3=PIR3

    if TXB0CON.3 = 0 then 'last message is clear (bit 3 is TXREQ)

    DataBytesTx = 4 ' experiment with changing DLC
    TXB0DLC = DataBytesTx
    CanDataTx[0] = K
    CanDataTx[1] = K+1 ' each byte has a different value
    CanDataTx[2] = K+2
    CanDataTx[3] = K+3
    CanDataTx[4] = K+4
    CanDataTx[5] = K+5
    CanDataTx[6] = K+6
    CanDataTx[7] = K+7
    k=k+1

    TXB0CON = %00000011 'tx highest priority
    TXB0D0 = CanDataTx[0]
    TXB0D1 = CanDataTx[1]
    TXB0D2 = CanDataTx[2]
    TXB0D3 = CanDataTx[3]
    TXB0D4 = CanDataTx[4]
    TXB0D5 = CanDataTx[5]
    TXB0D6 = CanDataTx[6]
    TXB0D7 = CanDataTx[7]


    TXB0CON = %00001011 'request a send highest priority
    TREQ = TXB0CON.3



    PeripheralInterruptReg3=PIR3
    endif


    CANCON.3=0 ' set win bit back to default rxbuffer 0
    CanControlReg = CANCON
    CanStatusReg = CANSTAT

    'TXB0CON.3 = 0 'clear txreq - for test purposes only

    gosub blink
    Goto main
    '************************************************* ******************************
    '************************************************* ******************************
    '************************************************* ******************************
    '************************************************* ******************************
    '************************************************* ******************************

    End

    '************************************************* ***************************




    Blink:

    high portb.0
    pause 100
    low portb.0
    pause 150
    return

  2. #2
    Join Date
    Jul 2007
    Location
    Bavaria
    Posts
    28

    Default

    Hi Tim,

    I had a look at your code and noticed two things:

    one major bug:
    TRISB = %00000010 'Set portb.2 output (cantx) ,b.2 input (canrx) 18F248/4580
    should read TRISB = %000001000 "setportb.2 output, portb.3 input

    and one thing I don't understand:
    CANCON.3=1 ' set win bit to txbuffer 0
    what's this exactly good for, do you really need it?

    I didn't check your BRG settings, but if you set up transmitter and sender the same, it should work anyway :-)

    Regards,
    Ingo

  3. #3
    Join Date
    Jul 2009
    Posts
    10

    Default

    Hi Ingo,

    Well spotted on the major bug . I dont know how many times I checked that - obviously not looking properly - either I was misled by the bogus bus activity which convinced me the output pin was correctly configured , and / or was counting the bits from 1 rather than zero - whatever the reason - a rookie error.

    SO, it is all working beautifully now ! I have eight bytes being sent and received, I have tested the code running on 2 x 18F248, 1 x 18F248 and 1 x 18F4580 and finally 2 x 18F4580. All running nicely.

    Glad my understanding of the workings of the registers is validated but have a red face for not checking the TRISB setting.

    Thanks Ingo - I would rather have a red face AND working code than the other way around.

    CANCON.3=1, you are probably correct, this may not be necesary for the simple test program. see page 276 of the 4580 datsheet. bits 3,2 and 1 are called WIN bits, which set the particular transmit or receive buffer . Code 100 sets tx buffer0. I suspect I may have to set these bits depending on the tx buffer I wish to use, equally on the rx side I can use these to place data in a particular rx buffer. I think there may be a degree of automation to this process but have yet to play with the system to fully understand the workings.

    Now I must stop calling it the CANT bus and give its real name CAN bus !

    Got some work to do now, adding data transfer functionality and structuring code.



    Thanks again - Once done, I will send you a copy of my working system if you PM me or provide an e mail address.

  4. #4
    Join Date
    Apr 2009
    Posts
    15

    Default

    I've post in PBP pro but nobody seems to know, so let me post here since is a CanBus question:

    I've a small proto board (without datasheet) connected to a Can network and I'm trying to make a very simple app but I don't know how to do. It has a 18F258 (20Mhz clock) and MC2551 and I want just send a CAN command, nothing else. I know that the bus works at 100k, can anybody help me? just a help to start, I'm using now a CanUSB adapter with a PC but I want to use something lighter.

    I've read the code above but sincerily, I don't understand it.

    Regards

  5. #5
    Join Date
    Jul 2007
    Location
    Bavaria
    Posts
    28

    Default

    CAN my be a little confusing in the beginning, but I am sure you will work into this.
    PIC BASIC will not help you much regarding CAN communication, you will have to set all registers by hand.
    As preparation you should do the following things:

    1. check if your CAN transceiver has an enable pin and check its status
    2. get the baudrate generator calculator from Microchip to calculate BRGCON register settings

    Then you can prepare your code
    3. configure you CANTX and CANRX pins correctly
    4. set CANCON register to 128 for config mode
    5. configure the BRGCON1..3 with the calculated values
    6. configure the identifier registers TXB0SIDH and TXB0SIDL to generate the required message ID
    7. configure the data length register TXB0DLC to match the length of your CAN message
    8. configure the CIOCON register (usually CAN recessive high, bit 4 set)
    9. set CANCON to 0 for normal mode
    10. copy your CAN data to TXB0D0...TXB0D7
    11. set the TXB0CON register to 8 to send your CAN message

    .. and off you go!

  6. #6
    Join Date
    Apr 2009
    Posts
    15

    Default

    Quote Originally Posted by inse View Post
    CAN my be a little confusing in the beginning, but I am sure you will work into this....
    Thank for your step-by-step guide! I'll try to begin with the project this weekend.

    Regards

  7. #7
    Join Date
    Apr 2009
    Posts
    15

    Default

    Well, I've connected the board to a PC using a CANUSB adapter listening at 100Kbps, but does not receive anything. This is the code (18F258+MCP2510 20Mhz):

    Code:
    define OSC 20
    
    TRISB = %000001000 'portb.2 output, portb.3 input
    CANCON = $80 'configuration mode 
    'Set Data Rate based on 20Mhz to get 100 kbs:
    BRGCON1 = $04 
    BRGCON2 = $BF 
    BRGCON3 = $02 
    
    CIOCON = $10
    TXB0SIDH = $0 
    TXB0SIDL = $1 
    CANCON = $0 'normal mode 
    
    '
    ' Program
    '
    Main: 
            TXB0DLC = 8 
            TXB0D0 = 65
            TXB0D1 = 66
            TXB0D2 = 67
            TXB0D3 = 68
            TXB0D4 = 69
            TXB0D5 = 70
            TXB0D6 = 71
            TXB0D7 = 72
            TXB0CON = 8
        pause 1000
    Goto main
    End
    What's wrong?
    Last edited by zx81sp; - 21st February 2010 at 18:27.

Similar Threads

  1. canbus
    By nono2002 in forum mel PIC BASIC
    Replies: 2
    Last Post: - 1st January 2014, 21:35
  2. Canbus
    By Tobias in forum Off Topic
    Replies: 0
    Last Post: - 23rd September 2008, 00:54
  3. Using the 18F2480 CAN CANBUS
    By bwarb in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th August 2005, 14:45
  4. CANbus
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th March 2005, 16:43

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