View previous topic :: View next topic |
Author |
Message |
Mucit23
Joined: 10 May 2014 Posts: 8
|
MCP2515 Message Receiving Problem [Solved] |
Posted: Wed Aug 27, 2014 4:56 pm |
|
|
Hi Friends,
I'm using MCP2515 with stm32f10x for the CANbus communication. I can send a message to stm32 with MCP2515. I can receive incoming messages with STM32. But I can not send a message to mcp2515 from stm32.
STM32 side there is no problem. Because STM32 running in loopback mode.
This is My Code
++++++++++++++++++
CCS driver code mostly removed.
Reason: Forum rule #10
10. Don't post the CCS example code or drivers.
http://www.ccsinfo.com/forum/viewtopic.php?t=26245
- Forum Moderator
++++++++++++++++++
MCP2515.c
Code: |
/////////////////////////////////////////////////////////////////////////
//// mcp2515a.c NODE A ////
#include <mcp2515.h>
//IO pins connected to MCP2510
#ifndef EXT_CAN_CS
#define EXT_CAN_CS PIN_C2
#define EXT_CAN_SI PIN_C5
#define EXT_CAN_SO PIN_C4
#define EXT_CAN_SCK PIN_C3
#define EXT_CAN_RESET PIN_C0 //CCS library does not use this pin by default
////////////////////////////////////////////////////////////////////////
//
// can_init()
//
//
//////////////////////////////////////////////////////////////////////////////
void can_init(void) {
.
.
.
can_set_mode(CAN_OP_LOOPBACK);
}
////////////////////////////////////////////////////////////////////////
//
// can_set_baud()
//
//
////////////////////////////////////////////////////////////////////////
void can_set_baud(void) {
mcp2510_write(CNF1, 0x01);
mcp2510_write(CNF2, 0xB8);
mcp2510_write(CNF3, 0x05);
}
|
MCP2515.h
Code: |
/////////////////////////////////////////////////////////////////////////
//// can-mcp2510.h ////
//// ////
|
My main code
Code: | #include <16F886.h>
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOMCLR
#FUSES NOFCMEN
#FUSES NOLVP
#use delay(int=8000000)
#include <lcd_driver.c>
#include <mcp2515.c>
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
struct rx_stat rxstat;
int32 rx_id;
int buffer[8];
int rx_len;
void main()
{
set_tris_a(0x3F);
set_tris_b(0x00);
set_tris_c(0x00);
output_a(0x00);
output_b(0x00);
output_c(0x00);
setup_oscillator(OSC_8MHZ);
setup_adc(ADC_OFF);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_spi(SPI_DISABLED);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
delay_ms(100);
lcd_init();
can_init();
lcd_clear();
lcd_gotoxy(1,1);
printf(lcd_putc,"MCP2515 Can Test");
while(TRUE)
{
can_getd(rx_id, &buffer[0], rx_len, rxstat);
lcd_gotoxy(1,2);
printf(lcd_putc,"Rx_id=0x%4X",buffer[0]);
}
} |
For some reason I can not get any incoming messages from MCP2515. But I can send a message.
What do you think could be the problem?
Thank You
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19522
|
|
Posted: Thu Aug 28, 2014 1:04 am |
|
|
As soon as you use fast_io, it becomes your responsibility to set TRIS correctly.
You are programming the input bit as an output..... |
|
|
Mucit23
Joined: 10 May 2014 Posts: 8
|
|
Posted: Thu Aug 28, 2014 2:45 am |
|
|
I'm so sorry for added to CCS driver.
@Ttelmah, Thank you for your answer.
I removed definition #use_fast_io(c). I was hoping this change will solve the problem. But the problem continues in the same way.
I did a few tests.
I first wrote CANCTRL value of 0x80 to the register.
Code: | mcp2510_write(CANCTRL,0x80); |
Then I read CANSTAT register.
Code: | temp=mcp2510_read(CANSTAT); |
I read the value 0x80
For this reason, I don't think there is a problem with Writing and reading operations. I Think that is another problem.
What else could be the problem?
Thank you for helping.
Regards |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
Re: MCP2515 Message Receiving Problem |
Posted: Thu Aug 28, 2014 3:29 am |
|
|
Mucit23 wrote: |
Code: |
////////////////////////////////////////////////////////////////////////
//
// can_set_baud()
//
//
////////////////////////////////////////////////////////////////////////
void can_set_baud(void) {
mcp2510_write(CNF1, 0x01);
mcp2510_write(CNF2, 0xB8);
mcp2510_write(CNF3, 0x05);
}
|
|
Instead of overriding the defines in the CCS header file, you have edited both the .h and the .c files, making them non-standard. In particular you've changed the reasonably well organised baud rate setting stuff and changed them into "magic" numbers. Its very difficult to check if those numbers, 0x01, 0xB8 and 0x05, are right for what you want to do. By far the best way of using the CCS code is to leave it as it is, and NOT edit it in any way, and then provide your overriding defines in your code.
Its important to understand that the CCS driver code expects to generate 125kbps CAN baudrate when the CAN interface, in this case the mcp2515, is clocked at 20MHz. What clock are you running the mcp2515 at? Is it driven by the processor clock, which is at 8MHz? Does it have its own crystal? If so, what frequency? What CAN baudrate do you want? Do those magic numbers really give you that rate from your clock? |
|
|
Mucit23
Joined: 10 May 2014 Posts: 8
|
|
Posted: Thu Aug 28, 2014 4:27 pm |
|
|
Hi @RF_Developer,
I use a PC program to calculate Baud Rate the values.
The program link: http://www.intrepidcs.com/support/mbtime.htm
I'm calculates baud rate with this program.
0x01, 0xB8 and 0x05, these values for 250Kbits.
MCP2515 is working with 16MHz crystal. PIC16F886 is running with 8 MHz internal oscillator
I want 250kbits baud rate. I can communicate at the speed of 250 kbps with magic values. Because I measure the speed of communication with the logic analyzer.
I think the problem with MCP2515 library. Do you have a library that you know is working?
Thank You
Regards |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 28, 2014 4:51 pm |
|
|
Fix your read loop. Call can_kbhit() to see if data is available. If so,
then call the rest of the code. Also add a delay after you get the message
so you can see it displayed.
Example of revised loop code:
Code: |
while(TRUE)
{
if(can_kbhit()) // Is data available ?
{
can_getd(rx_id, &buffer[0], rx_len, rxstat);
lcd_gotoxy(1,2);
printf(lcd_putc,"Rx_id=0x%4X", buffer[0]);
delay_ms(1000); // Display message for 1 second
}
} |
|
|
|
Mucit23
Joined: 10 May 2014 Posts: 8
|
|
Posted: Sat Aug 30, 2014 1:39 pm |
|
|
Hi
I tried giving you part of the program. But it didn't work.
I think it is related to the Rx filter. Does MCP2510 receives all incoming messages? Do you need to change the filter settings?
Code: | //receive acceptance filter n standard indifier
.
#define RXF0SIDH 0x00
#define RXF0SIDL 0x01
#define RXF1SIDH 0x04
#define RXF1SIDL 0x05
#define RXF2SIDH 0x08
#define RXF2SIDL 0x09
#define RXF3SIDH 0x10
#define RXF3SIDL 0x11
#define RXF4SIDH 0x14
#define RXF4SIDL 0x15
#define RXF5SIDH 0x18
#define RXF5SIDL 0x19
//receive acceptance filter n extended indifier
#define RXF0EIDH 0x02
#define RXF0EIDL 0x03
#define RXF1EIDH 0x06
#define RXF1EIDL 0x07
#define RXF2EIDH 0x0a
#define RXF2EIDL 0x0b
#define RXF3EIDH 0x12
#define RXF3EIDL 0x13
#define RXF4EIDH 0x16
#define RXF4EIDL 0x17
#define RXF5EIDH 0x1a
#define RXF5EIDL 0x1b
//receive acceptance mask n standard identifer mask
#define RXM0SIDH 0x20
#define RXM0SIDL 0x21
#define RXM1SIDH 0x24
#define RXM1SIDL 0x25
//receive acceptance mask n extended identifer mask
#define RXM0EIDH 0x22
#define RXM0EIDL 0x23
#define RXM1EIDH 0x26
#define RXM1EIDL 0x27
.
.
.
|
Edit;
I also tried interrupts. Tx interrupts is working.
I am writing 0xFF to register CANTINTE.
I'm sending a message MCP2515, then I read CANINTF register. I read the value 0x04. Also INT Pin is running.
My Results are like this.
Thank You
Regards. |
|
|
Mucit23
Joined: 10 May 2014 Posts: 8
|
|
Posted: Sat Aug 30, 2014 3:51 pm |
|
|
Thanks to everyone
My problem was solved. The problem was at the incoming identifier type. I was sending a message with standard ID. But can init function was extended_id to be used is specified.
Now, I'm getting a message with standard_id.
Thank You
Regards |
|
|
|