Close

I2C and complex wire protocols

A project log for BlueBasic: BASIC for Bluetooth

Embedded BASIC interpreter for CC2540 and CC2541 Bluetooth LE modules

tim-wilkinsonTim Wilkinson 08/18/2014 at 20:190 Comments

One thing I always felt the Arduino made a bit "manual" was writing the various wire protocols you need to talk to common devices. There are various one-wire protocols out there for different sensors (e.g. temperature sensors like the DHT-22) and, consequentially, you have to hunt around to find a good library to talk to them, or write one yourself. While I cannot possibly include a library for every protocol, I decided to add a better way to construct a wire protocol.

So I added the "WIRE" command to Blue Basic. Essentially this allows a set of command to be written to read, write, pause, sample, and generally manipulate one of more chip pins. This makes it much simpler to write a complex protocol. For example, to read the state of the DHT-22 temperature/humidity sensor, the WIRE protocol looks like this:

WIRE P0(0) OUTPUT HIGH, LOW, WAIT 1000, INPUT PULLUP PULSE B, OUTPUT HIGH

This line outputs a 1000us start pulse to the device attached to P0_0, and then reads back the sequence of pulses from the device info the array B. B can then we reassembled into the temperature value.

As a side effect of this, I also added an I2C command (i2c is not provided natively on the CC2540) which internally uses the WIRE protocol.

Discussions