Arduino VSCP CAN sample 1

Version 0.0.1 2012-08-31
Copyright (C) Åke Hedman, Grodans Paradis AB
akhe@grodansparadis.com, http://www.vscp.org

Download this sample here download

This is a simple implementation of VSCP on an Arduino Duemilanove board ( Duemilanove ) with a CAN shield from sparkfun.com ( Sparfun CAN Shield ). It may or may not work on other board combinations.

As the CAN library written for the shield did not handle extended id's which are needed by VSCP and is very poorly written I decided to use a library by David Harding and changed this code to my needs.

If you are new to VSCP please start by reading this short primer VSCP Primer also there is some introduction videos at VSCP and you can also find the full specification at that address.

VSCP is constructed to make it easy (and fun) to build control systems that can be controlled remotely and work with or without a master device. VSCP is free and will always be free to use and implement i products.

A software suite called VSCP & Friends is available that can be run on Windows and Linux. Can be downloaded here Download



This sample module implements support for the Joystick of the CAN shield and it also allow you to control some outputs and read one analog input. Additional support such as supporting an LCD display, supporting the GPS, supporting the memory card can easily be added.

The sample code has a eight line decision matrix which can be programmed to perform actions when VSCP events (CAN messages) is received by the unit.

Getting events from the joystick

Just move the joystick. The click of the joystick is also init. Hold it for more then two seconds and the init-functionality of the module will be activated and the nickname discovery process will start.

Get events from serial channel

It is possible to both send and receive serial data. If you want to get events from the serial channel you first have to set the baudrate. Do this in registers 10/11. Default baudrate is 9600 baud. The port will initially be closed and by setting bit 0 of register 9 you can open the port. This bit is persistent so when set to one it will be set to one the next time you power up the module. Bit 1 enable stream events from the serial channel. When set incoming data on the serial channel will be sent out as events on the VSCP bus. You can have two kinds of events sent out. Just plain streaming data or streaming data with zone. Select which one you want i bit 2.
Events are sent out as soon as possible so depending on baudrate events will have one to seven characters each.

Controlling LED's

The CAN shield supports two LED's D7 and D8. D8 is also status LED used by the VSCP stack. It blinks for instance during initialization. Both can be controlled in register 19.

I/O

The sample code have four free I/O's D3,D4,D5,D6. In register 2,3,4,5 you can read the current state of the I/O channel(bit 0), set it as an input or output (bit 1) enable pullup (bit 2).
It is also possible to get continous reports of I/O status. Just write the interval in seconds you want between I/O evenst in register 17. Zero disable continous reports.

Get continous A/D measurements

The sample code support one A/D converter. The current value of this A/D converter can be obtained by reading register 7/8. It is also possible to get continous events reporting the value. Just set the time between reports in register 18 in seconds. Set to zero to disable.

Decision Matrix

The module have a decision matrix with eight elements. In the decision matrix you can program what the module can do with incoming events and what incoming events that should make it do it. All VSCP events can make an action happen. It's up to yo to decide what. See the VSCÅ specification about how this is done. The VSCP Works tool is a perfect companion in programming the matrix as it can help you in configure it in clear text. See actions later in this document about what actions that are available.

Registers

Register 0: Zone for module (r/w).

Register 1: Subzone for module (r/w).

Register 2: Control register for D3 (r/w).
Bit 0: Current state of bit.
Bit 1: 0=Input, 1=output.
Bit 2: Enable pull up if set to one.

Register 3: Control register for D4 (r/w).
Bit 0: Current state of bit.
Bit 1: 0=Input, 1=output.
Bit 2: Enable pull up if set to one.

Register 4: Control register for D5 (r/w).
Bit 0: Current state of bit.
Bit 1: 0=Input, 1=output.
Bit 2: Enable pull up if set to one.

Register 5: Control register for D6 (r/w).
Bit 0: Current state of bit.
Bit 1: 0=Input, 1=output.
Bit 2: Enable pull up if set to one.

Register 6: Control register for A0 (r/w).

Register 7: Analog converter 0 - MSB byte(read only).

Register 8: Analog converter 0 - LSB byte(read only).

Register 9: Serial port control (r/w).
Bit 0: Activate port if set to one. This is persistant so if set to one the port will be open automatically on next restart of board.
Bit 1: If set to one send stream events for incoming data on serial port.
Bit 2: Set to one to send stream data with zone/subzone.

Register 10: Serial baudrate MSB (r/w).

Register 11: Serial baudrate LSB (r/w). The actual baudrate change happens when the LSB byte is written so it is important to write MSB byte first and then LSB.

Register 12: Joystick control (r/w). Bit 0: Add index of joystick button to subzone before sending On-event.
Bit 1: Add code of joystick button to subzone before sending Button-event.
Bit 2: On event is sent if zero, button events if set to one.

Register 13: Zone used for On events. (r/w).

Register 14: Subzone used for On events. (r/w).

Register 15: Zone used for Button events. (r/w).

Register 16: Subzone used for Button events. (r/w).

Register 17: Second between I/O state events. Set to zero to disable. See events below. (r/w).

Register 18: Second between A/D state events. Set to zero to disable. See events below. (r/w)

Register 19: Control LED's D7/D8(status). Bit 0: LED D7. 1=On, 0=Off.
Bit 1: LED D8. 1=On, 0=Off.

Register 64-127: A eight row decision matrix.

Decision matrix

Actions

Action = 0: NOOP, no operation.
Action = 1: CLASS1.INFORMATION(20), Type = 18 (0x12) Good day event is sent.
Action = 2: Open serial port.
Action = 3: Close serial port.
Action = 4: Send data on serial port. Index into data by action parameter.
Action = 5: CLASS1.DATA(15), Type = 1 (0x01) I/O – value event is sent. Se description in events section below.
Action = 6: CLASS1.DATA(15), Type = 2 (0x02) A/D value event is sent. Se description in events section below.

Events generated by the node

CLASS1.INFORMATION(20), Type=9 Node Heartbeat

This event is sent every minute by the node.

byte 0: Allways zero.
byte 1: Zone for module. Always 0xff.
byte 2: Subzone for module. Always 0xff.

CLASS1.INFORMATION(20), Type=3 On

This event will be generated for the joystick of the CAN shield. The datapart of the event will contain information on the joystick direction.

byte 0: Index coded as below.
byte 1: Zone for module.
byte 2: subzone for module.

Index coding.

UP	  1
RIGHT	  2
DOWN	  3
CLICK	  4
LEFT	  5

The CLICK position of the joystick is special as it also will take the baord into init-mode if held for more then two seconds.

CLASS1.INFORMATION(20), Type=1 Button

This event will be generated for the joystick of the CAN shield. The datapart of the event will contain information on the joystick direction.

byte 0: Zone for module.
byte 1: Subzone for module.
byte 2: 0
byte 3: Code for button.

Button coding

UP		1
RIGHT	        2
DOWN	        3
CLICK	        4
LEFT	        5	


CLASS1.INFORMATION(20), Type = 36 (0x24) Stream Data.

Serial data is sent as stream event if enabled.

byte 0: Index of package. Icreased by one for each event sent.
byte 1-7: Serial data. Number of bytes varies.

CLASS1.INFORMATION(20), Type = 38 (0x26) Stream Data with zone.

Serial data is sent as stream event if enabled.

byte 0: Index of package. Icreased by one for each event sent.
byte 1: Zone for module.
byte 2: Subzone for module.
byte 3-7: Serial data. Number of bytes varies.

CLASS1.INFORMATION(20), Type = 18 (0x12) Good day

This event is sent as a result of Action=1.
byte 0: Always set to 0.
byte 1: Zone for module.
byte 2: Subzone for module.

CLASS1.DATA(15), Type = 1 (0x01) I/O – value

This event is sent as a result of Action=5 and/or is sent on regular intervals specified in register 17.

byte 0: Datacoding=0, bitfield. See section 8 in specification. byte 1: Representing state of I/O pins

bit 0: D3
bit 1: D4
bit 2: D5
bit 3: D6
The rest of the pins are set to zero.	


CLASS1.DATA(15), Type = 2 (0x02) A/D value – value

This event is sent as a result of Action=6 and/or is sent on regular intervals specyfied in register 18.

byte 0: Datacoding=96, 16-bit integer. See section 8 in specification.
byte 1: MSB of integer.
byte 2: LSB of integer.

Bootloader

No CAN bootloader is implemented.

 
modules/arduino_sample_1.txt · Last modified: 2014/03/27 07:33 (external edit)
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki