|
|
View previous topic :: View next topic |
Author |
Message |
quad3
Joined: 23 Jan 2007 Posts: 5
|
CAN problem (beginner) |
Posted: Thu Jan 25, 2007 9:35 am |
|
|
Hi everyone. I'm using CAN for school project but i'm stumped. I'm trying to get 2 PIC 18f458 to talk to each other. i'm using the MCP 2551.
I'm testing the hardware setup using ex_can.c for both PIC. and i'm using 4MHz oscillator
Quote: | #include <18F458.h>
#fuses XT,NOPROTECT,NOLVP,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#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=0;
int1 tx_ext=1;
int tx_len=8;
int tx_pri=3;
int i;
for (i=0;i<8;i++) {
out_data[i]=0xFC;
in_data[i]=0;
}
printf("\r\n\r\nCCS CAN EXAMPLE\r\n");
setup_timer_2(T2_DIV_BY_4,79,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock
can_init();
//can_set_mode(CAN_OP_LOOPBACK); //commented out
enable_interrupts(INT_TIMER2); //enable timer2 interrupt
enable_interrupts(GLOBAL); //enable all interrupts (else timer2 wont happen)
printf("\r\nRunning...");
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
printf("\r\nGOT: BUFF=%U ID=%LU LEN=%U OVF=%U ", rxstat.buffer, rx_id, rx_len, rxstat.err_ovfl);
printf("FILT=%U RTR=%U EXT=%U INV=%U", rxstat.filthit, rxstat.rtr, rxstat.ext, rxstat.inv);
printf("\r\n DATA = ");
for (i=0;i<rx_len> 2000))
{
ms=0;
i=can_putd(tx_id, out_data, tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
if (i != 0xFF) { //success, a transmit buffer was open
printf("\r\nPUT %U: ID=%LU LEN=%U ", i, tx_id, tx_len);
printf("PRI=%U EXT=%U RTR=%U\r\n DATA = ", tx_pri, tx_ext, tx_rtr);
for (i=0;i<tx_len;i++) {
printf("%X ",out_data[i]);
}
printf("\r\n");
}
else { //fail, no transmit buffer was open
printf("\r\nFAIL on PUTD\r\n");
}
}
}
} |
here's the hardware setup
http://i68.photobucket.com/albums/i19/Ms_Aching/canTran.png
from the serial window, i observed than the pic transmitted up to 3 times but after that it went idle (the serial window shows value "00 00 00" over and over again. the pic received nothing.
is there anything wrong with teh hardware or the codes?
how do i troubleshoot? if i were to observe the tx and rx node using oscilloscope, what should i expect to see?
thank you to those who'll help |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 25, 2007 12:40 pm |
|
|
You need to read Humberto's post at the top of the list. It says to
use a Code block (not Quote) and especially it says to Disable HTML.
Because you have HTML enabled, it garbled part of your post. Example:
Quote: | for (i=0;i<rx_len> 2000))
{ |
Also, in your hardware diagram, you show two 120 ohm resistors.
I'm not sure how to interpret your drawing. Do you have two
resistors on each MCP2551 driver chip ? If so, that's wrong.
There should one resistor for each CAN node. In other words,
each MCP2551 should have one 120 ohm resistor very close to it,
connected across the CANL and CANH lines.
On my CAN bus board, I set the Rs resistor = 0 ohms. |
|
|
quad3
Joined: 23 Jan 2007 Posts: 5
|
|
Posted: Tue Jan 30, 2007 12:05 pm |
|
|
thanks for replying PCM programmer. took note on the terminating resistor.
but after i tested the hardware again, the outcome is still the same( the serial windows show the node transmit 3 times only but the scope still detects squarewaves)
I also found a thread with similar problem
http://www.ccsinfo.com/forum/viewtopic.php?t=23825&highlight=excan
so i tried the solution which is to define CAN_ENABLE_DRIVE_HIGH to 1
Code: |
#define CAN_ENABLE_DRIVE_HIGH 1
#include <can-18xxx8.c> |
but still the no change. i thought from the older thread this should have solve the problem? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 30, 2007 12:51 pm |
|
|
Try to make each node work separately in Loopback mode.
See the following thread for sample code.
http://www.ccsinfo.com/forum/viewtopic.php?t=20106
If you can get each node to work in loopback mode, it will
prove that each PIC is running OK, and the CAN module is
working, and that the compiler generates good code. |
|
|
quad3
Joined: 23 Jan 2007 Posts: 5
|
|
Posted: Tue Jan 30, 2007 6:50 pm |
|
|
the 2 nodes work fine separately. i've tried the loopback mode on each node |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 30, 2007 9:06 pm |
|
|
I have a couple 18F458 boards. I can test it Wednesday morning.
Edited to add:
I can do it on Sunday. I got the two boards out of the storage closet
and made power supply adapters for them, so I can use them.
I can't commit any more time to it during the week.
--------
Last edited by PCM programmer on Thu Feb 01, 2007 2:53 pm; edited 1 time in total |
|
|
sjbaxter
Joined: 26 Jan 2006 Posts: 141 Location: Cheshire, UK
|
|
Posted: Wed Jan 31, 2007 5:59 am |
|
|
Have a look at:
http://www.ccsinfo.com/forum/viewtopic.php?t=29646
NOTE TO FORUM MODERATORS:
If guests (non registered members) were stopped from posting and forced to register, then quad3 could PM the guest on the thread in the link above and between them they could sort this out !! Isn't that what forums are all about ?! _________________ Regards,
Simon. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 04, 2007 5:15 pm |
|
|
Here is a simple test program which allows you to prove that two CAN
boards can talk to each other. Each board has an 18F458 PIC on it,
with an MCP2551 (or equivalent) CAN bus driver and a 120 ohm resistor
connected across the CANL and CANH pins (one on each board).
I tested this program with PCH vs. 3.249.
What this program does:
First you type in characters on a terminal window in your PC. The
chars are sent to Board #1 by an RS-232 cable. Then Board #1
transmits them to Board #2 over the CAN bus. Board #2 then
sends each character back to Board #1 over the CAN bus. Finally,
Board #1 sends them back to the PC over the serial cable. It's just
a big loop.
So when you run this program, you just open up a terminal window
on your PC, and start typing. For example, type:
You should see it appear on your terminal window. (Make sure you
have "local echo" turned off in your terminal program).
Here is the signal path that the characters follow:
Code: |
rs232 CAN CAN rs232
PC -----> Board #1 ---> Board #2 ---> Board #1 -----> PC
|
This diagram shows the cables:
Code: |
rs232 CAN
PC <-----> Board1 <---> Board2
|
The program below is in two parts. It has to be compiled separately
for each of the two CAN boards. After you compile the code for each
specific board, you must program it into the PIC on that board with
your programmer. Remember, this was only tested with PCH vs. 3.249.
The board for which the program is compiled is controlled by the
following line within the program:
If it's uncommented, the program will be compiled for Board #1.
If it's commented out, then the program will be for Board #2.
Here is the program:
Code: |
#include <18F458.h>
#fuses HS, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <can-18xxx8.c>
#define BOARD1_ID 24
#define BOARD2_ID 25
// Uncomment this line to compile code for Board #1.
// Comment it out to compile for Board #2.
#define BOARD1 1
//======================================
void main()
{
struct rx_stat rxstat;
int32 rx_id;
int32 tx_id;
int8 rx_len;
int8 buffer[8];
can_init();
#ifdef BOARD1 // For Board #1
while(1)
{
buffer[0] = getc(); // Wait for a character
// Transmit it to board #2.
can_putd(BOARD2_ID, buffer, 1, 1, 1, 0);
buffer[0] = 0; // Clear the buffer byte
// Wait for the char to be echoed back.
while(!can_kbhit());
if(can_getd(rx_id, buffer, rx_len, rxstat))
{
if(rx_id == BOARD1_ID) // Is it for this board ?
{
putc(buffer[0]); // If so, display the char
}
}
}
#else // For Board #2
while(1)
{
if(can_kbhit()) // Message available ?
{
// If so, get the message.
if(can_getd(rx_id, buffer, rx_len, rxstat))
{
if(rx_id == BOARD2_ID) // Is it for this board ?
{
// If so, echo back the character.
can_putd(BOARD1_ID, buffer, 1, 1, 1, 0);
}
}
}
}
#endif
} |
|
|
|
Stephane Guest
|
#define BOARD1 1 |
Posted: Sun Jan 13, 2008 10:30 am |
|
|
Hello PCM Programmer
I'm french and there's a thing i'm not sure to understand when you wrote :
The board for which the program is compiled is controlled by the
following line within the program:
Quote:
#define BOARD1 1
If it's uncommented, the program will be compiled for Board #1.
If it's commented out, then the program will be for Board #2.
Can you tell me if i'm right if i use this for Board1 :
#define BOARD1 1
And this for Board2 :
#define BOARD1 OUT
Am i wrong ?
I will test this next week with PCWH and i'll tell you if this program work on it
Thanks
Stephane |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 13, 2008 10:33 am |
|
|
"Commented out" means to make the line into a comment by
adding the two slashes in front of it. Then the compiler will
consider the line to be a comment and it won't be compiled.
Do it this way:
Code: | // #define BOARD1 1 |
|
|
|
Stephane Guest
|
long CCP_1 |
Posted: Sun Jan 13, 2008 10:38 am |
|
|
Another question :...
When i try to compile your source code i've got a error message in the
18F448.h
A #DEVICE required before this line :
long CCP_1;
In the same file there's :
long CCP_2;
and there's no error message for this line..
if i add a #define in front of long CCP_1; another error message appears...
An idea ?
Thanks again
Stephane |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 13, 2008 10:51 am |
|
|
You are a beginner with the CCS compiler. Please take some time
to learn to use the compiler before starting a project on the CAN bus.
Learn to use the compiler. Make simple programs such as "blinking
an LED". Then when you know the compiler, you can do the CAN bus
projects. |
|
|
Stephane Guest
|
CAN |
Posted: Mon Jan 14, 2008 4:04 am |
|
|
Thank you for your rapid answer
Sorry to disturb yourself
But... i'm not a beginner on CCS...
I can play with LEDS, with the RS232 without problem..
But there are some english terms (i'm french) i do not understand
"commented OUT" is easy to understand for an english speaking person.. it may be confused for a foreign one
And some technical problems like this new one i've tried to resolve
long CCP_1; ?
Thanks for your understanding
Stephane |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 14, 2008 12:06 pm |
|
|
1. What is your compiler version ?
2. Try re-installing the compiler. See if that fixes the problem. |
|
|
Stephane Guest
|
PCWH |
Posted: Tue Jan 15, 2008 12:52 pm |
|
|
Hello
My software version : PCWH 4.058
Reinstall : the same problem.
I can't understand the error message is not in the program but in the 18F448.h file...
The program is the one you give in this topic.
I hope it will work, this will be a good beginning to work with CAN bus and CCS Compiler
Thank you again for your help
Stephane |
|
|
|
|
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
|