Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

CAN communication between two nodes (PIC18F458 and MCP2551 ) hardware

Status
Not open for further replies.

Megala

Newbie level 2
Joined
Oct 30, 2019
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
54
greetings,
i m doing a project to communicate between two micro controllers (pic18f458) using CAN. i referred the book 'Advanced PIC micro controller project in C' (author: Dogan ibrahim). i compiled the code using Mikro C . i m using PIC development kit board for my project. my project is to sense temperature from one node and it should be displayed in another node. but while doing hardware part i m not even getting transmission. that means CAN bus is not established. i have a doubt in developing CAN bus in hardware...
1. is that twisted cable is compulsory between two CAN transceiver modules?
2. is termination required between two MCP2551 module?

and my code is

for temperature collector node is




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
void main()
{
unsigned char temperature;
unsigned char RxTx_Data[8];
long id;
unsigned short CAN_Init_flags, dt, CAN_Send_flags,CAN_Read_flags;
unsigned short Rx_Data_Len;
char txt[4];
unsigned long mV;
unsigned int temp;
//Lcd_Init();
TRISA = 0XFF;
TRISB = 0x08;
ADC_Init();
 
 
/*CAN_Init_Flags = 0;
CAN_Send_Flags = 0;
CAN_Rcv_Flags  = 0;    */
 
 
CAN_Init_flags = _CAN_CONFIG_SAMPLE_THRICE &
                 _CAN_CONFIG_PHSEG2_PRG_ON &
                 _CAN_CONFIG_STD_MSG &
                 _CAN_CONFIG_DBL_BUFFER_ON &
                 _CAN_CONFIG_VALID_XTD_MSG &
                 _CAN_CONFIG_LINE_FILTER_OFF;
 
CAN_Send_flags = _CAN_TX_PRIORITY_0 &
                 _CAN_TX_XTD_FRAME &
                 _CAN_TX_NO_RTR_FRAME;
CAN_Read_flags = 0;
 
CANInitialize(1,1,6,7,6, CAN_Init_flags);
 
CANSetOperationMode(_CAN_MODE_CONFIG, 0xFF);
CANSetMask(_CAN_MASK_B1,-1, _CAN_CONFIG_XTD_MSG);
CANSetMask(_CAN_MASK_B2,-1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(_CAN_FILTER_B2_F3,500,_CAN_CONFIG_XTD_MSG);
CANSetOperationMode(_CAN_MODE_NORMAL, 0xFF);
 
 
for(;;)
{
dt=0;
while(!dt)
dt= CANRead(&id, RxTx_Data,&Rx_Data_Len,&CAN_Read_flags);
if(id==500 && RxTx_Data[0]=='T')
{
temp=Adc_Read(0);
mV=(unsigned long)temp*5000/1024;
temperature=mV/10;
RxTx_Data[0]=temperature;
id=3;
CANWrite(id, RxTx_Data, 1, CAN_Send_flags);
}
}
}
 
 
and for display node is
 
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
 
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
 
void main()
{
unsigned char temperature;
char RxTx_Data[8];
unsigned short CAN_Init_flags,dt, CAN_Send_flags,CAN_Read_flags;
char txt[4];
unsigned short Rx_Data_Len;
long id;
TRISC = 0X00;
TRISB = 0x08;
PORTC=0;
 
 
/*CAN_Init_Flags = 0;
CAN_Send_Flags = 0;
CAN_Rcv_Flags  = 0;    */
 
 
CAN_Init_flags = _CAN_CONFIG_SAMPLE_THRICE &
                 _CAN_CONFIG_PHSEG2_PRG_ON &
                 _CAN_CONFIG_STD_MSG &
                 _CAN_CONFIG_DBL_BUFFER_ON &
                 _CAN_CONFIG_VALID_XTD_MSG &
                 _CAN_CONFIG_LINE_FILTER_OFF;
CAN_Send_flags = _CAN_TX_PRIORITY_0 &
                 _CAN_TX_XTD_FRAME &
                 _CAN_TX_NO_RTR_FRAME;
CAN_Read_flags = 0;
 
CANInitialize(1,1,6,7,6, CAN_Init_flags);
 
CANSetOperationMode(_CAN_MODE_CONFIG, 0xFF);
CANSetMask(_CAN_MASK_B1,-1, _CAN_CONFIG_XTD_MSG);
CANSetMask(_CAN_MASK_B2,-1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(_CAN_FILTER_B2_F3,3,_CAN_CONFIG_XTD_MSG);
CANSetOperationMode(_CAN_MODE_NORMAL, 0xFF);
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"CAN BUS");
Delay_ms(1000);
for(;;)
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"temp");
Delay_ms(1000);
RxTx_Data[0]='T';
id=500;
CANWrite(id, RxTx_Data,1,CAN_Send_Flags);
dt=0;
while(!dt)
dt= CANRead(&id, RxTx_Data,&Rx_Data_Len,&CAN_Read_flags);
if(id==3)
{
temperature = RxTx_Data[0];
ByteToStr(temperature,txt);
Lcd_Out(1,8,txt);
delay_ms(1000);
}
}
}


i m new to this forum. if i admitted anything in unprofessional manner just make me a note.
let me correct myself.thank you for your time .
 
Last edited by a moderator:

A short CAN test connection between two nodes must not necessary use a twisted pair but a termination resistor is always required.
 

Not related to your issue but you have to replace Rxx_bit in below code


Code C - [expand]
1
2
3
4
5
6
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;



with

LATxx_bit

as you are using PIC18F device.

- - - Updated - - -

Also, I see that the sensor node code is missing the ADCON1 register configuration. It is needed to configure the RA0/AN0 pin as an analog input pin.
 

Most of the MCP2551 modules available on eBay and other online vendor websites have the terminating resistor on them (120R). So, no need for you to add a terminating resistor if you are using such a module.
 

thank you for ur response. Though i included 120 ohm i m not getting output.
 

How are you testing that you are not getting any output?
You say you are using a PIC development board - which one?
Can you toggle the RB3 (CAN output) pin with your own bit of code?
Where did you cent the CAN library that you are using? Do you trust it? (I say this because it is unusual to have constants that begin with an underscore - symbols that begin with an underscore are typically reserved for the compiler's internal use.)
Why are you using such an old chip?
Susan
 

TRISC should be 0xC0 for both PIC18F devices.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top