View previous topic :: View next topic |
Author |
Message |
foufou_tn
Joined: 13 Apr 2012 Posts: 9
|
can bus help me plz |
Posted: Fri Apr 13, 2012 4:59 pm |
|
|
Hi plz help me to make a can bus connection for 2 pic18f458.
Thats my work:
http://www.monsterup.com/image.php?url=upload/1334391552452.png
http://www.monsterup.com/image.php?url=upload/1334391552390.jpg
I use 8MHZ crystal and i need to send information from board1 to board2 then i put them in lcd but it does not work. The baord2 write in lcd that there is information but the receied information is not the right data :'(
Here is the code for board 1:
Code: |
#include <can-18xxx8.c>
int data[1];
void main()
{
can_init();
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
while(true)
{
data[0] =9;
can_putd(0x400,data,1,1, TRUE,FALSE);
}
}
|
and for board2:
Code: | #include <can-18xxx8.c>
#define LCD_ENABLE_PIN PIN_C0
#define LCD_RS_PIN PIN_C1
#define LCD_RW_PIN PIN_C6
#define LCD_DATA4 PIN_C2
#define LCD_DATA5 PIN_C3
#define LCD_DATA6 PIN_C4
#define LCD_DATA7 PIN_C5
#include <lcd.c>
int buffer[8], rx_len, rx_stat;
int32 rx_id;
int x=0,y=9;
void main()
{
can_init();
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
lcd_init();
while(true)
{
if (can_kbhit())
{
x=5;
lcd_gotoxy(1,1);
printf(lcd_putc,"id=%d",x);
can_getd(rx_id,buffer, rx_len,rx_stat);
if(y==buffer[0])
{lcd_gotoxy(1,2);
printf(lcd_putc,"data=%d",y);}
}
}
}
|
plz i need help in that and sorry for my bad english
[/img]
Last edited by foufou_tn on Sat Apr 14, 2012 2:24 am; edited 2 times in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 13, 2012 8:43 pm |
|
|
Your images are not viewable. You have to be a member of the Futura
forum to see the images. No one is going to sign up on Futura just to
view your images.
This post shows how to connect two 18F458 boards and then test the
CAN bus connection between the two boards:
http://www.ccsinfo.com/forum/viewtopic.php?t=29627&start=7 |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Fri Apr 13, 2012 8:51 pm |
|
|
My experience in CAN is :
1/ Use loopback mode to check whether each Pic is working OK. This also checks if your code works.
2/ Double check the wiring, CAN uses very simple wiring schematic but it can be cumbersome if just one pin is not correctly connected, especially pin Rs has to be sure connected to ground without resistor.
Hope it could help you |
|
|
foufou_tn
Joined: 13 Apr 2012 Posts: 9
|
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
|
Posted: Sat Apr 14, 2012 2:43 am |
|
|
the resistor on the MCP2551 Rs pin is far too low.
look at the datasheet
it should be between 10K and 120K, try 47K |
|
|
foufou_tn
Joined: 13 Apr 2012 Posts: 9
|
|
Posted: Sat Apr 14, 2012 4:11 am |
|
|
thx MikeW i will try but i did not found this in datasheet plz tell me my c code is okey or i have to make changes in my code ??? thx |
|
|
foufou_tn
Joined: 13 Apr 2012 Posts: 9
|
|
Posted: Sat Apr 14, 2012 4:59 am |
|
|
plz i have write 2 code just tell me plz if this will work ?
If no where is the problem :(
this is the code for board1
Code: |
#include <can-18xxx8.c>
int i=0;
void main()
{
struct rx_stat rxstat;
int32 rx_id;
int32 tx_id;
int8 rx_len;
int8 buffer[8];
can_init();
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
for (i=0;i<8;i++)
{buffer[i]=0;}
buffer[0]= 'A';
while(true)
{
can_putd(0x400,buffer,1, 1, 1, 0);
delay_ms(500);
}
}
|
and this is the code for board 2
Code: |
#include "C:\Users\MATRIX\Desktop\2 carte inchalah\prog\prog c2\main.h"
#include <can-18xxx8.c>
#define LCD_ENABLE_PIN PIN_C0
#define LCD_RS_PIN PIN_C1
#define LCD_RW_PIN PIN_C6
#define LCD_DATA4 PIN_C2
#define LCD_DATA5 PIN_C3
#define LCD_DATA6 PIN_C4
#define LCD_DATA7 PIN_C5
#include <lcd.c>
int x=0;
char dat;
void main()
{
struct rx_stat rxstat;
int32 rx_id;
int32 tx_id;
int8 rx_len;
int8 buffer[8];
can_init();
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
while(true)
{
if(can_kbhit())
{
if(can_getd(rx_id, buffer, rx_len, rxstat))
{
if(rx_id ==0x400 )
{
x=5;
lcd_gotoxy(1,1);
printf(lcd_putc,"id=%d",x);
dat=buffer[0];
lcd_gotoxy(1,2);
printf(lcd_putc,"data=%c",dat);
}
}
}
}
}
|
|
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
|
Posted: Sat Apr 14, 2012 6:58 am |
|
|
it is in the datasheet,
page 4, figure 1-1 |
|
|
foufou_tn
Joined: 13 Apr 2012 Posts: 9
|
|
Posted: Sat Apr 14, 2012 7:10 am |
|
|
i have change the resistors to 47k as you say and i have test the last 2 code and its not working who can help me about this plz cuz thats thing is so importing to my future plz. |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Sat Apr 14, 2012 8:38 am |
|
|
As I said. you first should check whether your code work in loopback mode.
You can search in this forum there is many code on this mode. |
|
|
foufou_tn
Joined: 13 Apr 2012 Posts: 9
|
|
Posted: Sat Apr 14, 2012 10:44 am |
|
|
its not working i have to change the crystal from 8mhz to 20mhz or not ??? |
|
|
hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
|
Posted: Sat Apr 14, 2012 11:06 am |
|
|
Here is my code for dsPic30f4011 in the loopback mode. See if you can try it out
Code: | #include <30F4011.h>
#DEVICE ADC=10
#include <stdlib.h>
#include <can-dsPIC30.h>
#include <can-dsPIC30.c>
#FUSES FRC,NOWDT
#use delay(clock=4000000)
void main()
{
struct rx_stat rxstat;
int32 rx_id;
int32 tx_id;
int8 rx_len;
int8 buffer[8];
int8 i;
set_tris_b(0x00);
can_init();
can_set_mode(CAN_OP_LOOPBACK);
for (i=0;i<8;i++)
{
buffer[i] = 0;
}
while(1)
{
buffer[0] = 3;
buffer[1] = 2;
buffer[2] = 1;
buffer[3] = 0;
for (i=0;i<4;i++)
{
can_putd(25,&buffer[i],1,1,1,0);
delay_ms(1000);
}
if(can_kbhit())
{
if(can_getd(rx_id, &buffer, rx_len, rxstat))
{
if(rx_id == 25)
{
output_b(buffer[0]);
delay_ms(1000);
output_b(buffer[1]);
delay_ms(1000);
output_b(buffer[2]);
delay_ms(1000);
output_b(buffer[3]);
delay_ms(1000);
}
}
}
}
} |
The purpose of this code is output the value to port b. So if it is not fit with your hardware, you can change it slightly to display on LCD. |
|
|
foufou_tn
Joined: 13 Apr 2012 Posts: 9
|
|
Posted: Sat Apr 14, 2012 1:41 pm |
|
|
Thank you so much. I will try this tomorrow and tell you what happen |
|
|
rotting79
Joined: 12 Mar 2012 Posts: 1
|
|
|
RHA
Joined: 25 Apr 2006 Posts: 31 Location: Germany
|
|
Posted: Sun Apr 15, 2012 11:24 pm |
|
|
Maybe you have to setup CAN like this :
Code: | //--- Setup CAN for 200kbps at 16 MHz (has to be done before loading CAN-Routines) ---
#define CAN_BRG_SYNCH_JUMP_WIDTH 0 // synchronized jump width ( Tq = X+1)
#define CAN_BRG_PROPAGATION_TIME 0 // propagation time select ( Tq = X+1)
#define CAN_BRG_PHASE_SEGMENT_1 4 // phase segment 1 ( Tq = X+1)
#define CAN_BRG_PHASE_SEGMENT_2 2 // phase segment 2 time select ( Tq = X+1)
#define CAN_BRG_PRESCALAR 3 // baud rate generator prescalar (1Tq = (2 x (PRE + 1))/Fosc )
#define CAN_USE_EXTENDED_ID FALSE // Use of extended ID (True or False)
#define CAN_ENABLE_DRIVE_HIGH 1 // CANTX will be tri-state/drive VDD when recessive
//--- Load CAN-Routines ---
#include <can-18xxx8.c> |
The setup has to be done before loading the library for CAN.
On several systems (depends on the used CAN transceiver) this is important :
Code: | #define CAN_ENABLE_DRIVE_HIGH 1 // CANTX will be tri-state/drive VDD when recessive |
If you use the MCP2551 in most cases you don´t need this because the MCP2551 is having a pull-up resistor on TxD. If you use the ISO1050 (transceiver with galvanic barrier) you need to drive the TxD high when recessive or use a pull-up resistor on TxD. Otherwise you will get problems with communication because the ISO1050 has only an internal pull-up of 1MOhm and that´s in most cases too high (you will get rouble with noises). |
|
|
|