Archive for the ‘CAN Bus’ Category

New CAN Main Board

I made a few adjustments to the board layout and also to the pcb-gcode setting and milled another circuit board. This one turned out nicely, even with a couple hiccups. First, the board wasn’t taped to the fixture exactly level, so a corner with nothing important didn’t mill through the copper (it was only the board outline). Then a phantom E-stop signal stopped the machine before it was done and had to restart from the beginning (I upped the debounce value to correct this). I thought it was another beverage coaster while watching it mill, but let it finish and it turned out fine. I ran the drill routine with a .035″ bit and everything but the screw terminal block and power jack fit. I was able to make the terminal block fit by sanding down the edges of the pins, but the power jack I’ll just use some wires and plug later.

Freshly milled CAN Main board, version 0.2.

Since the majority of components fit, I soldered this one up to give me an idea of what the next round of changes need to be. The main one is to increase the size of the pads to make it easier to solder and to increase the isolation path around them to prevent bridges to the ground plane. I’ll also have to change the through holes to the right size in the Eagle component library I made. At least the spacings were all right so everything still fit. Another change will be to the text ratio so the thin areas of copper don’t peel off when milled. They probably would have been fine if the second pass wasn’t done, but whatever.

Just needs a power connection and it’s ready to go.

Anyways, the CAN Main board only contains the basic circuit to run a 18F4580 micrcocontroller with the MCP2551 CAN transceiver; power from a 7805, 20MHz crystal oscillator, ICD plug, reset button, CAN status LED and then the rest of the pins are brought out to female headers to plug in a shield. I also included an area for the CAN bus termination resistor and three capacitors with jumpers, and a place to connect the cable shielding to the board’s ground. There is a jumper to bypass the 7805 regulator to use a 5V wall wart directly which are more common these days.


Troubleshooting a CAN Circuit

I’ll bet the term troubleshooting came from someone who had trouble, then started shooting. At least that’s what I would do if I still lived in an area where I could shoot something (damn neighbors). I’ve been trying for weeks to get the K-Node circuit to work with any of my other CAN capable boards, with no avail. First, I thought the bus had too much noise, and used the oscilloscope to find a extra 60 Hz wave causing mayhem. This was removed without success and then thought a shitty clock signal was to blame. It only slightly was inhibiting communication (it only caused a few errors, but not enough to totally block the message).

Anyways, turns out the CAN transceiver was screwed up beyond use. The MCP2551 chip is very sensitive to whatever, and fries when either of the bus wires are disturbed. I should have known, but didn’t. The previous post outlines my attempt at clearing up any extra noise on the bus. Then thought it could have been an inaccurate clock to the microcontroller. I bought some nice 20Mhz oscillators and replaced the crystal/caps on both the SpartaNode and K-Node boards, without success.

So before I threw both circuits, the scope, and all the other equipment I have out the window, I replaced the MCP2551 IC. Of course it worked right away. Guh, I suppose the lesson here is to make sure the CAN bus wires are secured very, very well and to absotively, not-arino hot swap the wires. I’ll pretend it wasn’t all for waste since the random, mysterious communication errors don’t show up during testing. Now that I can send a message between the K-Node and SpartaNode, I can run the CAN bus through my house and really test the system.

The Physical CAN bus

May 21, 2012 4 comments

I made some long overdue progress on my Smart House project that uses CAN bus. Up till now, the physical bus were two scrap wires I twisted together and soldered on a couple pins for plugging into breadboards. This wasn’t going to be an option once it was time to run the actual wires through my house, since it will be more than a few feet. A search for a single twisted pair, around 18 AWG and unsheathed yielded nothing, unfortunately. I settled on some security cable, which is two conductor 22 AWG, shielded and sheathed, as well as readily available for cheap at any hardware or big box home store.

Noisy noise on CAN bus.

For a test bus, I randomly cut a piece 12 feet long and soldered on 120 ohm resistors to each end, then connected to the K-node and SpartaNode. Of course it didn’t work, and probing with an oscilloscope showed a nice 60 Hz wave causing interference. The wave was present with or without power on and also near or far from other wires. The interference wave’s amplitude varied from about 1 volt peak to peak, to over 3V when moved around and was superimposed on any CAN messages on the bus. I suppose Murphy’s electrical law was in effect; the one about a wire being an antenna when you don’t want it to be.

How to get cap values.

The good news is the book, CAN System Engineering, held the solution. By using three capacitors per end, all the 60 Hz wave was removed and a crisp CAN message was able to travel just like in the test setup. The values of the capacitors are the same for both ends and depend on the length of the bus. For mine, I needed a 68pF cap between the CAN High and Low wires, and two 220pF caps from each to ground. I soldered these to the board (but just realized I shouldn’t have, d’oh) and added a place to connect the cable shield. The two nodes are still showing a communication error, so the same CAN message is being repeated for a response, but the at least you can see it’s nice and clean looking. I suspect it is a software issue.




CAN Code for Initialization

March 18, 2012 1 comment

Here’s some code to get going. The ECAN section of the 18F4580 datasheet describes the modules function and how to use it, so I’ll only briefly describe what’s going on.

To write to certain ECAN configuration registers, you must request to enter a Config mode, then wait for the module to enter it. The Baud rate settings are tricky, but can be found easily from here, Request form for CANopen Bit Timings. Only one Receive Filter is used and linked to Receive Buffer 0. To add more Enable the filter in RFCONx, associate to a Receive buffer with RXFBCONx, then set its identifier. Last is to exit the Config mode by requesting either Normal mode or Loopback mode for testing. An important point here is that the ECAN module won’t exit config mode unless the microcontroller is correctly connected to a CAN driver IC, even for going to Loopback mode.

void ECAN_Init(void)
    LATB = 0x00;
    TRISBbits.TRISB3 = 1;

    CANCON = 0x80;    //request CONFIG mode

    ECANCON = 0x40;    //Sets ECAN to Mode 1

    BRGCON1 = 0x09;   //Sets Baud rate
    BRGCON2 = 0x84;
    BRGCON3 = 0x00;

    BSEL0 = 0xfc;

    RXM0EIDH = 0x00;    // 0's for EID and SID
    RXM0EIDL = 0x00;
    RXM0SIDH = 0xFF;    // Standard ID FILTER
    RXM0SIDL = 0xE0;    //Mask0 = 0x7ff

    RXFCON0 = 0x01;        //filter 0 enabled
    RXFCON1 = 0x00;        //all others disabled

    MSEL0 = 0xfc;        //Mask0 assigned to Filter0
    MSEL1 = 0xFF;        //all others no mask
    MSEL2 = 0xFF;
    MSEL3 = 0xFF;

    RXFBCON0 = 0x00;    //filter 0 assoc w/ RXB0, 1 w/ RXB1

    RXB0CONbits.RXM1 = 1;

    RXF0SIDH = 0x04;    //Recv Filter0 = 0x020
    RXF0SIDL = 0x00;    //Std. Identifier
    RXF0EIDH = 0x00;
    RXF0EIDL = 0x00;

    CANCON = 0x00;        //Set ECAN to normal mode
    //CANCON = 0x40;        //Set ECAN to Loopback mode(TEST)
    while(CANSTATbits.OPMODE2 == 1);    //wait till out of Config Mode
}//end of ECAN_Init()

When setting up a test bus, start with one node set in Loopback mode. In this mode, the acknowledge bits are ignored and allows the node to receive CAN messages transmitted from itself.This will confirm the baud and filter settings are correct. I made a loop of sending and receiving with counters, then displaying on an LCD(nokia 6100). Once I had my two breadboards able to send and receive the same number of messages, I connected them together. Of course the micrcontroller must be connected to a CAN driver IC, the MCP2551, with a 60Ω resistor connected between the CANH and CANL pins, or the module would never get out of config mode.

The datasheet describes how to transmit a message, and doesn’t need any extra configuration. To receive a message, the ECAN module will accept and place data in the proper register, then set a flag that can be polled. An interrupt can be used too.

Hopefully this helps get others going. Later.

THIS IS SpartaNode!

March 14, 2012 Leave a comment

I mean the prototype sensor node for my house CAN-bus, decked out in MSU colors. So with the exception of possibly the next node, no other circuit boards will have a theme or use primitive construction techniques.  The real sensor node will use a PIC18F2580 SOIC package, have a board less than 50cm square and use the shield concept swiped from an Arduino. At least that’s what I put together to have made by someone else. As a side note, I’d like to know what perf board would have hole spacing of √(.02)” to allow the screw terminals shown jammed in diagonally.

Since this 40-pin microcontroller is under-utilized, I’ll use it to test more nodes in the future. It was quicker to spend a day or so cutting, stripping and soldering wires than wait weeks for real circuit boards. This will at least allow me to start a test bus, even if it only has a couple nodes.

The next steps are software, and how to use the built in features of CAN smartly. A lot of this is already in spec form, like SAE-J1939 for heavy machinery that costs money all the way to OpenCAN that’s less money. These  schemes are more for systems that are time critical and people’s live being at risk. None of these are applicable to a normal house, so a new scheme can be developed. My goal is to release open source boards and basic code libraries for hackers and to provide design services for people who don’t think any of this is fun (weirdos).


February 29, 2012 6 comments

Here’s the first permanent board for a node that is intended for the kitchen.The bell doesn’t work yet, but the rest of the circuit is there, including everyone’s favorite feature, a clock! This is a copy of the breadboard Node #2 and its program, so just receives the message from Node #1, but not the sensor reading. The RTC is provided by the DS1305 to the right of the PIC and uses the crystal hidden inside the blue shrink tube below (it was an SMD part).

The K-Node

K-Node Circuit

Since this circuit is going up on the wall, I tried to make it look nice. The paint scheme is all the 99¢ spray variety as usual, gloss black for the piece of pine board and flat black for the perf board. To hold the LCD, I drilled undersized pockets (about the width of the flats) for the #4 nuts and stamped the hex with a rubber mallet and punch. After painting they were glued in with superglue. The circuit board and bell is similar, but are size #6 and the nuts are on the bottom.

Circuit board mountings.

The bell seems original to my house, but was terrible sounding when I first bought the place. I though it was wall papered over, but was just painted close to a hundred times. Once I stripped off the paint it rang instead of clattered. It rings fine from a nice beefy power supply, but sagged the puny wall wart and browns out the microcontroller. Guh, so I’ll have to get real readings and shit to get that going. Anyways, here’s what is looks like inside.

Bell guts.

So this concludes the physical portion of the design. The MCP2551 and the ECAN engine in the PIC18F4580 takes care of that for the most part. So now I’m on to software development.