|
|
View previous topic :: View next topic |
Author |
Message |
pic_beginner
Joined: 07 Feb 2011 Posts: 2
|
pic18F2580 and CAN-bus |
Posted: Mon Feb 07, 2011 11:57 am |
|
|
I am trying to connect NOx sensor with my pic via can-bus,
(NOx sensor: 500kbaud, can-format motorola, identifier: standard.)
and I don't receive any messages from it.
loopback mode is ok.
Code: |
#include <18F2580.h>
#fuses INTRC_IO,NOWDT,PROTECT,NOLVP,NODEBUG,EBTRB,NOMCLR
#use delay(clock=16000000)
#include <stdlib.h>
#include <mcp4921_3.c>
#include <mcp4921_4.c>
#include <can-18F4580.c>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define OSC_16MHZ
#define OSC_INTRC
void main(void)
{ can_init();
can_set_mode(CAN_OP_CONFIG);
BRGCON1.brp = 1;
BRGCON1.sjw = 0;
BRGCON2.prseg = 0;
BRGCON2.seg1ph = 4;
BRGCON3.seg2ph = 2;
BRGCON2.sam=1;
BRGCON2.seg2phts=1;
BRGCON3.wakfil=0;
can_set_mode(CAN_OP_NORMAL);
while(TRUE)
{
if ( can_kbhit() ) //if data is waiting in buffer...
{
if(can_getd(ID,&buffer[0], rx_len, rxstat)) { //...then get data from buffer
br=7;
}
else {
br=1;
}
}
//every two seconds, send new data if transmit buffer is empty
if ( can_tbe() )
{
i=can_putd(0x120,&data1[0],8,1,1,0);
//put data on transmit buffer
if (i != 0xFF) { //success, a transmit buffer was open
bt=7;
}
else
{
bt=1;
}
delay_ms(200);
}
}
}
|
|
|
|
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
|
Posted: Tue Feb 08, 2011 6:07 am |
|
|
It would probably help if you set your baud rate to 500k instead of the 800k baud you are currently setting.
Here is how you currently have it:
16,000,000 hertz clock.
All set values are one higher than set so you have a prescale of 2. That's 16M/2 = 8Mhz. Then you've got 1 start TQ, 1 propagation TQ, 5 segment 1 TQ, 3 segment 2 TQ for a total of 10 time quanta. 8000000 / 10 = 800,000 baud. You really need 16 TQ in order to attain 500,000 baud. So, try the following:
Code: |
BRGCON1.brp = 1;
BRGCON1.sjw = 0;
BRGCON2.prseg = 2;
BRGCON2.seg1ph = 5;
BRGCON3.seg2ph = 5;
|
Now it's 1 start, 3 prop, 6 seg1, 6 seg 2 for a total of 16 TQ. 16,000,000Mhz / 2 (baud prescaler) / 16 = 500,000 baud. |
|
|
pic_beginner
Joined: 07 Feb 2011 Posts: 2
|
|
Posted: Thu Feb 10, 2011 11:06 am |
|
|
thanks a lot |
|
|
Barrieri
Joined: 17 Feb 2016 Posts: 1
|
|
Posted: Wed Feb 17, 2016 2:46 am |
|
|
Hi pic_beginner, have you solved your issue regarding the NOx sensor communication ? We are currently trying to communicate to a NOx module from a VW Golf, with a part number 5WK97308, however we are having a hard time trying to find our way. Any help would be greatly appreciated.
Regards |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Wed Feb 17, 2016 5:55 am |
|
|
Hi Barrieri,
Are you aware that the original thread is 5 years old?
Are you aware that this forum is exclusively for the CCS 'C' compiler? Are you using the CCS 'C' compiler?
Have a nice day! _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9216 Location: Greensville,Ontario
|
|
Posted: Wed Feb 17, 2016 6:44 am |
|
|
Hmmm, I got curious about CAN TQ..... (OK I'm delaying snow shovelling...)
Downloaded a BOSCH paper on TQ...YEESH no wonder people have problems with CAN ! 10 friggin pages on how to create a BIT !
There must be a heckuva lot of 'overhead' per byte of data transferred.
Query. Does anyone know how many 'overhead' bytes to a 'data' byte there are?
Jay |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Feb 17, 2016 7:37 am |
|
|
temtronic wrote: | Does anyone know how many 'overhead' bytes to a 'data' byte there are? |
CAN is a bit orientated protocol, therefore its not a question of how many bytes there are, its how many bits. With standard 11-bit addressing there are 44 "overhead", i.e. protocol-related bits, in a frame. It may carry 0 to 8
data bytes, i.e. 0 to 64 data bits. Its also bit-stuffed, and this may add some, or none, extra bits depending on the content of the frame, but no more than one extra bit for every five.
So a full frame is at least 108 bits long. For bus capacity and transmission latency purposes, I tend to estimate frames as 100 bits long for simplicty, and because many will not be full. So at 100kbs, a CAN bus could sustain bursts of 1000 messages per second, and each message takes at least 1ms to send, but in practice you can get no more than around 70% of that capacity, and that's if you're really careful. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|