Cd4051 Multiplexer Tutorial Is Here!

Mux C++ code for Patch SM. I shared this on Discord but thought it should be here as well. I made the code a bit more readable. Credit to Takumi for original code. You do indeed need to stop the ADC at Init.

#include "daisy_patch_sm.h"
#include "daisy_seed.h"

using namespace daisy;
using namespace patch_sm;

/** Global Hardware access */
DaisyPatchSM hw;

int main(void)
{
    /** Initialize our hardware */
    hw.Init();
    hw.adc.Stop();

    /** Configure ADC channel */
    AdcChannelConfig adc_cfg;

    /** Set ADC to pin A2/ADC_9 for 8 inputs and digital pins to D1, D2, D3  */
    adc_cfg.InitMux(hw.GetPin(DaisyPatchSM::PinBank::A, 2), 8, 
    hw.GetPin(DaisyPatchSM::PinBank::D, 1), 
    hw.GetPin(DaisyPatchSM::PinBank::D, 2), 
    hw.GetPin(DaisyPatchSM::PinBank::D, 3)); 

    /** Initialize the ADC with our configuration */
    hw.adc.Init(&adc_cfg, 1);

    /** Start the ADC conversions in the background */
    hw.adc.Start();

    /** Startup the USB Serial port */
    hw.StartLog();

    while(1)
    {
        /** Print the values via Serial every 250ms 
         *  Values will be 0 when inputs are 0V, 65535 when inputs are 3v3 */
        System::Delay(250); 

        hw.Print("Input 1: %d", hw.adc.GetMux(0, 0));  hw.Print(" | ");
        hw.Print("Input 2: %d", hw.adc.GetMux(0, 1));  hw.Print(" | ");
        hw.Print("Input 3: %d", hw.adc.GetMux(0, 2));  hw.Print(" | ");
        hw.Print("Input 4: %d", hw.adc.GetMux(0, 3));  hw.Print(" | ");
        hw.Print("Input 5: %d", hw.adc.GetMux(0, 4));  hw.Print(" | ");
        hw.Print("Input 6: %d", hw.adc.GetMux(0, 5));  hw.Print(" | ");
        hw.Print("Input 7: %d", hw.adc.GetMux(0, 6));  hw.Print(" | ");
        hw.Print("Input 8: %d", hw.adc.GetMux(0, 7));  hw.PrintLine(" ")
    }
}
1 Like