MiCS6814-I2C-Firmware/MiCS6814-I2C-Firmware-V1/MiCS6814-I2C-Firmware-V1.h

108 lines
4.1 KiB
C
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2018 Nis Wechselberg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Firmware for MiCS-6841-based I2C adapter board.
* Implements a behaviour compatible to the Seeedstudio
* Grove Mutichannel Gas Sensor-library.
*
* Using ATmega core from:
* https://raw.githubusercontent.com/carlosefr/atmega/master/package_carlosefr_atmega_index.json
*
* Pin mapping:
* Heater Enable (out): PB0 / Digital 8 (Active low)
* NH3 resistance (in): PC0 / Analog 0
* RED resistance (in): PC1 / Analog 1
* OX resistance (in): PC2 / Analog 2
*
* SDA: PC4 / Analog 4
* SCL: PC5 / Analog 5
*/
#ifndef MiCS6814_V1_H
#define MiCS6814_V1_H
// Include Wire library for I2C support
#include <Wire.h>
#include <EEPROM.h>
// Pin mapping constants
#define HEATER 8
#define NH3_IN A0
#define RED_IN A1
#define OX_IN A2
/*
* I2C commands
*
* Taken from Grove Mutichannel Gas Sensor Library, just some renaming done here
*/
#define CMD_GET_NH3 1 // Retrieve current resistance for NH3 sensor channel
#define CMD_GET_RED 2 // Retrieve current resistance for RED sensor channel
#define CMD_GET_OX 3 // Retrieve current resistance for OX sensor channel
#define CMD_GET_ALL 4 // Retrieve all sensor resistance channels
#define CMD_CHANGE_I2C 5 // Change I2C address of the sensor
#define CMD_READ_EEPROM 6 // Read stored uint16_t data from EEPROM (MSB is transmitted first)
#define CMD_SET_R0 7 // Set custom R0 values
#define CMD_GET_R0 8 // Retrieve current R0 values
#define CMD_GET_R0_DEFAULT 9 // Retrieve default R0 values
#define CMD_CONTROL_LED 10 // Control status LED (no LED on my board, but Grove supports is, so ... meh!)
#define CMD_CONTROL_PWR 11 // Heater control
/* EEPROM Addresses
*
* These match the addresses used in the Grove library
* If the library was implemented in a sensible fashion I would
* be free to use my own mapping here.
* However, the library accesses eeprom data directly quite frequently, so ... meh!
*/
#define EEPROM_VERSION_ID 0 // Identifier for the current version, Grove uses value 1126 to identify version 2 of their sensor.
#define EEPROM_R0_DEFAULT_NH3 2
#define EEPROM_R0_DEFAULT_RED 4
#define EEPROM_R0_DEFAULT_OX 6
#define EEPROM_R0_NH3 8
#define EEPROM_R0_RED 10
#define EEPROM_R0_OX 12
#define EEPROM_I2C_ADDR 20
#define DATA_VERSION_ID 1126
#define DATA_I2C_ADDR 0x04
#define DATA_R0_DEFAULT_NH3 123
#define DATA_R0_DEFAULT_RED 123
#define DATA_R0_DEFAULT_OX 123
// Runtime data
uint32_t timer; // Timer value of last update
uint16_t resistanceNH3; // Current resistance value for NH3 channel
uint16_t resistanceRED; // Current resistance value for RED channel
uint16_t resistanceOX; // Current resistance value for OX channel
uint8_t i2CResp[6]; // Buffer for I2C response
uint8_t i2CRespLength; // Amount of bytes to be sent to master
#endif