0.1.2 PCF8591

This commit is contained in:
rob tillaart 2021-12-23 14:26:13 +01:00
parent a78374791f
commit cdefd56437
13 changed files with 125 additions and 65 deletions

View File

@ -2,6 +2,10 @@ compile:
# Choosing to run compilation tests on 2 different Arduino platforms # Choosing to run compilation tests on 2 different Arduino platforms
platforms: platforms:
- uno - uno
- leonardo # - due
- due # - zero
- zero # - leonardo
- m4
- esp32
# - esp8266
# - mega2560

View File

@ -4,10 +4,14 @@ name: Arduino CI
on: [push, pull_request] on: [push, pull_request]
jobs: jobs:
arduino_ci: runTest:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: Arduino-CI/action@master - uses: ruby/setup-ruby@v1
# Arduino-CI/action@v0.1.1 with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2020-2021 Rob Tillaart Copyright (c) 2020-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -2,15 +2,16 @@
// FILE: PCF8591.cpp // FILE: PCF8591.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// DATE: 2020-03-12 // DATE: 2020-03-12
// VERSION: 0.1.1 // VERSION: 0.1.2
// PURPOSE: I2C PCF8591 library for Arduino // PURPOSE: I2C PCF8591 library for Arduino
// URL: https://github.com/RobTillaart/PCF8591 // URL: https://github.com/RobTillaart/PCF8591
// //
// HISTORY: // HISTORY:
// 0.0.1 2020-03-12 initial version // 0.0.1 2020-03-12 initial version
// 0.0.2 2020-07-22 testing, refactor, documentation and examples // 0.0.2 2020-07-22 testing, refactor, documentation and examples
// 0.1.0 2021-01-04 arduino-CI // 0.1.0 2021-01-04 Arduino-CI
// 0.1.1 2021-01-14 added WireN + improve error handling. // 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" #include "PCF8591.h"
@ -178,5 +179,5 @@ int PCF8591::lastError()
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -3,7 +3,7 @@
// FILE: PCF8591.h // FILE: PCF8591.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// DATE: 2020-03-12 // DATE: 2020-03-12
// VERSION: 0.1.1 // VERSION: 0.1.2
// PURPOSE: I2C PCF8591 library for Arduino // PURPOSE: I2C PCF8591 library for Arduino
// URL: https://github.com/RobTillaart/PCF8591 // URL: https://github.com/RobTillaart/PCF8591
// //
@ -16,19 +16,20 @@
#include "Wire.h" #include "Wire.h"
#define PCF8591_LIB_VERSION (F("0.1.1")) #define PCF8591_LIB_VERSION (F("0.1.2"))
#define PCF8591_OK 0x00 #define PCF8591_OK 0x00
#define PCF8591_PIN_ERROR 0x81 #define PCF8591_PIN_ERROR 0x81
#define PCF8591_I2C_ERROR 0x82 #define PCF8591_I2C_ERROR 0x82
#define PCF8591_MODE_ERROR 0x83 #define PCF8591_MODE_ERROR 0x83
#define PCF8591_CHANNEL_ERROR 0x84 #define PCF8591_CHANNEL_ERROR 0x84
#define PCF8591_ADDRESS_ERROR 0x85 #define PCF8591_ADDRESS_ERROR 0x85
// INTERNAL USE ONLY // INTERNAL USE ONLY
#define PCF8591_DAC_FLAG 0x40 #define PCF8591_DAC_FLAG 0x40
#define PCF8591_INCR_FLAG 0x04 #define PCF8591_INCR_FLAG 0x04
class PCF8591 class PCF8591
{ {
@ -36,9 +37,9 @@ public:
explicit PCF8591(const uint8_t address = 0x48, TwoWire *wire = &Wire); explicit PCF8591(const uint8_t address = 0x48, TwoWire *wire = &Wire);
#if defined (ESP8266) || defined(ESP32) #if defined (ESP8266) || defined(ESP32)
bool begin(uint8_t sda, uint8_t scl, uint8_t val = 0); bool begin(uint8_t sda, uint8_t scl, uint8_t value = 0);
#endif #endif
bool begin(uint8_t val = 0); bool begin(uint8_t value = 0);
bool isConnected(); bool isConnected();
@ -62,14 +63,17 @@ public:
int lastError(); int lastError();
private: private:
uint8_t _address; uint8_t _address;
uint8_t _control; uint8_t _control;
uint8_t _dac; uint8_t _dac;
uint8_t _adc[4]; uint8_t _adc[4];
int _error; int _error;
TwoWire* _wire; TwoWire* _wire;
}; };
// END OF FILE
// -- END OF FILE --

View File

@ -1,22 +1,24 @@
[![Arduino CI](https://github.com/RobTillaart/PCF8591/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) [![Arduino CI](https://github.com/RobTillaart/PCF8591/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/PCF8591/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PCF8591/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/PCF8591/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PCF8591/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PCF8591/blob/master/LICENSE) [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PCF8591/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/PCF8591.svg?maxAge=3600)](https://github.com/RobTillaart/PCF8591/releases) [![GitHub release](https://img.shields.io/github/release/RobTillaart/PCF8591.svg?maxAge=3600)](https://github.com/RobTillaart/PCF8591/releases)
# PCF8591 # PCF8591
Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC Arduino Library for PCF8591 I2C 4 channel 8 bit ADC + 1 channel 8 bit DAC.
## Description ## Description
**warning** during tests I could overclock the chip at 650 kHz but it is only specified **warning** during tests I could overclock the chip up to 650 KHz but it is only specified
to run at 100 kHz. After getting pretty hot it broke down. to run at 100 kHz. After getting pretty hot 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. 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 At 100 KHz one gets \> 2000 reads per second for **analogRead()** and
\> 2000 writes per second for **analogWrite()**. \> 2000 writes per second for **analogWrite()**.
Note that most time is probably spend on I2C communication. Note that most time is probably spend on I2C communication.
@ -32,52 +34,61 @@ First tests shows it is 2.6 x faster than 4 individual reads.
### Generic ### Generic
- **PCF8591(const uint8_t address, TwoWire \*wire = &Wire)** constructor with I2C address, default is 0x48, optional set the WireN I2C bus. - **PCF8591(const uint8_t address, TwoWire \*wire = &Wire)** constructor with I2C address,
- **begin(uint8_t sda, uint8_t scl, uint8_t val = 0)** set wire pins for ESP series. 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. Returns **true** if successful. Also set initial value for the DAC. Returns **true** if successful.
- **begin(uint8_t val = 0)** Set initial value for the DAC. Returns **true** if successful. - **bool begin(uint8_t value = 0)** Set initial value for the DAC. Returns **true** if successful.
- **isConnected()** test to see if chip can be reached. - **bool isConnected()** test to see if chip can be reached.
### ADC part ### ADC part
The PCF8591 has four 8 bit ADC channels. Values = 0..255 The PCF8591 has four 8 bit ADC channels. Values = 0..255
- **enableINCR()** used in analogRead4(); Could become private in the future. - **void enableINCR()** used in analogRead4(); Could become private in the future.
- **disableINCR()** idem. - **void disableINCR()** idem.
- **isINCREnabled()** idem. - **bool isINCREnabled()** idem.
- **analogRead(uint8_t channel, uint8_t mode = 0)** read one of the 4 analog ports. - **uint8_t analogRead(uint8_t channel, uint8_t mode = 0)** read one of the 4 analogue ports.
Default mode is single ports. For comparator modes test datasheet. Default mode is single ports. For comparator modes test datasheet.
- **analogRead4()** read all 4 channels in one call. - **uint8_t analogRead4()** read all 4 channels in one call.
Uses **enableINCR()** to do that efficiently. Uses **enableINCR()** to do that efficiently.
It is about 2.6 x faster than 4 individual **analogRead()**, although the latter It is about 2.6 x faster than 4 individual **analogRead()**, although the latter
allows for optimized timing per channel. allows for optimized timing per channel.
Only 4x single ports mode supported for now, comparator modi needs investigation. Only 4x single ports mode supported for now, comparator modi needs investigation.
- **lastRead(uint8_t channel)** get last read value from cache. - **uint8_t lastRead(uint8_t channel)** get last read value from cache.
This cache is filled both by **analogRead()** and **analogRead4()**. See example sketch. This cache is filled both by **analogRead()** and **analogRead4()**. See example sketch.
### DAC part ### DAC part
The PCF8591 has one 8 bit DAC. Value 0..255 The PCF8591 has one 8 bit DAC. output value 0..255 == 0..Vref Volts (datasheet)
- **enableDAC()** switch on the analog output - **void enableDAC()** switch on the analogue output.
- **disableDAC()** switch off the analog output (high impedance) Sort of energy saving mode. - **void disableDAC()** switch off the analogue output (high impedance) Sort of energy saving mode.
- **isDACEnabled()** check the modus operandi. - **bool isDACEnabled()** check the modus operandi.
- **analogWrite(uint8_t value = 0)** writes a value 0..255 to the DAC. Check datasheet for voltage. - **bool analogWrite(uint8_t value = 0)** writes a value 0..255 to the DAC. Check datasheet for voltage.
- **lastWrite()** get last written value from cache. - **uint8_t lastWrite()** get last written value from cache.
- **lastError()** always check this value after a read / write to see if it was OK (== 0). - **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 After the read the error value is reset to OK.
## Future
- Improve comparator modi support, how to do that?
dedicated functions? return type int8_t?
- **analogRead4()** needs investigation for the other modi. Does it work?
Is it user understandable?
## Operations ## Operations
See examples. See examples.
## Future
- 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.
- **analogRead4()** needs investigation for the other modi.
- Does it work?
- Is it user understandable?
- good example...
- ...

View File

@ -1,15 +1,16 @@
// //
// FILE: PCF8591_demo.ino // FILE: PCF8591_demo.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo // PURPOSE: demo
// DATE: 2020-07-22 // DATE: 2020-07-22
// URL: https://github.com/RobTillaart/PCF8591 // URL: https://github.com/RobTillaart/PCF8591
#include "PCF8591.h" #include "PCF8591.h"
PCF8591 dev(0x48); PCF8591 dev(0x48);
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@ -24,6 +25,7 @@ void setup()
dev.begin(); dev.begin();
} }
void loop() void loop()
{ {
test_DAC(); test_DAC();
@ -57,6 +59,7 @@ void test_DAC()
Serial.println(); Serial.println();
} }
void test_ADC_mode(uint8_t mode) 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
@ -75,4 +78,6 @@ void test_ADC_mode(uint8_t mode)
Serial.println(); Serial.println();
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -1,17 +1,18 @@
// //
// FILE: PCF8591_incremental_read.ino // FILE: PCF8591_incremental_read.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo // PURPOSE: demo
// DATE: 2020-07-22 // DATE: 2020-07-22
// URL: https://github.com/RobTillaart/PCF8591 // URL: https://github.com/RobTillaart/PCF8591
#include "PCF8591.h" #include "PCF8591.h"
PCF8591 dev(0x48); PCF8591 dev(0x48);
uint32_t start, stop, dura1, dura2; uint32_t start, stop, dura1, dura2;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@ -76,8 +77,11 @@ void setup()
Serial.println("\nDone...\n"); Serial.println("\nDone...\n");
} }
void loop() void loop()
{ {
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -1,20 +1,22 @@
// //
// FILE: PCF8591_demo.ino // FILE: PCF8591_demo.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo // PURPOSE: demo
// DATE: 2020-07-22 // DATE: 2020-07-22
// URL: https://github.com/RobTillaart/PCF8591 // URL: https://github.com/RobTillaart/PCF8591
//
// NOTE: output is written in markdown format of a table // NOTE: output is written in markdown format of a table
// (so oeasy to include in the readme.md ;)
// can be changed to tab based or comma based output // can be changed to tab based or comma based output
#include "PCF8591.h" #include "PCF8591.h"
PCF8591 dev(0x48); PCF8591 dev(0x48);
uint32_t start, stop; uint32_t start, stop;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@ -39,10 +41,12 @@ void setup()
test2(); test2();
} }
void loop() void loop()
{ {
} }
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
void test1() void test1()
@ -64,6 +68,7 @@ void test1()
Serial.println(); Serial.println();
} }
void test2() void test2()
{ {
Serial.println(F("| - Wire clock KHz - | - analogWrite() OK% - | - analogRead() OK% - |")); Serial.println(F("| - Wire clock KHz - | - analogWrite() OK% - | - analogRead() OK% - |"));
@ -83,6 +88,7 @@ void test2()
Serial.println(); Serial.println();
} }
void test_DAC() void test_DAC()
{ {
dev.enableDAC(); dev.enableDAC();
@ -98,6 +104,7 @@ void test_DAC()
Serial.print((stop - start) * 0.001); Serial.print((stop - start) * 0.001);
} }
void test_ADC() void test_ADC()
{ {
volatile uint8_t x = 0; volatile uint8_t x = 0;
@ -111,6 +118,7 @@ void test_ADC()
Serial.print((stop - start) * 0.001); Serial.print((stop - start) * 0.001);
} }
void test_DAC_error() void test_DAC_error()
{ {
float perc = 0; float perc = 0;
@ -126,6 +134,7 @@ void test_DAC_error()
Serial.print(perc); Serial.print(perc);
} }
void test_ADC_error() void test_ADC_error()
{ {
float perc = 0; float perc = 0;
@ -139,4 +148,6 @@ void test_ADC_error()
Serial.print(perc); Serial.print(perc);
} }
// -- END OF FILE -- // -- END OF FILE --

View File

@ -1,6 +1,6 @@
# Syntax Coloring Map For PCF8591 # Syntax Colouring Map For PCF8591
# Datatypes (KEYWORD1) # Data types (KEYWORD1)
PCF8591 KEYWORD1 PCF8591 KEYWORD1
# Methods and Functions (KEYWORD2) # Methods and Functions (KEYWORD2)

View File

@ -15,8 +15,9 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/PCF8591.git" "url": "https://github.com/RobTillaart/PCF8591.git"
}, },
"version": "0.1.1", "version": "0.1.2",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "*" "platforms": "*",
"headers": "PCF8591.h"
} }

View File

@ -1,5 +1,5 @@
name=PCF8591 name=PCF8591
version=0.1.1 version=0.1.2
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=PCF8591 library for Arduino. Supports multiple I2C WireN bus. sentence=PCF8591 library for Arduino. Supports multiple I2C WireN bus.

View File

@ -39,17 +39,32 @@
unittest_setup() unittest_setup()
{ {
fprintf(stderr, "PCF8591_LIB_VERSION: %s\n", (char *) PCF8591_LIB_VERSION);
} }
unittest_teardown() unittest_teardown()
{ {
} }
unittest(test_constants)
{
assertEqual(PCF8591_OK , 0x00);
assertEqual(PCF8591_PIN_ERROR , 0x81);
assertEqual(PCF8591_I2C_ERROR , 0x82);
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);
}
unittest(test_constructor) unittest(test_constructor)
{ {
fprintf(stderr, "VERSION: %s\n", PCF8591_LIB_VERSION);
PCF8591 dev(0x48); PCF8591 dev(0x48);
assertTrue(dev.begin()); assertTrue(dev.begin());