Skip to content

Commit

Permalink
update setChannel()** (#3)
Browse files Browse the repository at this point in the history
- change return type of **bool setChannel()**
- verify the channel parameter of **bool setChannel()**
- add parameter to **bool setChannel(channel, disable = true)**
- correct the channel math in setChannel()
- update readme.md
  • Loading branch information
RobTillaart authored May 28, 2024
1 parent dadb727 commit 5f7ee5a
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.2.1] - 2024-05-28
- change return type of **bool setChannel()**
- verify the channel parameter of **bool setChannel()**
- add parameter to **bool setChannel(channel, disable = true)**
- correct the channel math in setChannel()
- update readme.md

## [0.2.0] - 2024-04-03
- fix ghost channels when using for OUTPUT
- add disable/enable in setChannel()
Expand Down
15 changes: 10 additions & 5 deletions HC4051.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// FILE: HC4051.h
// AUTHOR: Rob Tillaart
// DATE: 2023-01-25
// VERSION: 0.2.0
// VERSION: 0.2.1
// PURPOSE: Arduino library for CD74HC4051 1 x 8 channel multiplexer and compatibles.
// URL: https://github.com/RobTillaart/HC4051



#include "Arduino.h"

#define HC4051_LIB_VERSION (F("0.2.0"))
#define HC4051_LIB_VERSION (F("0.2.1"))


class HC4051
Expand Down Expand Up @@ -39,15 +39,19 @@ class HC4051
}


void setChannel(uint8_t channel)
bool setChannel(uint8_t channel, bool disable = true)
{
uint8_t _new = channel & 0x0F;
if (channel > 7) return false;
uint8_t _new = channel;
if (_new != _channel)
{
uint8_t _changed = _new ^ _channel;
uint8_t mask = 0x04;
uint8_t i = 2;
disable(); // prevent ghost channels.
if (disable)
{
this->disable(); // prevent ghost channels.
}
while (mask)
{
// only write changed pins. // AVR only?
Expand All @@ -60,6 +64,7 @@ class HC4051
enable();
_channel = _new;
}
return true;
}


Expand Down
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ multiplexer / demultiplexer and compatible devices.
The HC4051 allows e.g one analog port read up to 8 different analog channels,
or one digital port to read the state of 8 buttons.

It is also possible to use the HC4067 to select an OUTPUT channel.
It is also possible to use the HC4051 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.
Expand Down Expand Up @@ -51,7 +51,7 @@ To elaborate.

Typical connection is to connect the three **select pins** to four IO pins of your board.

The optional **enablePin (INH)** must be connected to GND if not used.
The optional **enablePin(INH)** must be connected to GND if not used.
This way the device will be continuous enabled.

Example multiplexing analog in.
Expand Down Expand Up @@ -104,19 +104,27 @@ This will result in another subset of the Y pins to select from.
- **HC4051(uint8_t A, uint8_t B, uint8_t C, uint8_t enablePin = 255)** constructor.
Set the three 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..7, this value is not checked, only the lower 3 bits will be used.
- **bool setChannel(uint8_t channel, bool disable = true)** set the current channel.
Valid values 0..7, this value is checked (since 0.2.1).
Returns false if channel out of range.
If the channel is already selected it does not change it.
Note the three channels will not change at the very same moment,
possibly resulting in an invalid selection for a (very short) time.
The disable flag can be set to false so the device is not disabled during channel switching.
Default the device is disabled during channel switching to prevent (very short) ghost channels.
Note that a call to **setChannel()** will always enable the device again.
Note the device cannot be disabled if there is no enable pin configured.
- **uint8_t getChannel()** returns the current channel 0..7.
The selected channel is also returned when the multiplexer is disabled.


#### Enable

These functions work only if enablePin is set in the constructor.
These functions work only if a valid **enablePin** is set in the constructor.

- **void enable()** enables the HC4051 to multiplex.
- **void disable()** disables the HC4051, no channel is selected.
- **bool isEnabled()** returns the current status of the HC4067.
- **void enable()** enables the device to multiplex.
- **void disable()** disables the device, no channel is selected.
- **bool isEnabled()** returns the current status of the device.
Also returns true if the enablePin is not set in the constructor.


Expand Down
1 change: 1 addition & 0 deletions examples/HC4051_demo/HC4051_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// FILE: HC4051_demo.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for HC4051 8 channel (simple) multiplexer
// URL: https://github.com/RobTillaart/HC4051


#include "HC4051.h"
Expand Down
40 changes: 40 additions & 0 deletions examples/HC4051_ghost_channels/HC4051_ghost_channels.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// FILE: HC4051_ghost_channels.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for HC4051 8 channel (simple) multiplexer
// showing (very short) ghost channels.
// you might need a scope to see this.
// URL: https://github.com/RobTillaart/HC4051


#include "HC4051.h"

HC4051 mp(4, 5, 6);


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("HC4051 LIBRARY VERSION: ");
Serial.println(HC4051_LIB_VERSION);
Serial.println();

delay(1000);
}


void loop()
{
for (uint8_t channel = 0; channel < 8; channel++)
{
// do not disable the device during setChannel.
mp.setChannel(channel, false);
Serial.println(analogRead(A0));
delay(100);
}
Serial.println();
}


// -- END OF FILE --
1 change: 1 addition & 0 deletions examples/HC4051_performance/HC4051_performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// FILE: HC4051_performance.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Demo for HC4051 8 channel (simple) multiplexer
// URL: https://github.com/RobTillaart/HC4051


#include "HC4051.h"
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ HC4051 KEYWORD1
# Methods and Functions (KEYWORD2)
setChannel KEYWORD2
getChannel KEYWORD2

enable KEYWORD2
disable KEYWORD2
isEnabled KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/HC4051.git"
},
"version": "0.2.0",
"version": "0.2.1",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=HC4051
version=0.2.0
version=0.2.1
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for a HC4051 1x8 channel multiplexer
Expand Down

0 comments on commit 5f7ee5a

Please sign in to comment.