|
|
View previous topic :: View next topic |
Author |
Message |
hasankmr
Joined: 01 Jul 2014 Posts: 4 Location: Türkiye
|
Can with 18f458 Mcp2551 |
Posted: Wed Jul 02, 2014 2:56 am |
|
|
Hi,
I am working on Can bus project. I have a master and a slave. İf the button is pressing on slave, sending a message for master from slave. But I am not getting message on master. My sending message is crossing from slave mcp2551, from canbus line and from master mcp2551. But coming signal of master mcp2551 Rx is different from my sending signal. The end piece of signal is different. Why? How can I fix this problem ? And can_khbit() function isn't working .
My master code:
Code: | #include <18F458.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,parity=N,stop=1)
#define CAN_USE_EXTENDED_ID TRUE
#include <can-18xxx8.c>
#define led pin_a0
#define led_yanlis pin_a1
#define basarili pin_a2
#define basarisiz pin_a3
#int_canrx0
void canrx0_int ( ) {
// TODO: add CAN recieve code here
printf("\r\nVeri alindi\n");
}
#int_canrx1
void canrx1_int ( ) {
// TODO: add CAN recieve code here
printf("\r\nVeri alindi\n");
}
#int_canerr
void canerr_int ( ) {
// TODO: add CAN error handling code here
printf("\r\nHata var\n");
}
void main()
{
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_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
setup_timer_2(T2_DIV_BY_4,79,16);
output_high(basarili);
delay_ms(10);
output_low(basarili);
delay_ms(10);
printf("\r\nCanbus Ornek Master\r\n");
struct rx_stat rxstat;
int32 rx_id=0;
int gelen[8];
int rx_len;
int i;
int giden[8];
int32 tx_id=0x202;
int1 tx_rtr=1;
int1 tx_ext=0;
int tx_len=8;
int tx_pri=3;
for(i=0;i<8;i++)
{
gelen[i]=0;
giden[i]=0;
}
can_init();
can_set_mode(CAN_OP_CONFIG);
BRGCON1.brp=4;
BRGCON1.sjw=0;
BRGCON2.prseg=2;
BRGCON2.seg1ph=5;
BRGCON2.sam=FALSE;
BRGCON2.seg2phts=FALSE;
BRGCON3.seg2ph=5;
BRGCON3.wakfil=TRUE;
can_set_mode(CAN_OP_NORMAL);
enable_interrupts(int_canrx0);
enable_interrupts(int_canrx1);
enable_interrupts(int_canerr);
enable_interrupts(GLOBAL); //enable all interrupts (else timer2 wont happen)
printf("\r\nCalisiyor...");
while(true)
{
if(can_kbhit()) //if data is waiting in buffer...
{
output_high(basarili);
delay_ms(10);
output_low(basarili);
delay_ms(10);
if(can_getd(rx_id,&gelen[0],rx_len,rxstat)) //...then get data from buffer
{
printf("\r\nGOT: BUFF=%U ID=%LU LEN=%U OVF=%U ", rxstat.buffer, rx_id, rx_len, rxstat.err_ovfl);
printf("FILT=%U RTR=%U EXT=%U INV=%U", rxstat.filthit, rxstat.rtr, rxstat.ext, rxstat.inv);
printf("\r\n gelen = ");
for (i=0;i<rx_len;i++) {
printf("%X ",gelen[i]);
}
printf("\r\n");
if(rx_id==0x201)
{
output_high(led);
delay_ms(500);
output_low(led);
delay_ms(500);
}
else
{
printf("\r\nHatali bilgi\r\n");
output_high(led_yanlis);
delay_ms(500);
output_low(led_yanlis);
delay_ms(500);
}
}
else
printf("\r\nVeri alinirken hata olustu.\r\n");
}
//every two seconds, send new data if transmit buffer is empty
if(rx_id==0x201)
{
if (can_tbe())
{
ms=0;
i=can_putd(tx_id,giden,tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
//i=can_putd(tx_id,giden, tx_len,1,0,0);
if (i!= 0xFF) //success, a transmit buffer was open
{
printf("\r\nPUT %U: ID=%LU LEN=%U ", i, tx_id, tx_len);
printf("PRI=%U EXT=%U RTR=%U\r\n DATA = ",tx_pri,tx_ext,tx_rtr);
for (i=0;i<tx_len;i++) {
printf("%X ",giden[i]);
}
printf("\r\n");
output_high(basarili);
delay_ms(10);
output_low(basarili);
delay_ms(10);
}
else
{
printf("\r\nVeri yazilirken hata olustu.\r\n");
output_high(basarisiz);
delay_ms(10);
output_low(basarisiz);
delay_ms(10);
}
}
}
}
}
|
My slave code:
Code: | #include <18F458.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //Resistor/Capacitor Osc
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPUT //No Power Up Time
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,parity=N,stop=1)
#define buton pin_c0
#define led1 pin_a0
#define led2 pin_a1
#define led_kesme pin_a2
#define led_iptal pin_a3
#define CAN_USE_EXTENDED_ID TRUE
#include <can-18xxx8.c>
#int_canrx0
void canrx0_int ( ) {
// TODO: add CAN recieve code here
printf("\r\nVeri alindi\n");
}
#int_canrx1
void canrx1_int ( ) {
// TODO: add CAN recieve code here
printf("\r\nVeri alindi\n");
}
#int_canerr
void canerr_int ( ) {
// TODO: add CAN error handling code here
printf("\r\nHata var\n");
}
void main()
{
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_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
struct rx_stat rxstat;
int32 rx_id;
int gelen[8];
int giden[8];
int rx_len;
int32 tx_id=0x201;
int1 tx_rtr=1;
int1 tx_ext=0;
int tx_len=8;
int tx_pri=3;
int i;
for(i=0;i<8;i++)
{
gelen[i]=0;
giden[i]=0;
}
// setup_timer_2(T2_DIV_BY_4,79,16); //setup up timer2 to interrupt every 1ms if using 20Mhz clock
can_init();
printf("\r\nCanbus Ornek Slave");
can_set_mode(CAN_OP_CONFIG);
BRGCON1.brp=4;
BRGCON1.sjw=0;
BRGCON2.prseg=2;
BRGCON2.seg1ph=5;
BRGCON2.sam=FALSE;
BRGCON2.seg2phts=FALSE;
BRGCON3.seg2ph=5;
BRGCON3.wakfil=TRUE;
can_set_mode(CAN_OP_NORMAL);
enable_interrupts(int_canrx0);
enable_interrupts(int_canrx1);
enable_interrupts(int_canerr);
enable_interrupts(GLOBAL); //enable all interrupts (else timer2 wont happen)
printf("\r\nCalistiriliyor...");
output_high(pin_a2);
delay_ms(10);
output_low(pin_a2);
delay_ms(10);
while(true)
{
if (can_kbhit())
{
if(can_getd(rx_id, &gelen[0],rx_len, rxstat))
{
printf("\r\nGOT: BUFF=%U ID=%LU LEN=%U OVF=%U ", rxstat.buffer, rx_id, rx_len, rxstat.err_ovfl);
printf("FILT=%U RTR=%U EXT=%U INV=%U", rxstat.filthit, rxstat.rtr, rxstat.ext, rxstat.inv);
printf("\r\n gelen= ");
for (i=0;i<rx_len;i++) {
printf("%X ",gelen[i]);
}
printf("\r\n");
if(rx_id==0x202)
{
output_high(led1);
delay_ms(1000);
output_low(led1);
delay_ms(1000);
}
else
{
printf("\r\nHatili bilgi\r\n");
output_high(led2);
delay_ms(1000);
output_low(led2);
delay_ms(1000);
}
}
else
printf("\r\nVeri alinirken hata olustu.\r\n");
}
/* if(input(buton)==1)
{
b1=1;
output_high(pin_a5);
delay_ms(200);
output_low(pin_a5);
delay_ms(200);
}*/
if ( can_tbe() && input(buton)==1)
{
output_high(pin_a5);
delay_ms(200);
output_low(pin_a5);
delay_ms(200);
ms=0;
giden[0]=1;
i=can_putd(tx_id,giden,tx_len,tx_pri,tx_ext,tx_rtr); //put data on transmit buffer
// i=can_putd(tx_id,giden,tx_len,1,0,0);
if (i != 0xFF)
{
printf("\r\nPUT %U: ID=%LU LEN=%U ", i, tx_id, tx_len);
printf("PRI=%U EXT=%U RTR=%U\r\n DATA = ", tx_pri, tx_ext, tx_rtr);
for (i=0;i<tx_len;i++) {
printf("%X ",giden[i]);
}
printf("\r\n");
output_high(led_kesme);
delay_ms(20);
output_low(led_kesme);
delay_ms(30);
}
else
{
printf("\r\nGonderme hatasi\r\n");
output_high(led_iptal);
delay_ms(6000);
output_low(led_iptal);
delay_ms(6000);
}
}
}
}
|
My isis picture:
https://www.dropbox.com/s/6cr6xpmgbbd16r4/can.png
My oscilloscope picture:
https://www.dropbox.com/s/uzl6rv8amdv9uqe/20140702_104233.jpg
Blue is transmitted signal,
Yellow is received signal. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Wed Jul 02, 2014 5:49 am |
|
|
There is one only little 'glaring' fault. May have nothing to do with your problem, but:
setup_spi(SPI_SS_DISABLED);
Is wrong.
This says to _enable_ the SPI, and turn off slave select.
Correct syntax is:
setup_spi(FALSE);
Haven't checked whether the SPI pins have any connection with the pins you are using, but 'worth fixing'.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 02, 2014 3:56 pm |
|
|
Your scope photo shows a slow rise time on the Rx output pin of the 2nd
MCP2551. Check the connections on the 2nd MCP2551 chip. Verify that
all connections are good. If that doesn't help, then swap the two MCP
chips. (Hopefully they are in sockets). See if you get a different result.
My feeling is that you either have a poor connection somewhere, or the
2nd MCP chip is bad. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
|
hasankmr
Joined: 01 Jul 2014 Posts: 4 Location: Türkiye
|
Re: Can with 18f458 Mcp2551 |
Posted: Sat Jul 05, 2014 3:24 am |
|
|
When I changed this part , the problem fixed.
Code: | #define CAN_USE_EXTENDED_ID FALSE |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 05, 2014 11:18 am |
|
|
How would changing the CAN ID length change the rise time of the Rx
waveform from the MCP2551 receiver ? I don't think it can. Something
else was causing the problem and it got fixed by you touching the circuit
or changing something else. |
|
|
hasankmr
Joined: 01 Jul 2014 Posts: 4 Location: Türkiye
|
|
Posted: Fri Jul 11, 2014 12:18 am |
|
|
PCM programmer wrote: | How would changing the CAN ID length change the rise time of the Rx
waveform from the MCP2551 receiver ? I don't think it can. Something
else was causing the problem and it got fixed by you touching the circuit
or changing something else. |
I don't know. But it is working. |
|
|
hasankmr
Joined: 01 Jul 2014 Posts: 4 Location: Türkiye
|
Re: Can with 18f458 Mcp2551 |
Posted: Fri Jul 11, 2014 12:34 am |
|
|
Project is working without problem. If I am not sending any message from slave, slave pic isn't working after a long time. When I am resetting the pic, it is working. What is the problem? Sometimes I have to send message from slave? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Fri Jul 11, 2014 3:26 am |
|
|
Impossible to answer.
You are going to need to do some diagnostics.
First, add a test at the beginning of the code for restart_cause, and have some way of recording/displaying what this is.
Then when you reset the chip, see what this says.
If it is 'MCLR_FROM_RUN' then the code is actually running OK, but is sitting in a loop, waiting for something to happen. MCLR_FROM_SLEEP, would suggest that the code is dropping off the end. etc. etc..
Things it could be include a brownout (you have the fuse to check for this disabled), through spikes into the MCLR pin (even if not using MCLR, this needs protection, since it does not have the normal protection diodes), through a code fault (things like a Port B changed interrupt handler that doesn't read the port, etc, etc..).
The possibilities are nearly infinite.
Start with the restart cause.
Then test a chip with much simpler code in the same physical environment (a basic LED flasher for example). If this hangs after a while, then the problem is electrical (supply line spike, RF into a pin, etc.). |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|