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

Two Nodes on CAN BUS

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



Joined: 10 Jan 2014
Posts: 2

View user's profile Send private message

Two Nodes on CAN BUS
PostPosted: Fri Jan 10, 2014 10:16 am     Reply with quote

Hello,
I am relatively new to PIC programming. I have some experience in writing C code.
Currently I am trying to build the CAN BUS and using two nodes (A and B).

Objective:
I want to be able to press a button on node a and turn on led on node b, then press the button on node b and turn on led on node A.

I am able to get this code to turn on led on only one side, and am struggling for a week now. Any guidance will be appreciated.

Version of my Compiler: 5.016
PIC Microcontroller: PIC18F2580 for both nodes
Standard Run Mode
Target voltage: 5V system
Target oscillator speed: 20MHz

Here is the code I have for the Node A...
Code:

#include <18F2580.h>
#fuses H4,NOPROTECT,NOLVP,NOWDT
#use delay(clock=20000000)

#include <can-18xxx8.c>

#define PIN_LED1 PIN_B0 // LED PIN

#define BUTTON PIN_B4 ///Here is a change, use pin B4
#define BUTTON_PRESSED  !input (BUTTON)

#define LED1_HIGH output_low(PIN_LED1)
#define LED1_LOW  output_high(PIN_LED1)

#define WRITE_REGISTER_C_ID 0x300
#define WRITE_REGISTER_D_ID 0x18FEDF00


void main()
{
int buffer[8];
int i;
int32 rx_id;
int a_leds=0;
struct rx_stat rxstat;
int rx_len;



setup_adc_ports(AN0_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
     
      for(i=0;i<8;i++)
      {
         buffer[i]=0;
      }
     
      can_init();
     
while(True)
      {
        if (can_kbhit())
        {
        if (can_getd(rx_id, &buffer[0], rx_len, rxstat));
         if(rx_id == 0x400)
         {
            a_leds=(buffer[0]);
            if(bit_test(a_leds,0))
            {
               LED1_HIGH;
            }
            else
            {
                LED1_LOW;
            }
          }
         } continue;
      }
continue;

if (BUTTON_PRESSED)
            {
              buffer[0]=0xF0;
              delay_ms(10);
            }
            else
            {
              buffer[0]=0xFF;
            }
     
         can_putd(0x300, &buffer[0],1,1,1,0);
     
         i=read_adc();
         delay_ms(1);
         buffer[0]=0xAA;
         buffer[1]=i;
         buffer[2]=0xBB;
     
         can_putd(0x18FEDF00, &buffer[0], 3, 1, 1, 0);}




NODE B:
Code:

#include <18F2580.h>
#fuses H4,NOPROTECT,NOLVP,NOWDT
#use delay(clock=20000000)

#include <can-18xxx8.c>

#define PIN_LED1 PIN_B0 // LED PIN

#define BUTTON PIN_B4 ///Here is a change, use pin B4
#define BUTTON_PRESSED  !input (BUTTON)

#define LED1_HIGH output_low(PIN_LED1)
#DEFINE LED1_LOW  output_high(PIN_LED1)

#define WRITE_REGISTER_A_ID 0x400
#define WRITE_REGISTER_B_ID 0x12BADF00


void main()
{
int buffer[8];
int i;
int32 rx_id;
int a_leds=0;
struct rx_stat rxstat;
int rx_len;


      setup_adc_ports(AN0_ANALOG);
      setup_adc(ADC_CLOCK_INTERNAL);
      set_adc_channel(0);
     
      for(i=0;i<8;i++)
      {
         buffer[i]=0;
      }
     
      can_init();
       
while(TRUE)
       {  if (BUTTON_PRESSED)
            {
           
              buffer[0]=0x00;
              delay_ms(10);
            }
            else
            {
              buffer[0]=0x11;
            }
         
         can_putd(WRITE_REGISTER_A_ID, &buffer[0],1,1,1,0);
     
         i=read_adc();
         delay_ms(1);
         buffer[0]=i;
         buffer[1]=0xBB;
         buffer[2]=0xCC;
         can_putd(WRITE_REGISTER_B_ID, &buffer[0], 3, 1, 1, 0);
         }
 while(TRUE)
     
      {
        if (can_kbhit())
        {
         can_getd(rx_id, &buffer[0], rx_len, rxstat);
         if(rx_id == 0x300)
         {
            a_leds=(buffer[0]);
            if(bit_test(a_leds,0))
            {
               LED1_HIGH;
            }
            else
            {
                LED1_LOW;
            }
         }
         
        }
       
      } }   



I am using PCAN-VIEW to see the messages on the BUS. I am able to see the messages from Node B to A, but not from Node A and when I press the button on Node A... nothing happens on Node B.

Help!
alan



Joined: 12 Nov 2012
Posts: 356
Location: South Africa

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 10:34 am     Reply with quote

In your NODE A code you never get to the BUTTON statement. You sit in a forever while loop;

Regards
swagistan



Joined: 10 Jan 2014
Posts: 2

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 10:46 am     Reply with quote

Hi Alan,
Thanks for the feedback. I originally had two while loops and same issue. Then i changed this to the if statement, but still see the same problem...

I may be missing something very simple. Would you advise do i need to change something in Node B as well? for this to work?
alan



Joined: 12 Nov 2012
Posts: 356
Location: South Africa

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 11:04 am     Reply with quote

You never exit your while loop, so your if loop never evaluates, so can't send a message.
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