+ Post New Thread
Results 1 to 7 of 7
-
30th October 2019, 09:34 #1
- Join Date
- Oct 2019
- Posts
- 2
- Helped
- 0 / 0
- Points
- 43
- Level
- 1
CAN communication between two nodes (PIC18F458 and MCP2551 ) hardware
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); } } }
let me correct myself.thank you for your time .Last edited by bassa; 30th October 2019 at 10:22. Reason: add code syntax
-
Advertisement
-
30th October 2019, 16:34 #2
- Join Date
- Jan 2008
- Location
- Bochum, Germany
- Posts
- 45,804
- Helped
- 13925 / 13925
- Points
- 262,035
- Level
- 100
Re: CAN communication between two nodes (PIC18F458 and MCP2551 ) hardware
A short CAN test connection between two nodes must not necessary use a twisted pair but a termination resistor is always required.
-
1st November 2019, 01:00 #3
Re: CAN communication between two nodes (PIC18F458 and MCP2551 ) hardware
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.
-
Advertisement
-
1st November 2019, 08:48 #4
Re: CAN communication between two nodes (PIC18F458 and MCP2551 ) hardware
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.
-
13th November 2019, 18:50 #5
- Join Date
- Oct 2019
- Posts
- 2
- Helped
- 0 / 0
- Points
- 43
- Level
- 1
Re: CAN communication between two nodes (PIC18F458 and MCP2551 ) hardware
thank you for ur response. Though i included 120 ohm i m not getting output.
-
13th November 2019, 23:34 #6
- Join Date
- Jan 2015
- Posts
- 1,109
- Helped
- 349 / 349
- Points
- 7,924
- Level
- 21
Re: CAN communication between two nodes (PIC18F458 and MCP2551 ) hardware
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
-
Advertisement
-
19th November 2019, 06:56 #7
Re: CAN communication between two nodes (PIC18F458 and MCP2551 ) hardware
TRISC should be 0xC0 for both PIC18F devices.
+ Post New Thread
Please login