Using an ATtiny85 to meassure volume in a fuel tank as an One Wire Slave:As a part of my home automation system, I needed an indicator for the amount of fuel oil remaining in the tank. The house is my vacation house which I monitor and control via internet.
One Wire Slave emulation:This was the hard part. There are many articles on the net about the subject, but most of them did not work with my ATtiny85. This posting is very nice and it works, but for my purpose I needed to structure the protocol for more general use. Now the OneWireSlave protocol is isolated in it’s own library as an object. The OneWireSlave object has four methods/events: uint8_t crc8(const uint8_t* data, uint8_t numBytes); // calculate crc8 of a buffer "data" with the length "numBytes" void begin(void (*onCommand)(uint8_t), uint8_t* owId); // Initialize the OneWireSlave object and sets the callback // routine "onCommand". // When the OneWireSlave object detects a Command (set pio, // get pio, set scratchpad, get scratchpad), "onCommand" is called. // Based on the command, the OneWire slave will respond by // reading or writing a buffer of bytes. void read(uint8_t* buffer, uint8_t numBytes, void (*complete)()); // ask OneWireSlave object to receive ‘length’ bytes into ‘buffer’. // call function "complete" on fininsh. void write(uint8_t* buffer, uint8_t numBytes, void (*complete)()); // ask OneWireSlave object to send 'numBytes' from 'buffer' // call function "complete" on fininsh. void reset(); // ask OneWireSlave to reset connection (send break) These five methods/events makes it easy to implement any OneWire device. I2C Master protocolThe protocol is implemented in it's own library as an object. The object has the following methodes: void transmitTo(unsigned char address); // initiate transmission to an I2C device void requestFrom(unsigned char address); // request bytes from I2C device unsigned char rx(unsigned char ack); // receive byte from I2C device - ack is 0 if last byte unsigned char tx(unsigned char b); // send byte to I2C device void stop(); // stop sequence The ATtiny85:The ATtiny85 is a small, cheap 8-pin DIL chip. In my application, two pins are used for VCC (pin 8) and GND (pin 4). One pin is used for reset (pin 1).
This leaves 5 pins for the user.
The ATtiny85 is programmed with an Arduino Nano using Arduino IDE. Downloads: |