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

Tired topic, I'm sorry - CANBus (18F4685)

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



Joined: 28 May 2006
Posts: 56

View user's profile Send private message

Tired topic, I'm sorry - CANBus (18F4685)
PostPosted: Mon Dec 07, 2009 11:01 pm     Reply with quote

Hello everyone,
It seems the same subjects keep coming up, over and over. I'm sorry to be the one to revive this one again.

First the background:
I have two breadboards setup identically:
18F4685
LED wired active high via 330ohm resistor on PIN_A0.
/MCLR via 47k ohm resistor to +5v
Reset push button going from MCLR to GND.

I am using a PICKIT2 clone I've had for years. I am using CCS PCWH 4.042.
(This is my first project using the v4 IDE...very different.)

And I'm trying to get CANBus to work.

I am currently staying with family so I do not have my entire development environment with me, specifically, I do not have a TTL to serial converter for debugging with me.

My goal is to have these two 18F4685s talking CAN using the example code found at http://www.ccsinfo.com/forum/viewtopic.php?t=29627&start=6

I want to use the internal osc on both PICs, and I would like them running at full speed.

I am able to blink the LEDs at rates seem right (watching the light blink and counting to myself, one-one-thousand, two-one-thousand, etc).

I have a wire from PIN 35 of one PIC to PIN 35 of the other, the same for pin 36. I have a 120ohm resistor between these two pins, right next to the jumper wires, on each board, I have also tried it without termination.

I have modified the code to blink LEDs instead of sending serial data.

The problem is: they don't blink. By moving the blink commands around it seems Board2 does not ever RX the packet, thus Board1 never sends a second one.

I have programmed both boards with Board1 firmware set to do loopback, and they work. I had to change the code to check to see if it was BOARD2's ID so my code would blink the LED correctly, and it did.

My question:
Does it sound like my hardware is setup correctly? Can I use the internal osc @ 32mhz like this? Am I even setting it up correctly?

What else can I check?

Code is below:
Code:

#include <18F4685.h>
#fuses INTRC,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,MCLR
#use delay(clock=32000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <can-18F4580.c>  // Use correct driver

#define BOARD1_ID  24
#define BOARD2_ID  25

// Uncomment this line to compile code for Board #1.
// Comment it out to compile for Board #2.
#define BOARD1   1   

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

output_high(PIN_A0);
delay_ms(100);
output_low(PIN_A0);
delay_ms(100);

can_init();

//can_set_mode(CAN_OP_LOOPBACK);

#ifdef BOARD1   // For Board #1
while(1)
  {
   output_high(PIN_A0);
   delay_ms(100);
   buffer[0] = 'a';   // Wait for a character

   // Transmit it to board #2.
   can_putd(BOARD2_ID, buffer, 1, 1, 1, 0);

   buffer[0] = 0;   // Clear the buffer byte

   // Wait for the char to be echoed back.
   while(!can_kbhit());
output_low(PIN_A0);
delay_ms(50);
output_high(PIN_A0);
delay_ms(50);
output_low(PIN_A0);
delay_ms(50);
output_high(PIN_A0);
delay_ms(50);
output_low(PIN_A0);
delay_ms(50);
output_high(PIN_A0);
delay_ms(50);

   if(can_getd(rx_id, buffer, rx_len, rxstat))
     {
      if(rx_id == BOARD1_ID)  // Is it for this board ?
        {
         putc(buffer[0]);  // If so, display the char
         output_low(PIN_A0);
         delay_ms(300);

        }
      }
   
  }
#else  // For Board #2
while(1)
  {
  output_low(PIN_A0);
  delay_ms(50);
  output_high(PIN_A0);
  delay_ms(50); 
 
   if(can_kbhit())  // Message available ?
     {
      // If so, get the message.
      if(can_getd(rx_id, buffer, rx_len, rxstat))
        {
         if(rx_id == BOARD2_ID)  // Is it for this board ?
           {
            // If so, echo back the character.
            can_putd(BOARD1_ID, buffer, 1, 1, 1, 0);
            output_high(PIN_A0);
            delay_ms(1000);           
           }
         }
      }
  }
#endif
 
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 07, 2009 11:41 pm     Reply with quote

Quote:

I have a wire from PIN 35 of one PIC to PIN 35 of the other, the same
for pin 36. I have a 120ohm resistor between these two pins, right next
to the jumper wires, on each board, I have also tried it without
termination.

That's not how it works. Get two of the CAN bus driver chips and wire it
up as shown in this schematic:
http://www.microchip.com/forums/download.axd?file=0;406254
Also add a ground connection between the two boards.


---------
Edit: Fixed link


Last edited by PCM programmer on Tue Aug 24, 2010 1:55 am; edited 1 time in total
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Tue Dec 08, 2009 1:19 am     Reply with quote

I wouldn't use the internal oscillator for CAN.

You need the accuracy of a crystal for real world applications.

It will set you off on the wrong foot to use the internal RC oscillator.

Mike
kd5uzz



Joined: 28 May 2006
Posts: 56

View user's profile Send private message

PostPosted: Tue Dec 08, 2009 7:24 am     Reply with quote

Are there any PICs that don't need the MCP2551?
Guest








PostPosted: Tue Dec 08, 2009 8:05 am     Reply with quote

MikeW wrote:
I wouldn't use the internal oscillator for CAN.

You need the accuracy of a crystal for real world applications.

It will set you off on the wrong foot to use the internal RC oscillator.

Mike


If I have to add the MCP2551, there goes my small footprint, I guess I can add a crystal also (Might as well run at 40mhz then..).

Would it be that big of a deal if I'm only talking to my devices? I thought CAN provided a clock line to eliminate the need for high accuracy clocks.
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Tue Dec 08, 2009 11:02 am     Reply with quote

I think you need to research some info on CANbus.

If you don't understand the fundamentals, you are going to flounder very quickly.

If all you are looking to do is communicate between 2 PIC boards, don't use CAN.

You are asking questions which mean you haven't done even basic research.

CAN is not self clocking, and yes it needs a CAN transceiver to be called "CAN".
Guest








PostPosted: Tue Dec 08, 2009 1:36 pm     Reply with quote

I have spent a good many hours reading the actual CAN docs and everything I've been able to find on these forums, but that was about a year ago, when I first wanted to start this project. It seems I confused a few points. I understand the signaling is done in a differential manner, I confused this with having a clock signal. It is coming back to me now.

Until now, I have never seen a schematic or very good explanation of how to use a PIC that has an internal CAN module. I knew you had to use an SPI CAN module on other PICs, but had never found much detail otherwise.

My goal is to have a small network of 5 to 10 PICs on the network, and all need the ability to communicate to any other node.

I'm not happy with I2C, not even in multi master mode. I love the error correction and message prioritization features of CAN. And the broadcast nature of CAN fits well with my design and allows for some very interesting design concepts.

If there is another technology I should consider, I'm all for it, just let me know.
Guest








PostPosted: Tue Dec 22, 2009 12:09 am     Reply with quote

For the record...

I ordered a few of the MCP2551s (order from Mouser on Thursday, got them the next day, WOW!), hooked them up per PCM's schematic. Didn't even reload the code, worked right away.

Thanks! Now I'm off to playing with filters..
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