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 Node with PIC16F877A and MCP2515
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 20, 2009 3:15 pm     Reply with quote

Quote:

#define EXT_CAN_CS PIN_D1
#define EXT_CAN_SI PIN_C4
#define EXT_CAN_SO PIN_C5
#define EXT_CAN_SCK PIN_C3

These pins are wrong. They are swapped.

It should be done like this:
Code:

#define EXT_CAN_CS   PIN_D1
#define EXT_CAN_SI   PIN_C5
#define EXT_CAN_SO   PIN_C4
#define EXT_CAN_SCK  PIN_C3
girichavana



Joined: 21 Oct 2008
Posts: 17

View user's profile Send private message

PostPosted: Tue Oct 20, 2009 11:36 pm     Reply with quote

Hi,

Thank you.

With the above modifications I got the loopback mode perfectly.

Now I am trying to transmit the data b/w each other.

I will be thankful if you provide any sample code to tx-rx.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 21, 2009 12:20 am     Reply with quote

Look at the link that I gave you on the first page of this thread
that has code for two boards to talk to each other.
girichavana



Joined: 21 Oct 2008
Posts: 17

View user's profile Send private message

PostPosted: Wed Oct 21, 2009 2:22 am     Reply with quote

Thanks a lot

I got some ack b/w the two nodes

With some other information i will get back to you


Thanks and regards
girichavana



Joined: 21 Oct 2008
Posts: 17

View user's profile Send private message

PostPosted: Wed Oct 21, 2009 5:48 am     Reply with quote

Hi,

I worked with your test program and it is working fine

Now I modified your code to work like a chatting system b/w two PC's as shown below

PC-1 <-----> NODE1 <-----> NODE2 <-----> PC-2

NODE1 Code:
Code:

//NODE - 1
#include <16F877A.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define CAN_DO_DEBUG FALSE

#define EXT_CAN_CS   PIN_D1
#define EXT_CAN_SI   PIN_C5
#define EXT_CAN_SO   PIN_C4
#define EXT_CAN_SCK  PIN_C3

#define BOARD1_ID  24
#define BOARD2_ID  25

#define use_portb_lcd      //  LCD port
#include "lcd.c"

#include <can-mcp251x.c>

static unsigned char ch;
static int1 temp_flag=0;

#INT_RDA
void RS232_Handler()
{
    ch = getch();
   
   temp_flag=1;   
}

void main(void)
{
   
   struct rx_stat rxstat;
   int32 rx_id;
   int32 tx_id;
   int8 rx_len;               
   int8 buffer[8];

    enable_interrupts(INT_RDA);
      enable_interrupts(GLOBAL);

   puts("Can Sample");
    lcd_init();
   lcd_putc("\f  Can Sample    \n");
   lcd_putc("     NODE --- 1 ");
   can_init();
   lcd_putc("\f  Can Init Cmptd\n");
   lcd_putc("     NODE --- 1 ");
    puts("Init Completed");
   while(1)
     {
   
       
      
        if(temp_flag==1)
      {
         disable_interrupts(GLOBAL);
         buffer[0]=ch;
         can_putd(BOARD2_ID, buffer, 1, 1, 1, 0);   
         temp_flag=0;
         enable_interrupts(GLOBAL);
         puts("Ch transmitted");
      }
         if(can_kbhit())  // Message available ?
        {
                         // If so, get the message.
            if(can_getd(rx_id, buffer, rx_len, rxstat))
           {
               lcd_putc("\f");

             lcd_gotoxy(1,1);printf(lcd_putc,"CSTAT=%2x",buffer[0]);
            lcd_gotoxy(10,1);printf(lcd_putc,"CTRL=%2x",rx_id);
               
            if(rx_id == BOARD1_ID)  // Is it for this board ?
                 {
                  lcd_gotoxy(1,2);lcd_putc("Node-2 Data Rcvd");
               puts("Data Rcvd Node-2");
                 }
           }
         }
     }
}
 



Node2 code:
Code:

//NODE - 2
#include <16F877A.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define CAN_DO_DEBUG FALSE

#define EXT_CAN_CS   PIN_D1
#define EXT_CAN_SI   PIN_C5
#define EXT_CAN_SO   PIN_C4
#define EXT_CAN_SCK  PIN_C3

#define BOARD1_ID  24
#define BOARD2_ID  25

#define use_portb_lcd      //  LCD port
#include "lcd.c"

#include <can-mcp251x.c>

static unsigned char ch;
static int1 temp_flag=0;

#INT_RDA
void RS232_Handler()
{
    ch = getch();
   
   temp_flag=1;   
}

void main(void)
{
   
   struct rx_stat rxstat;
   int32 rx_id;
   int32 tx_id;
   int8 rx_len;               
   int8 buffer[8];

    enable_interrupts(INT_RDA);
      enable_interrupts(GLOBAL);

   puts("Can Sample");
    lcd_init();
   lcd_putc("\f  Can Sample    \n");
   lcd_putc("     NODE --- 2 ");
   can_init();
   lcd_putc("\f  Can Init Cmptd\n");
   lcd_putc("     NODE --- 2 ");
    puts("Init Completed");
   while(1)
     {
   
       
        if(temp_flag==1)
      {
         disable_interrupts(GLOBAL);
         buffer[0]=ch;
         can_putd(BOARD1_ID, buffer, 1, 1, 1, 0);   
         temp_flag=0;
         enable_interrupts(GLOBAL);
         puts("Ch transmitted");
      }
         if(can_kbhit())  // Message available ?
        {
                         // If so, get the message.

            if(can_getd(rx_id, buffer, rx_len, rxstat))
           {
               lcd_putc("\f");

             lcd_gotoxy(1,1);printf(lcd_putc,"CSTAT=%2x",buffer[0]);
            lcd_gotoxy(10,1);printf(lcd_putc,"CTRL=%2x",rx_id);
               
            if(rx_id == BOARD2_ID)  // Is it for this board ?
                 {
                  lcd_gotoxy(1,2);lcd_putc("Node-1 Data Rcvd");
               puts("Data Rcvd Node-1");
                 }
           }
         }
     }

With the above code I am getting the data transmission from PC2 to PC1
but its not transmitting data from PC1 to PC2.

Please Help
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 21, 2009 12:20 pm     Reply with quote

You made the program. You should debug it.
taito



Joined: 07 Oct 2013
Posts: 1

View user's profile Send private message

changing the crystal
PostPosted: Mon Oct 07, 2013 10:52 pm     Reply with quote

If I were to change the crystal to 20Mhz and try to transmit CAN at 500kbps will the code above suffice for the 16f877a?
_________________
The world is for you to explore.. Learn and put it to good use.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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