PDA

View Full Version : CANbus



Timmoore
- 17th August 2009, 11:50
Hi Folks,
Anyone got experience using CAN interface on the 18F4580 or similar microchip device ? Lots of resources available using 16F876 etc with 2510 external interface but had no success converting code to suit a device with built in CAN interface. Have spent a couple of weeks now poring over Datasheets and experimenting with code. Very nearly out of ideas - help!

Archangel
- 17th August 2009, 19:06
Try these links:
1. from microchip: http://ww1.microchip.com/downloads/en/DeviceDoc/00876A.pdf
2. A Special google link made to search this forum or the entire internet, your choice: http://www.google.com/custom?hl=en&cof=AH%3Aleft%3BS%3Ahttp%3A%2F%2Fwww.picbasic.co.u k%2Fforum%3BL%3Ahttp%3A%2F%2Fwww.crownhill.co.uk%2 Flogo.gif%3BLH%3A37%3BLW%3A174%3B&domains=picbasic.co.uk&q=CANBUS&btnG=Search&sitesearch=picbasic.co.uk

inse
- 19th August 2009, 06:06
Hi Tim,

I made a CAN interface using a PIC 18F248.
What exactly is your problem?

Regards,

Ingo

Timmoore
- 20th August 2009, 00:34
Hi Ingo,

Thank you for coming forward. I am not having any success in achieving a Can transmission in Normal mode. I have two 18f4580s connected together on 400mm of twisted pair and 120 ohm terminating resistors. The master is set to transmit only, the 'slave' to reply with received data byte values plus decimal one.

I have both configured to 125Khz data rate based on 16 Mhz xtals.
Using an oscilloscope monitoring the Can bus I can see activity. Both units output the same stream. A dominant 30uS pulse, followed by fifteen 10uS wide dominant pulses at 200uS intervals, this does not correspond to the data rate or the standard frame. I have checked my BRGCON settings against the microchip BRG calculator and everything is correct. After a restart, the respective 'transmissions' may appear synchronized, but can often overlap. Because of this I do not think either is properly responding to the prescence of the other. Runnning the receiving node in ICD The rx buffers are not accepting any data and rx buffer flags are never set. Equally the master -(tx only) never clears TXReq bit , Tx errors are flagged (PIE3 etc) . In short I am a little lost. Plenty indications of things going wrong, but no real indications as to the cause.

I have a nominal bit time of 8Tq, I read somewhere that increasing this to around 13 may help. Did not try this yet as I had more pressing things to attend to.

Hope this is not too confusing - trying to be concise.

Timmoore
- 20th August 2009, 11:25
Thanks Joe S for the links, I already looked at most of Microchip data sheets and Application notes. Lots on M2510 but not much on ECAN.

Just checked the 18F4580 Errata, but there is nothing in there wrt CAN.

INSE, I just had a look at the 18F248 data sheet and the CAN module has the same basic register set as the 4580 ECAN module. The CAN module is more appropriate to my requirements and my code should not require any major changes to run on 18F248. I have ordered a few of them today - did you have any problems or did you find everything pretty straightforward ?

inse
- 20th August 2009, 11:46
Hi Tim,

I suggest you start setting up communication in one direction first and add the reply function in the next step.
Make your receiver blink a LED or put out something on a terminal when it has successfully received data.
I have been debugging my CAN settings with a Microchip MCP2515 eval board that I won on a exhibition years ago.
CAN is not that easy if you start from zero, so it was very helpful to have something at hand that HAD to be working...

My CAN node sends data to a unit and only acknowledges incoming messages.



while (RXB0CON.7 = 1) or (RXB1CON.7 = 1) 'check receive flag and wait for silence on CAN-Bus
RXB0CON.7 = 0
RXB1CON.7 = 0
pause 100
@ clrwdt
wend

After that the PIC shuts down, maybe you can implement a check of the receive flag to blink a LED...

How does your CAN setup look like?
My init sequence:


CANCON = 128 'Config Mode
brgcon1 = $01
brgcon2 = $BF
brgcon3 = $07
ciocon = 32 'CANTX recessive high

TXB0SIDH = $AE 'Identifier für Klemmenstatus =$575
TXB0SIDL = $A0 'Identifier für Klemmenstatus
TXB0DLC = 1 ' Länge 1 Byte

CANCON = 0 ' Normal Mode

I got the BRGCON values from the Microchip calculator for 100 kBaud and 10MHz clock.

I see from your post, that you more or less already tried out the tips I could give you.

I also noticed that the terminator resistor disturbed transmission, so I skipped it.
Which CAN transceiver are you using?
Can you post some snippets of your code?
If your PIC has internal PLL, did you configure it correctly?

Regards,

Ingo

Timmoore
- 20th August 2009, 18:11
Hi Ingo

Code wise, I am doing the same configuration with the exception that my BRG con values are for 125Khz and 16Mhz. Beyond that I am setting data byte values for tx. I will send you code for perusal in due course.

I think given what I am seeing with the scope, it is clear that there is something fundamentally wrong with timing. You are correct in your suggestion about the PLL. This is the first time I have used this particular pic. I have just checked the data sheet and it looks like I may have to set the configuration. Need to read up a bit as there are a few registers involved.

The transceiver is MCP 2551, I am seeing very clean signals on the twp so I dont think the resistors are presenting a problem. Depending on the outcome of the PLL investigation, I can remove one, then both to check the effect.

Rgds

Tim

Timmoore
- 21st August 2009, 18:28
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

inse
- 21st August 2009, 20:46
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

Timmoore
- 22nd August 2009, 14:06
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.

zx81sp
- 16th February 2010, 12:30
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

inse
- 17th February 2010, 12:39
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!

zx81sp
- 18th February 2010, 18:32
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

zx81sp
- 21st February 2010, 17:24
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):



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?

inse
- 21st February 2010, 19:10
Your program looks OK at first glance altough I didn't verify the BRG values.
What puzzles me is: do you really use an PIC18F258 in combination with an MCP2510?
Or do you use the PIC18F258 with the MCP2551 CAN transceiver as mentioned earlier?
The MCP2551 has a standby pin - how is it controlled?
It must be tied low for operation!
Check this and we can do further examination on the code.
Are you sure you have set up and connected your CAN-USB adaptor correctly?
A scope could show you if there is any activity on the bus.

zx81sp
- 21st February 2010, 21:30
Your program looks OK at first glance altough I didn't verify the BRG values.

I've got the BRG values using Microchip Can Bit Timing Calculator (from www.intrepidcs.com), it claims to be compatible with MCP2510, I think that the values are same for my MCP2551, also tried with the default values that the calculator shows (03/BF/07) but anyway nothing changes.


What puzzles me is: do you really use an PIC18F258 in combination with an MCP2510?
Or do you use the PIC18F258 with the MCP2551 CAN transceiver as mentioned earlier?

Sorry, the right combo is 18F258+MCP2551.


The MCP2551 has a standby pin - how is it controlled?
It must be tied low for operation!

RS Pin is connected to GND


Check this and we can do further examination on the code.
Are you sure you have set up and connected your CAN-USB adaptor correctly?

When I connect the CANUSB to the CAN network I can see the traffic, so I think that the problem it's not there. When I connect the adapter directly to the board I can't see activity, if the PBP code is right I must receive a command every second.

Thanks for your help!

inse
- 22nd February 2010, 18:14
Another point to check just came into my mind:
The PIC you are using has internal PLL - did you check its configuration?

zx81sp
- 22nd February 2010, 20:32
Another point to check just came into my mind:
The PIC you are using has internal PLL - did you check its configuration?

Not sure if this is what you ask for, but I've checked the programmer options, OSC is setting to HS, there is a option for HS w/PLL on but it's not enabled.

Can you verify the BRG values?

inse
- 22nd February 2010, 21:37
Hmm...
I got $03 $BF $07 for 20MHz and 100kbps from the calculator.

Furthermore, your CIOCON configuration differs from the one I suggested, try setting it to $20.
Another idea: I am not sure whether you have to set the DLC register in config mode or if normal mode is possible..

zx81sp
- 22nd February 2010, 21:58
Hmm...
I calculated $03 $BF $07 for 20MHz and 100kbps

Yes, in my previous post you can see that I've tried also with that values with no luck :( I'm really frustrated :mad:

inse
- 27th February 2010, 12:33
For anyone willing to dig into the CAN-bus I conclude this thread:

ZX81SP finally got his code running - it was a matter of oscillator configuration.
I have listed the necessary steps for setting up CAN above - except that the CAN Bit timing calculator is available from www.intrepidcs.com, not from Microchip.

Christopher4187
- 2nd July 2012, 17:13
This is about the only useful thread I've found regarding CAN communication. This is way above my level of expertise but I was never one to give up. My objective is to communicate with a locomotive that uses CAN communication.

Can anyone tell me if I use the same PIC and code above, will it allow me to transmit data on the CAN network? I realize my configurations will probably be different but what about the RX side of the CAN? Will it be easier than the transmit?

inse
- 3rd July 2012, 19:10
Christopher,
it's hard to give sensible answers as you supply no details about the project.
Do you know what the communication is going to look like?
If you are going to setup a bidirectional communication a CAN analyzer will be a very useful tool.

Ingo

Christopher4187
- 3rd July 2012, 22:24
Glad to see you are still around, Ingo. I will create another thread so I don't hijack this one.