A friend of mine, who is doing customizations for motorcycles, asked me if I would be able to do some CAN Bus programming. He was looking for a device that could mute the radio on a Harley Davidson when the intercom is being used. The activation of the intercom would be indicated via a pin high / pin low signal.
As it so happened, I started digging into CAN Bus with one of those ELM327 dongles and my Android phone just a few weeks before he asked me (this will be the topic of another post, promised). So I was quite excited to do something useful with the knowledge I just recently gained, getting to solve a real world
problem challenge ;o)
I decided to go for the AVR-H128-CAN header board by Olimex, which is not too expensive and comes in a nice format (even though it is missing some holes to install the board with spacers instead of having it “floating” around). The board is based on the Atmel AT90CAN128 and one of the things I like the most about Atmel is that they are not only providing the hardware but also tons of documentation and even a software library to start with. (I might as well have gone with any other MCU (German, translated), like the Atmega328P, which is being used in the Arduino Uno, but the AT90CAN128 (as the name is already suggesting) comes with a built-in CAN controller. With the Atmega328P you would need to add a CAN controller (e.g. Microchip MCP2515) to the circuit. All you have to add to it is a CAN transceiver (e.g. Microchip MCP 2551) and you are good to go.
I’m not going into detail about CAN bus communication, since there is already A LOT of documentation around. But this much shall be said: If you want to talk to a CAN bus, appart from the hardware you also need message IDs and message data. This was something Harald already got from Harley Davidson, so sniffing the CAN bus was not necessary anymore. The ID for the radio commands is 0x383 and the data for muting / unmuting the radio is just two bytes, either 0x08 0x1E or 0x08 0x1C.
The software can be found in my github repository.
From there it was pretty straight forward… or was it? Well, since I chose a non-Arduino approach I also wanted to avoid the Arduino overhead that comes with the IDE and toolchain. With notepad++ as my favorite
text source code editor I decided to use WinAVR to compile the source code.
The biggest challenge was to create a makefile, which is one of the many things Arduino is
hiding from keeping transparent to you (there was a quite nice article about this on hackaday). The next challenge was uploading the binary to the board. Using the mySmartUsb light dongle and the on board ICSP was quite easy, but then you also have to deal with fuses. Setting them wrong could either damage the MCU or lock it up. But again, there is so much information around, I won’t waste your time elaborating about this.