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 support@ccsinfo.com

Sending a data from one CAN NODE to another using PIC16F877A

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







Sending a data from one CAN NODE to another using PIC16F877A
PostPosted: Wed May 21, 2008 12:45 am     Reply with quote

hiii
We have tried to send a data from a CAN node to another using MCP2515, MCP2551 , PIC16F877A... there is error in the transmission progam in picc........please help on this ....Here is our code:

Code:
#define MCP2515_CS   PIN_C1
#define MCP2515_RESET_PIN PIN_C0
#define READCMD 0x03
#define WRITECMD 0x02
#include<16F877A.h>
#include<header.h>
#include<MCP2515.h>
int8 A,B,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);
   
   reg=spi_read(0);
 
   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);
 
}

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()

   TRISB=TRISD=0x00;
   setup_spi(SPI_MASTER|SPI_H_TO_L |SPI_XMIT_L_TO_H| SPI_CLK_DIV_4   );
   RESETMCP2515();
 

   CONFIGMCP2515();

A=READREG(TXB0CTRL);
B=READREG(CANINTF);

WRITEREG(CANINTE,0XFF);       //TO ENABLE INTERRUPT
WRITEREG(TXB0CTRL,0X03);     // setting highest priority transmit buff 0
WRITEREG(TXB1CTRL,0X02);
WRITEREG(TXB2CTRL,0X01);
WRITEREG(TXB0SIDH,0X0F);       //identifer 0F
WRITEREG(TXB0SIDL,0X00);
WRITEREG(TXB0D0,0XFF);           //data to b send
WRITEREG(TXB0DLC,0X01);        // DATA LENGTH

BIT_SET(A,3);

L1:   IF (BIT_TEST(A,4))               // CHECK BUS AVAILABILITY
       {
    PORTB=READREG(TXB0CTRL);

  GOTO L1;
                     }


IF(BIT_TEST(B,2))

{
 
  PORTD=0XFF;                //IF MSG  SENTSUCCESSFULLY DISPLAY FF

}

PORTB=READREG(TXB0CTRL);
PORTD=READREG(CANINTF);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 21, 2008 1:00 am     Reply with quote

I suggest that you use the CCS driver for the MCP2515 instead of writing
your own:
Quote:
c:\program files\picc\drivers\can-mcp251x.c
c:\program files\picc\drivers\can-mcp251x.h


In the follow thread, I have posted instructions on how to connect two
boards with the CAN bus and test the communications. The demo
program uses CCS driver functions, so even though the program
is for an 18F458, it should also work with the can-mcp251x.c driver.
Just change it to include that file instead of can-18xxx8.c.
Quote:
http://www.ccsinfo.com/forum/viewtopic.php?t=29627
Jennifer
Guest







Data TX b/w CAN nodes (PIC16F877A & MCP2515) in PICC
PostPosted: Wed Jun 04, 2008 12:19 am     Reply with quote

hiii
We have tried to send a data from a CAN node to another using MCP2515, MCP2551 , PIC16F877A... there is error in the programs in picc........please help on this ....Here is our code for transmission and reception programs.There is error in bus availabilty.

TRANSMISSION PROGRAM:
Code:
#define MCP2515_CS   PIN_C1
#define MCP2515_RESET_PIN PIN_C0
#define READCMD 0x03
#define WRITECMD 0x02
#include<16F877A.h>
#include<PIC16F877A.h>
#include<MCP2515.h>
int8 reg,A;

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);
   reg=spi_read(0);
   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);
}

void CONFIGMCP2515()
{
   int cnfr1 = 0x00;       //(Fosc=20Mhz,buadrate=1Mbps)
   int cnfr2 = 0xA0;
   int cnfr3 = 0x02;
   
   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=TRISB=0x00;
   setup_spi(SPI_MASTER|SPI_H_TO_L |SPI_XMIT_L_TO_H| SPI_CLK_DIV_4   );
   RESETMCP2515();
   CONFIGMCP2515();
 
   OUTPUT_B(0X00);
   OUTPUT_D(0X00);

   WRITEREG(CANINTE,0XFF);          //TO ENABLE INTERRUPT
   WRITEREG(TXB0CTRL,0X03);                 //priority
   WRITEREG(TXB1CTRL,0X02);
   WRITEREG(TXB2CTRL,0X01);
   WRITEREG(TXB0SIDH,0X0F);                 //MSB ID
   WRITEREG(TXB0SIDL,0X00);          //LSB ID
   WRITEREG(TXB0DLC,0X01);          //data length

   WRITEREG(TXB0D0,0X09);          //DATA TO BE SEND
   WRITEREG(TXB0CTRL,0X0B);                 //to initialize trsn(TXREQ set)
   reg=READREG(TXB0CTRL);
 

   L1:   if (BIT_TEST(reg,4))               //CHECKING BUS AVAILABILITY
       {
          
        PORTB=reg;
       
   GOTO L1;

       }

     else
       {
        A=READREG(CANINTF);

        if(BIT_TEST(A,2))         // (CANINTF.TX0IF=1?)
          {
 
           PORTD=0X0F;                     //MSG  SENT(SUCCESS)

          }

       else

        PORTD=0X0A;                        // NOT SEND
       }
}



RECEPTION PROGRAM:
Code:

#define MCP2515_CS   PIN_C1
#define MCP2515_RESET_PIN PIN_C0
#define READCMD 0x03
#define WRITECMD 0x02
#include<16F877A.h>
#include<PIC16F877A.h>
#include<MCP2515.h>

int8 reg,X;

void RESETMCP2515()
{
   output_high(MCP2515_CS);
   output_low(MCP2515_RESET_PIN);
   delay_us(10);
   output_high(MCP2515_RESET_PIN);
}

int READREG(int REGADDR)
{
   output_low(MCP2515_CS);
   spi_write(READCMD);
   spi_write(REGADDR);
   reg=spi_read(0);
   output_high(MCP2515_CS);
   return(reg);
}

void WRITEREG(int REGADDR,int REGVALUE)
{
   output_low(MCP2515_CS);
   spi_write(WRITECMD);
   spi_write(REGADDR);
   spi_write(REGVALUE);
   output_high(MCP2515_CS);
}

void CONFIGMCP2515()
{
   int cnfr1 = 0x00;
   int cnfr2 = 0xA0;
   int cnfr3 = 0x02;
   
   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()

   
   setup_spi(SPI_MASTER|SPI_H_TO_L |SPI_XMIT_L_TO_H| SPI_CLK_DIV_4   );
   RESETMCP2515();
   CONFIGMCP2515();
 
   WRITEREG(CANINTF,0X00);
   WRITEREG(RXB0CTRL,0X20);           
   WRITEREG(RXF0SIDH,0X0F);           // FILTER VALUE
   WRITEREG(RXF0SIDL,0X00);
   WRITEREG(CANINTE,0XFF);
   X=READREG(CANINTF);
 
L1:   if(bit_test(X,0))
       {
   TRISD=TRISB=0x00;
       
        PORTB=READREG(RXB0D0);
       }

      else
         goto L1;
     
   
}
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