mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.3 PCF8591
This commit is contained in:
parent
38d797b858
commit
ddd5c8e42a
@ -1,3 +1,18 @@
|
||||
platforms:
|
||||
rpipico:
|
||||
board: rp2040:rp2040:rpipico
|
||||
package: rp2040:rp2040
|
||||
gcc:
|
||||
features:
|
||||
defines:
|
||||
- ARDUINO_ARCH_RP2040
|
||||
warnings:
|
||||
flags:
|
||||
|
||||
packages:
|
||||
rp2040:rp2040:
|
||||
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
|
||||
|
||||
compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
@ -8,4 +23,6 @@ compile:
|
||||
- m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
||||
# - mega2560
|
||||
- rpipico
|
||||
|
||||
|
38
libraries/PCF8591/CHANGELOG.md
Normal file
38
libraries/PCF8591/CHANGELOG.md
Normal file
@ -0,0 +1,38 @@
|
||||
# Change Log PCF8591
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.1.3] - 2022-11-21
|
||||
- add RP2040 to build-CI
|
||||
- add changelog.md
|
||||
- move code from .h to .cpp
|
||||
- update unit test
|
||||
|
||||
|
||||
## [0.1.2] - 2021-12-23
|
||||
- update library.json
|
||||
- update readme.md
|
||||
- update license
|
||||
- minor edits
|
||||
|
||||
## [0.1.1] - 2021-01-14
|
||||
- added WireN
|
||||
- improve error handling.
|
||||
|
||||
## [0.1.0] - 2021-01-04
|
||||
- add Arduino-CI
|
||||
|
||||
----
|
||||
|
||||
## [0.0.2] - 2020-07-22
|
||||
- testing
|
||||
- refactor
|
||||
- add documentation and examples
|
||||
|
||||
## [0.0.1] - 2020-03-12
|
||||
- initial version
|
||||
|
@ -2,28 +2,26 @@
|
||||
// FILE: PCF8591.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2020-03-12
|
||||
// VERSION: 0.1.2
|
||||
// VERSION: 0.1.3
|
||||
// PURPOSE: I2C PCF8591 library for Arduino
|
||||
// URL: https://github.com/RobTillaart/PCF8591
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.0.1 2020-03-12 initial version
|
||||
// 0.0.2 2020-07-22 testing, refactor, documentation and examples
|
||||
// 0.1.0 2021-01-04 Arduino-CI
|
||||
// 0.1.1 2021-01-14 added WireN + improve error handling.
|
||||
// 0.1.2 2021-12-23 update library.json, readme, license, minor edits
|
||||
|
||||
|
||||
#include "PCF8591.h"
|
||||
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
#define PCF8591_DAC_FLAG 0x40
|
||||
#define PCF8591_INCR_FLAG 0x04
|
||||
|
||||
|
||||
PCF8591::PCF8591(const uint8_t address, TwoWire *wire)
|
||||
{
|
||||
if ((address < 0x48) || (address > 0x4F))
|
||||
{
|
||||
_error = PCF8591_ADDRESS_ERROR;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_address = address;
|
||||
_wire = wire;
|
||||
_control = 0;
|
||||
@ -68,7 +66,28 @@ bool PCF8591::isConnected()
|
||||
return( _error == PCF8591_OK);
|
||||
}
|
||||
|
||||
// ADC PART
|
||||
//////////////////////////////////////////////////////////
|
||||
//
|
||||
// ADC PART
|
||||
//
|
||||
void PCF8591::enableINCR()
|
||||
{
|
||||
_control |= PCF8591_INCR_FLAG;
|
||||
};
|
||||
|
||||
|
||||
void PCF8591::disableINCR()
|
||||
{
|
||||
_control &= ~PCF8591_INCR_FLAG;
|
||||
};
|
||||
|
||||
|
||||
bool PCF8591::isINCREnabled()
|
||||
{
|
||||
return ((_control & PCF8591_INCR_FLAG) > 0);
|
||||
};
|
||||
|
||||
|
||||
uint8_t PCF8591::analogRead(uint8_t channel, uint8_t mode)
|
||||
{
|
||||
if (mode > 3)
|
||||
@ -103,7 +122,7 @@ uint8_t PCF8591::analogRead(uint8_t channel, uint8_t mode)
|
||||
}
|
||||
_error = PCF8591_OK;
|
||||
|
||||
// NOTE: one must read two values to get an up to date value.
|
||||
// NOTE: one must read two values to get an up to date value.
|
||||
// Page 8 datasheet.
|
||||
_wire->beginTransmission(_address);
|
||||
_wire->write(_control);
|
||||
@ -126,12 +145,12 @@ uint8_t PCF8591::analogRead4()
|
||||
_control &= 0b01000100; // clear all except flags
|
||||
uint8_t channel = 0;
|
||||
_control |= channel;
|
||||
|
||||
|
||||
enableINCR();
|
||||
_wire->beginTransmission(_address);
|
||||
_wire->write(_control);
|
||||
_error = _wire->endTransmission(); // default == 0 == PCF8591_OK
|
||||
if (_error != 0)
|
||||
if (_error != 0)
|
||||
{
|
||||
_error = PCF8591_I2C_ERROR;
|
||||
disableINCR();
|
||||
@ -155,7 +174,34 @@ uint8_t PCF8591::analogRead4()
|
||||
}
|
||||
|
||||
|
||||
// DAC PART
|
||||
uint8_t PCF8591::lastRead(uint8_t channel)
|
||||
{
|
||||
return _adc[channel];
|
||||
};
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
//
|
||||
// DAC PART
|
||||
//
|
||||
void PCF8591::enableDAC()
|
||||
{
|
||||
_control |= PCF8591_DAC_FLAG;
|
||||
};
|
||||
|
||||
|
||||
void PCF8591::disableDAC()
|
||||
{
|
||||
_control &= ~PCF8591_DAC_FLAG;
|
||||
};
|
||||
|
||||
|
||||
bool PCF8591::isDACEnabled()
|
||||
{
|
||||
return ((_control & PCF8591_DAC_FLAG) > 0);
|
||||
};
|
||||
|
||||
|
||||
bool PCF8591::analogWrite(uint8_t value)
|
||||
{
|
||||
_wire->beginTransmission(_address);
|
||||
@ -171,6 +217,17 @@ bool PCF8591::analogWrite(uint8_t value)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint8_t PCF8591::lastWrite()
|
||||
{
|
||||
return _dac;
|
||||
};
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////
|
||||
//
|
||||
// ERROR HANDLING
|
||||
//
|
||||
int PCF8591::lastError()
|
||||
{
|
||||
int e = _error;
|
||||
@ -179,5 +236,5 @@ int PCF8591::lastError()
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -3,20 +3,16 @@
|
||||
// FILE: PCF8591.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2020-03-12
|
||||
// VERSION: 0.1.2
|
||||
// VERSION: 0.1.3
|
||||
// PURPOSE: I2C PCF8591 library for Arduino
|
||||
// URL: https://github.com/RobTillaart/PCF8591
|
||||
//
|
||||
// HISTORY:
|
||||
// see PCF8591.cpp file
|
||||
//
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define PCF8591_LIB_VERSION (F("0.1.2"))
|
||||
#define PCF8591_LIB_VERSION (F("0.1.3"))
|
||||
|
||||
#define PCF8591_OK 0x00
|
||||
#define PCF8591_PIN_ERROR 0x81
|
||||
@ -26,11 +22,6 @@
|
||||
#define PCF8591_ADDRESS_ERROR 0x85
|
||||
|
||||
|
||||
// INTERNAL USE ONLY
|
||||
#define PCF8591_DAC_FLAG 0x40
|
||||
#define PCF8591_INCR_FLAG 0x04
|
||||
|
||||
|
||||
class PCF8591
|
||||
{
|
||||
public:
|
||||
@ -43,24 +34,30 @@ public:
|
||||
|
||||
bool isConnected();
|
||||
|
||||
// ADC PART
|
||||
// auto increment not tested ==> use with care!
|
||||
void enableINCR() { _control |= PCF8591_INCR_FLAG; };
|
||||
void disableINCR() { _control &= ~PCF8591_INCR_FLAG; };
|
||||
bool isINCREnabled() { return ((_control & PCF8591_INCR_FLAG) > 0); };
|
||||
|
||||
uint8_t analogRead(uint8_t channel, uint8_t mode = 0);
|
||||
uint8_t analogRead4(); // returns PCF8591_OK or error code.
|
||||
uint8_t lastRead(uint8_t channel) { return _adc[channel]; };
|
||||
// ADC PART
|
||||
// auto increment not tested ==> use with care!
|
||||
void enableINCR();
|
||||
void disableINCR();
|
||||
bool isINCREnabled();
|
||||
|
||||
// DAC PART
|
||||
void enableDAC() { _control |= PCF8591_DAC_FLAG; };
|
||||
void disableDAC() { _control &= ~PCF8591_DAC_FLAG; };
|
||||
bool isDACEnabled() { return ((_control & PCF8591_DAC_FLAG) > 0); };
|
||||
// analogRead() returns the value.
|
||||
uint8_t analogRead(uint8_t channel, uint8_t mode = 0);
|
||||
// analogRead4() returns PCF8591_OK or an error code.
|
||||
uint8_t analogRead4();
|
||||
// access the 4 channels read with analogRead4()
|
||||
uint8_t lastRead(uint8_t channel);
|
||||
|
||||
bool analogWrite(uint8_t value = 0); // returns true on success.
|
||||
uint8_t lastWrite() { return _dac; }; // last successful write
|
||||
|
||||
// DAC PART
|
||||
void enableDAC();
|
||||
void disableDAC();
|
||||
bool isDACEnabled();
|
||||
|
||||
bool analogWrite(uint8_t value = 0); // returns true on success.
|
||||
uint8_t lastWrite(); // returns last successful write
|
||||
|
||||
// ERROR HANDLING
|
||||
int lastError();
|
||||
|
||||
|
||||
@ -75,5 +72,5 @@ private:
|
||||
};
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -43,11 +43,11 @@ Also set initial value for the DAC. Returns **true** if successful.
|
||||
- **bool isConnected()** test to see if chip can be reached.
|
||||
|
||||
|
||||
### ADC part
|
||||
### ADC channels
|
||||
|
||||
The PCF8591 has four 8 bit ADC channels. Values = 0..255.
|
||||
|
||||
- **void enableINCR()** used in analogRead4(); Could become private in the future.
|
||||
- **void enableINCR()** used in analogRead4(). Could become private in the future.
|
||||
- **void disableINCR()** idem.
|
||||
- **bool isINCREnabled()** idem.
|
||||
- **uint8_t analogRead(uint8_t channel, uint8_t mode = 0)** read one of the 4 analogue ports.
|
||||
@ -57,23 +57,38 @@ Uses **enableINCR()** to do that efficiently.
|
||||
It is about 2.6 x faster than 4 individual **analogRead()**, although the latter
|
||||
allows for optimized timing per channel.
|
||||
Only 4x single ports mode supported for now, comparator modi needs investigation.
|
||||
Returns **PCF8591_OK** or an error code.
|
||||
- **uint8_t lastRead(uint8_t channel)** get last read value from cache.
|
||||
This cache is filled both by **analogRead()** and **analogRead4()**. See example sketch.
|
||||
|
||||
|
||||
### DAC part
|
||||
### DAC channel
|
||||
|
||||
The PCF8591 has one 8 bit DAC. output value 0..255 == 0..Vref Volts (datasheet).
|
||||
|
||||
- **void enableDAC()** switch on the analogue output.
|
||||
- **void disableDAC()** switch off the analogue output (high impedance) Sort of energy saving mode.
|
||||
- **void disableDAC()** switch off the analogue output (high impedance). Sort of energy saving mode.
|
||||
- **bool isDACEnabled()** check the modus operandi.
|
||||
- **bool analogWrite(uint8_t value = 0)** writes a value 0..255 to the DAC. Check datasheet for voltage.
|
||||
Note, this is a real voltage not a PWM signal like **analogWrite()** on an UNO.
|
||||
- **uint8_t lastWrite()** get last written value from cache.
|
||||
|
||||
### Error codes
|
||||
|
||||
- **int lastError()** always check this value after a read / write to see if it was OK (== 0).
|
||||
After the read the error value is reset to OK.
|
||||
|
||||
To elaborate
|
||||
|
||||
| error code | Value | Notes |
|
||||
|:------------------------|:-------:|:--------|
|
||||
| PCF8591_OK | 0x00 |
|
||||
| PCF8591_PIN_ERROR | 0x81 |
|
||||
| PCF8591_I2C_ERROR | 0x82 |
|
||||
| PCF8591_MODE_ERROR | 0x83 |
|
||||
| PCF8591_CHANNEL_ERROR | 0x84 |
|
||||
| PCF8591_ADDRESS_ERROR | 0x85 |
|
||||
|
||||
|
||||
## Operations
|
||||
|
||||
@ -82,15 +97,21 @@ See examples.
|
||||
|
||||
## Future
|
||||
|
||||
#### must
|
||||
- improve documentation
|
||||
|
||||
#### should
|
||||
- add / improve comparator modi support, datasheet (par.8.2 figure 4)
|
||||
- int16_t readComparator10()
|
||||
- int16_t readComparator30() - return type correct?
|
||||
- int16_t readComparator31()
|
||||
- int16_t readComparator32()
|
||||
- set modi and read.
|
||||
|
||||
|
||||
#### could
|
||||
- **analogRead4()** needs investigation for the other modi.
|
||||
- Does it work?
|
||||
- Is it user understandable?
|
||||
- good example...
|
||||
- ...
|
||||
- good example
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/PCF8591.git"
|
||||
},
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=PCF8591
|
||||
version=0.1.2
|
||||
version=0.1.3
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=PCF8591 library for Arduino. Supports multiple I2C WireN bus.
|
||||
|
@ -36,7 +36,6 @@
|
||||
#include "PCF8591.h"
|
||||
|
||||
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "PCF8591_LIB_VERSION: %s\n", (char *) PCF8591_LIB_VERSION);
|
||||
@ -56,10 +55,6 @@ unittest(test_constants)
|
||||
assertEqual(PCF8591_MODE_ERROR , 0x83);
|
||||
assertEqual(PCF8591_CHANNEL_ERROR, 0x84);
|
||||
assertEqual(PCF8591_ADDRESS_ERROR, 0x85);
|
||||
|
||||
fprintf(stderr, "increment flags\n");
|
||||
assertEqual(PCF8591_DAC_FLAG , 0x40);
|
||||
assertEqual(PCF8591_INCR_FLAG , 0x04);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user