From 7a30666fc6493f9757e568f78f9f770118770e1a Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Wed, 3 Apr 2024 19:07:25 +0200 Subject: [PATCH] 0.2.0 HC4067 --- libraries/HC4067/.github/FUNDING.yml | 1 + .../HC4067/.github/workflows/arduino-lint.yml | 3 +- .../.github/workflows/arduino_test_runner.yml | 4 +- .../HC4067/.github/workflows/jsoncheck.yml | 5 +- libraries/HC4067/CHANGELOG.md | 11 +++ libraries/HC4067/HC4067.h | 18 +++-- libraries/HC4067/LICENSE | 2 +- libraries/HC4067/README.md | 70 ++++++++++++------- .../HC4067_16_buttons/HC4067_16_buttons.ino | 3 +- .../HC4067_4_channel_plotter.ino | 3 +- .../examples/HC4067_demo/HC4067_demo.ino | 6 +- .../HC4067_performance/HC4067_performance.ino | 24 +++++-- .../HC4067_performance/output_0.2.0.txt | 22 ++++++ .../examples/HC4067_test/HC4067_test.ino | 2 +- .../examples/HC4067_write/HC4067_write.ino | 50 +++++++++++++ libraries/HC4067/library.json | 2 +- libraries/HC4067/library.properties | 2 +- 17 files changed, 177 insertions(+), 51 deletions(-) create mode 100644 libraries/HC4067/examples/HC4067_performance/output_0.2.0.txt create mode 100644 libraries/HC4067/examples/HC4067_write/HC4067_write.ino diff --git a/libraries/HC4067/.github/FUNDING.yml b/libraries/HC4067/.github/FUNDING.yml index 90d9ab4c..6a2d6138 100644 --- a/libraries/HC4067/.github/FUNDING.yml +++ b/libraries/HC4067/.github/FUNDING.yml @@ -1,4 +1,5 @@ # These are supported funding model platforms github: RobTillaart +custom: "https://www.paypal.me/robtillaart" diff --git a/libraries/HC4067/.github/workflows/arduino-lint.yml b/libraries/HC4067/.github/workflows/arduino-lint.yml index 8a26f14a..70d8d1c3 100644 --- a/libraries/HC4067/.github/workflows/arduino-lint.yml +++ b/libraries/HC4067/.github/workflows/arduino-lint.yml @@ -5,8 +5,9 @@ on: [push, pull_request] jobs: lint: runs-on: ubuntu-latest + timeout-minutes: 5 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: arduino/arduino-lint-action@v1 with: library-manager: update diff --git a/libraries/HC4067/.github/workflows/arduino_test_runner.yml b/libraries/HC4067/.github/workflows/arduino_test_runner.yml index fadfa904..be74547e 100644 --- a/libraries/HC4067/.github/workflows/arduino_test_runner.yml +++ b/libraries/HC4067/.github/workflows/arduino_test_runner.yml @@ -6,12 +6,14 @@ on: [push, pull_request] jobs: runTest: runs-on: ubuntu-latest + timeout-minutes: 20 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: 2.6 - run: | + # sudo sysctl vm.mmap_rnd_bits=28 gem install arduino_ci arduino_ci.rb diff --git a/libraries/HC4067/.github/workflows/jsoncheck.yml b/libraries/HC4067/.github/workflows/jsoncheck.yml index 37a11298..fd05f5f8 100644 --- a/libraries/HC4067/.github/workflows/jsoncheck.yml +++ b/libraries/HC4067/.github/workflows/jsoncheck.yml @@ -9,10 +9,11 @@ on: jobs: test: runs-on: ubuntu-latest + timeout-minutes: 5 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: json-syntax-check - uses: limitusus/json-syntax-check@v1 + uses: limitusus/json-syntax-check@v2 with: pattern: "\\.json$" diff --git a/libraries/HC4067/CHANGELOG.md b/libraries/HC4067/CHANGELOG.md index 5c4fdba1..736c644c 100644 --- a/libraries/HC4067/CHANGELOG.md +++ b/libraries/HC4067/CHANGELOG.md @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.2.0] - 2024-04-03 +- fix ghost channels when using for OUTPUT + - add disable/enable in setChannel() +- improve setChannel(), only write changed pins. +- update HC4067_performance.ino +- update readme.md +- update GitHub actions +- minor edits + +---- + ## [0.1.2] - 2023-11-04 - update readme.md diff --git a/libraries/HC4067/HC4067.h b/libraries/HC4067/HC4067.h index 9f049d5c..f1ec384a 100644 --- a/libraries/HC4067/HC4067.h +++ b/libraries/HC4067/HC4067.h @@ -3,7 +3,7 @@ // FILE: HC4067.h // AUTHOR: Rob Tillaart // DATE: 2023-01-25 -// VERSION: 0.1.2 +// VERSION: 0.2.0 // PURPOSE: Arduino library for CD74HC4067 1 x 16 channel multiplexer and compatibles. // URL: https://github.com/RobTillaart/HC4067 @@ -11,7 +11,7 @@ #include "Arduino.h" -#define HC4067_LIB_VERSION (F("0.1.2")) +#define HC4067_LIB_VERSION (F("0.2.0")) class HC4067 @@ -42,16 +42,24 @@ public: void setChannel(uint8_t channel) { - if ((channel & 0x0F) != _channel) + uint8_t _new = channel & 0x0F; + if (_new != _channel) { - _channel = channel & 0x0F; + uint8_t _changed = _new ^ _channel; uint8_t mask = 0x08; uint8_t i = 3; + disable(); // prevent ghost channels. while (mask) { - digitalWrite(_pins[i--], (mask & _channel)); + // only write changed pins. // AVR only? + if (mask & _changed) + { + digitalWrite(_pins[i--], (mask & _new)); + } mask >>= 1; } + enable(); + _channel = _new; } } diff --git a/libraries/HC4067/LICENSE b/libraries/HC4067/LICENSE index 16ef1551..03053375 100644 --- a/libraries/HC4067/LICENSE +++ b/libraries/HC4067/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023-2023 Rob Tillaart +Copyright (c) 2023-2024 Rob Tillaart Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/libraries/HC4067/README.md b/libraries/HC4067/README.md index b79af15d..83059106 100644 --- a/libraries/HC4067/README.md +++ b/libraries/HC4067/README.md @@ -22,10 +22,14 @@ multiplexer / demultiplexer and compatible devices. The HC4067 allows e.g one analog port read up to 16 different analog channels, or one digital port to read the state of 16 buttons. +It is also possible to use the HC4067 to select an OUTPUT channel. +The signal pin can be connected to VCC (5V) or an IO pin set to OUTPUT. +Only the selected channel can show the HIGH level of the IO pin if set to HIGH. +Not selected pins will all be set to LOW. -The channel selection is done with four select lines **S0..S3** +The channel selection is done with four select lines **S0..S3**. -The device can be enabled/disabled by the enable line **E** +The device can be enabled/disabled by the enable line **E**. #### Compatibles @@ -40,14 +44,16 @@ Not tested, considered compatible. - https://github.com/RobTillaart/HC4052 (2x4 mux) - https://github.com/RobTillaart/HC4053 (3x2 mux) - https://github.com/RobTillaart/HC4067 (1x16 mux) +- https://github.com/RobTillaart/MAX14661 (2x16 mux, I2C) +- https://tronixstuff.com/2013/08/05/part-review-74hc4067-16-channel-analog-multiplexerdemultiplexer/ ## Hardware connection -Typical connection is to connect the four **select pins** to four IO Pins of your board. +Typical connection is to connect the four **select pins** to four IO pins of your board. -The optional **enablePin E** must be connected to GND if not used. -This way the device is continuous enabled. +The optional **enablePin E** must be connected to **GND** if not used. +This way the device will be continuous enabled. Example multiplexing analog in. @@ -76,6 +82,25 @@ Example multiplexing analog in. ``` +#### Less Select lines + +Note: the library does not meant for this mode, although it should work. +The GND-ed pins should be set to 255 (not tested). + +It is possible to use less IO pins to connect to the S0..S3. +The ones not connected to an IO pin must be connected to GND (preferred). + +| S0 | S1 | S2 | S3 | pins | notes | +|:-----:|:-----:|:-----:|:-----:|:------:|:-------:| +| IO | IO | IO | IO | 0-15 | default usage +| IO | IO | IO | GND | 0-7 | +| IO | IO | GND | GND | 0-3 | +| IO | GND | GND | GND | 0-1 | + +Of course it is possible to set a Select pin to VCC instead of GND. +This will result in another subset of the Y pins to select from. + + ## Interface ```cpp @@ -89,50 +114,41 @@ Set the 4 select pins and optional the enable pin. If the enablePin == 255 it is considered not used. - **void setChannel(uint8_t channel)** set the current channel. Valid values 0..15, this value is not checked, only the lower 4 bits will be used. -- **uint8_t getChannel()** get current channel 0..15. +- **uint8_t getChannel()** returns the current channel 0..15. +The selected channel is also returned when the multiplexer is disabled. #### Enable These functions work only if enablePin is set in the constructor. -- **void enable()** idem. -- **void disable()** idem. -- **bool isEnabled()** idem. -Also returns true if enablePin is not set. +- **void enable()** enables the HC4067 to multiplex. +- **void disable()** disables the HC4067, no channel is selected. +- **bool isEnabled()** returns the current status of the HC4067. +Also returns true if the enablePin is not set in the constructor. ## Future #### Must -- elaborate documentation - - links etc. - +- documentation #### Should -- optimizations - - performance setChannel -- investigate how to use with only 3 lines or 2 lines. - - set s3 / s2 to LOW always or so - +- test performance **setChannel()** on ESP32 / fast board. + - should optimized version be AVR only? #### Could - next() and prev() as channel selector. - - internal channel variable needed. -- move code to .cpp file -- investigate - - can it be used as 16 channel OUTPUT (yes but) - - is it buffered? - + - what to do with range borders? ==> stop? #### Won't (unless requested) -- optimizations - - only do digitalWrite when changed? gain is minimal. - - now takes 24 micros on UNO if set. +- check channel in setChannel() ? + - return true if in range, false otherwise. +- move code to .cpp file ## Support diff --git a/libraries/HC4067/examples/HC4067_16_buttons/HC4067_16_buttons.ino b/libraries/HC4067/examples/HC4067_16_buttons/HC4067_16_buttons.ino index b1f3f278..73aae8d3 100644 --- a/libraries/HC4067/examples/HC4067_16_buttons/HC4067_16_buttons.ino +++ b/libraries/HC4067/examples/HC4067_16_buttons/HC4067_16_buttons.ino @@ -7,11 +7,12 @@ #include "HC4067.h" -HC4067 mp(4, 5, 6, 7); +HC4067 mp(4, 5, 6, 7); // no enable pin defined connect to GND. const int inputPin = 10; uint16_t values; + void setup() { Serial.begin(115200); diff --git a/libraries/HC4067/examples/HC4067_4_channel_plotter/HC4067_4_channel_plotter.ino b/libraries/HC4067/examples/HC4067_4_channel_plotter/HC4067_4_channel_plotter.ino index 2ee7bf61..a93a2b3a 100644 --- a/libraries/HC4067/examples/HC4067_4_channel_plotter/HC4067_4_channel_plotter.ino +++ b/libraries/HC4067/examples/HC4067_4_channel_plotter/HC4067_4_channel_plotter.ino @@ -14,11 +14,12 @@ // 4..7 channel select pins // 8 = enable pin -HC4067 mp(4, 5, 6, 7, 8); +HC4067 mp(4, 5, 6, 7, 8); uint32_t lastTime = 0; + void setup() { Serial.begin(115200); diff --git a/libraries/HC4067/examples/HC4067_demo/HC4067_demo.ino b/libraries/HC4067/examples/HC4067_demo/HC4067_demo.ino index 2993223a..e19b73b6 100644 --- a/libraries/HC4067/examples/HC4067_demo/HC4067_demo.ino +++ b/libraries/HC4067/examples/HC4067_demo/HC4067_demo.ino @@ -6,7 +6,7 @@ #include "HC4067.h" -HC4067 mp(4, 5, 6, 7); +HC4067 mp(4, 5, 6, 7); // no enable pin defined connect to GND. void setup() @@ -26,9 +26,11 @@ void loop() for (uint8_t channel = 0; channel < 16; channel++) { mp.setChannel(channel); - Serial.println(analogRead(A0)); + Serial.print(analogRead(A0)); + Serial.print("\t"); delay(100); } + Serial.println(); } diff --git a/libraries/HC4067/examples/HC4067_performance/HC4067_performance.ino b/libraries/HC4067/examples/HC4067_performance/HC4067_performance.ino index 2ce280b6..d219f6fb 100644 --- a/libraries/HC4067/examples/HC4067_performance/HC4067_performance.ino +++ b/libraries/HC4067/examples/HC4067_performance/HC4067_performance.ino @@ -6,10 +6,11 @@ #include "HC4067.h" -HC4067 mp(4, 5, 6, 7); +HC4067 mp(4, 5, 6, 7, 8); uint32_t start, stop; + void setup() { Serial.begin(115200); @@ -17,21 +18,30 @@ void setup() Serial.print("HC4067 LIBRARY VERSION: "); Serial.println(HC4067_LIB_VERSION); Serial.println(); + delay(100); - delay(1000); - start = micros(); mp.setChannel(10); - stop = micros(); + + for (uint8_t ch = 0; ch < 16; ch ++) + { + start = micros(); + mp.setChannel(ch); + stop = micros(); + Serial.print("Channel "); + Serial.print(ch); + Serial.print(": \t"); + Serial.println(stop - start); + delay(100); + } - Serial.print("SetChannel: \t"); - Serial.println(stop - start); + mp.setChannel(10); delay(100); start = micros(); mp.setChannel(10); stop = micros(); - Serial.print("SetChannel: \t"); + Serial.print("\nSetChannel: \t"); Serial.println(stop - start); delay(100); diff --git a/libraries/HC4067/examples/HC4067_performance/output_0.2.0.txt b/libraries/HC4067/examples/HC4067_performance/output_0.2.0.txt new file mode 100644 index 00000000..c14181d3 --- /dev/null +++ b/libraries/HC4067/examples/HC4067_performance/output_0.2.0.txt @@ -0,0 +1,22 @@ + +HC4067_performance.ino +HC4067 LIBRARY VERSION: 0.2.0 + +Channel 0: 24 +Channel 1: 20 +Channel 2: 28 +Channel 3: 20 +Channel 4: 32 +Channel 5: 20 +Channel 6: 24 +Channel 7: 24 +Channel 8: 36 +Channel 9: 20 +Channel 10: 24 +Channel 11: 20 +Channel 12: 32 +Channel 13: 24 +Channel 14: 24 +Channel 15: 20 + +SetChannel: 8 diff --git a/libraries/HC4067/examples/HC4067_test/HC4067_test.ino b/libraries/HC4067/examples/HC4067_test/HC4067_test.ino index 50d25e7a..89c54392 100644 --- a/libraries/HC4067/examples/HC4067_test/HC4067_test.ino +++ b/libraries/HC4067/examples/HC4067_test/HC4067_test.ino @@ -6,7 +6,7 @@ #include "HC4067.h" -HC4067 mp(4, 5, 6, 7, 8); // explicitly set enable pin +HC4067 mp(4, 5, 6, 7, 8); // explicitly set enable pin (8) void setup() diff --git a/libraries/HC4067/examples/HC4067_write/HC4067_write.ino b/libraries/HC4067/examples/HC4067_write/HC4067_write.ino new file mode 100644 index 00000000..da4d036a --- /dev/null +++ b/libraries/HC4067/examples/HC4067_write/HC4067_write.ino @@ -0,0 +1,50 @@ +// +// FILE: HC4067_write.ino +// AUTHOR: Rob Tillaart +// PURPOSE: Demo for HC4067 16 channel (simple) multiplexer + + +#include "HC4067.h" + +HC4067 mp(4, 5, 6, 7, 3); // explicitly set enable pin (3) + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("HC4067 LIBRARY VERSION: "); + Serial.println(HC4067_LIB_VERSION); + Serial.println(); + + delay(1000); + + pinMode(8, OUTPUT); + digitalWrite(8, HIGH); + + Serial.println(mp.isEnabled()); + mp.enable(); + Serial.println(mp.isEnabled()); + mp.disable(); + Serial.println(mp.isEnabled()); + mp.enable(); + delay(1000); +} + + +void loop() +{ + // rotate over the channels + for (uint8_t channel = 0; channel < 16; channel++) + { + mp.setChannel(channel); + Serial.println(channel); + delay(250); + } + + // Toggle the OUTPUT + digitalWrite(8, digitalRead(8)); +} + + +// -- END OF FILE -- diff --git a/libraries/HC4067/library.json b/libraries/HC4067/library.json index 6daedb3a..22c5db21 100644 --- a/libraries/HC4067/library.json +++ b/libraries/HC4067/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/HC4067.git" }, - "version": "0.1.2", + "version": "0.2.0", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/libraries/HC4067/library.properties b/libraries/HC4067/library.properties index 5a660f73..33c1b1c5 100644 --- a/libraries/HC4067/library.properties +++ b/libraries/HC4067/library.properties @@ -1,5 +1,5 @@ name=HC4067 -version=0.1.2 +version=0.2.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for a HC4067 1 x 16 channel multiplexer