Close

I2C slave: Firmware testing

A project log for FCThermostat: Another overengeneered IOT device

A new way of thinking to a IOT thermostat:

nicolNicolò 10/26/2018 at 21:440 Comments

I'm testing out the firmware for the baseboard. Right now I have basic UART bridge (I have only tested the TX from Master to Slave) and the IO Expander functions. Interrupts are not implemented yet. I'm working on PinChangeInterrupts for the IO Expander.

This is part of the sketch of the master:

#include <Wire.h>

void setup() {
  Wire.begin();  

  Wire.beginTransmission(42); // Transmit to device #42
  Wire.write(0x02);           // Write to the port direction register
  Wire.write(0xF0);           // Register data
  Wire.endTransmission();     // Stop transmitting

  delay(500); // Some delay to make is easy to see on the logic analyzer

  Wire.beginTransmission(42);   // Transmit to device #42
  Wire.write(0x07);             // Write to the TX buffer register
  Wire.write("HELLO!\n");       // Register data
  Wire.endTransmission();       // Stop transmitting
}

void loop() {
  bool changed = false;
  uint8_t byte_t = 0;

  unsigned long currentMillis = millis();
  // See "Examples > Blink without delay" to understand how this work
  if (currentMillis - red_millis >= red_int) {
    red_millis = currentMillis;
    // Instead of writing the pin we toggle a bit on the register
    bitWrite(byte_t, 1, !state_red);
    state_red = !state_red;
    changed = true;
  }

  if (changed) {
    Wire.beginTransmission(42); // Transmit to device #42
    Wire.write(0x04);           // Port status register
    Wire.write(byte_t);         // Port data
    Wire.endTransmission();     // Stop transmitting
  }
}

Discussions