CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

CAN CONTROLLER MCP2515 & PIC16F877A SPI interface using

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jennifer
Guest







CAN CONTROLLER MCP2515 & PIC16F877A SPI interface using
PostPosted: Mon May 12, 2008 1:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 12, 2008 1:28 am     Reply with quote

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
PostPosted: Tue May 13, 2008 1:44 am     Reply with quote

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.. Sad



#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

View user's profile Send private message

PostPosted: Tue May 13, 2008 12:57 pm     Reply with quote

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:
Code:
int8 reg;
JENNIFER
Guest







SPI interface between PIC16f877A and MCP2515
PostPosted: Wed May 14, 2008 12:29 am     Reply with quote

Thank you for your help.We got the result .......
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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