View previous topic :: View next topic |
Author |
Message |
jennifer Guest
|
CAN CONTROLLER MCP2515 & PIC16F877A SPI interface using |
Posted: Mon May 12, 2008 1:14 am |
|
|
Hi
i am not getting result while MCP2515 & PIC 16F877A SPI Interface in PICC, Problem in the program........we are trying to write a value to CAN Register and read that value to output in port d in pic 16f877a
here the codes:
#define MCP2515_CS PIN_B5
#define MCP2515_RESET_PIN PIN_B0
#define READCMD 0x03
#define WRITECMD 0x02
#include<16F877A.h>
#include<PIC16F877A.h>
#include<MCP2515.h>
int reg;
int outvalue;
void RESETMCP2515()
{
output_high(MCP2515_CS);
output_low(MCP2515_RESET_PIN);
delay_us(10);
output_high(MCP2515_RESET_PIN);
}
int READREG(int regaddres)
{
output_low(MCP2515_CS);
spi_write(READCMD);
spi_write(regaddres);
clear_BF();
reg=spi_read();
output_high(MCP2515_CS);
return(reg);
}
void WRITEREG(int regaddres,int regvalue)
{
output_low(MCP2515_CS);
spi_write(WRITECMD);
spi_write(regaddres);
spi_write(regvalue);
output_high(MCP2515_CS);
delay_us(10);
}
void MAIN()
{
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
WRITEREG(CANCTRL,0x80); //set to config mode
WRITEREG(CANCTRL, 0x00); //reset to normal mode
trisd=0x00;
trisc=0x10;
WRITEREG(TXB1D0,0X0d);
reg=READREG(TXB1D0);
portd=reg;
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 12, 2008 1:28 am |
|
|
Did you get that code from this thread ?
http://www.ccsinfo.com/forum/viewtopic.php?t=18539
Your code has many errors. You need to look closely at the code
in the link above. Read all the posts. It explains how to correctly
setup the SPI mode for the MCP2515. Also look closely at how
you are using the spi_read() function. Note that the code in the
link uses a parameter of 0 with that function. That parameter
is required if you want the PIC to generate the SPI clock, and
you do need to generate it. Follow the code in that thread. |
|
|
jennifer Guest
|
spi interfacing between PIC16F877A & MCP2515 |
Posted: Tue May 13, 2008 1:44 am |
|
|
I tried to modify my code according to ur instructions..yet v didnt get the output..the modified code is given below..pls help me..
#define MCP2515_CS PIN_B5
#define MCP2515_RESET_PIN PIN_B0
#define READCMD 0x03
#define WRITECMD 0x02
#include<16F877A.h>
#include<PIC16F877A.h>
#include<MCP2515.h>
int reg;
void RESETMCP2515()
{
output_high(MCP2515_CS);
output_low(MCP2515_RESET_PIN);
delay_us(10);
output_high(MCP2515_RESET_PIN);
}
int READREG(int regaddres)
{
output_low(MCP2515_CS);
spi_write(READCMD);
spi_write(regaddres);
spi_read(0);
reg=spi_read();
output_high(MCP2515_CS);
return(reg);
}
void WRITEREG(int regaddres,int regvalue)
{
output_low(MCP2515_CS);
spi_write(WRITECMD);
spi_write(regaddres);
spi_write(regvalue);
output_high(MCP2515_CS);
delay_us(10);
}
void CONFIGMCP2515()
{
int cnfr1 = 0x90;
int cnfr2 = 0x92;
int cnfr3 = 0x84;
WRITEREG(CANCTRL, 0x80); //set to config mode
WRITEREG(CNF1, cnfr1);
WRITEREG(CNF2, cnfr2);
WRITEREG(CNF3, cnfr3);
WRITEREG(CANCTRL, 0x00); //reset to normal mode
}
void MAIN()
{
trisd=0x00;
setup_spi(SPI_MASTER|SPI_H_TO_L |SPI_XMIT_L_TO_H| SPI_CLK_DIV_4 );
RESETMCP2515();
CONFIGMCP2515();
WRITEREG(TXB2D2,0XF0);
reg=READREG(TXB2D2);
portd=reg;
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 13, 2008 12:57 pm |
|
|
But you didn't pay attention to the code in that link.
Here's one routine from that link:
Code: | int ReadRegister(int regaddr)
{
output_low(MCP2515_CS);
spi_write(READCMD);
spi_write(regaddr);
rreg = spi_read(0);
output_high(MCP2515_CS);
return(rreg);
} |
Here's your code. Notice how the spi_read() section
is different in your code. Why are you doing two
read operations ? This is why it doesn't work.
Details are important.
Quote: |
int READREG(int regaddres)
{
output_low(MCP2515_CS);
spi_write(READCMD);
spi_write(regaddres);
spi_read(0);
reg=spi_read();
output_high(MCP2515_CS);
return(reg);
} |
Go through the rest of your code and watch the details.
Also, ideally, both of the routines shown above should have
declared a local variable for the return value.
For example, you should have this at the start of your routine:
|
|
|
JENNIFER Guest
|
SPI interface between PIC16f877A and MCP2515 |
Posted: Wed May 14, 2008 12:29 am |
|
|
Thank you for your help.We got the result ....... |
|
|
|