View previous topic :: View next topic |
Author |
Message |
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
dsPIC CAN |
Posted: Wed Jun 13, 2018 1:46 pm |
|
|
Hello,
I am trying to setup dspic33fj64gp802 can module with vp232 driver to receive data from car sensors like speedometer, rpm etc.
I have some experience with dspic but I have no idea how CAN works.
And yes I am trying to figure this out but it is way more complex than uart or spi etc...
Is there any example available?
Thanks in advance. _________________ George. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Jun 13, 2018 3:03 pm |
|
|
Obvious place to start would be the CCS supplied CAN examples, probably need to be 'tweaked' for your PIC, but it would be where I'd start.
From what I've read and seen her , CAN has a LOT of 'overhead' and 'complexity' compared to other serial communication protocols. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Thu Jun 14, 2018 12:38 am |
|
|
As a comment, you need to get something like a PC CAN device, to give you some hope of doing debugging.
Yes CAN is a complex bus, but a lot of this is handled for you by the CAN controller. However this involves a lot of settings.
It is a differential bus (like RS485), but sends a '0' by driving the wires and a '1' by leaving the wires undriven. Each transmitting device is required to monitor the bus as it sends. Now the key here is that if two devices both start at the same time, a transaction begins with the ID. A higher ID number will have a '1' earlier in the message than a low number. The '1' is the undriven state, so at this point, the device with the higher number will see a '0' being sent on the bus, from the other device. At this point it must give up and relinquish the bus. A low ID will always win.
Everything sent is sent as 'messages', and the key is that the controller has sophisticated filtering to determine which messages are actually passed through to the processor. You can set bits in the filter mask to determine which messages will actually be passed to you to look at. Lovely thing is you don't then receive lots of messages you don't want.
Before starting you need to know the clock rate for your bus, and use one of the online tools to calculate the timing parameters needed to setup the PIC to match this speed. Get this wrong and you won't get anywhere!.
I'd honestly start with a PC CAN driver, and just get yourself able to receive single messages from this, before trying to get involved with talking to other devices. Getting all the settings right is complex. |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Thu Jun 14, 2018 3:50 am |
|
|
Thanks for all replies.
I already ordered Microchip usb CAN analyzer today from mouser.
I was hoping for a working example to save me some time.
I am not lazy. But Microchip's CAN is not as easy at first look at least.
Thanks again. _________________ George. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Thu Jun 14, 2018 1:21 pm |
|
|
There are three CAN examples with the compiler. Provided you have a recent compiler these have been updated to support the Ds chips. |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Sat Jun 16, 2018 12:13 pm |
|
|
I am trying the examples and of course I am lost!
The dispic is running at 79227500 Hz and I need 500Kbps for the can bus.
So what are the settings???
Code: |
#define CAN_BRG_PRESCALAR 4 //Set CAN Baud Rate to 125K
#define CAN_BRG_PHASE_SEGMENT_1 1 //Tq = (2(1+PRESCALAR))/(Fosc/2)
#define CAN_BRG_PHASE_SEGMENT_2 1 //Tq = (2(1+4)/(20000000/2) = 0.000001
#define CAN_BRG_PROPAGATION_TIME 2 //Baud Rate = 1/(((PHASE_SEGMENT_1+1)+(PHASE_SEGMENT_2+1)+(PROPAGATION_TIME+1)+(JUMP_WIDTH+1))*Tq)
#define CAN_BRG_SYNCH_JUMP_WIDTH 0 //Baud Rate = 1/(((1+1)+(1+1)+(2+1)+(0+1))*0.000001) = 125000
|
_________________ George. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Jun 16, 2018 2:17 pm |
|
|
hmm OK, I don't use that PIC or CAN but given the clock speed and CAN speed you need a divider of 158.455, YUCK....(79,227,500 / 500,000)
Any chance you can change the PIC clock speed for a better divider? Maybe read the CAN section for allowable 'mismatch' in speed. The problem there is if the other device is say 3% high while you are 2% low, it's really off 5% and that may not be acceptable.
In the past I've used xtals like 2.457600MHZ to get exact UART clock speeds. While some mismatch may work 'on the bench', in the real world where it gets cold or hot, 'funny' things can happen !
Good news is ,several people here have used CAN, so just wait a bit !
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Jun 17, 2018 2:55 am |
|
|
Honestly you need to be using a crystal.
The internal oscillator is not guaranteed to be stable enough for CAN, and doesn't give good division factors. You are going to start with a timing error of nearly 1%, because of the division factors available, and then have the internal oscillator drift on top of this. This is not good enough for even the worst case CAN system.... |
|
|
|