mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
0.3.0 PCF8591
This commit is contained in:
parent
0cdfbc25fa
commit
a79de888fa
@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.3.0] - 2023-12-10
|
||||
- update API, begin
|
||||
- add **uint8_t getADdress()**
|
||||
- update readme.md
|
||||
- update examples
|
||||
- minor edits
|
||||
|
||||
----
|
||||
|
||||
## [0.2.1] - 2023-09-23
|
||||
- add Wire1 support for ESP32
|
||||
- update readme.md
|
||||
|
@ -2,8 +2,8 @@
|
||||
// FILE: PCF8591.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2020-03-12
|
||||
// VERSION: 0.2.1
|
||||
// PURPOSE: I2C PCF8591 library for Arduino
|
||||
// VERSION: 0.3.0
|
||||
// PURPOSE: Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC.
|
||||
// URL: https://github.com/RobTillaart/PCF8591
|
||||
|
||||
|
||||
@ -15,13 +15,8 @@
|
||||
#define PCF8591_INCR_FLAG 0x04
|
||||
|
||||
|
||||
PCF8591::PCF8591(const uint8_t address, TwoWire *wire)
|
||||
PCF8591::PCF8591(uint8_t address, TwoWire *wire)
|
||||
{
|
||||
if ((address < 0x48) || (address > 0x4F))
|
||||
{
|
||||
_error = PCF8591_ADDRESS_ERROR;
|
||||
return;
|
||||
}
|
||||
_address = address;
|
||||
_wire = wire;
|
||||
_control = 0;
|
||||
@ -34,25 +29,13 @@ PCF8591::PCF8591(const uint8_t address, TwoWire *wire)
|
||||
}
|
||||
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
bool PCF8591::begin(uint8_t sda, uint8_t scl, uint8_t val)
|
||||
{
|
||||
if ((sda < 255) && (scl < 255))
|
||||
{
|
||||
_wire->begin(sda, scl);
|
||||
} else {
|
||||
_wire->begin();
|
||||
}
|
||||
if (!isConnected()) return false;
|
||||
analogWrite(val);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
bool PCF8591::begin(uint8_t val)
|
||||
{
|
||||
_wire->begin();
|
||||
if ((_address < 0x48) || (_address > 0x4F))
|
||||
{
|
||||
_error = PCF8591_ADDRESS_ERROR;
|
||||
return false;
|
||||
}
|
||||
if (!isConnected()) return false;
|
||||
analogWrite(val);
|
||||
return true;
|
||||
@ -67,6 +50,12 @@ bool PCF8591::isConnected()
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCF8591::getAddress()
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
//
|
||||
// ADC PART
|
||||
|
@ -3,8 +3,8 @@
|
||||
// FILE: PCF8591.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2020-03-12
|
||||
// VERSION: 0.2.1
|
||||
// PURPOSE: I2C PCF8591 library for Arduino
|
||||
// VERSION: 0.3.0
|
||||
// PURPOSE: Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC.
|
||||
// URL: https://github.com/RobTillaart/PCF8591
|
||||
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define PCF8591_LIB_VERSION (F("0.2.1"))
|
||||
#define PCF8591_LIB_VERSION (F("0.3.0"))
|
||||
|
||||
#define PCF8591_OK 0x00
|
||||
#define PCF8591_PIN_ERROR 0x81
|
||||
@ -31,15 +31,12 @@
|
||||
class PCF8591
|
||||
{
|
||||
public:
|
||||
explicit PCF8591(const uint8_t address = 0x48, TwoWire *wire = &Wire);
|
||||
explicit PCF8591(uint8_t address = 0x48, TwoWire *wire = &Wire);
|
||||
|
||||
// set initial value for DAC, default 0
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
bool begin(uint8_t sda, uint8_t scl, uint8_t value = 0);
|
||||
#endif
|
||||
bool begin(uint8_t value = 0);
|
||||
|
||||
bool isConnected();
|
||||
uint8_t getAddress();
|
||||
|
||||
|
||||
// ADC PART
|
||||
|
@ -19,7 +19,7 @@ Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC.
|
||||
**warning** during tests I could overclock the PCF8591 chip up to 650 KHz.
|
||||
However it is only specified to run at 100 kHz.
|
||||
After some time it was getting pretty hot and it broke down.
|
||||
So overclocking is fun but not recommended.
|
||||
So overclocking is fun but **not recommended**.
|
||||
|
||||
PCF8591 has one 8 bit ADC on board for 4 channels. The ADC is 8 bit and quite fast.
|
||||
At 100 KHz one gets \> 2000 reads per second for **analogRead()** and
|
||||
@ -32,23 +32,37 @@ The **lastRead()** function is needed to get access to the values.
|
||||
First tests shows it is 2.6 x faster than 4 individual reads.
|
||||
|
||||
|
||||
#### 0.3.0 Breaking change
|
||||
|
||||
Version 0.3.0 introduced a breaking change.
|
||||
You cannot set the pins in **begin()** any more.
|
||||
This reduces the dependency of processor dependent Wire implementations.
|
||||
The user has to call **Wire.begin()** and can optionally set the Wire pins
|
||||
before calling **begin()**.
|
||||
|
||||
|
||||
#### Related
|
||||
|
||||
- https://github.com/RobTillaart/MCP_DAC
|
||||
- https://github.com/RobTillaart/MCP_ADC
|
||||
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
```cpp
|
||||
#include "PCF8591.h"
|
||||
```
|
||||
|
||||
|
||||
#### Constructor
|
||||
|
||||
- **PCF8591(const uint8_t address = 0x48, TwoWire \*wire = &Wire)** constructor with I2C address.
|
||||
- **PCF8591(uint8_t address = 0x48, TwoWire \*wire = &Wire)** constructor with I2C address.
|
||||
Default is 0x48, optional set the WireN I2C bus.
|
||||
- **bool begin(uint8_t sda, uint8_t scl, uint8_t value = 0)** set wire pins for ESP series.
|
||||
Also set initial value for the DAC.
|
||||
- **bool begin(uint8_t value = 0)** Set initial value for the DAC.
|
||||
Returns false if address out of range, or if it cannot be seen on the I2C bus.
|
||||
Returns **true** if successful.
|
||||
- **bool begin(uint8_t value = 0)** Set initial value for the DAC.
|
||||
Returns **true** if successful.
|
||||
- **bool isConnected()** test to see if chip can be reached.
|
||||
- **bool isConnected()** test to see if address can be reached on the I2C bus.
|
||||
- **uint8_t getAddress()** returns address set in constructor.
|
||||
|
||||
|
||||
#### ADC channels
|
||||
@ -143,9 +157,11 @@ See examples.
|
||||
|
||||
#### Should
|
||||
|
||||
- add examples for comparator calls.
|
||||
- add examples
|
||||
- for comparator calls.
|
||||
- schema?
|
||||
|
||||
- add examples boards
|
||||
- ESP32, RP2040 (pins)
|
||||
|
||||
#### Could
|
||||
|
||||
|
@ -15,13 +15,12 @@ void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("PCF8591_LIB_VERSION: ");
|
||||
Serial.println(PCF8591_LIB_VERSION);
|
||||
|
||||
Wire.begin();
|
||||
Wire.setClock(100000UL);
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
dev.begin(21, 22);
|
||||
#endif
|
||||
dev.begin();
|
||||
}
|
||||
|
||||
@ -33,7 +32,7 @@ void loop()
|
||||
test_ADC_mode(0);
|
||||
delay(1000);
|
||||
|
||||
// differential modes, check datasheet
|
||||
// differential modes, check datasheet
|
||||
test_ADC_mode(1);
|
||||
delay(1000);
|
||||
test_ADC_mode(2);
|
||||
@ -62,7 +61,7 @@ void test_DAC()
|
||||
|
||||
void test_ADC_mode(uint8_t mode)
|
||||
{
|
||||
uint8_t channels[] = {4, 3, 3, 2 }; // channels per mode
|
||||
uint8_t channels[] = {4, 3, 3, 2 }; // channels per mode
|
||||
Serial.println(__FUNCTION__);
|
||||
Serial.println("--------------");
|
||||
Serial.println("CH0\tCH1\tCH2\tCH3");
|
||||
@ -79,5 +78,5 @@ void test_ADC_mode(uint8_t mode)
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -17,14 +17,13 @@ void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("PCF8591_LIB_VERSION: ");
|
||||
Serial.println(PCF8591_LIB_VERSION);
|
||||
|
||||
Wire.begin();
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
dev.begin(21, 22); // adjust pins if needed.
|
||||
#else
|
||||
dev.begin();
|
||||
#endif
|
||||
|
||||
|
||||
if (! dev.isConnected())
|
||||
{
|
||||
|
@ -21,14 +21,12 @@ void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("PCF8591_LIB_VERSION: ");
|
||||
Serial.println(PCF8591_LIB_VERSION);
|
||||
|
||||
Wire.begin();
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
dev.begin(21, 22); // adjust pins if needed.
|
||||
#else
|
||||
dev.begin();
|
||||
#endif
|
||||
|
||||
if (! dev.isConnected())
|
||||
{
|
||||
@ -77,7 +75,7 @@ void test2()
|
||||
{
|
||||
uint32_t clk = 50000UL * i;
|
||||
Serial.print("| ");
|
||||
Serial.print(clk/1000);
|
||||
Serial.print(clk / 1000);
|
||||
Wire.setClock(clk);
|
||||
test_DAC_error();
|
||||
delay(10);
|
||||
@ -151,4 +149,4 @@ void test_ADC_error()
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
@ -6,6 +6,7 @@ PCF8591 KEYWORD1
|
||||
# Methods and Functions (KEYWORD2)
|
||||
begin KEYWORD2
|
||||
isConnected KEYWORD2
|
||||
getAddress KEYWORD2
|
||||
|
||||
enableINCR KEYWORD2
|
||||
disableINCR KEYWORD2
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/PCF8591.git"
|
||||
},
|
||||
"version": "0.2.1",
|
||||
"version": "0.3.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "*",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=PCF8591
|
||||
version=0.2.1
|
||||
version=0.3.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=PCF8591 library for Arduino. Supports multiple I2C WireN bus.
|
||||
|
@ -61,6 +61,9 @@ unittest(test_constants)
|
||||
unittest(test_constructor)
|
||||
{
|
||||
PCF8591 dev(0x48);
|
||||
|
||||
Wire.begin();
|
||||
|
||||
assertTrue(dev.begin());
|
||||
|
||||
assertTrue(dev.isConnected());
|
||||
@ -72,8 +75,10 @@ unittest(test_constructor)
|
||||
unittest(test_ADC_INCR)
|
||||
{
|
||||
PCF8591 dev(0x48);
|
||||
|
||||
Wire.begin();
|
||||
assertTrue(dev.begin());
|
||||
|
||||
|
||||
assertFalse(dev.isINCREnabled());
|
||||
dev.enableINCR();
|
||||
assertTrue(dev.isINCREnabled());
|
||||
@ -85,6 +90,8 @@ unittest(test_ADC_INCR)
|
||||
unittest(test_DAC)
|
||||
{
|
||||
PCF8591 dev(0x48);
|
||||
|
||||
Wire.begin();
|
||||
assertTrue(dev.begin());
|
||||
|
||||
assertFalse(dev.isDACEnabled());
|
||||
|
Loading…
Reference in New Issue
Block a user