View previous topic :: View next topic |
Author |
Message |
denkid1 Guest
|
I have a some problem about a CAN Communications |
Posted: Mon Jan 17, 2005 8:24 pm |
|
|
Hello all,
CPU : 18F258
OSC : 40Mhz (HS with PLL)
Compiler CCSC3190
I don't know initializing can
i wanna 500Kbps speed
this code is not response.
as you can see, any data receiving, LED Flicking
but, LED is not active
Code: |
#include "18F248.h"
#device *=16 adc=10
#use delay(clock=40000000)
#include "can-18xxx8.c"
int16 ms;
#int_timer2
void isr_timer2(void) {
ms++; //keep a running timer that increments every milli-second
}
void main() {
struct rx_stat rxstat;
int32 rx_id;
int in_data[8];
int rx_len;
//send a request (tx_rtr=1) for 8 bytes of data (tx_len=8) from id 24 (tx_id=24)
int out_data[8];
int32 tx_id=24;
int1 tx_rtr=1;
int1 tx_ext=0;
int tx_len=8;
int tx_pri=3;
int i;
for (i=0;i<8;i++) {
out_data[i]=0;
in_data[i]=0;
}
setup_timer_2(T2_DIV_BY_4,159,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock
can_init();
enable_interrupts(INT_TIMER2); //enable timer2 interrupt
enable_interrupts(GLOBAL); //enable all interrupts (else timer2 wont happen)
while(TRUE)
{
if ( can_kbhit() ) //if data is waiting in buffer...
{
// if(can_getd(rx_id, &in_data[0], rx_len, rxstat))
// { //...then get data from buffer
output_low(PIN_B4);
delay_ms(100);
output_high(PIN_B4);
}
}
}
}
|
## Set Baudrates
Code: |
void can_set_baud(void) {
// 40Mhz 500kbps
BRGCON1.brp=4;
BRGCON1.sjw=1;
BRGCON2.prseg=1;
BRGCON2.seg1ph=4;
BRGCON2.sam=0; //0
BRGCON2.seg2phts=1; //1
BRGCON3.seg2ph=4;
BRGCON3.wakfil=0; //0
}
void can_set_mode(CAN_OP_MODE mode) {
CANCON.reqop=mode;
while( (CANSTAT.opmode) != mode );
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 18, 2005 12:44 am |
|
|
My suggestion is to first get your CAN demo board working with
the CCS example files and drivers. Don't try to invent an example
by yourself. Use the search engine on this board to search for
posts about how to make the CCS examples work.
When you have the basic example working, then try to increase the
speed. Get a CAN bus bit timing calculator and use it to find the
parameters for the higher speed. Here is a link to one of the calculators
available on the net
http://intrepidcs.com/modules/CmodsDownload/upload/Software/MBTime.zip
Edit: Updated link.
Last edited by PCM programmer on Fri Oct 03, 2008 2:59 pm; edited 2 times in total |
|
|
denkid1 Guest
|
[re] very thanks |
Posted: Tue Jan 18, 2005 2:06 am |
|
|
thank you very much... |
|
|
JPA Guest
|
Re: I have a some problem about a CAN Communications |
Posted: Tue Jan 18, 2005 3:47 am |
|
|
## Set Baudrates
Code: |
void can_set_baud(void) {
// 40Mhz 500kbps
BRGCON1.brp=4;
BRGCON1.sjw=1;
BRGCON2.prseg=1;
BRGCON2.seg1ph=4;
BRGCON2.sam=0; //0
BRGCON2.seg2phts=1; //1
BRGCON3.seg2ph=4;
BRGCON3.wakfil=0; //0
}
void can_set_mode(CAN_OP_MODE mode) {
CANCON.reqop=mode;
while( (CANSTAT.opmode) != mode );
}
| [/quote]
With these settings, I think the baud rate will be 307 692 bit/s and not 500 00 ! If you read PIC datasheet, you'll see that:
BRGCON1.brp=4; (means: Tq = (2 x 5) / 40 000 000 = 0.25 �s)
BRGCON1.sjw=1; (Syn jump width = 2 Tq)
BRGCON2.prseg=1; (Prop Seg = 2 Tq)
BRGCON2.seg1ph=4; (Ph1 seg = 5 Tq)
BRGCON3.seg2ph=4; (Ph2 seg = 5 Tq)
So overall bit length is:
Sync seg + Propseg + Ph1 seg + Ph2 seg = 1 + 2 + 5 +5 = 13 Tq which is 3.25 �s or a baud rate of 307 692 bits/s.
Correct settings for a 500 kbits baud rate should be:
BRGCON1.brp=3; (means: Tq = (2 x 4) / 40 000 000 = 0.20 �s)
BRGCON2.prseg=0; (Prop Seg = 1 Tq)
BRGCON2.seg1ph=3; (Ph1 seg = 4 Tq)
BRGCON3.seg2ph=3; (Ph2 seg = 4 Tq)
An introduction to Microchip CAN module can be found in the MCP2515 datasheet, which obviously uses the same module that the PIC18Fxx8 for CAN bus.
Also note that when I first try to use CAN on PIC (16F84 + MCP2515), the CCS supplied CAN library was too big to fit into program memory. From that I digged into the libraries and found that they were really not optimized for code size; so I rewrote one for my own use, which fit in the little memory of PC16F84 and leaves enough room for doing other tasks (RS232 communication with PC, input scan and transmit of the input port states).
Hope this will help you. |
|
|
denkid1 Guest
|
[re] Thanks a lot |
Posted: Wed Jan 19, 2005 4:24 am |
|
|
Thank you for help. |
|
|
saucompeng
Joined: 19 Jul 2011 Posts: 4
|
CanBaudrate |
Posted: Tue Sep 04, 2012 9:19 am |
|
|
I have same problem.
I want to settings for 125KBps hS with PLL 40 mhz :/ |
|
|
|