View previous topic :: View next topic |
Author |
Message |
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 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 15, 2008 1:03 pm |
|
|
Try this program. It blinks an LED on Pin B0. Does this program
compile with no errors ?
Code: |
#include <18F448.h>
#fuses HS, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main()
{
while(1)
{
output_high(PIN_B0);
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
} |
|
|
|
Stephane Guest
|
LEDS |
Posted: Fri Jan 18, 2008 10:31 am |
|
|
Sorry I didn't see your answer (i didn't see there was a second page)
Same error message with this little program...
Yet i have tried another simple program with leds for a 18f448 just before yours (CAN BUS) and it was OK.
... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 18, 2008 11:37 am |
|
|
I was able to duplicate your problem. I commented out the #device
statement in the 18F448.h file, and I got the error message that you get.
Example:
Quote: | // Standard Header file for the PIC18F448 device
//#device PIC18F448
#nolist |
Question:
Did you do this ? Did you comment out the #device statement ?
If so, please put it back the way it was. Never edit the .H files. |
|
|
Stephane Guest
|
pic18f448 |
Posted: Sun Jan 20, 2008 12:15 pm |
|
|
First Thank you to help me
I didn't modify the 18f448.h :
I know this is a already made file and i don't touch this kind of file
I cannot find these lines in 18f448.h :
// Standard Header file for the PIC18F448 device
#device PIC18F448
#nolist
Maybe i've got a bad 18f448.h file ?
Another possible reason :
When i create a new PIC WIZARD PROJECT i choose the 18f448 PIC, 8 000 000 Mhz, High Speed Oscillator and i choose BUS CAN > USE CAN BUS
Do i have to choose other options ?
thank you again |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 20, 2008 1:57 pm |
|
|
I don't use the IDE version of the CCS compiler (such as PCWH).
I use the "command line" version, and I use MPLAB as the IDE.
In the command line version, all .H files have a #device statement for
the PIC at the start of the file.
I don't know what CCS is doing in the IDE version. |
|
|
Stephane Guest
|
News... |
Posted: Tue Jan 22, 2008 11:23 am |
|
|
i reinstalled my PIC C Compiler and i didn't update it
my current version in PCWH 3.212
And the program is compiled correctly ....
So for the moment it's OK !
I'll try it with 2 easypic flash modules soon and i'll say to you if it works fine.
Thank you for the time you gave to me
Feedback soon
(Next tuesday i think)
Stephane |
|
|
vinh Guest
|
new question |
Posted: Mon Mar 03, 2008 10:10 pm |
|
|
hi
I work in forklift truck field and I have 1 question to ask:
I have 6 controllers and two resistors 120 ohm and using CAN bus to
communicate. How can controllers communicate in this situation or they
have a CANchip interface that can communicate 6 controllers.
Hoping who know, reply soon.
thanks |
|
|
|