Author Topic: CAN bus using PIC  (Read 7664 times)

0 Members and 1 Guest are viewing this topic.

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
CAN bus using PIC
« on: April 04, 2014, 06:20:56 am »
I am trying to setup a CAN bus for simple communication between 2 PIC MCUs. One of them (PIC18F4550) is sending an ASCII character which is read by the other (PIC18F2550) & displayed via UART. I am not able to compile the code in MPLAB-X for the receiving PIC. It shows the following-

CLEAN SUCCESSFUL (total time: 535ms)
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `E:/Microchip/PIC18F2550_C/PIC18F2550_C.X'
make  -f nbproject/Makefile-default.mk dist/default/production/PIC18F2550_C.X.production.hex
make[2]: Entering directory `E:/Microchip/PIC18F2550_C/PIC18F2550_C.X'
"E:\Microchip\XC\xc8\v1.21\bin\xc8.exe" --pass1  --chip=18F2550 -Q -G  --double=32 --float=24 --emi=wordwrite --opt=default,+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=0 --asmlist --summary=default,-psect,-class,+mem,+hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib --output=-mcof,+elf "--errformat=%%f:%%l: error: (%%n) %%s" "--warnformat=%%f:%%l: warning: (%%n) %%s" "--msgformat=%%f:%%l: advisory: (%%n) %%s"    -obuild/default/production/CAN_RX.p1  CAN_RX.c
(908) exit status = 1
CAN_RX.c:87: error: (314) ";" expected
CAN_RX.c:87: warning: (374) missing basic type; int assumed
CAN_RX.c:87: error: (314) ";" expected
CAN_RX.c:87: warning: (374) missing basic type; int assumed
CAN_RX.c:95: error: (314) ";" expected
CAN_RX.c:95: warning: (374) missing basic type; int assumed
CAN_RX.c:95: error: (314) ";" expected
CAN_RX.c:95: warning: (374) missing basic type; int assumed
CAN_RX.c:107: warning: (1385) variable "RC6" is deprecated (declared at E:\Microchip\XC\xc8\v1.21\include\pic18f2550.h:9266)
CAN_RX.c:170: warning: (357) illegal conversion of integer to pointer
CAN_RX.c:170: warning: (357) illegal conversion of integer to pointer
CAN_RX.c:170: warning: (357) illegal conversion of integer to pointer
CAN_RX.c:176: warning: (357) illegal conversion of integer to pointer
CAN_RX.c:176: warning: (357) illegal conversion of integer to pointer
CAN_RX.c:176: warning: (357) illegal conversion of integer to pointer
make[2]: *** [build/default/production/CAN_RX.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `E:/Microchip/PIC18F2550_C/PIC18F2550_C.X'
make[1]: Leaving directory `E:/Microchip/PIC18F2550_C/PIC18F2550_C.X'

BUILD FAILED (exit value 2, total time: 4s)




I am not able to understand the mistake I am making (I suspect that the CAN library functions are not receiving the input arguments in the desired data format but I can't figure out how to go around it)
Code I used-

[See attachment]
« Last Edit: April 04, 2014, 08:29:47 am by GeoffS »
 

Offline Lajon

  • Contributor
  • Posts: 31
  • Country: se
Re: CAN bus using PIC
« Reply #1 on: April 04, 2014, 08:43:07 pm »
 
Quote
  unsigned long msg_ID_1=0;
You can't assign a value in the declaration of a union member, remove the =0 assignment.
 
Quote
          msg_stat=CAN2510DataRead(CAN2510_RXB0, *msgID0, *numbytes0, *rxdata0);
 
CAN2510DataRead expects 3 pointers, your msgID0, numbytes0 and rxdata0 are already pointers, so don't dereference them, i.e., the call should be
msg_stat=CAN2510DataRead(CAN2510_RXB0, msgID0, numbytes0, rxdata0);
/Lars
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: CAN bus using PIC
« Reply #2 on: April 09, 2014, 05:23:49 pm »
 
Quote
  unsigned long msg_ID_1=0;
You can't assign a value in the declaration of a union member, remove the =0 assignment.
 
Quote
          msg_stat=CAN2510DataRead(CAN2510_RXB0, *msgID0, *numbytes0, *rxdata0);
 
CAN2510DataRead expects 3 pointers, your msgID0, numbytes0 and rxdata0 are already pointers, so don't dereference them, i.e., the call should be
msg_stat=CAN2510DataRead(CAN2510_RXB0, msgID0, numbytes0, rxdata0);
/Lars

Did as you said. Thanks a ton man. But if you don't mind, can you please explain the pointer problem? I haven't worked with pointers before much so I didn't understand the reason of error properly.
 

Offline aroby

  • Regular Contributor
  • *
  • Posts: 217
  • Country: us
Re: CAN bus using PIC
« Reply #3 on: April 09, 2014, 08:20:03 pm »
But if you don't mind, can you please explain the pointer problem? I haven't worked with pointers before much so I didn't understand the reason of error properly.

If the function accepts a pointer, you need to send it a pointer.  Dereferencing a pointer means retrieving the value at the address contained in the pointer.  So if numbytes0 is a pointer, when you say *numbytes0, you are referring to what it points to, not the pointer itself.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: CAN bus using PIC
« Reply #4 on: April 10, 2014, 01:51:54 am »
But if you don't mind, can you please explain the pointer problem? I haven't worked with pointers before much so I didn't understand the reason of error properly.

If the function accepts a pointer, you need to send it a pointer.  Dereferencing a pointer means retrieving the value at the address contained in the pointer.  So if numbytes0 is a pointer, when you say *numbytes0, you are referring to what it points to, not the pointer itself.

Okay that makes sense. And the union problem, it applies to structures as well?
 

Offline jav

  • Contributor
  • Posts: 37
Re: CAN bus using PIC
« Reply #5 on: April 10, 2014, 08:20:55 am »
Okay that makes sense. And the union problem, it applies to structures as well?
Unions/structs can be initialized this way:

Code: [Select]
union can2510_msg_ID_1
{
    unsigned long msg_ID_1;
    unsigned char msg_ID_UART_compatible[3];
}can_msg_ID_1 = { 0 };

If you're using ANSI C99 (I don't know which C flavor your compiler uses) you can do:

Code: [Select]
union can2510_msg_ID_1
{
    unsigned long msg_ID_1;
    unsigned char msg_ID_UART_compatible[3];
}can_msg_ID_1 = { .msg_ID_1 = 0 };

to select which members of the struct/union are initialized.
 

Offline Udayan SinhaTopic starter

  • Regular Contributor
  • *
  • Posts: 71
Re: CAN bus using PIC
« Reply #6 on: April 10, 2014, 12:14:16 pm »
Okay that makes sense. And the union problem, it applies to structures as well?
Unions/structs can be initialized this way:

Code: [Select]
union can2510_msg_ID_1
{
    unsigned long msg_ID_1;
    unsigned char msg_ID_UART_compatible[3];
}can_msg_ID_1 = { 0 };

If you're using ANSI C99 (I don't know which C flavor your compiler uses) you can do:

Code: [Select]
union can2510_msg_ID_1
{
    unsigned long msg_ID_1;
    unsigned char msg_ID_UART_compatible[3];
}can_msg_ID_1 = { .msg_ID_1 = 0 };

to select which members of the struct/union are initialized.

Oh...okay. Thanks
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf