0.2.1 HC4051

This commit is contained in:
Rob Tillaart 2024-05-28 21:35:11 +02:00
parent 7e8de04ca1
commit b8f33abe9d
9 changed files with 78 additions and 15 deletions

View File

@ -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/). 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 ## [0.2.0] - 2024-04-03
- fix ghost channels when using for OUTPUT - fix ghost channels when using for OUTPUT
- add disable/enable in setChannel() - add disable/enable in setChannel()

View File

@ -3,7 +3,7 @@
// FILE: HC4051.h // FILE: HC4051.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// DATE: 2023-01-25 // DATE: 2023-01-25
// VERSION: 0.2.0 // VERSION: 0.2.1
// PURPOSE: Arduino library for CD74HC4051 1 x 8 channel multiplexer and compatibles. // PURPOSE: Arduino library for CD74HC4051 1 x 8 channel multiplexer and compatibles.
// URL: https://github.com/RobTillaart/HC4051 // URL: https://github.com/RobTillaart/HC4051
@ -11,7 +11,7 @@
#include "Arduino.h" #include "Arduino.h"
#define HC4051_LIB_VERSION (F("0.2.0")) #define HC4051_LIB_VERSION (F("0.2.1"))
class HC4051 class HC4051
@ -39,15 +39,19 @@ public:
} }
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) if (_new != _channel)
{ {
uint8_t _changed = _new ^ _channel; uint8_t _changed = _new ^ _channel;
uint8_t mask = 0x04; uint8_t mask = 0x04;
uint8_t i = 2; uint8_t i = 2;
disable(); // prevent ghost channels. if (disable)
{
this->disable(); // prevent ghost channels.
}
while (mask) while (mask)
{ {
// only write changed pins. // AVR only? // only write changed pins. // AVR only?
@ -60,6 +64,7 @@ public:
enable(); enable();
_channel = _new; _channel = _new;
} }
return true;
} }

View File

@ -22,7 +22,7 @@ multiplexer / demultiplexer and compatible devices.
The HC4051 allows e.g one analog port read up to 8 different analog channels, 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. 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. 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. 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. Not selected pins will all be set to LOW.
@ -51,7 +51,7 @@ To elaborate.
Typical connection is to connect the three **select pins** to four IO pins of your board. 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. This way the device will be continuous enabled.
Example multiplexing analog in. Example multiplexing analog in.
@ -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. - **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. Set the three select pins and optional the enable pin.
If the enablePin == 255 it is considered not used. If the enablePin == 255 it is considered not used.
- **void setChannel(uint8_t channel)** set the current channel. - **bool setChannel(uint8_t channel, bool disable = true)** set the current channel.
Valid values 0..7, this value is not checked, only the lower 3 bits will be used. 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. - **uint8_t getChannel()** returns the current channel 0..7.
The selected channel is also returned when the multiplexer is disabled. The selected channel is also returned when the multiplexer is disabled.
#### Enable #### 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 enable()** enables the device to multiplex.
- **void disable()** disables the HC4051, no channel is selected. - **void disable()** disables the device, no channel is selected.
- **bool isEnabled()** returns the current status of the HC4067. - **bool isEnabled()** returns the current status of the device.
Also returns true if the enablePin is not set in the constructor. Also returns true if the enablePin is not set in the constructor.

View File

@ -2,6 +2,7 @@
// FILE: HC4051_demo.ino // FILE: HC4051_demo.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: Demo for HC4051 8 channel (simple) multiplexer // PURPOSE: Demo for HC4051 8 channel (simple) multiplexer
// URL: https://github.com/RobTillaart/HC4051
#include "HC4051.h" #include "HC4051.h"

View File

@ -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 --

View File

@ -2,6 +2,7 @@
// FILE: HC4051_performance.ino // FILE: HC4051_performance.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// PURPOSE: Demo for HC4051 8 channel (simple) multiplexer // PURPOSE: Demo for HC4051 8 channel (simple) multiplexer
// URL: https://github.com/RobTillaart/HC4051
#include "HC4051.h" #include "HC4051.h"

View File

@ -6,6 +6,7 @@ HC4051 KEYWORD1
# Methods and Functions (KEYWORD2) # Methods and Functions (KEYWORD2)
setChannel KEYWORD2 setChannel KEYWORD2
getChannel KEYWORD2 getChannel KEYWORD2
enable KEYWORD2 enable KEYWORD2
disable KEYWORD2 disable KEYWORD2
isEnabled KEYWORD2 isEnabled KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/HC4051.git" "url": "https://github.com/RobTillaart/HC4051.git"
}, },
"version": "0.2.0", "version": "0.2.1",
"license": "MIT", "license": "MIT",
"frameworks": "*", "frameworks": "*",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=HC4051 name=HC4051
version=0.2.0 version=0.2.1
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=Arduino library for a HC4051 1x8 channel multiplexer sentence=Arduino library for a HC4051 1x8 channel multiplexer