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

CAN Bus problem for beginner

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



Joined: 16 Apr 2009
Posts: 2

View user's profile Send private message

CAN Bus problem for beginner
PostPosted: Thu Apr 16, 2009 12:22 pm     Reply with quote

I have two 18F458 and I want to communicate to each other via CAN bus. I am beginner and that is my first project with CAN. I know that my problem will be very simple for you Very Happy . If any one helps, I will be happy. There is a led at one node and a button at the other node. I set up this example on the breadboard. I examined EX_CAN.C and other two CAN examples. In fact, I don't know what my problem is. Anyone can see any software wrong?? If there is no wrong on software, I will look at my hardware. Here is my codes and information about project:
-Compiler is PCH C compiler 4.057
-Baud rate is 250Kbps
-Prob. Delay=1 ,Phs_seg1=4,Phs_Seg2=4,SJW=1,Brp=1 and oscillator is 8 MHZ (as you see on codes)
Code:

#include <18F458.h>
#device adc=8
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES H4                       //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOOSCSEN                 //Oscillator switching is disabled, main oscillator is source
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


Transmitter Codes:
Code:

#include "C:\Program Files\PICC\Projects\lcd\transmitter.h"
#define CAN_USE_EXTENDED_ID TRUE
#include <can-18xxx8.c>
#define BUTTON    PIN_A4
#define tx_id    0x201
#define BUTTON_PRESSED  !input(BUTTON)
void main()
{
int buffer[8];
     int i;
  for(i=0;i<8;i++) {
      buffer[i]=0
}
  can_init();
    can_set_mode(CAN_OP_CONFIG);
   BRGCON1.brp=1;
   BRGCON1.sjw=1;
   BRGCON2.prseg=1;
   BRGCON2.seg1ph=4;
   BRGCON2.sam=FALSE;
   BRGCON2.seg2phts=FALSE; 
   BRGCON3.seg2ph=2;
   BRGCON3.wakfil=TRUE;

 can_set_mode(CAN_OP_NORMAL);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   // TODO: USER CODE!!
   if (BUTTON_PRESSED) {
         while (BUTTON_PRESSED) {}
         delay_ms(200);
can_putd(tx_id, &buffer[0], 1, 1, 1, 0);
   }
}


Receiver Node:
Code:

#include "C:\Program Files\PICC\Projects\lcd\led.h"
#define CAN_USE_EXTENDED_ID TRUE
#use delay(clock=8000000)
#define CAN_DO_DEBUG TRUE
#include <can-18xxx8.c>
#define PIN_LED1  PIN_A5
#define LED1_HIGH output_low(PIN_LED1)
#define LED1_LOW  output_high(PIN_LED1)
#define RESPOND_TO_ID_LED  0x201
void main()
{
 struct rx_stat rxstat;
   int32 rx_id;
   int buffer[8];
 int rx_len;
 int i;
  for(i=0;i<8;i++) {
      buffer[i]=0;
   }
   can_init();
   can_set_mode(CAN_OP_CONFIG);
    BRGCON1.brp=1;
   BRGCON1.sjw=1;
   BRGCON2.prseg=1;
   BRGCON2.seg1ph=4;
   BRGCON2.sam=FALSE;
   BRGCON2.seg2phts=FALSE; 
   BRGCON3.seg2ph=2;
   BRGCON3.wakfil=TRUE;

   can_set_mode(CAN_OP_NORMAL);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
 // TODO: USER CODE!!
     while(TRUE)
   {
while(!can_kbhit());
 if ( can_kbhit() )   //message available?.
      {
         if(can_getd(rx_id, &buffer[0], rx_len, rxstat)) { //...then get data from buffer
            if (rx_id == RESPOND_TO_ID_LED) {
                LED1_HIGH;
            }
            else
            {
            LED1_LOW;
            }
             }
         }
      }
 }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 16, 2009 12:44 pm     Reply with quote

Quote:
#FUSES H4
#use delay(clock=8000000)

oscillator is 8 MHZ

The H4 fuse is not correct if you have an 8 MHz crystal and you want
to run the PIC at 8 MHz. You should use the HS fuse. Change it.

Read this thread for a link to some Loopback code which will allow you
to test each CAN bus node individually to see if they work.
Also, there is test code for two CAN bus boards which are connected
together. It will test if they can communicate with each other:
http://www.ccsinfo.com/forum/viewtopic.php?t=29627
rodrii



Joined: 16 Apr 2009
Posts: 2

View user's profile Send private message

thx
PostPosted: Fri Apr 17, 2009 2:11 am     Reply with quote

Thank you very much for your quick reply. I don't know how I made mistake like that Very Happy . From your message I understood that this program is right and if I have a problem about CAN bus, the reason of lack of communication will be hardware.
I also have some questions. If you reply, I will be happier. My CAN bus is on breadboard, is that can be problem?? Is there any simulation program like proteus for PIC?
And can you send me any sample simple program like this? But without pc connections I mean no PC will connect to PICs.
Thank you again for your reply.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 17, 2009 11:49 am     Reply with quote

Quote:

From your message I understood that this program is right and if I have
a problem about CAN bus, the reason of lack of communication will be
hardware.

It could be hardware or software. By using my test programs, you can
prove if your hardware is good. The test programs should work. If
they don't, you likely have a hardware problem which can then be fixed.

First, prove that your hardware is good. Once that is done, then test it
with your own code.

Quote:

And can you send me any sample simple program like this? But without pc connections I mean no PC will connect to PICs.

I don't want to re-write my two-board test program to use switches and
LEDs. It's too much work.
Guest








ok
PostPosted: Sat Apr 18, 2009 4:33 am     Reply with quote

thx again for your reply. at first I will do your program in my hardware...
then I can solve my problem, I think..
If i have a problem, i will post again Very Happy
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