mirror of https://github.com/arendst/Tasmota.git
v5.12.0b
5.12.0b * Add Multichannel Gas sensor using MultiChannel_Gas_Sensor library (#1245)
This commit is contained in:
parent
ecfa14d28b
commit
77be56363a
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Seeed Technology Inc.
|
||||
|
||||
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.
|
|
@ -0,0 +1,45 @@
|
|||
# Mutichannel_Gas_Sensor
|
||||
This Arduino library is used for driving "Xadow - Mutichannel Gas Sensor" and "Grove - Mutichanel Gas Sensor"
|
||||
|
||||
|
||||
## Usage:
|
||||
|
||||
mutichannelGasSensor.begin(0x04);
|
||||
|
||||
mutichannelGasSensor.powerOn();
|
||||
|
||||
then read the concentration of the specific gas you want to measure:
|
||||
|
||||
mutichannelGasSensor.measure_NH3();
|
||||
|
||||
mutichannelGasSensor.measure_CO();
|
||||
|
||||
mutichannelGasSensor.measure_NO2();
|
||||
|
||||
mutichannelGasSensor.measure_C3H8();
|
||||
|
||||
mutichannelGasSensor.measure_C4H10();
|
||||
|
||||
mutichannelGasSensor.measure_CH4();
|
||||
|
||||
mutichannelGasSensor.measure_H2();
|
||||
|
||||
mutichannelGasSensor.measure_C2H5OH();
|
||||
|
||||
For details please move to [wiki page](http://www.seeedstudio.com/wiki/Grove_-_Multichannel_Gas_Sensor).
|
||||
|
||||
|
||||
|
||||
----
|
||||
|
||||
This software is written by Jacky Zhang (![](http://www.seeedstudio.com/wiki/images/8/8f/Email_addr_of_jacky_zhang.png)) from [Seeed Technology Inc.](http://www.seeed.cc) and is licensed under [The MIT License](http://opensource.org/licenses/mit-license.php). Check License.txt/LICENSE for the details of MIT license.<br>
|
||||
|
||||
Contributing to this software is warmly welcomed. You can do this basically by<br>
|
||||
[forking](https://help.github.com/articles/fork-a-repo), committing modifications and then [pulling requests](https://help.github.com/articles/using-pull-requests) (follow the links above<br>
|
||||
for operating guide). Adding change log and your contact into file header is encouraged.<br>
|
||||
Thanks for your contribution.
|
||||
|
||||
Seeed is a hardware innovation platform for makers to grow inspirations into differentiating products. By working closely with technology providers of all scale, Seeed provides accessible technologies with quality, speed and supply chain knowledge. When prototypes are ready to iterate, Seeed helps productize 1 to 1,000 pcs using in-house engineering, supply chain management and agile manufacture forces. Seeed also team up with incubators, Chinese tech ecosystem, investors and distribution channels to portal Maker startups beyond.
|
||||
|
||||
|
||||
[![Analytics](https://ga-beacon.appspot.com/UA-46589105-3/Mutichannel_Gas_Sensor)](https://github.com/igrigorik/ga-beacon)
|
|
@ -0,0 +1,20 @@
|
|||
// Get firmware version of Grove Multichannel Gas Sensor
|
||||
#include <Wire.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
#define SENSOR_ADDR 0X04 // default to 0x04
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
gas.begin(SENSOR_ADDR);
|
||||
|
||||
unsigned char version = gas.getVersion();
|
||||
Serial.print("Version = ");
|
||||
Serial.println(version);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
// change i2c address
|
||||
// Loovee
|
||||
// 2016-11-10
|
||||
|
||||
#include <Wire.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
#define SENSOR_ADDR_OLD 0x04 // default to 0x04
|
||||
#define SENSOR_ADDR_NEW 0x19 // change i2c address to 0x19
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
gas.begin(SENSOR_ADDR_OLD); //
|
||||
gas.change_i2c_address(SENSOR_ADDR_NEW);
|
||||
Serial.print("I2C ADDRESS SET TO 0X");;
|
||||
Serial.println(SENSOR_ADDR_NEW, HEX);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// END FILE
|
|
@ -0,0 +1,57 @@
|
|||
// get RAW data from the sensor
|
||||
// Loovee
|
||||
// 2016-11-10
|
||||
|
||||
#include <Wire.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
#define SENSOR_ADDR 0X04 // default to 0x04
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
gas.begin(SENSOR_ADDR); //
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
float R0_NH3, R0_CO, R0_NO2;
|
||||
float Rs_NH3, Rs_CO, Rs_NO2;
|
||||
float ratio_NH3, ratio_CO, ratio_NO2;
|
||||
|
||||
R0_NH3 = gas.getR0(0);
|
||||
R0_CO = gas.getR0(1);
|
||||
R0_NO2 = gas.getR0(2);
|
||||
|
||||
Rs_NH3 = gas.getRs(0);
|
||||
Rs_CO = gas.getRs(1);
|
||||
Rs_NO2 = gas.getRs(2);
|
||||
|
||||
ratio_NH3 = Rs_NH3/R0_NH3;
|
||||
ratio_CO = Rs_CO/R0_CO;
|
||||
ratio_NO2 = Rs_NH3/R0_NO2;
|
||||
|
||||
Serial.println("R0:");
|
||||
Serial.print(R0_NH3);
|
||||
Serial.print('\t');
|
||||
Serial.print(R0_CO);
|
||||
Serial.print('\t');
|
||||
Serial.println(R0_NO2);
|
||||
|
||||
Serial.println("Rs:");
|
||||
Serial.print(Rs_NH3);
|
||||
Serial.print('\t');
|
||||
Serial.print(Rs_CO);
|
||||
Serial.print('\t');
|
||||
Serial.println(Rs_NO2);
|
||||
|
||||
Serial.println("ratio:");
|
||||
Serial.print(ratio_NH3);
|
||||
Serial.print('\t');
|
||||
Serial.print(ratio_CO);
|
||||
Serial.print('\t');
|
||||
Serial.println(ratio_NO2);
|
||||
|
||||
Serial.println("------------------------");
|
||||
delay(1000);
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
This is a demo to test gas library
|
||||
This code is running on Xadow-mainboard, and the I2C slave is Xadow-gas
|
||||
There is a ATmega168PA on Xadow-gas, it get sensors output and feed back to master.
|
||||
the data is raw ADC value, algorithm should be realized on master.
|
||||
|
||||
please feel free to write email to me if there is any question
|
||||
|
||||
Jacky Zhang, Embedded Software Engineer
|
||||
qi.zhang@seeed.cc
|
||||
17,mar,2015
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200); // start serial for output
|
||||
Serial.println("power on!");
|
||||
gas.begin(0x04);//the default I2C address of the slave is 0x04
|
||||
gas.powerOn();
|
||||
Serial.print("Firmware Version = ");
|
||||
Serial.println(gas.getVersion());
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
float c;
|
||||
|
||||
c = gas.measure_NH3();
|
||||
Serial.print("The concentration of NH3 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_CO();
|
||||
Serial.print("The concentration of CO is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_NO2();
|
||||
Serial.print("The concentration of NO2 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_C3H8();
|
||||
Serial.print("The concentration of C3H8 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_C4H10();
|
||||
Serial.print("The concentration of C4H10 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_CH4();
|
||||
Serial.print("The concentration of CH4 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_H2();
|
||||
Serial.print("The concentration of H2 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_C2H5OH();
|
||||
Serial.print("The concentration of C2H5OH is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
delay(1000);
|
||||
Serial.println("...");
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
This is a demo to test gas library
|
||||
This code is running on Xadow-mainboard, and the I2C slave is Xadow-gas
|
||||
There is a ATmega168PA on Xadow-gas, it get sensors output and feed back to master.
|
||||
the data is raw ADC value, algorithm should be realized on master.
|
||||
|
||||
please feel free to write email to me if there is any question
|
||||
|
||||
Jacky Zhang, Embedded Software Engineer
|
||||
qi.zhang@seeed.cc
|
||||
17,mar,2015
|
||||
*/
|
||||
|
||||
#include "xadow.h"
|
||||
#include <Wire.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
void setup()
|
||||
{
|
||||
Xadow.init();
|
||||
|
||||
Serial.begin(9600); // start serial for output
|
||||
Serial.println("power on!");
|
||||
|
||||
gas.begin(0x04);//the default I2C address of the slave is 0x04
|
||||
//gas.changeI2cAddr(0x10);
|
||||
//gas.doCalibrate();
|
||||
|
||||
gas.powerOn();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
float c;
|
||||
|
||||
c = gas.measure_NH3();
|
||||
Serial.print("The concentration of NH3 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_CO();
|
||||
Serial.print("The concentration of CO is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_NO2();
|
||||
Serial.print("The concentration of NO2 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_C3H8();
|
||||
Serial.print("The concentration of C3H8 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_C4H10();
|
||||
Serial.print("The concentration of C4H10 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_CH4();
|
||||
Serial.print("The concentration of CH4 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_H2();
|
||||
Serial.print("The concentration of H2 is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
c = gas.measure_C2H5OH();
|
||||
Serial.print("The concentration of C2H5OH is ");
|
||||
if(c>=0) Serial.print(c);
|
||||
else Serial.print("invalid");
|
||||
Serial.println(" ppm");
|
||||
|
||||
Xadow.greenLed(LEDON);
|
||||
delay(500);
|
||||
Xadow.greenLed(LEDOFF);
|
||||
delay(500);
|
||||
Serial.println("...");
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,513 @@
|
|||
// Atmega chip programmer
|
||||
// Author: Nick Gammon
|
||||
// Date: 22nd May 2012
|
||||
// Version: 1.17
|
||||
|
||||
// Version 1.1: Reset foundSig to -1 each time around the loop.
|
||||
// Version 1.2: Put hex bootloader data into separate files
|
||||
// Version 1.3: Added verify, and MD5 sums
|
||||
// Version 1.4: Added signatures for ATmeag8U2/16U2/32U2 (7 May 2012)
|
||||
// Version 1.5: Added signature for ATmega1284P (8 May 2012)
|
||||
// Version 1.6: Allow sketches to read bootloader area (lockbyte: 0x2F)
|
||||
// Version 1.7: Added choice of bootloaders for the Atmega328P (8 MHz or 16 MHz)
|
||||
// Version 1.8: Output an 8 MHz clock on pin 9
|
||||
// Version 1.9: Added support for Atmega1284P, and fixed some bugs
|
||||
// Version 1.10: Corrected flash size for Atmega1284P.
|
||||
// Version 1.11: Added support for Atmega1280. Removed MD5SUM stuff to make room.
|
||||
// Version 1.12: Added signatures for ATtiny2313A, ATtiny4313, ATtiny13
|
||||
// Version 1.13: Added signature for Atmega8A
|
||||
// Version 1.14: Added bootloader for Atmega8
|
||||
// Version 1.15: Removed extraneous 0xFF from some files
|
||||
// Version 1.16: Added signature for Atmega328
|
||||
// Version 1.17: Allowed for running on the Leonardo, Micro, etc.
|
||||
|
||||
/*
|
||||
|
||||
Copyright 2012 Nick Gammon.
|
||||
|
||||
|
||||
PERMISSION TO DISTRIBUTE
|
||||
|
||||
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.
|
||||
|
||||
|
||||
LIMITATION OF LIABILITY
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
const unsigned long BAUD_RATE = 115200;
|
||||
|
||||
const byte CLOCKOUT = 9;
|
||||
const byte RESET = 10; // --> goes to reset on the target board
|
||||
|
||||
#if ARDUINO < 100
|
||||
const byte SCK = 13; // SPI clock
|
||||
#endif
|
||||
|
||||
// number of items in an array
|
||||
#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))
|
||||
|
||||
// programming commands to send via SPI to the chip
|
||||
enum {
|
||||
progamEnable = 0xAC,
|
||||
|
||||
// writes are preceded by progamEnable
|
||||
chipErase = 0x80,
|
||||
writeLockByte = 0xE0,
|
||||
writeLowFuseByte = 0xA0,
|
||||
writeHighFuseByte = 0xA8,
|
||||
writeExtendedFuseByte = 0xA4,
|
||||
|
||||
pollReady = 0xF0,
|
||||
|
||||
programAcknowledge = 0x53,
|
||||
|
||||
readSignatureByte = 0x30,
|
||||
readCalibrationByte = 0x38,
|
||||
|
||||
readLowFuseByte = 0x50, readLowFuseByteArg2 = 0x00,
|
||||
readExtendedFuseByte = 0x50, readExtendedFuseByteArg2 = 0x08,
|
||||
readHighFuseByte = 0x58, readHighFuseByteArg2 = 0x08,
|
||||
readLockByte = 0x58, readLockByteArg2 = 0x00,
|
||||
|
||||
readProgramMemory = 0x20,
|
||||
writeProgramMemory = 0x4C,
|
||||
loadExtendedAddressByte = 0x4D,
|
||||
loadProgramMemory = 0x40,
|
||||
|
||||
}; // end of enum
|
||||
|
||||
// structure to hold signature and other relevant data about each chip
|
||||
typedef struct {
|
||||
byte sig [3];
|
||||
char * desc;
|
||||
unsigned long flashSize;
|
||||
unsigned int baseBootSize;
|
||||
const byte * bootloader;
|
||||
unsigned long loaderStart; // bytes
|
||||
unsigned int loaderLength; // bytes
|
||||
unsigned long pageSize; // bytes
|
||||
byte lowFuse, highFuse, extFuse, lockByte;
|
||||
} signatureType;
|
||||
|
||||
const unsigned long kb = 1024;
|
||||
|
||||
// hex bootloader data
|
||||
#include "bootloader_atmega168.h"
|
||||
|
||||
// see Atmega328 datasheet page 298
|
||||
signatureType signatures [] =
|
||||
{
|
||||
// signature description flash size bootloader size
|
||||
|
||||
// Attiny84 family
|
||||
{ { 0x1E, 0x91, 0x0B }, "ATtiny24", 2 * kb, 0 },
|
||||
{ { 0x1E, 0x92, 0x07 }, "ATtiny44", 4 * kb, 0 },
|
||||
{ { 0x1E, 0x93, 0x0C }, "ATtiny84", 8 * kb, 0 },
|
||||
|
||||
// Attiny85 family
|
||||
{ { 0x1E, 0x91, 0x08 }, "ATtiny25", 2 * kb, 0 },
|
||||
{ { 0x1E, 0x92, 0x06 }, "ATtiny45", 4 * kb, 0 },
|
||||
{ { 0x1E, 0x93, 0x0B }, "ATtiny85", 8 * kb, 0 },
|
||||
|
||||
// Atmega328 family
|
||||
{ { 0x1E, 0x92, 0x0A }, "ATmega48PA", 4 * kb, 0 },
|
||||
{ { 0x1E, 0x93, 0x0F }, "ATmega88PA", 8 * kb, 256 },
|
||||
{ { 0x1E, 0x94, 0x0B }, "ATmega168PA", 16 * kb, 256,
|
||||
atmega168_optiboot, // loader image
|
||||
//0x3E00, // start address
|
||||
0x0,
|
||||
sizeof atmega168_optiboot,
|
||||
128, // page size (for committing)
|
||||
0xC6, // fuse low byte: external full-swing crystal
|
||||
0xde, // fuse high byte: SPI enable, brown-out detection at 2.7V
|
||||
0xf8, // fuse extended byte: boot into bootloader, 512 byte bootloader
|
||||
0xcf }, // lock bits: SPM is not allowed to write to the Boot Loader section.
|
||||
}; // end of signatures
|
||||
|
||||
// if signature found in above table, this is its index
|
||||
int foundSig = -1;
|
||||
byte lastAddressMSB = 0;
|
||||
|
||||
// execute one programming instruction ... b1 is command, b2, b3, b4 are arguments
|
||||
// processor may return a result on the 4th transfer, this is returned.
|
||||
byte program (const byte b1, const byte b2 = 0, const byte b3 = 0, const byte b4 = 0)
|
||||
{
|
||||
SPI.transfer (b1);
|
||||
SPI.transfer (b2);
|
||||
SPI.transfer (b3);
|
||||
return SPI.transfer (b4);
|
||||
} // end of program
|
||||
|
||||
// read a byte from flash memory
|
||||
byte readFlash (unsigned long addr)
|
||||
{
|
||||
byte high = (addr & 1) ? 0x08 : 0; // set if high byte wanted
|
||||
addr >>= 1; // turn into word address
|
||||
|
||||
// set the extended (most significant) address byte if necessary
|
||||
byte MSB = (addr >> 16) & 0xFF;
|
||||
if (MSB != lastAddressMSB)
|
||||
{
|
||||
program (loadExtendedAddressByte, 0, MSB);
|
||||
lastAddressMSB = MSB;
|
||||
} // end if different MSB
|
||||
|
||||
return program (readProgramMemory | high, highByte (addr), lowByte (addr));
|
||||
} // end of readFlash
|
||||
|
||||
// write a byte to the flash memory buffer (ready for committing)
|
||||
byte writeFlash (unsigned long addr, const byte data)
|
||||
{
|
||||
byte high = (addr & 1) ? 0x08 : 0; // set if high byte wanted
|
||||
addr >>= 1; // turn into word address
|
||||
program (loadProgramMemory | high, 0, lowByte (addr), data);
|
||||
} // end of writeFlash
|
||||
|
||||
// show a byte in hex with leading zero and optional newline
|
||||
void showHex (const byte b, const boolean newline = false, const boolean show0x = true)
|
||||
{
|
||||
if (show0x)
|
||||
Serial.print (F("0x"));
|
||||
// try to avoid using sprintf
|
||||
char buf [4] = { ((b >> 4) & 0x0F) | '0', (b & 0x0F) | '0', ' ' , 0 };
|
||||
if (buf [0] > '9')
|
||||
buf [0] += 7;
|
||||
if (buf [1] > '9')
|
||||
buf [1] += 7;
|
||||
Serial.print (buf);
|
||||
if (newline)
|
||||
Serial.println ();
|
||||
} // end of showHex
|
||||
|
||||
// convert a boolean to Yes/No
|
||||
void showYesNo (const boolean b, const boolean newline = false)
|
||||
{
|
||||
if (b)
|
||||
Serial.print (F("Yes"));
|
||||
else
|
||||
Serial.print (F("No"));
|
||||
if (newline)
|
||||
Serial.println ();
|
||||
} // end of showYesNo
|
||||
|
||||
// poll the target device until it is ready to be programmed
|
||||
void pollUntilReady ()
|
||||
{
|
||||
while ((program (pollReady) & 1) == 1)
|
||||
{} // wait till ready
|
||||
} // end of pollUntilReady
|
||||
|
||||
// commit page
|
||||
void commitPage (unsigned long addr)
|
||||
{
|
||||
//Serial.print (F("Committing page starting at 0x"));
|
||||
//Serial.println (addr, HEX);
|
||||
|
||||
addr >>= 1; // turn into word address
|
||||
|
||||
// set the extended (most significant) address byte if necessary
|
||||
byte MSB = (addr >> 16) & 0xFF;
|
||||
if (MSB != lastAddressMSB)
|
||||
{
|
||||
program (loadExtendedAddressByte, 0, MSB);
|
||||
lastAddressMSB = MSB;
|
||||
} // end if different MSB
|
||||
|
||||
program (writeProgramMemory, highByte (addr), lowByte (addr));
|
||||
pollUntilReady ();
|
||||
} // end of commitPage
|
||||
|
||||
// write specified value to specified fuse/lock byte
|
||||
void writeFuse (const byte newValue, const byte instruction)
|
||||
{
|
||||
if (newValue == 0)
|
||||
return; // ignore
|
||||
|
||||
program (progamEnable, instruction, 0, newValue);
|
||||
pollUntilReady ();
|
||||
} // end of writeFuse
|
||||
|
||||
void getFuseBytes ()
|
||||
{
|
||||
Serial.print (F("LFuse = "));
|
||||
showHex (program (readLowFuseByte, readLowFuseByteArg2), true);
|
||||
Serial.print (F("HFuse = "));
|
||||
showHex (program (readHighFuseByte, readHighFuseByteArg2), true);
|
||||
Serial.print (F("EFuse = "));
|
||||
showHex (program (readExtendedFuseByte, readExtendedFuseByteArg2), true);
|
||||
Serial.print (F("Lock byte = "));
|
||||
showHex (program (readLockByte, readLockByteArg2), true);
|
||||
Serial.print ("Clock calibration = ");
|
||||
showHex (program (readCalibrationByte), true);
|
||||
} // end of getFuseBytes
|
||||
|
||||
// burn the bootloader to the target device
|
||||
void writeBootloader ()
|
||||
{
|
||||
|
||||
if (signatures [foundSig].bootloader == 0)
|
||||
{
|
||||
Serial.println (F("No bootloader support for this device."));
|
||||
return;
|
||||
} // end if
|
||||
|
||||
int i;
|
||||
|
||||
byte lFuse = program (readLowFuseByte, readLowFuseByteArg2);
|
||||
|
||||
byte newlFuse = signatures [foundSig].lowFuse;
|
||||
byte newhFuse = signatures [foundSig].highFuse;
|
||||
byte newextFuse = signatures [foundSig].extFuse;
|
||||
byte newlockByte = signatures [foundSig].lockByte;
|
||||
|
||||
unsigned long addr = signatures [foundSig].loaderStart;
|
||||
unsigned int len = signatures [foundSig].loaderLength;
|
||||
unsigned long pagesize = signatures [foundSig].pageSize;
|
||||
unsigned long pagemask = ~(pagesize - 1);
|
||||
const byte * bootloader = signatures [foundSig].bootloader;
|
||||
|
||||
|
||||
Serial.print (F("Bootloader address = 0x"));
|
||||
Serial.println (addr, HEX);
|
||||
Serial.print (F("Bootloader length = "));
|
||||
Serial.print (len);
|
||||
Serial.println (F(" bytes."));
|
||||
|
||||
byte subcommand = 'U';
|
||||
|
||||
// Atmega328P or Atmega328
|
||||
if (signatures [foundSig].sig [0] == 0x1E &&
|
||||
signatures [foundSig].sig [1] == 0x95 &&
|
||||
(signatures [foundSig].sig [2] == 0x0F || signatures [foundSig].sig [2] == 0x14)
|
||||
)
|
||||
{
|
||||
Serial.println (F("Type 'L' to use Lilypad (8 MHz) loader, or 'U' for Uno (16 MHz) loader ..."));
|
||||
do
|
||||
{
|
||||
subcommand = toupper (Serial.read ());
|
||||
} while (subcommand != 'L' && subcommand != 'U');
|
||||
|
||||
if (subcommand == 'L') // use internal 8 MHz clock
|
||||
{
|
||||
Serial.println (F("Using Lilypad 8 MHz loader."));
|
||||
bootloader = atmega168_optiboot;
|
||||
newlFuse = 0xE2; // internal 8 MHz oscillator
|
||||
newhFuse = 0xDA; // 2048 byte bootloader, SPI enabled
|
||||
addr = 0x7800;
|
||||
len = sizeof atmega168_optiboot;
|
||||
} // end of using the 8 MHz clock
|
||||
else
|
||||
Serial.println (F("Using Uno Optiboot 16 MHz loader."));
|
||||
} // end of being Atmega328P
|
||||
|
||||
unsigned long oldPage = addr & pagemask;
|
||||
|
||||
Serial.println (F("Type 'V' to verify, or 'G' to program the chip with the bootloader ..."));
|
||||
char command;
|
||||
do
|
||||
{
|
||||
command = toupper (Serial.read ());
|
||||
} while (command != 'G' && command != 'V');
|
||||
|
||||
if (command == 'G')
|
||||
{
|
||||
Serial.println (F("Erasing chip ..."));
|
||||
program (progamEnable, chipErase); // erase it
|
||||
pollUntilReady ();
|
||||
Serial.println (F("Writing bootloader ..."));
|
||||
for (i = 0; i < len; i += 2)
|
||||
{
|
||||
unsigned long thisPage = (addr + i) & pagemask;
|
||||
// page changed? commit old one
|
||||
if (thisPage != oldPage)
|
||||
{
|
||||
commitPage (oldPage);
|
||||
oldPage = thisPage;
|
||||
}
|
||||
|
||||
unsigned char c1 = pgm_read_byte(bootloader + i);
|
||||
unsigned char c2 = pgm_read_byte(bootloader + i+1);
|
||||
|
||||
writeFlash (addr + i, c1);
|
||||
writeFlash (addr + i + 1, c2);
|
||||
} // end while doing each word
|
||||
|
||||
Serial.println();
|
||||
|
||||
// commit final page
|
||||
commitPage (oldPage);
|
||||
Serial.println ("Written.");
|
||||
} // end if programming
|
||||
|
||||
Serial.println (F("Verifying ..."));
|
||||
|
||||
// count errors
|
||||
unsigned int errors = 0;
|
||||
// check each byte
|
||||
for (i = 0; i < signatures [foundSig].loaderLength; i++)
|
||||
{
|
||||
//if(i==0)Serial.print(" ");
|
||||
byte found = readFlash (addr + i);
|
||||
|
||||
byte expected = pgm_read_byte(bootloader + i);
|
||||
if (found != expected)
|
||||
{
|
||||
if (errors <= 100)
|
||||
{
|
||||
Serial.print (F("Verification error at address "));
|
||||
Serial.print (addr + i, HEX);
|
||||
Serial.print (F(". Got: "));
|
||||
showHex (found);
|
||||
Serial.print (F(" Expected: "));
|
||||
showHex (expected, true);
|
||||
} // end of haven't shown 100 errors yet
|
||||
errors++;
|
||||
} // end if error
|
||||
} // end of for
|
||||
|
||||
Serial.println("\r\n");
|
||||
if (errors == 0)
|
||||
Serial.println (F("No errors found."));
|
||||
else
|
||||
{
|
||||
Serial.print (errors, DEC);
|
||||
Serial.println (F(" verification error(s)."));
|
||||
if (errors > 100)
|
||||
Serial.println (F("First 100 shown."));
|
||||
return; // don't change fuses if errors
|
||||
} // end if
|
||||
|
||||
if (command == 'G')
|
||||
{
|
||||
Serial.println (F("Writing fuses ..."));
|
||||
|
||||
writeFuse (newlFuse, writeLowFuseByte);
|
||||
writeFuse (newhFuse, writeHighFuseByte);
|
||||
writeFuse (newextFuse, writeExtendedFuseByte);
|
||||
writeFuse (newlockByte, writeLockByte);
|
||||
|
||||
// confirm them
|
||||
getFuseBytes ();
|
||||
} // end if programming
|
||||
|
||||
Serial.println (F("Done."));
|
||||
|
||||
} // end of writeBootloader
|
||||
|
||||
|
||||
void startProgramming ()
|
||||
{
|
||||
byte confirm;
|
||||
pinMode (RESET, OUTPUT);
|
||||
pinMode (SCK, OUTPUT);
|
||||
|
||||
// we are in sync if we get back programAcknowledge on the third byte
|
||||
do
|
||||
{
|
||||
delay (100);
|
||||
// ensure SCK low
|
||||
digitalWrite (SCK, LOW);
|
||||
// then pulse reset, see page 309 of datasheet
|
||||
digitalWrite (RESET, HIGH);
|
||||
delay (1); // pulse for at least 2 clock cycles
|
||||
digitalWrite (RESET, LOW);
|
||||
delay (25); // wait at least 20 mS
|
||||
SPI.transfer (progamEnable);
|
||||
SPI.transfer (programAcknowledge);
|
||||
confirm = SPI.transfer (0);
|
||||
SPI.transfer (0);
|
||||
} while (confirm != programAcknowledge);
|
||||
|
||||
Serial.println (F("Entered programming mode OK."));
|
||||
} // end of startProgramming
|
||||
|
||||
void getSignature ()
|
||||
{
|
||||
foundSig = -1;
|
||||
lastAddressMSB = 0;
|
||||
|
||||
byte sig [3];
|
||||
Serial.print (F("Signature = "));
|
||||
for (byte i = 0; i < 3; i++)
|
||||
{
|
||||
sig [i] = program (readSignatureByte, 0, i);
|
||||
showHex (sig [i]);
|
||||
} // end for each signature byte
|
||||
Serial.println ();
|
||||
|
||||
for (int j = 0; j < NUMITEMS (signatures); j++)
|
||||
{
|
||||
if (memcmp (sig, signatures [j].sig, sizeof sig) == 0)
|
||||
{
|
||||
foundSig = j;
|
||||
Serial.print (F("Processor = "));
|
||||
Serial.println (signatures [j].desc);
|
||||
Serial.print (F("Flash memory size = "));
|
||||
Serial.print (signatures [j].flashSize, DEC);
|
||||
Serial.println (F(" bytes."));
|
||||
return;
|
||||
} // end of signature found
|
||||
} // end of for each signature
|
||||
|
||||
Serial.println (F("Unrecogized signature."));
|
||||
} // end of getSignature
|
||||
|
||||
void setup ()
|
||||
{
|
||||
Serial.begin (BAUD_RATE);
|
||||
while (!Serial) ; // for Leonardo, Micro etc.
|
||||
Serial.println ();
|
||||
Serial.println (F("Atmega chip programmer."));
|
||||
Serial.println (F("Written by Nick Gammon."));
|
||||
|
||||
digitalWrite (RESET, HIGH); // ensure SS stays high for now
|
||||
SPI.begin ();
|
||||
|
||||
// slow down SPI for benefit of slower processors like the Attiny
|
||||
SPI.setClockDivider (SPI_CLOCK_DIV64);
|
||||
|
||||
pinMode (CLOCKOUT, OUTPUT);
|
||||
|
||||
// set up Timer 1
|
||||
TCCR1A = _BV (COM1A0); // toggle OC1A on Compare Match
|
||||
TCCR1B = _BV(WGM12) | _BV(CS10); // CTC, no prescaling
|
||||
OCR1A = 0; // output every cycle
|
||||
|
||||
} // end of setup
|
||||
|
||||
void loop ()
|
||||
{
|
||||
startProgramming ();
|
||||
getSignature ();
|
||||
getFuseBytes ();
|
||||
|
||||
// if we found a signature try to write a bootloader
|
||||
if (foundSig != -1)
|
||||
writeBootloader ();
|
||||
|
||||
// release reset
|
||||
digitalWrite (RESET, HIGH);
|
||||
|
||||
Serial.println (F("Type 'C' when ready to continue with another chip ..."));
|
||||
while (toupper (Serial.read ()) != 'C')
|
||||
{}
|
||||
|
||||
} // end of loop
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,339 @@
|
|||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
|
@ -0,0 +1,35 @@
|
|||
// Calibration code for Grove - Multichannel Gas Sensor
|
||||
// Note that it need 10 minutes pre-heat before calibration
|
||||
// This code is writen by Loovee@2016-5-18
|
||||
|
||||
#include <Wire.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
#define SENSOR_ADDR 0X04 // default to 0x04
|
||||
#define PRE_HEAT_TIME 0 // pre-heat time, 10-30 minutes is recommended
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
gas.begin(SENSOR_ADDR); //
|
||||
Serial.println("power on, and pre-heat");
|
||||
|
||||
for(int i=60*PRE_HEAT_TIME; i>=0; i--)
|
||||
{
|
||||
Serial.print(i/60);
|
||||
Serial.print(":");
|
||||
Serial.println(i%60);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
Serial.println("Begin to calibrate...");
|
||||
gas.doCalibrate();
|
||||
Serial.println("Calibration ok");
|
||||
|
||||
gas.display_eeprom();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
// factory setting
|
||||
// Loovee
|
||||
// 2016-11-10
|
||||
|
||||
#include <Wire.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
#define SENSOR_ADDR 0X04 // default to 0x04
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("Please input something to continue");
|
||||
while(!Serial.available());
|
||||
|
||||
gas.begin(SENSOR_ADDR); //
|
||||
|
||||
Serial.println("FACTORY SETTING OK");
|
||||
|
||||
float R0_NH3, R0_CO, R0_NO2;
|
||||
|
||||
R0_NH3 = gas.getR0(0);
|
||||
R0_CO = gas.getR0(1);
|
||||
R0_NO2 = gas.getR0(2);
|
||||
|
||||
Serial.print("R0_NH3 = ");
|
||||
Serial.println(R0_NH3);
|
||||
Serial.print("R0_CO = ");
|
||||
Serial.println(R0_CO);
|
||||
Serial.print("R0_NO2 = ");
|
||||
Serial.println(R0_NO2);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
|
@ -0,0 +1,272 @@
|
|||
/*firmware of multichannel gas sensor v2.0*3
|
||||
write by loovee
|
||||
2016-11-6
|
||||
|
||||
Factory adc value of 3 channels:
|
||||
NH3 = 860
|
||||
CO = 950
|
||||
NO2 = 155
|
||||
|
||||
Default address is 0x04
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
#define DEFAULT_I2C_ADDR 0x04
|
||||
#define DBG 1
|
||||
// EEPROM ADDRESS
|
||||
#define ADDR_IS_SET 0 // if this is the first time to run, if 1126, set
|
||||
#define ADDR_FACTORY_ADC_NH3 2
|
||||
#define ADDR_FACTORY_ADC_CO 4
|
||||
#define ADDR_FACTORY_ADC_NO2 6
|
||||
|
||||
#define ADDR_USER_ADC_HN3 8
|
||||
#define ADDR_USER_ADC_CO 10
|
||||
#define ADDR_USER_ADC_NO2 12
|
||||
#define ADDR_IF_CALI 14 // IF USER HAD CALI
|
||||
#define ADDR_I2C_ADDRESS 20
|
||||
// I2C COMMAND
|
||||
|
||||
#define CMD_ADC_RES0 1 // NH3
|
||||
#define CMD_ADC_RES1 2 // CO
|
||||
#define CMD_ADC_RES2 3 // NO2
|
||||
#define CMD_ADC_RESALL 4 // ALL CHANNEL
|
||||
#define CMD_CHANGE_I2C 5 // CHANGE I2C
|
||||
#define CMD_READ_EEPROM 6 // READ EEPROM VALUE, RETURN UNSIGNED INT
|
||||
#define CMD_SET_R0_ADC 7 // SET R0 ADC VALUE
|
||||
#define CMD_GET_R0_ADC 8 // GET R0 ADC VALUE
|
||||
#define CMD_GET_R0_ADC_FACTORY 9 // GET FACTORY R0 ADC VALUE
|
||||
#define CMD_CONTROL_LED 10
|
||||
#define CMD_CONTROL_PWR 11
|
||||
|
||||
// EEPROM READ AND WRITE - UNSIGNED INT
|
||||
void eeprom_write(int addr, unsigned int value)
|
||||
{
|
||||
EEPROM.write(addr, value>>8);
|
||||
EEPROM.write(addr+1, value&0xff);
|
||||
}
|
||||
|
||||
unsigned int eeprom_read(int addr)
|
||||
{
|
||||
unsigned int r = EEPROM.read(addr);
|
||||
r <<= 8;
|
||||
r+= EEPROM.read(addr+1);
|
||||
return r;
|
||||
}
|
||||
|
||||
const int pin_pwr = 8;
|
||||
const int pin_led = 9;
|
||||
|
||||
const int pin_NH3 = A0; // RES0
|
||||
const int pin_CO = A1; // RES1
|
||||
const int pin_NO2 = A2; // RES2
|
||||
|
||||
unsigned char i2c_address = 0;
|
||||
|
||||
#define LED_ON() digitalWrite(pin_led, LOW)
|
||||
#define LED_OFF() digitalWrite(pin_led, HIGH)
|
||||
|
||||
void factory_init()
|
||||
{
|
||||
#if DBG
|
||||
Serial.print("FACTORY: ");
|
||||
#endif
|
||||
if(1126 != eeprom_read(ADDR_IS_SET)) // IF FACTORY SET
|
||||
{
|
||||
#if DBG
|
||||
Serial.println("YES");
|
||||
#endif
|
||||
eeprom_write(ADDR_IS_SET, 1126);
|
||||
eeprom_write(ADDR_FACTORY_ADC_NH3, 860);
|
||||
eeprom_write(ADDR_FACTORY_ADC_CO, 950);
|
||||
eeprom_write(ADDR_FACTORY_ADC_NO2, 155);
|
||||
eeprom_write(ADDR_USER_ADC_HN3, 860);
|
||||
eeprom_write(ADDR_USER_ADC_CO, 950);
|
||||
eeprom_write(ADDR_USER_ADC_NO2, 155);
|
||||
eeprom_write(ADDR_IF_CALI, 0);
|
||||
eeprom_write(ADDR_I2C_ADDRESS, DEFAULT_I2C_ADDR);
|
||||
}
|
||||
#if DBG
|
||||
else Serial.println("NO");
|
||||
#endif
|
||||
}
|
||||
|
||||
int getAnalog(int pin)
|
||||
{
|
||||
long sum = 0;
|
||||
for(int i=0; i<64; i++)
|
||||
{
|
||||
sum += analogRead(pin);
|
||||
}
|
||||
return sum>>6;
|
||||
}
|
||||
|
||||
unsigned int ADC_RES0 = 0;
|
||||
unsigned int ADC_RES1 = 0;
|
||||
unsigned int ADC_RES2 = 0;
|
||||
|
||||
unsigned char raw_adc[6];
|
||||
|
||||
void updateValue()
|
||||
{
|
||||
static unsigned long timer_s = millis();
|
||||
if(millis()-timer_s < 1000)return;
|
||||
timer_s = millis();
|
||||
|
||||
ADC_RES0 = getAnalog(pin_NH3);
|
||||
ADC_RES1 = getAnalog(pin_CO);
|
||||
ADC_RES2 = getAnalog(pin_NO2);
|
||||
|
||||
raw_adc[0] = ADC_RES0>>8;
|
||||
raw_adc[1] = ADC_RES0;
|
||||
raw_adc[2] = ADC_RES1>>8;
|
||||
raw_adc[3] = ADC_RES1;
|
||||
raw_adc[4] = ADC_RES2>>8;
|
||||
raw_adc[5] = ADC_RES2;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
#if DBG
|
||||
Serial.begin(115200);
|
||||
#endif
|
||||
pinMode(pin_pwr, OUTPUT);
|
||||
digitalWrite(pin_pwr, HIGH);
|
||||
pinMode(pin_led, OUTPUT);
|
||||
|
||||
factory_init();
|
||||
i2c_address = eeprom_read(ADDR_I2C_ADDRESS);
|
||||
#if DBG
|
||||
Serial.print("i2d address = 0x");
|
||||
Serial.println(i2c_address, HEX);
|
||||
#endif
|
||||
for(int i=0; i<5; i++)
|
||||
{
|
||||
digitalWrite(pin_led, LOW);
|
||||
delay(100);
|
||||
digitalWrite(pin_led, HIGH);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
Wire.begin(i2c_address); // join i2c bus with address
|
||||
Wire.onReceive(receiveCallback); // register receive callback
|
||||
Wire.onRequest(requestCallback); // register request callback
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
updateValue();
|
||||
}
|
||||
|
||||
unsigned char recvCmd = 0;
|
||||
unsigned char recvDta = 0;
|
||||
unsigned char recvDtaStr[10];
|
||||
|
||||
void receiveCallback(int dtaCount)
|
||||
{
|
||||
if(dtaCount == 1)
|
||||
{
|
||||
recvCmd = Wire.read();
|
||||
}
|
||||
else if(dtaCount == 2) // set i2c address
|
||||
{
|
||||
recvCmd = Wire.read();
|
||||
recvDta = Wire.read();
|
||||
|
||||
if(CMD_CHANGE_I2C == recvCmd)
|
||||
{
|
||||
i2c_address = recvDta;
|
||||
eeprom_write(ADDR_I2C_ADDRESS, i2c_address);
|
||||
Wire.begin(i2c_address);
|
||||
}
|
||||
else if(CMD_CONTROL_LED == recvCmd)
|
||||
{
|
||||
if(0 == recvDta)LED_OFF();
|
||||
else if(1 == recvDta)LED_ON();
|
||||
}
|
||||
else if(CMD_CONTROL_PWR == recvCmd)
|
||||
{
|
||||
if(0 == recvDta)digitalWrite(pin_pwr, LOW);
|
||||
else if(1 == recvDta)digitalWrite(pin_pwr, HIGH);
|
||||
}
|
||||
}
|
||||
else if(dtaCount == 7) // set ADC value
|
||||
{
|
||||
recvCmd = Wire.read();
|
||||
unsigned int dta[3];
|
||||
|
||||
for(int i=0; i<3; i++)
|
||||
{
|
||||
dta[i] = Wire.read();
|
||||
dta[i] <<= 8;
|
||||
dta[i] += Wire.read();
|
||||
}
|
||||
|
||||
if(recvCmd == CMD_SET_R0_ADC)
|
||||
{
|
||||
eeprom_write(ADDR_USER_ADC_HN3, dta[0]);
|
||||
eeprom_write(ADDR_USER_ADC_CO, dta[1]);
|
||||
eeprom_write(ADDR_USER_ADC_NO2, dta[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char rcDta[10];
|
||||
void requestCallback()
|
||||
{
|
||||
switch(recvCmd)
|
||||
{
|
||||
case CMD_ADC_RES0: // NH3
|
||||
Wire.write(&raw_adc[0], 2); // HIGH FIRST
|
||||
|
||||
break;
|
||||
|
||||
case CMD_ADC_RES1: // CO
|
||||
Wire.write(&raw_adc[2], 2); // HIGH FIRST
|
||||
break;
|
||||
|
||||
case CMD_ADC_RES2: // NO2
|
||||
Wire.write(&raw_adc[4], 2); // HIGH FIRST
|
||||
break;
|
||||
|
||||
case CMD_ADC_RESALL:
|
||||
Wire.write(raw_adc, 6);
|
||||
break;
|
||||
|
||||
case CMD_READ_EEPROM:
|
||||
rcDta[0] = EEPROM.read(recvDta);
|
||||
rcDta[1] = EEPROM.read(recvDta+1);
|
||||
Wire.write(rcDta, 2);
|
||||
break;
|
||||
|
||||
case CMD_GET_R0_ADC:
|
||||
|
||||
rcDta[0] = EEPROM.read(ADDR_USER_ADC_HN3);
|
||||
rcDta[1] = EEPROM.read(ADDR_USER_ADC_HN3+1);
|
||||
rcDta[2] = EEPROM.read(ADDR_USER_ADC_CO);
|
||||
rcDta[3] = EEPROM.read(ADDR_USER_ADC_CO+1);
|
||||
rcDta[4] = EEPROM.read(ADDR_USER_ADC_NO2);
|
||||
rcDta[5] = EEPROM.read(ADDR_USER_ADC_NO2+1);
|
||||
|
||||
Wire.write(rcDta, 6);
|
||||
|
||||
break;
|
||||
|
||||
case CMD_GET_R0_ADC_FACTORY:
|
||||
|
||||
rcDta[0] = EEPROM.read(ADDR_FACTORY_ADC_NH3);
|
||||
rcDta[1] = EEPROM.read(ADDR_FACTORY_ADC_NH3+1);
|
||||
rcDta[2] = EEPROM.read(ADDR_FACTORY_ADC_CO);
|
||||
rcDta[3] = EEPROM.read(ADDR_FACTORY_ADC_CO+1);
|
||||
rcDta[4] = EEPROM.read(ADDR_FACTORY_ADC_NO2);
|
||||
rcDta[5] = EEPROM.read(ADDR_FACTORY_ADC_NO2+1);
|
||||
|
||||
Wire.write(rcDta, 6);
|
||||
break;
|
||||
|
||||
default:;
|
||||
}
|
||||
}
|
||||
|
||||
// END FILE
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"name": "Mutichannel_Gas_Sensor",
|
||||
"keywords": "Gas Sensor",
|
||||
"description": "Grove - Multichannel Gas Sensor",
|
||||
"homepage": "http://wiki.seeed.cc/Grove-Multichannel_Gas_Sensor/",
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor.git"
|
||||
},
|
||||
"version": "0.0.1",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
name=Grove - Multichannel Gas Sensor
|
||||
version=0.0.1
|
||||
author=WEMOS.CC <support@wemos.cc>
|
||||
maintainer=WEMOS.CC
|
||||
sentence=Library for the <a href="http://wiki.seeed.cc/Grove-Multichannel_Gas_Sensor/"> Grove - Multichannel Gas Sensor</a>.
|
||||
paragraph=Library for the Grove - Multichannel Gas Sensor.
|
||||
category=Device Control
|
||||
url=https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor.git
|
||||
architectures=*
|
|
@ -0,0 +1,729 @@
|
|||
/*
|
||||
MutichannelGasSensor.cpp
|
||||
2015 Copyright (c) Seeed Technology Inc. All right reserved.
|
||||
|
||||
Author: Jacky Zhang
|
||||
2015-3-17
|
||||
http://www.seeed.cc/
|
||||
modi by Jack, 2015-8
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Seeed Technology Inc.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <Wire.h>
|
||||
#include <Arduino.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: begin
|
||||
** Descriptions: initialize I2C
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::begin(int address)
|
||||
{
|
||||
__version = 1; // version 1/2
|
||||
r0_inited = false;
|
||||
|
||||
Wire.begin();
|
||||
i2cAddress = address;
|
||||
__send_error = false;
|
||||
__version = getVersion();
|
||||
}
|
||||
|
||||
bool MutichannelGasSensor::isError()
|
||||
{
|
||||
return __send_error;
|
||||
}
|
||||
|
||||
unsigned char MutichannelGasSensor::getVersion()
|
||||
{
|
||||
if(get_addr_dta(CMD_READ_EEPROM, ADDR_IS_SET) == 1126) // get version
|
||||
{
|
||||
__version = 2;
|
||||
return __version;
|
||||
}
|
||||
if (!__send_error) {
|
||||
__version = 1;
|
||||
}
|
||||
else {
|
||||
__version = -1;
|
||||
}
|
||||
return __version;
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::begin()
|
||||
{
|
||||
begin(DEFAULT_I2C_ADDR);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: sendI2C
|
||||
** Descriptions: send one byte to I2C Wire
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::sendI2C(unsigned char dta)
|
||||
{
|
||||
__send_error = false;
|
||||
Wire.beginTransmission(i2cAddress); // transmit to device #4
|
||||
Wire.write(dta); // sends one byte
|
||||
if (Wire.endTransmission()) { // stop transmitting
|
||||
__send_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg)
|
||||
{
|
||||
int trys = 0;
|
||||
START:
|
||||
__send_error = false;
|
||||
Wire.beginTransmission(i2cAddress);
|
||||
Wire.write(addr_reg);
|
||||
if (Wire.endTransmission()) { // stop transmitting
|
||||
trys++;
|
||||
__send_error = true;
|
||||
if (trys > 3) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Wire.requestFrom(i2cAddress, (uint8_t)2);
|
||||
|
||||
unsigned int dta = 0;
|
||||
|
||||
unsigned char raw[10];
|
||||
int cnt = 0;
|
||||
|
||||
while(Wire.available())
|
||||
{
|
||||
raw[cnt++] = Wire.read();
|
||||
}
|
||||
|
||||
if(cnt == 0)goto START;
|
||||
|
||||
dta = raw[0];
|
||||
dta <<= 8;
|
||||
dta += raw[1];
|
||||
|
||||
switch(addr_reg)
|
||||
{
|
||||
case CH_VALUE_NH3:
|
||||
|
||||
if(dta > 0)
|
||||
{
|
||||
adcValueR0_NH3_Buf = dta;
|
||||
}
|
||||
else
|
||||
{
|
||||
dta = adcValueR0_NH3_Buf;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case CH_VALUE_CO:
|
||||
|
||||
if(dta > 0)
|
||||
{
|
||||
adcValueR0_CO_Buf = dta;
|
||||
}
|
||||
else
|
||||
{
|
||||
dta = adcValueR0_CO_Buf;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case CH_VALUE_NO2:
|
||||
|
||||
if(dta > 0)
|
||||
{
|
||||
adcValueR0_NO2_Buf = dta;
|
||||
}
|
||||
else
|
||||
{
|
||||
dta = adcValueR0_NO2_Buf;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:;
|
||||
}
|
||||
return dta;
|
||||
}
|
||||
|
||||
unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg, unsigned char __dta)
|
||||
{
|
||||
int trys = 0;
|
||||
START:
|
||||
__send_error = false;
|
||||
Wire.beginTransmission(i2cAddress);
|
||||
Wire.write(addr_reg);
|
||||
Wire.write(__dta);
|
||||
if (Wire.endTransmission()) { // stop transmitting
|
||||
trys++;
|
||||
__send_error = true;
|
||||
if (trys > 3) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Wire.requestFrom(i2cAddress, (uint8_t)2);
|
||||
|
||||
unsigned int dta = 0;
|
||||
|
||||
unsigned char raw[10];
|
||||
int cnt = 0;
|
||||
|
||||
while(Wire.available())
|
||||
{
|
||||
raw[cnt++] = Wire.read();
|
||||
}
|
||||
|
||||
if(cnt == 0)goto START;
|
||||
|
||||
dta = raw[0];
|
||||
dta <<= 8;
|
||||
dta += raw[1];
|
||||
|
||||
|
||||
return dta;
|
||||
}
|
||||
|
||||
|
||||
void MutichannelGasSensor::write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len)
|
||||
{
|
||||
__send_error = false;
|
||||
Wire.beginTransmission(addr);
|
||||
for(int i=0; i<dta_len; i++)
|
||||
{
|
||||
Wire.write(dta[i]);
|
||||
}
|
||||
if (Wire.endTransmission()) {
|
||||
__send_error = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readData
|
||||
** Descriptions: read 4 bytes from I2C slave
|
||||
*********************************************************************************************************/
|
||||
int16_t MutichannelGasSensor::readData(uint8_t cmd)
|
||||
{
|
||||
uint16_t timeout = 0;
|
||||
uint8_t buffer[4];
|
||||
uint8_t checksum = 0;
|
||||
int16_t rtnData = 0;
|
||||
|
||||
//send command
|
||||
sendI2C(cmd);
|
||||
//wait for a while
|
||||
delay(2);
|
||||
//get response
|
||||
Wire.requestFrom(i2cAddress, (uint8_t)4); // request 4 bytes from slave device
|
||||
while(Wire.available() == 0)
|
||||
{
|
||||
if(timeout++ > 100)
|
||||
return -2;//time out
|
||||
delay(2);
|
||||
}
|
||||
if(Wire.available() != 4)
|
||||
return -3;//rtnData length wrong
|
||||
buffer[0] = Wire.read();
|
||||
buffer[1] = Wire.read();
|
||||
buffer[2] = Wire.read();
|
||||
buffer[3] = Wire.read();
|
||||
checksum = (uint8_t)(buffer[0] + buffer[1] + buffer[2]);
|
||||
if(checksum != buffer[3])
|
||||
return -4;//checksum wrong
|
||||
rtnData = ((buffer[1] << 8) + buffer[2]);
|
||||
|
||||
return rtnData;//successful
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readR0
|
||||
** Descriptions: read R0 stored in slave MCU
|
||||
*********************************************************************************************************/
|
||||
int16_t MutichannelGasSensor::readR0(void)
|
||||
{
|
||||
int16_t rtnData = 0;
|
||||
|
||||
rtnData = readData(0x11);
|
||||
|
||||
if(rtnData > 0)
|
||||
res0[0] = rtnData;
|
||||
else
|
||||
return rtnData; //unsuccessful
|
||||
|
||||
rtnData = readData(0x12);
|
||||
if(rtnData > 0)
|
||||
res0[1] = rtnData;
|
||||
else
|
||||
return rtnData; //unsuccessful
|
||||
|
||||
rtnData = readData(0x13);
|
||||
if(rtnData > 0)
|
||||
res0[2] = rtnData;
|
||||
else
|
||||
return rtnData; //unsuccessful
|
||||
|
||||
return 1;//successful
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readR
|
||||
** Descriptions: read resistance value of each channel from slave MCU
|
||||
*********************************************************************************************************/
|
||||
int16_t MutichannelGasSensor::readR(void)
|
||||
{
|
||||
int16_t rtnData = 0;
|
||||
|
||||
rtnData = readData(0x01);
|
||||
if(rtnData >= 0)
|
||||
res[0] = rtnData;
|
||||
else
|
||||
return rtnData;//unsuccessful
|
||||
|
||||
rtnData = readData(0x02);
|
||||
if(rtnData >= 0)
|
||||
res[1] = rtnData;
|
||||
else
|
||||
return rtnData;//unsuccessful
|
||||
|
||||
rtnData = readData(0x03);
|
||||
if(rtnData >= 0)
|
||||
res[2] = rtnData;
|
||||
else
|
||||
return rtnData;//unsuccessful
|
||||
|
||||
return 0;//successful
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readR
|
||||
** Descriptions: calculate gas concentration of each channel from slave MCU
|
||||
** Parameters:
|
||||
gas - gas type
|
||||
** Returns:
|
||||
float value - concentration of the gas
|
||||
*********************************************************************************************************/
|
||||
float MutichannelGasSensor::calcGas(int gas)
|
||||
{
|
||||
|
||||
float ratio0, ratio1, ratio2;
|
||||
if(1 == __version)
|
||||
{
|
||||
if(!r0_inited)
|
||||
{
|
||||
if(readR0() >= 0) r0_inited = true;
|
||||
else return -1.0f;
|
||||
}
|
||||
|
||||
if(readR() < 0)
|
||||
return -2.0f;
|
||||
|
||||
ratio0 = (float)res[0] / res0[0];
|
||||
ratio1 = (float)res[1] / res0[1];
|
||||
ratio2 = (float)res[2] / res0[2];
|
||||
}
|
||||
else if(2 == __version)
|
||||
{
|
||||
// how to calc ratio/123
|
||||
ledOn();
|
||||
int A0_0 = get_addr_dta(6, ADDR_USER_ADC_HN3);
|
||||
int A0_1 = get_addr_dta(6, ADDR_USER_ADC_CO);
|
||||
int A0_2 = get_addr_dta(6, ADDR_USER_ADC_NO2);
|
||||
|
||||
int An_0 = get_addr_dta(CH_VALUE_NH3);
|
||||
int An_1 = get_addr_dta(CH_VALUE_CO);
|
||||
int An_2 = get_addr_dta(CH_VALUE_NO2);
|
||||
|
||||
ratio0 = (float)An_0/(float)A0_0*(1023.0-A0_0)/(1023.0-An_0);
|
||||
ratio1 = (float)An_1/(float)A0_1*(1023.0-A0_1)/(1023.0-An_1);
|
||||
ratio2 = (float)An_2/(float)A0_2*(1023.0-A0_2)/(1023.0-An_2);
|
||||
|
||||
}
|
||||
|
||||
float c = 0;
|
||||
|
||||
switch(gas)
|
||||
{
|
||||
case CO:
|
||||
{
|
||||
c = pow(ratio1, -1.179)*4.385; //mod by jack
|
||||
break;
|
||||
}
|
||||
case NO2:
|
||||
{
|
||||
c = pow(ratio2, 1.007)/6.855; //mod by jack
|
||||
break;
|
||||
}
|
||||
case NH3:
|
||||
{
|
||||
c = pow(ratio0, -1.67)/1.47; //modi by jack
|
||||
break;
|
||||
}
|
||||
case C3H8: //add by jack
|
||||
{
|
||||
c = pow(ratio0, -2.518)*570.164;
|
||||
break;
|
||||
}
|
||||
case C4H10: //add by jack
|
||||
{
|
||||
c = pow(ratio0, -2.138)*398.107;
|
||||
break;
|
||||
}
|
||||
case GAS_CH4: //add by jack
|
||||
{
|
||||
c = pow(ratio1, -4.363)*630.957;
|
||||
break;
|
||||
}
|
||||
case H2: //add by jack
|
||||
{
|
||||
c = pow(ratio1, -1.8)*0.73;
|
||||
break;
|
||||
}
|
||||
case C2H5OH: //add by jack
|
||||
{
|
||||
c = pow(ratio1, -1.552)*1.622;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(2==__version)ledOff();
|
||||
return isnan(c)?-3:c;
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: changeI2cAddr
|
||||
** Descriptions: change I2C address of the slave MCU, and this address will be stored in EEPROM of slave MCU
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::changeI2cAddr(uint8_t newAddr)
|
||||
{
|
||||
Wire.beginTransmission(i2cAddress); // transmit to device
|
||||
Wire.write(0x23); // sends one byte
|
||||
Wire.write(newAddr); // sends one byte
|
||||
if (Wire.endTransmission()){ // stop transmitting
|
||||
__send_error = true;
|
||||
} else {
|
||||
i2cAddress = newAddr;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: doCalibrate
|
||||
** Descriptions: tell slave to do a calibration, it will take about 8s
|
||||
after the calibration, must reread the R0 values
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::doCalibrate(void)
|
||||
{
|
||||
|
||||
if(1 == __version)
|
||||
{
|
||||
START:
|
||||
|
||||
sendI2C(0x22);
|
||||
if(readR0() > 0)
|
||||
{
|
||||
for(int i=0; i<3; i++)
|
||||
{
|
||||
Serial.print(res0[i]);
|
||||
Serial.print('\t');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
delay(5000);
|
||||
Serial.println("continue...");
|
||||
for(int i=0; i<3; i++)
|
||||
{
|
||||
Serial.print(res0[i]);
|
||||
Serial.print('\t');
|
||||
}
|
||||
Serial.println();
|
||||
goto START;
|
||||
}
|
||||
}
|
||||
else if(2 == __version)
|
||||
{
|
||||
unsigned int i, a0, a1, a2;
|
||||
while(1)
|
||||
{
|
||||
a0 = get_addr_dta(CH_VALUE_NH3);
|
||||
a1 = get_addr_dta(CH_VALUE_CO);
|
||||
a2 = get_addr_dta(CH_VALUE_NO2);
|
||||
|
||||
Serial.print(a0);
|
||||
Serial.print('\t');
|
||||
Serial.print(a1);
|
||||
Serial.print('\t');
|
||||
Serial.print(a2);
|
||||
Serial.println('\t');
|
||||
ledOn();
|
||||
|
||||
int cnt = 0;
|
||||
for(i=0; i<20; i++)
|
||||
{
|
||||
if((a0 - get_addr_dta(CH_VALUE_NH3)) > 2 || (get_addr_dta(CH_VALUE_NH3) - a0) > 2)cnt++;
|
||||
if((a1 - get_addr_dta(CH_VALUE_CO)) > 2 || (get_addr_dta(CH_VALUE_CO) - a1) > 2)cnt++;
|
||||
if((a2 - get_addr_dta(CH_VALUE_NO2)) > 2 || (get_addr_dta(CH_VALUE_NO2) - a2) > 2)cnt++;
|
||||
|
||||
if(cnt>5)
|
||||
{
|
||||
break;
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
ledOff();
|
||||
if(cnt <= 5)break;
|
||||
delay(200);
|
||||
}
|
||||
|
||||
Serial.print("write user adc value: ");
|
||||
Serial.print(a0);Serial.print('\t');
|
||||
Serial.print(a1);Serial.print('\t');
|
||||
Serial.print(a2);Serial.println('\t');
|
||||
|
||||
unsigned char tmp[7];
|
||||
|
||||
tmp[0] = 7;
|
||||
|
||||
tmp[1] = a0>>8;
|
||||
tmp[2] = a0&0xff;
|
||||
|
||||
tmp[3] = a1>>8;
|
||||
tmp[4] = a1&0xff;
|
||||
|
||||
tmp[5] = a2>>8;
|
||||
tmp[6] = a2&0xff;
|
||||
|
||||
write_i2c(i2cAddress, tmp, 7);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: powerOn
|
||||
** Descriptions: power on sensor heater
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::powerOn(void)
|
||||
{
|
||||
if(__version == 1)
|
||||
sendI2C(0x21);
|
||||
else if(__version == 2)
|
||||
{
|
||||
dta_test[0] = 11;
|
||||
dta_test[1] = 1;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: powerOff
|
||||
** Descriptions: power off sensor heater
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::powerOff(void)
|
||||
{
|
||||
if(__version == 1)
|
||||
sendI2C(0x20);
|
||||
else if(__version == 2)
|
||||
{
|
||||
dta_test[0] = 11;
|
||||
dta_test[1] = 0;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
}
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::display_eeprom()
|
||||
{
|
||||
if(__version == 1)
|
||||
{
|
||||
Serial.println("ERROR: display_eeprom() is NOT support by V1 firmware.");
|
||||
return ;
|
||||
}
|
||||
|
||||
Serial.print("ADDR_IS_SET = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_IS_SET));
|
||||
Serial.print("ADDR_FACTORY_ADC_NH3 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NH3));
|
||||
Serial.print("ADDR_FACTORY_ADC_CO = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_CO));
|
||||
Serial.print("ADDR_FACTORY_ADC_NO2 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NO2));
|
||||
Serial.print("ADDR_USER_ADC_HN3 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_HN3));
|
||||
Serial.print("ADDR_USER_ADC_CO = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_CO));
|
||||
Serial.print("ADDR_USER_ADC_NO2 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_NO2));
|
||||
Serial.print("ADDR_I2C_ADDRESS = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_I2C_ADDRESS));
|
||||
}
|
||||
|
||||
float MutichannelGasSensor::getR0(unsigned char ch) // 0:CH3, 1:CO, 2:NO2
|
||||
{
|
||||
if(__version == 1)
|
||||
{
|
||||
Serial.println("ERROR: getR0() is NOT support by V1 firmware.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int a = 0;
|
||||
switch(ch)
|
||||
{
|
||||
case 0: // CH3
|
||||
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_HN3);
|
||||
Serial.print("a_ch3 = ");
|
||||
Serial.println(a);
|
||||
break;
|
||||
|
||||
case 1: // CO
|
||||
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_CO);
|
||||
Serial.print("a_co = ");
|
||||
Serial.println(a);
|
||||
break;
|
||||
|
||||
case 2: // NO2
|
||||
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_NO2);
|
||||
Serial.print("a_no2 = ");
|
||||
Serial.println(a);
|
||||
break;
|
||||
|
||||
default:;
|
||||
}
|
||||
|
||||
float r = 56.0*(float)a/(1023.0-(float)a);
|
||||
return r;
|
||||
}
|
||||
|
||||
float MutichannelGasSensor::getRs(unsigned char ch) // 0:CH3, 1:CO, 2:NO2
|
||||
{
|
||||
|
||||
if(__version == 1)
|
||||
{
|
||||
Serial.println("ERROR: getRs() is NOT support by V1 firmware.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int a = 0;
|
||||
switch(ch)
|
||||
{
|
||||
case 0: // NH3
|
||||
a = get_addr_dta(1);
|
||||
break;
|
||||
|
||||
case 1: // CO
|
||||
a = get_addr_dta(2);
|
||||
break;
|
||||
|
||||
case 2: // NO2
|
||||
a = get_addr_dta(3);
|
||||
break;
|
||||
|
||||
default:;
|
||||
}
|
||||
|
||||
float r = 56.0*(float)a/(1023.0-(float)a);
|
||||
return r;
|
||||
}
|
||||
|
||||
// 1. change i2c address to 0x04
|
||||
// 2. change adc value of R0 to default
|
||||
void MutichannelGasSensor::factory_setting()
|
||||
{
|
||||
|
||||
unsigned char tmp[7];
|
||||
|
||||
unsigned char error;
|
||||
unsigned char address = 0;
|
||||
|
||||
for(address = 1; address < 127; address++ )
|
||||
{
|
||||
// The i2c_scanner uses the return value of
|
||||
// the Write.endTransmisstion to see if
|
||||
// a device did acknowledge to the address.
|
||||
Wire.beginTransmission(address);
|
||||
error = Wire.endTransmission();
|
||||
if (error == 0)
|
||||
{
|
||||
// change i2c to 0x04
|
||||
|
||||
Serial.print("I2C address is: 0x");
|
||||
Serial.println(address, HEX);
|
||||
Serial.println("Change I2C address to 0x04");
|
||||
|
||||
dta_test[0] = CMD_CHANGE_I2C;
|
||||
dta_test[1] = 0x04;
|
||||
write_i2c(address, dta_test, 2);
|
||||
|
||||
i2cAddress = 0x04;
|
||||
delay(100);
|
||||
getVersion();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int a0 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NH3);
|
||||
unsigned int a1 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_CO);
|
||||
unsigned int a2 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NO2);
|
||||
|
||||
tmp[0] = 7;
|
||||
tmp[1] = a0>>8;
|
||||
tmp[2] = a0&0xff;
|
||||
tmp[3] = a1>>8;
|
||||
tmp[4] = a1&0xff;
|
||||
|
||||
tmp[5] = a2>>8;
|
||||
tmp[6] = a2&0xff;
|
||||
delay(100);
|
||||
write_i2c(i2cAddress, tmp, 7);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::change_i2c_address(unsigned char addr)
|
||||
{
|
||||
dta_test[0] = CMD_CHANGE_I2C;
|
||||
dta_test[1] = addr;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
|
||||
|
||||
Serial.print("FUNCTION: CHANGE I2C ADDRESS: 0X");
|
||||
Serial.print(i2cAddress, HEX);
|
||||
Serial.print(" > 0x");
|
||||
Serial.println(addr, HEX);
|
||||
|
||||
i2cAddress = addr;
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::ledOn()
|
||||
{
|
||||
dta_test[0] = CMD_CONTROL_LED;
|
||||
dta_test[1] = 1;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::ledOff()
|
||||
{
|
||||
dta_test[0] = CMD_CONTROL_LED;
|
||||
dta_test[1] = 0;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
}
|
||||
|
||||
MutichannelGasSensor gas;
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
|
@ -0,0 +1,140 @@
|
|||
/*
|
||||
MutichannelGasSensor.h
|
||||
2015 Copyright (c) Seeed Technology Inc. All right reserved.
|
||||
|
||||
Author: Jacky Zhang
|
||||
2015-3-17
|
||||
http://www.seeed.cc/
|
||||
modi by Jack, 2015-8
|
||||
|
||||
V2 by Loovee
|
||||
2016-11-11
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Seeed Technology Inc.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#ifndef __MUTICHANNELGASSENSOR_H__
|
||||
#define __MUTICHANNELGASSENSOR_H__
|
||||
|
||||
#define DEFAULT_I2C_ADDR 0x04
|
||||
|
||||
#define ADDR_IS_SET 0 // if this is the first time to run, if 1126, set
|
||||
#define ADDR_FACTORY_ADC_NH3 2
|
||||
#define ADDR_FACTORY_ADC_CO 4
|
||||
#define ADDR_FACTORY_ADC_NO2 6
|
||||
|
||||
#define ADDR_USER_ADC_HN3 8
|
||||
#define ADDR_USER_ADC_CO 10
|
||||
#define ADDR_USER_ADC_NO2 12
|
||||
#define ADDR_IF_CALI 14 // IF USER HAD CALI
|
||||
|
||||
#define ADDR_I2C_ADDRESS 20
|
||||
|
||||
#define CH_VALUE_NH3 1
|
||||
#define CH_VALUE_CO 2
|
||||
#define CH_VALUE_NO2 3
|
||||
|
||||
#define CMD_ADC_RES0 1 // NH3
|
||||
#define CMD_ADC_RES1 2 // CO
|
||||
#define CMD_ADC_RES2 3 // NO2
|
||||
#define CMD_ADC_RESALL 4 // ALL CHANNEL
|
||||
#define CMD_CHANGE_I2C 5 // CHANGE I2C
|
||||
#define CMD_READ_EEPROM 6 // READ EEPROM VALUE, RETURN UNSIGNED INT
|
||||
#define CMD_SET_R0_ADC 7 // SET R0 ADC VALUE
|
||||
#define CMD_GET_R0_ADC 8 // GET R0 ADC VALUE
|
||||
#define CMD_GET_R0_ADC_FACTORY 9 // GET FACTORY R0 ADC VALUE
|
||||
#define CMD_CONTROL_LED 10
|
||||
#define CMD_CONTROL_PWR 11
|
||||
|
||||
enum{CO, NO2, NH3, C3H8, C4H10, GAS_CH4, H2, C2H5OH};
|
||||
|
||||
class MutichannelGasSensor{
|
||||
|
||||
private:
|
||||
|
||||
int __version;
|
||||
int __send_error;
|
||||
unsigned char dta_test[20];
|
||||
|
||||
unsigned int readChAdcValue(int ch);
|
||||
unsigned int adcValueR0_NH3_Buf;
|
||||
unsigned int adcValueR0_CO_Buf;
|
||||
unsigned int adcValueR0_NO2_Buf;
|
||||
|
||||
public:
|
||||
|
||||
uint8_t i2cAddress; //I2C address of this MCU
|
||||
uint16_t res0[3]; //sensors res0
|
||||
uint16_t res[3]; //sensors res
|
||||
bool r0_inited;
|
||||
|
||||
|
||||
inline unsigned int get_addr_dta(unsigned char addr_reg);
|
||||
inline unsigned int get_addr_dta(unsigned char addr_reg, unsigned char __dta);
|
||||
inline void write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len);
|
||||
|
||||
void sendI2C(unsigned char dta);
|
||||
int16_t readData(uint8_t cmd);
|
||||
int16_t readR0(void);
|
||||
int16_t readR(void);
|
||||
float calcGas(int gas);
|
||||
|
||||
public:
|
||||
|
||||
void begin(int address);
|
||||
void begin();
|
||||
void changeI2cAddr(uint8_t newAddr);
|
||||
void powerOn(void);
|
||||
void powerOff(void);
|
||||
void doCalibrate(void);
|
||||
|
||||
//get gas concentration, unit: ppm
|
||||
float measure_CO(){return calcGas(CO);}
|
||||
float measure_NO2(){return calcGas(NO2);}
|
||||
float measure_NH3(){return calcGas(NH3);}
|
||||
float measure_C3H8(){return calcGas(C3H8);}
|
||||
float measure_C4H10(){return calcGas(C4H10);}
|
||||
float measure_CH4(){return calcGas(GAS_CH4);}
|
||||
float measure_H2(){return calcGas(H2);}
|
||||
float measure_C2H5OH(){return calcGas(C2H5OH);}
|
||||
|
||||
float getR0(unsigned char ch); // 0:CH3, 1:CO, 2:NO2
|
||||
float getRs(unsigned char ch); // 0:CH3, 1:CO, 2:NO2
|
||||
|
||||
public:
|
||||
bool isError();
|
||||
void ledOn();
|
||||
void ledOff();
|
||||
void display_eeprom();
|
||||
void factory_setting();
|
||||
void change_i2c_address(unsigned char addr);
|
||||
unsigned char getVersion();
|
||||
};
|
||||
|
||||
extern MutichannelGasSensor gas;
|
||||
|
||||
#endif
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
|
@ -1,5 +1,6 @@
|
|||
/* 5.12.0b
|
||||
* Add serial debug info
|
||||
* Add Multichannel Gas sensor using MultiChannel_Gas_Sensor library (#1245)
|
||||
* Add optional usage of %d or %X suffices in MQTT client to append chipid (#1871)
|
||||
* Add optional usage of %d or %X suffices in MQTT topic to append chipid (#1871)
|
||||
* Add optional usage of %d or %04d in ota url to be replaced with chipid (#1871)
|
||||
|
|
|
@ -449,7 +449,7 @@ const char HTTP_SNS_SEAPRESSURE[] PROGMEM = "%s{s}%s " D_PRESSUREATSEALEVEL "{m}
|
|||
const char HTTP_SNS_ANALOG[] PROGMEM = "%s{s}%s " D_ANALOG_INPUT "%d{m}%d{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
||||
|
||||
#if defined(USE_MHZ19) || defined(USE_SENSEAIR)
|
||||
const char HTTP_SNS_CO2[] PROGMEM = "%s{s}%s " D_CO2 "{m}%d " D_UNIT_PARTS_PER_MILLION "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
||||
const char HTTP_SNS_CO2[] PROGMEM = "%s{s}%s " D_CO2 "{m}%d " D_UNIT_PARTS_PER_MILLION "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
||||
#endif // USE_WEBSERVER
|
||||
|
||||
const char S_MAIN_MENU[] PROGMEM = D_MAIN_MENU;
|
||||
|
|
|
@ -194,6 +194,8 @@
|
|||
// #define USE_ADS1115 // Add I2C code for ADS1115 16 bit A/D converter based on Adafruit ADS1x15 library (no library needed) (+0k7 code)
|
||||
// #define USE_ADS1115_I2CDEV // Add I2C code for ADS1115 16 bit A/D converter using library i2cdevlib-Core and i2cdevlib-ADS1115 (+2k code)
|
||||
// #define USE_INA219 // Add I2C code for INA219 Low voltage and current sensor (+1k code)
|
||||
// #define USE_MGS // Add I2C code for Xadow and Grove Mutichannel Gas sensor using library Multichannel_Gas_Sensor (+10k code)
|
||||
#define MGS_SENSOR_ADDR 0x04 // Default Mutichannel Gas sensor i2c address
|
||||
#endif // USE_I2C
|
||||
|
||||
// -- Serial sensors ------------------------------
|
||||
|
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
xsns_19_mgs.ino - Xadow and Grove Mutichannel Gas sensor support for Sonoff-Tasmota
|
||||
|
||||
Copyright (C) 2018 Palich2000 and Theo Arends
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef USE_I2C
|
||||
#ifdef USE_MGS
|
||||
/*********************************************************************************************\
|
||||
* Grove - Multichannel Gas Sensor
|
||||
* http://wiki.seeed.cc/Grove-Multichannel_Gas_Sensor/
|
||||
*
|
||||
* https://github.com/Seeed-Studio/Mutichannel_Gas_Sensor.git
|
||||
\*********************************************************************************************/
|
||||
|
||||
#ifndef MGS_SENSOR_ADDR
|
||||
#define MGS_SENSOR_ADDR 0x04 // Default Mutichannel Gas sensor i2c address
|
||||
#endif
|
||||
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
void MGSInit() {
|
||||
gas.begin(MGS_SENSOR_ADDR);
|
||||
}
|
||||
|
||||
boolean MGSPrepare()
|
||||
{
|
||||
gas.begin(MGS_SENSOR_ADDR);
|
||||
if (!gas.isError()) {
|
||||
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "MultiGasSensor", MGS_SENSOR_ADDR);
|
||||
AddLog(LOG_LEVEL_DEBUG);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
char* measure_gas(int gas_type, char* buffer)
|
||||
{
|
||||
float f = gas.calcGas(gas_type);
|
||||
dtostrfd(f, 2, buffer);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
const char HTTP_MGS_GAS[] PROGMEM = "%s{s}MGS %s{m}%s " D_UNIT_PARTS_PER_MILLION "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
||||
#endif // USE_WEBSERVER
|
||||
|
||||
void MGSShow(boolean json)
|
||||
{
|
||||
char buffer[25];
|
||||
if (json) {
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"MGS\":{\"NH3\":%s"), mqtt_data, measure_gas(NH3, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"CO\":%s"), mqtt_data, measure_gas(CO, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"NO2\":%s"), mqtt_data, measure_gas(NO2, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"C3H8\":%s"), mqtt_data, measure_gas(C3H8, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"C4H10\":%s"), mqtt_data, measure_gas(C4H10, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"CH4\":%s"), mqtt_data, measure_gas(GAS_CH4, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"H2\":%s"), mqtt_data, measure_gas(H2, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"C2H5OH\":%s}"), mqtt_data, measure_gas(C2H5OH, buffer));
|
||||
#ifdef USE_WEBSERVER
|
||||
} else {
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MGS_GAS, mqtt_data, "NH3", measure_gas(NH3, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MGS_GAS, mqtt_data, "CO", measure_gas(CO, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MGS_GAS, mqtt_data, "NO2", measure_gas(NO2, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MGS_GAS, mqtt_data, "C3H8", measure_gas(C3H8, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MGS_GAS, mqtt_data, "C4H10", measure_gas(C4H10, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MGS_GAS, mqtt_data, "CH4", measure_gas(GAS_CH4, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MGS_GAS, mqtt_data, "H2", measure_gas(H2, buffer));
|
||||
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_MGS_GAS, mqtt_data, "C2H5OH", measure_gas(C2H5OH, buffer));
|
||||
#endif // USE_WEBSERVER
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Interface
|
||||
\*********************************************************************************************/
|
||||
|
||||
#define XSNS_19
|
||||
|
||||
boolean Xsns19(byte function)
|
||||
{
|
||||
boolean result = false;
|
||||
static int detected = false;
|
||||
|
||||
if (i2c_flg) {
|
||||
switch (function) {
|
||||
case FUNC_INIT:
|
||||
// MGSInit();
|
||||
break;
|
||||
case FUNC_PREP_BEFORE_TELEPERIOD:
|
||||
detected = MGSPrepare();
|
||||
break;
|
||||
case FUNC_JSON_APPEND:
|
||||
if (detected) MGSShow(1);
|
||||
break;
|
||||
#ifdef USE_WEBSERVER
|
||||
case FUNC_WEB_APPEND:
|
||||
if (detected) MGSShow(0);
|
||||
break;
|
||||
#endif // USE_WEBSERVER
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // USE_MGS
|
||||
#endif // USE_I2C
|
Loading…
Reference in New Issue