diff --git a/AslAnalogMultiplexer/LICENSE.md b/AslAnalogMultiplexer/LICENSE.md new file mode 100644 index 0000000..0b11359 --- /dev/null +++ b/AslAnalogMultiplexer/LICENSE.md @@ -0,0 +1,23 @@ +Library for the CMOS 40xx analog multiplexer series. + +The MIT License (MIT) + +Copyright (c) 2017 Julian Sanin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/AslAnalogMultiplexer/README.md b/AslAnalogMultiplexer/README.md new file mode 100644 index 0000000..97f6532 --- /dev/null +++ b/AslAnalogMultiplexer/README.md @@ -0,0 +1,35 @@ +# CMOS 40xx Analog Multiplexer Series Library +A library to interface with analog multiplexers. Supports 4051, 4052, 4053, and +4067 series multiplexers. + +## How to use +``` +#include + +enum { + PIN_EN = 2, // The enable pin of the multiplexer or NOT_A_PIN. + PIN_S0 = 3, // Channel selector pin 0. + PIN_S1 = 4, // Channel selector pin 1. + PIN_S2 = 5, // Channel selector pin 2. + PIN_IO = A0 // Use here a pin that can work in analog & digital modes. +}; + +using namespace asl; + +AMxx4051 mux{ PIN_EN, PIN_S0, PIN_S1, PIN_S2 }; // 8:1 Multiplexer. + +void setup() { + Serial.begin(9600); + while (!Serial); +} + +void loop() { + mux.pinMode(PIN_IO, INPUT); // PIN_IO will be used to read from the mux. + mux.enable(); // Enable multiplexer. + for (int channel = 0; channel < 8; channel++) { + int value = mux.analogRead(channel); // Do analog read. + Serial.println(value); + } + Serial.println(); +} +``` diff --git a/AslAnalogMultiplexer/examples/AnalogMultiplexerExample/AnalogMultiplexerExample.ino b/AslAnalogMultiplexer/examples/AnalogMultiplexerExample/AnalogMultiplexerExample.ino new file mode 100644 index 0000000..f35f134 --- /dev/null +++ b/AslAnalogMultiplexer/examples/AnalogMultiplexerExample/AnalogMultiplexerExample.ino @@ -0,0 +1,50 @@ +#include + +// Selected Arduino pins. +enum { + PIN_EN = 2, // The enable pin of the multiplexer or NOT_A_PIN. + PIN_S0 = 3, // Channel selector pin 0. + PIN_S1 = 4, // Channel selector pin 1. + PIN_S2 = 5, // Channel selector pin 2. + PIN_S3 = 6, // Channel selector pin 3. + PIN_IO = A0 // Use here a pin that can work in analog & digital modes. +}; + +using namespace asl; + +AMxx4067 mux{ PIN_EN, PIN_S0, PIN_S1, PIN_S2, PIN_S3 }; // 16:1 Multiplexer. +// Other possible types are: +//AMxx4051 mux{ PIN_EN, PIN_S0, PIN_S1, PIN_S2 }; // 8:1 Multiplexer. +//AMxx4052 mux{ PIN_EN, PIN_S0, PIN_S1 }; // Dual 4:1 Multiplexer. +//AMxx4053 mux{ PIN_EN, PIN_S0 }; // Triple 2:1 Multiplexer. + +void setup() { + Serial.begin(9600); + while (!Serial); + + mux.pinMode(PIN_IO, INPUT); // PIN_IO will be used to read from the mux. + mux.enable(); // Enable multiplexer. + for (int channel = 0; channel < 16; channel++) { + int value = mux.analogRead(channel); // Do analog read. + Serial.println(value); + } + Serial.println(); + + mux.pinMode(PIN_IO, INPUT_PULLUP); // Enable weak pull up resistor. + for (int channel = 0; channel < 16; channel++) { + int value = mux.digitalRead(channel); // Do digital read. + Serial.println(value); + } + Serial.println(); + + mux.pinMode(PIN_IO, OUTPUT); // Switch to write mode. + for (int channel = 0; channel < 16; channel++) { + int value = channel % 2; + mux.digitalWrite(channel, value); // Do digital write (multiplexed). + Serial.println(value); + } + Serial.println(); + mux.disable(); // Switch off multiplexer. +} + +void loop() { } diff --git a/AslAnalogMultiplexer/keywords.txt b/AslAnalogMultiplexer/keywords.txt new file mode 100644 index 0000000..d19998a --- /dev/null +++ b/AslAnalogMultiplexer/keywords.txt @@ -0,0 +1,26 @@ +####################################### +# Syntax Coloring Map for +# AnalogMultiplexer +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +AnalogMultiplexer KEYWORD1 +AMxx40xx KEYWORD1 +AMxx4051 KEYWORD1 +AMxx4052 KEYWORD1 +AMxx4053 KEYWORD1 +AMxx4067 KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +enable KEYWORD2 +disable KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### \ No newline at end of file diff --git a/AslAnalogMultiplexer/library.properties b/AslAnalogMultiplexer/library.properties new file mode 100644 index 0000000..bb7aa9c --- /dev/null +++ b/AslAnalogMultiplexer/library.properties @@ -0,0 +1,9 @@ +name=AslAnalogMultiplexer +version=1.0.2 +author=Julian Sanin +maintainer=Julian Sanin +sentence=A library to interface with analog multiplexers. +paragraph=Supports 4051, 4052, 4053, and 4067 series multiplexers. +category=Uncategorized +url=https://github.com/j54n1n/arduino-libraries +architectures=* diff --git a/AslAnalogMultiplexer/src/AslAnalogMultiplexer.cpp b/AslAnalogMultiplexer/src/AslAnalogMultiplexer.cpp new file mode 100644 index 0000000..33cf93e --- /dev/null +++ b/AslAnalogMultiplexer/src/AslAnalogMultiplexer.cpp @@ -0,0 +1,80 @@ +/* + * Library for the CMOS 40xx analog multiplexer series. + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Julian Sanin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "AslAnalogMultiplexer.h" + +namespace asl { + +AnalogMultiplexer::AnalogMultiplexer(uint8_t pinEnable) { + _pinEnable = pinEnable; + _pinCommon = NOT_A_PIN; + if (NOT_A_PIN != _pinEnable) { + /*Arduino*/::pinMode(_pinEnable, OUTPUT); + } +} + +void AnalogMultiplexer::enable() { + if (NOT_A_PIN != _pinEnable) { + /*Arduino*/::digitalWrite(_pinEnable, LOW); + } +} + +void AnalogMultiplexer::disable() { + if (NOT_A_PIN != _pinEnable) { + /*Arduino*/::digitalWrite(_pinEnable, HIGH); + } +} + +void AnalogMultiplexer::pinMode(uint8_t pinCommon, uint8_t mode) { + if (NOT_A_PIN != digitalPinToPort(pinCommon)) { + _pinCommon = pinCommon; + /*Arduino*/::pinMode(_pinCommon, mode); + } else { + _pinCommon = NOT_A_PIN; + } +} + +void AnalogMultiplexer::digitalWrite(uint8_t channel, uint8_t value) { + if ((NOT_A_PIN != _pinCommon) && (NOT_A_PIN != selectChannel(channel))) { + /*Arduino*/::digitalWrite(_pinCommon, value); + } +} + +int AnalogMultiplexer::digitalRead(uint8_t channel) { + if ((NOT_A_PIN != _pinCommon) && (NOT_A_PIN != selectChannel(channel))) { + return /*Arduino*/::digitalRead(_pinCommon); + } + return LOW; +} + +int AnalogMultiplexer::analogRead(uint8_t channel) { + if ((NOT_A_PIN != _pinCommon) && (NOT_A_PIN != selectChannel(channel))) { + return /*Arduino*/::analogRead(_pinCommon); + } + return 0; +} + +} diff --git a/AslAnalogMultiplexer/src/AslAnalogMultiplexer.h b/AslAnalogMultiplexer/src/AslAnalogMultiplexer.h new file mode 100644 index 0000000..2bc066b --- /dev/null +++ b/AslAnalogMultiplexer/src/AslAnalogMultiplexer.h @@ -0,0 +1,171 @@ +/* + * Library for the CMOS 40xx analog multiplexer series. + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Julian Sanin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef ASL_ANALOG_MULTIPLEXER_H +#define ASL_ANALOG_MULTIPLEXER_H + +#include + +namespace asl { + +/// CMOS Logic Analog Multiplexer and Demultiplexer. +class AnalogMultiplexer { + + /// Channel enable. + uint8_t _pinEnable; + + /// Channel common input or output. + uint8_t _pinCommon; + +protected: + /// + /// Select channel (from 0 to MAX) to be connected to common pin. + /// + virtual uint8_t selectChannel(uint8_t channel) = 0; + +public: + explicit AnalogMultiplexer(uint8_t pinEnable); + + /// Enables the analog multiplexer channels. + void enable(); + + /// Disables the analog multiplexer channels. + void disable(); + + /// + /// Configures the specified common channel pin to behave either as an input + /// or an output. + /// + /// + /// The pin number of whose mode you wish to set. The selected pin will be + /// used for , + /// , and + /// . + /// + /// + /// , , or + /// . + /// + void pinMode(uint8_t pinCommon, uint8_t mode); + + /// + /// Write a or a value to a specified + /// common channel pin. + /// + /// + /// + /// The channel number. + /// + /// + /// or . + /// + void digitalWrite(uint8_t channel, uint8_t value); + + /// + /// Reads the value from a specified common channel pin, either + /// or . + /// + /// + /// The channel number of the multiplexer pin you want to read. + /// + /// + /// or . + /// + int digitalRead(uint8_t channel); + + /// + /// Reads the value from the specified common channel pin. + /// + /// + /// The channel number of the multiplexer pin you want to read. + /// + /// + /// 0 to 1023. + /// + int analogRead(uint8_t channel); +}; + +/// +/// Generic class for all types of analog mulitplexer with selector pins. +/// +template +class AMxx40xx : public AnalogMultiplexer { + + enum { MAX_SELECTORS = sizeof...(Args) }; + + /// 2^n selectors for each channel. + uint8_t _pinSelectors[MAX_SELECTORS]; + + /// + /// See . + /// + uint8_t selectChannel(uint8_t channel) { + if ((1 << MAX_SELECTORS) > channel) { + for (uint8_t i = 0; i < MAX_SELECTORS; i++) { + const uint8_t pinSelector = _pinSelectors[i]; + /*Arduino*/::digitalWrite(pinSelector, bitRead(channel, i)); + } + return channel + 1; + } + return NOT_A_PIN; + } + +public: + /// + /// Setup an analog multiplexer with given enable pin and selector pins. + /// + AMxx40xx(uint8_t pinEnable, Args... pinSelector) + : AnalogMultiplexer(pinEnable), _pinSelectors{pinSelector...} { + for (uint8_t i = 0; i < MAX_SELECTORS; i++) { + /*Arduino*/::pinMode(_pinSelectors[i], OUTPUT); + } + } +}; + +/// +/// 4051 Series Single 8:1 Analog Multiplexer. +/// +using AMxx4051 = AMxx40xx; + +/// +/// 4052 Series Dual 4:1 Analog Multiplexer. +/// +using AMxx4052 = AMxx40xx; + +/// +/// 4053 Series Triple 2:1 Analog Multiplexer. Assuming all 3 selectors are tied +/// together. +/// +using AMxx4053 = AMxx40xx; + +/// +/// 4067 Series Single 16:1 Analog Multiplexer. +/// +using AMxx4067 = AMxx40xx; + +} + +#endif //ASL_ANALOG_MULTIPLEXER_H pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy