View previous topic :: View next topic |
Author |
Message |
EL
Joined: 24 Feb 2008 Posts: 15 Location: POLSKA
|
PIC16F876 + MCP2515 |
Posted: Mon Feb 25, 2008 4:38 am |
|
|
Hi!
I am from Poland.
I apologize for my English
I would like to exchange among PIC16F876 information and MCP2515.
AN212 my equipment be leaning about note only Appendix A.
So as earlier wrote first I want to link across SPI transport with controller CAN.
From what do I have to begin?
This plot: http://www.ccsinfo.com/forum/viewtopic.php?t=18539 is probably good, but it lacks me "mcp2515.c" and implementation SPI...
I thank for all helps.
I greet
EL |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 25, 2008 11:42 am |
|
|
If you own the CCS compiler, then look in the drivers directory for
these two files:
Quote: |
c:\program files\picc\drivers\can-mcp251x.c
c:\program files\picc\drivers\can-mcp251x.h
|
Don't ask for us to post the files. We're not allowed to do it. |
|
|
Guest
|
|
Posted: Sun Mar 02, 2008 4:39 am |
|
|
I have these two files can-mcp251x.c and can-mcp251x.h , but I want only to check SPI.
I would like to write first and to read from MCP2515 anything... |
|
|
EL
Joined: 24 Feb 2008 Posts: 15 Location: POLSKA
|
|
Posted: Sun Mar 02, 2008 10:48 am |
|
|
Is someone who will say me how in CCS record some value to MCP2515 and read? through SPI?
this for me very important... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 02, 2008 12:47 pm |
|
|
First, this is the weekend. Don't expect an immediate answer on the weekend.
Then, use the forum's search page to search for MCP2515. Then you
will find answers like this one:
http://www.ccsinfo.com/forum/viewtopic.php?t=31005 |
|
|
EL
Joined: 24 Feb 2008 Posts: 15 Location: POLSKA
|
|
Posted: Tue Mar 04, 2008 6:07 am |
|
|
Hi!
I compiled this programme:
Code: |
#include <16F876.h>
#include <F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
//#include <can-mcp251x.h>
#define SPI_CS PIN_A1
#define SPI_SCK PIN_C3
#define SPI_SDI PIN_C5
#define SPI_SDO PIN_C4
#define SPI_RESET PIN_A5
#define READ 0x03
#define WRITE 0x02
void main()
{
int k;
int i;
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16 | SPI_SAMPLE_AT_END); // SSPCON=00100001,SSPSTAT=1000000
while (TRUE)
{
for (k=0; k<=0xFF; k++)
{
output_low(SPI_CS); //begin SPI comms
spi_write(WRITE); //send the write instruction
spi_write(0x34); //address of a register to test reading and writing
spi_write(k); //send the data
output_high(SPI_CS); //SPI comunication finished
//restart_wdt(); // restart the watchdog timer
delay_ms(50); //wait for 50ms before the read occurs
output_low(SPI_CS); //start another round of SPI comms
spi_write(READ); //send the read instruction
spi_write(0x34); //read from the register the data was written to
//clear_BF(); //clear bit BF before reading the input data
i = spi_read(0x00); //read the value of the register into i
output_high(SPI_CS); //SPI comunication finished
i = i ^ k ; //XOR together to make bits where an error has happened = '1'
output_b(i); //output the errors onto LEDs for visual inspection
//restart_wdt(); // restart the watchdog timer
delay_ms(50); //wait for 50ms before going round the loop again
}
}
} |
The diode flash on port B ...I do not know or it has to be so?
How do to check or I recorded to MCP2515 something and I read? |
|
|
EL
Joined: 24 Feb 2008 Posts: 15 Location: POLSKA
|
|
Posted: Tue Mar 04, 2008 2:17 pm |
|
|
This second programme:
Code: |
#include <16F876.h>
#include <F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(DEBUGGER)
#define SPI_CS PIN_A1
#define SPI_SCK PIN_C3
#define SPI_SDI PIN_C5
#define SPI_SDO PIN_C4
#define SPI_RESET PIN_A5
#define READ 0x03
#define WRITE 0x02
#byte SSPBUF = 0x13
#bit STAT_BF = 0x94.0
//#define tris_c4=0;
//#define tris_c5=1;
void WriteRegister(int regaddr, int regvalue)
{
int SPIDummy;
output_low(SPI_CS);
SSPBUF = WRITE;
while(!STAT_BF);
SPIDummy = SSPBUF;
SSPBUF = regaddr;
while(!STAT_BF);
SPIDummy = SSPBUF;
SSPBUF = regvalue;
while(!STAT_BF);
SPIDummy = SSPBUF;
output_high(SPI_CS);
}
int ReadRegister(int regaddr)
{
int spi_data;
output_low(SPI_CS);
SSPBUF = READ;
while(!STAT_BF);
spi_data = SSPBUF;
SSPBUF = regaddr;
while(!STAT_BF);
spi_data = SSPBUF;
SSPBUF = 0x00;
while(!STAT_BF);
output_high(SPI_CS);
return SSPBUF;
}
void main()
{
int k;
int i;
int data;
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_SAMPLE_AT_END); // SSPCON=00100001,SSPSTAT=1000000
WriteRegister(0x39,5);
data=ReadRegister(0x39);
while(TRUE)
{
output_low(PIN_A0);
delay_ms(500);
output_high(PIN_A0);
delay_ms(500);
};
}
|
I do not know or I do this well:(... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 04, 2008 2:33 pm |
|
|
Why can't you use the test program posted by doug_h in this thread ?
http://www.ccsinfo.com/forum/viewtopic.php?t=18539
And then change the setup_spi() statement to the correct one that
he suggests at the end of the thread.
If you did that, you might get it to work. |
|
|
EL
Joined: 24 Feb 2008 Posts: 15 Location: POLSKA
|
|
Posted: Tue Mar 04, 2008 3:06 pm |
|
|
I did not check this programme because I do not know or < mcp2515.c > this the same what <can - mcp 251x.c> and <can - mcp 251x.h>? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 04, 2008 3:27 pm |
|
|
I changed the #include statement so it includes can-mcp251x.c and it
compiles OK.
His test program only uses a few things from the CCS driver file.
He uses the "can_debug" macro, which is just a different name for
the printf function. It's printf.
He also uses a few register definitions, such as CANCTRL, CNF1, etc.
These come from the can-mcp251x.h file, which is #included by the .c
file above.
So it should be no problem. Just change the #include line in his test
program to this:
Quote: | #include <can-mcp251x.c> |
|
|
|
EL
Joined: 24 Feb 2008 Posts: 15 Location: POLSKA
|
|
Posted: Tue Mar 04, 2008 3:35 pm |
|
|
Programme was executed OK...only how to check or I write something and I read from MCP2515 trough SPI?
Does transport among PIC and MCP run? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 04, 2008 3:47 pm |
|
|
Quote: | how to check or I write something and I read from MCP2515 trough SPI? |
That's what the program is supposed to test. It tests if you can read
and write OK to the MCP2515.
Here's the response that you are supposed to get from his program.
Did you get this ? If so, the SPI connection is working OK.
Quote: |
CAN CFG TEST
Resetting the MCP2515
Reading registers...
CANSTAT = 40
CANCTRL = 43
CNF1 = 80
CNF2 = 00
CNF3 = 00
Writing status registers...
Setting CNF1 = 90
Setting CNF2 = 92
Setting CNF3 = 84
Reading back status registers...
CANSTAT = 00
CANCTRL = 00
CNF1 = 48
CNF2 = 49
CNF3 = 42
Running... |
|
|
|
EL
Joined: 24 Feb 2008 Posts: 15 Location: POLSKA
|
|
Posted: Tue Mar 04, 2008 5:29 pm |
|
|
I'm sorry still I learn and I use MPLAB'a as well as CCS'a 4.013.
I do not know where to read in compiler this?:(((
Quote: |
CAN CFG TEST
Resetting the MCP2515
Reading registers...
CANSTAT = 40
CANCTRL = 43
CNF1 = 80
CNF2 = 00
CNF3 = 00
Writing status registers...
Setting CNF1 = 90
Setting CNF2 = 92
Setting CNF3 = 84
Reading back status registers...
CANSTAT = 00
CANCTRL = 00
CNF1 = 48
CNF2 = 49
CNF3 = 42
Running...
|
I used outcome file always .lst and .cof ... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 04, 2008 5:42 pm |
|
|
You have to program the code into a PIC, and run the program.
You must have a board with a PIC and a MCP2515 on it. The board
must also have a RS-232 port on it. You must connect the RS-232
port to the RS-232 connector on a PC. You must run a terminal window
program on the PC to see the output.
Do you have a board ?
Using the CAN bus is an advanced PIC project. Or at least, it's more
than an intermediate project. It's clear that you are a complete
beginner, because you don't know about RS232 output. For that reason,
I believe that I have done enough on this thread. I assumed that you
were at least at the intermediate level. |
|
|
EL
Joined: 24 Feb 2008 Posts: 15 Location: POLSKA
|
|
Posted: Tue Mar 04, 2008 5:54 pm |
|
|
Unfortunately, I do not join with PC across RS -232.
I want to know only or I exchange from MCP2515 data.
In several days I will be dust card CAN installed at PC and I will check then...
And I would like now to test only SPI... |
|
|
|