feat: 全量同步 254 个常用的 Arduino 扩展库文件

This commit is contained in:
yczpf2019
2026-01-24 16:05:38 +08:00
parent c665ba662b
commit 397b9a23a3
6878 changed files with 2732224 additions and 1 deletions

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PCF8591_library</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Renzo Mischianti
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.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,228 @@
/**
* PCF8591 Analog Port Expand
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
*
* 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 "PCF8591.h"
#include "Wire.h"
/**
* Constructor
* @param address: i2c address
*/
PCF8591::PCF8591(uint8_t address){
_wire = &Wire;
_address = address;
};
#if !defined(__AVR) && !defined(__STM32F1__)
/**
*
* @param address: i2c address
* @param sda: sda pin
* @param scl: scl pin
*/
PCF8591::PCF8591(uint8_t address, uint8_t sda, uint8_t scl){
_wire = &Wire;
_address = address;
_sda = sda;
_scl = scl;
};
#ifdef ESP32
/**
* Constructor
* @param address: i2c address
*/
PCF8591::PCF8591(TwoWire *pWire, uint8_t address){
_wire = pWire;
_address = address;
};
/**
*
* @param address: i2c address
* @param sda: sda pin
* @param scl: scl pin
*/
PCF8591::PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl){
_wire = pWire;
_address = address;
_sda = sda;
_scl = scl;
};
#endif
#endif
/**
* wake up i2c controller
*/
void PCF8591::begin(){
#ifndef __AVR
_wire->begin(_sda, _scl);
#else
// Default pin for AVR some problem on software emulation
// #define SCL_PIN _scl
// #define SDA_PIN _sda
_wire->begin();
#endif
}
/**
* Read all analog input in one trasmission
* @param readType read datasheet for info
* SINGLE_ENDED_INPUT
* TREE_DIFFERENTIAL_INPUT
* TWO_SINGLE_ONE_DIFFERENTIAL_INPUT
* TWO_DIFFERENTIAL_INPUT
* @return
*/
struct PCF8591::AnalogInput PCF8591::analogReadAll(byte readType){
DEBUG_PRINTLN("Begin trasmission");
_wire->beginTransmission(_address); // wake up PCF8591
byte operation = AUTOINCREMENT_READ | readType | (_outputStatus&OUTPUT_MASK);
DEBUG_PRINTLN("Write operation");
_wire->write(operation); // control byte: reads ADC0 then auto-increment
DEBUG_PRINTLN("End write (If code stop here add pullup resistor on SDA SCL)");
_wire->endTransmission(); // end tranmission
DEBUG_PRINTLN("Request response");
_wire->requestFrom(_address, (uint8_t)5);
/*uint8_t control =*/_wire->read();
analogInput.ain0=_wire->read();
analogInput.ain1=_wire->read();
analogInput.ain2=_wire->read();
analogInput.ain3=_wire->read();
return analogInput;
};
/**
* Read one specified channel
* @param channel channel or analog identify (if readType is SINGLE_ENDED_INPUT)
* @param readType read datasheet for info
* SINGLE_ENDED_INPUT
* TREE_DIFFERENTIAL_INPUT
* TWO_SINGLE_ONE_DIFFERENTIAL_INPUT
* TWO_DIFFERENTIAL_INPUT
* @return
*/
uint8_t PCF8591::analogRead(uint8_t channel, byte readType){
DEBUG_PRINTLN("Begin trasmission");
_wire->beginTransmission(_address); // wake up PCF8591
byte operation = channel | readType| (_outputStatus&OUTPUT_MASK);
DEBUG_PRINTLN("Write operation");
_wire->write(operation); // control byte: reads ADC0 then auto-increment
DEBUG_PRINTLN("End write (If code stop here add pullup resistor on SDA SCL)");
_wire->endTransmission(); // end tranmission
DEBUG_PRINTLN("Request response");
_wire->requestFrom(_address, (uint8_t)2);
/*uint8_t control = */_wire->read();
uint8_t ana=_wire->read();
return ana;
};
/**
* Read voltage of analog input
* @param analogPin (Analog identifier)
* @param microcontrollerReferenceVoltage get voltage from microcontroller voltage (only AVR no esp8266 for esp 3.3v fixed)
* @param referenceVoltage if microcontrollerReferenceVoltage false take this value
* @return
*/
float PCF8591::voltageRead(uint8_t analogPin, bool microcontrollerReferenceVoltage, float referenceVoltage){
float voltageRef = referenceVoltage;
if (microcontrollerReferenceVoltage) voltageRef = PCF8591::readVcc()/1000.0;
float ana = PCF8591::analogRead(analogPin);
return ana*voltageRef/255;
};
/**
* To write votlage on output
* @param value voltage to write
* @param microcontrollerReferenceVoltage get voltage from microcontroller voltage (only AVR no esp8266 for esp 3.3v fixed)
* @param referenceVoltage if microcontrollerReferenceVoltage false take this value
*/
void PCF8591::voltageWrite(float value, bool microcontrollerReferenceVoltage, float referenceVoltage){
if (microcontrollerReferenceVoltage) referenceVoltage = PCF8591::readVcc()/1000.0;
uint8_t ana = value*255/referenceVoltage;
PCF8591::analogWrite(ana);
};
/**
* Write value on output pin
* @param value votlage in volts
*/
void PCF8591::analogWrite(uint8_t value){
_outputStatus = ENABLE_OUTPUT;
DEBUG_PRINTLN("Begin trasmission");
_wire->beginTransmission(_address);
DEBUG_PRINTLN("Write operation");
_wire->write(ENABLE_OUTPUT); // sets the PCF8591 into a DA mode
DEBUG_PRINTLN("Write value");
_wire->write(value); // sets the output
DEBUG_PRINTLN("End write (If code stop here add pullup resistor on SDA SCL)");
_wire->endTransmission();
};
long PCF8591::readVcc(void) {
#ifdef __AVR
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA, ADSC))
; // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high << 8) | low;
result = 1083630L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
// scale_constant = internal1.1Ref * 1023 * 1000
// internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function)
return result; // Vcc in millivolts
#else
// float vdd = readVcc(); // ESP.getVdd33(); //ESP.getVcc();
return 3300;
#endif
}

View File

@@ -0,0 +1,119 @@
/** \mainpage PCF8591 library
* PCF8591 Analog Port Expand
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
*
* 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 PCF8591_h
#define PCF8591_h
#include "Wire.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Uncomment to enable printing out nice debug messages.
// #define PCF8591_DEBUG
// Define where debug output will be printed.
#define DEBUG_PRINTER Serial
// Setup debug printing macros.
#ifdef PCF8591_DEBUG
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else
#define DEBUG_PRINT(...) {}
#define DEBUG_PRINTLN(...) {}
#endif
#include <math.h>
#define AIN0 B00000000
#define AIN1 B00000001
#define AIN2 B00000010
#define AIN3 B00000011
#define CHANNEL0 B00000000
#define CHANNEL1 B00000001
#define CHANNEL2 B00000010
#define CHANNEL3 B00000011
#define AUTOINCREMENT_READ B00000100
#define SINGLE_ENDED_INPUT B00000000
#define TREE_DIFFERENTIAL_INPUT B00010000
#define TWO_SINGLE_ONE_DIFFERENTIAL_INPUT B00100000
#define TWO_DIFFERENTIAL_INPUT B00110000
#define ENABLE_OUTPUT B01000000
#define DISABLE_OUTPUT B01000000
#define OUTPUT_MASK B01000000
class PCF8591 {
public:
struct AnalogInput {
uint8_t ain0;
uint8_t ain1;
uint8_t ain2;
uint8_t ain3;
} analogInput;
PCF8591(uint8_t address);
#if !defined(__AVR) && !defined(__STM32F1__)
PCF8591(uint8_t address, uint8_t sda, uint8_t scl);
#ifdef ESP32
PCF8591(TwoWire *pWire, uint8_t address);
PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl);
#endif
#endif
void begin(void);
struct AnalogInput analogReadAll(byte readType = SINGLE_ENDED_INPUT);
uint8_t analogRead(uint8_t channel, byte readType = SINGLE_ENDED_INPUT);
void analogWrite(uint8_t value);
void voltageWrite(float value, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);
float voltageRead(uint8_t analogPin, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);
private:
TwoWire *_wire;
uint8_t _address;
uint8_t _sda = SDA;
uint8_t _scl = SCL;
byte _outputStatus = DISABLE_OUTPUT;
long readVcc(void);
};
#endif

View File

@@ -0,0 +1,83 @@
You can find updated version of documentation on my site [PCF8591](https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/)
Library to use i2c analog IC with arduino and esp8266. Can read analog value and write analog value with only 2 wire (perfect for ESP-01).
Tutorial:
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder PCF8591. Check that the PCF8591 folder contains `PCF8591\\.cpp` and `PCF8591.h`. Place the DHT library folder your `<arduinosketchfolder>/libraries/` folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
# Reef complete PCF8591 Analog input and analog output to digital converter with i2c bus.
I try to simplify the use of this IC, with a minimal set of operation.
Constructor:
you must pas the address of i2c (to check the adress use this guide [I2cScanner](https://playground.arduino.cc/Main/I2cScanner))
```cpp
PCF8591(uint8_t address);
```
for esp8266 if you want specify SDA e SCL pin use this:
```cpp
PCF8591(uint8_t address, uint8_t sda, uint8_t scl);
```
then IC as you can see in the image have 4 analog input and 1 analog output:
![PCF8591 schema](https://github.com/xreef/PCF8591_library/blob/master/resources/PCF8591-Pin-Outs.png)
So to read all analog input in one trasmission you can do (the value is from 0 to 255):
```cpp
PCF8591::AnalogInput ai = pcf8591.analogReadAll();
Serial.print(ai.ain0);
Serial.print(" - ");
Serial.print(ai.ain1);
Serial.print(" - ");
Serial.print(ai.ain2);
Serial.print(" - ");
Serial.println(ai.ain3);
```
if you want read a single analog input or channel:
```cpp
int ana = pcf8591.analogRead(AIN0); // read analog 0
```
This IC have multiple type of read and you can use Analog input or analog channel (when you use single read analog input and channel are the same:
![Channel selection](https://github.com/xreef/PCF8591_library/blob/master/resources/channel_selection.PNG)
For example to read the value of channel 0 in Two differential input you must do:
```cpp
int ana = pcf8591.analogRead(CHANNEL0, TWO_DIFFERENTIAL_INPUT); // read analog 0
```
If you want write an analog value you must do (the value is from 0 to 255):
```cpp
pcf8591.analogWrite(128);
```
Additional feature is to read a write voltage:
For the calculation of voltage you must pass some parameter:
- microcontrollerReferenceVoltage: get voltage from microcontroller voltage (only AVR no esp8266 for esp 3.3v fixed)
- referenceVoltage: if microcontrollerReferenceVoltage false take this value
The command are:
```cpp
void voltageWrite(float value, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);
float voltageRead(uint8_t analogPin, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);
```
An examples is:
```cpp
pcf8591.voltageWrite(2.7); // 2.7Volts output
delay(3000);
float ana0V = pcf8591.voltageRead(AIN0); // Read voltage from analog 0
Serial.println(ana0V);
```
For the examples I use this wire schema on breadboard:
![Breadboard](https://github.com/xreef/PCF8591_library/blob/master/resources/simpleschema_bb.png)

View File

@@ -0,0 +1,67 @@
/*
* PCF8591 Analog Port Expand
* Read all analog pins and write value on analog ouput
*
* by Mischianti Renzo <http://www.mischianti.org>
*
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
*
* PCF8574 ----- Esp32
* A0 ----- GRD
* A1 ----- GRD
* A2 ----- GRD
* SDA ----- A4
* SCL ----- A5
*
*
*/
#include "Arduino.h"
#include "PCF8591.h"
#define PCF8591_I2C_ADDRESS 0x48
PCF8591 pcf8591(PCF8591_I2C_ADDRESS);
void setup()
{
Serial.begin(115200);
pcf8591.begin();
}
void loop()
{
PCF8591::AnalogInput ai = pcf8591.analogReadAll();
Serial.print(ai.ain0);
Serial.print(" - ");
Serial.print(ai.ain1);
Serial.print(" - ");
Serial.print(ai.ain2);
Serial.print(" - ");
Serial.println(ai.ain3);
delay(3000);
int ana = pcf8591.analogRead(AIN0);
Serial.print("AIN0 --> ");
Serial.println(ana);
ana = pcf8591.analogRead(AIN1);
Serial.print("AIN1 --> ");
Serial.println(ana);
ana = pcf8591.analogRead(AIN2);
Serial.print("AIN2 --> ");
Serial.println(ana);
ana = pcf8591.analogRead(AIN3);
Serial.print("AIN3 --> ");
Serial.println(ana);
delay(3000);
pcf8591.analogWrite(0);
delay(3000);
pcf8591.analogWrite(128);
delay(3000);
pcf8591.analogWrite(255);
delay(3000);
}

View File

@@ -0,0 +1,77 @@
/*
* PCF8591 Analog Port Expand
* Read all analog pins and write value on analog ouput
*
* by Mischianti Renzo <http://www.mischianti.org>
*
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
*
* PCF8574 ----- Esp32
* A0 ----- GRD
* A1 ----- GRD
* A2 ----- GRD
* SDA ----- 21
* SCL ----- 22
*
*
*/
#include "Arduino.h"
#include "PCF8591.h"
#define PCF8591_I2C_ADDRESS 0x48
// Instantiate Wire for generic use at 400kHz
TwoWire I2Cone = TwoWire(0);
// Instantiate Wire for generic use at 100kHz
TwoWire I2Ctwo = TwoWire(1);
// Set i2c address
//PCF8591 pcf8591(&I2Ctwo, PCF8591_I2C_ADDRESS);
PCF8591 pcf8591(&I2Ctwo, 0x20, 21, 22);
void setup()
{
Serial.begin(115200);
I2Cone.begin(16,17,400000); // SDA pin 16, SCL pin 17, 400kHz frequency
pcf8591.begin();
}
void loop()
{
PCF8591::AnalogInput ai = pcf8591.analogReadAll();
Serial.print(ai.ain0);
Serial.print(" - ");
Serial.print(ai.ain1);
Serial.print(" - ");
Serial.print(ai.ain2);
Serial.print(" - ");
Serial.println(ai.ain3);
delay(3000);
int ana = pcf8591.analogRead(AIN0);
Serial.print("AIN0 --> ");
Serial.println(ana);
ana = pcf8591.analogRead(AIN1);
Serial.print("AIN1 --> ");
Serial.println(ana);
ana = pcf8591.analogRead(AIN2);
Serial.print("AIN2 --> ");
Serial.println(ana);
ana = pcf8591.analogRead(AIN3);
Serial.print("AIN3 --> ");
Serial.println(ana);
delay(3000);
pcf8591.analogWrite(0);
delay(3000);
pcf8591.analogWrite(128);
delay(3000);
pcf8591.analogWrite(255);
delay(3000);
}

View File

@@ -0,0 +1,42 @@
/*
* PCF8591 Analog Port Expand
* Production of a sinusoïdal signal using a PCF8591 module
*
* by Yves Pelletier <http://electroniqueamateur.blogspot.com>
*
* http://electroniqueamateur.blogspot.com/2019/01/pcf8591-et-esp8266-ou-arduino.html
*
*
* PCF8574 ----- Esp32
* A0 ----- GRD
* A1 ----- GRD
* A2 ----- GRD
* SDA ----- A4
* SCL ----- A5
*
*
*/
#include "PCF8591.h" // bibliothèque https://github.com/xreef/PCF8591_library
#define PCF8591_I2C_ADDRESS 0x48 //adresse i2c du module PCF8591
PCF8591 pcf8591(PCF8591_I2C_ADDRESS);
int compteur;
void setup()
{
pcf8591.begin();
}
void loop(){
pcf8591.analogWrite(100 + 100 * sin(2*3.1416*compteur/200) ); // sinus
// pcf8591.analogWrite(compteur ); // dent de scie
compteur++;
if (compteur > 200){
compteur = 0;
}
delay(1);
}

View File

@@ -0,0 +1,46 @@
/*
* PCF8591 Analog Port Expand
* Read write voltage on pins
*
* by Mischianti Renzo <http://www.mischianti.org>
*
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
*
* PCF8574 ----- Esp32
* A0 ----- GRD
* A1 ----- GRD
* A2 ----- GRD
* SDA ----- A4
* SCL ----- A5
*
*
*/
#include "Arduino.h"
#include "PCF8591.h"
#define PCF8591_I2C_ADDRESS 0x48
PCF8591 pcf8591(PCF8591_I2C_ADDRESS);
void setup()
{
Serial.begin(115200);
pcf8591.begin();
}
void loop()
{
pcf8591.voltageWrite(2.7); // 2.7Volts output
delay(3000);
float ana0V = pcf8591.voltageRead(AIN0);
Serial.println(ana0V);
float ana1V = pcf8591.voltageRead(AIN1);
Serial.println(ana1V);
float ana2V = pcf8591.voltageRead(AIN2);
Serial.println(ana2V);
delay(3000);
}

View File

@@ -0,0 +1,21 @@
###########################################
# Syntax Coloring Map For PCF8591-library
###########################################
###########################################
# Datatypes (KEYWORD1)
###########################################
PCF8591 KEYWORD1
###########################################
# Methods and Functions (KEYWORD2)
###########################################
begin KEYWORD2
AnalogInput KEYWORD2
analogRead KEYWORD2
analogWrite KEYWORD2
voltageWrite KEYWORD2
voltageRead KEYWORD2

View File

@@ -0,0 +1,9 @@
name=PCF8591 library
version=0.9.0
author=Reef
maintainer=Renzo Mischianti <renzo.mischianti@gmail.com>
sentence=Arduino/ESP8266 library for PCF8591
paragraph=Use i2c digital expander with Arduino and ESP8266. Can read write digital values with only 2 wire (perfect for ESP-01).
category=Sensors
url=https://github.com/xreef/PCF8591_library
architectures=*