Created
September 25, 2022 00:13
-
-
Save chrisisbeef/456a32a701cef718eb201a17d39aad8e to your computer and use it in GitHub Desktop.
CD4052B Analog MUX for Wokwi (Input MUXing Only for Now)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "wokwi-api.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
DEFINE_PIN(CIO0); | |
DEFINE_PIN(CIO1); | |
DEFINE_PIN(CIO2); | |
DEFINE_PIN(CIO3); | |
DEFINE_PIN(CIO4); | |
DEFINE_PIN(CIO5); | |
DEFINE_PIN(CIO6); | |
DEFINE_PIN(CIO7); | |
DEFINE_PIN(VEE); | |
DEFINE_PIN(VSS); | |
DEFINE_PIN(VDD); | |
DEFINE_PIN(INH); | |
DEFINE_PIN(A); | |
DEFINE_PIN(B); | |
DEFINE_PIN(C); | |
DEFINE_PIN(COMIO); | |
typedef struct { | |
pin_t pin_comio; | |
uint32_t cio0_val; | |
uint32_t cio1_val; | |
uint32_t cio2_val; | |
uint32_t cio3_val; | |
uint32_t cio4_val; | |
uint32_t cio5_val; | |
uint32_t cio6_val; | |
uint32_t cio7_val; | |
uint8_t inh; | |
uint8_t a; | |
uint8_t b; | |
uint8_t c; | |
} chip_state_t; | |
static void bitWrite(uint8_t *x, char n, uint8_t value) { | |
if (value) | |
*x |= (1 << n); | |
else | |
*x &= ~(1 << n); | |
} | |
static void update_output(chip_state_t *chip) { | |
uint8_t result = 0b00000000; | |
bitWrite(&result, 0, pin_read(chip->c)); | |
bitWrite(&result, 1, pin_read(chip->b)); | |
bitWrite(&result, 2, pin_read(chip->a)); | |
bitWrite(&result, 3, pin_read(chip->inh)); | |
if (result == 0b00000000) { | |
pin_dac_write(chip->pin_comio, pin_adc_read(chip->cio0_val)); | |
} else if (result == 0b00000001) { | |
pin_dac_write(chip->pin_comio, pin_adc_read(chip->cio1_val)); | |
} else if (result == 0b00000010) { | |
pin_dac_write(chip->pin_comio, pin_adc_read(chip->cio2_val)); | |
} else if (result == 0b00000011) { | |
pin_dac_write(chip->pin_comio, pin_adc_read(chip->cio3_val)); | |
} else if (result == 0b00000100) { | |
pin_dac_write(chip->pin_comio, pin_adc_read(chip->cio4_val)); | |
} else if (result == 0b00000101) { | |
pin_dac_write(chip->pin_comio, pin_adc_read(chip->cio5_val)); | |
} else if (result == 0b000000110) { | |
pin_dac_write(chip->pin_comio, pin_adc_read(chip->cio6_val)); | |
} else if (result == 0b00000111) { | |
pin_dac_write(chip->pin_comio, pin_adc_read(chip->cio7_val)); | |
} else { | |
// Latch mode, don't make any changes | |
// TODO: This should maybe return "undefined" or something else? | |
} | |
} | |
void* chip_init() { | |
setvbuf(stdout, NULL, _IOLBF, 1024); | |
printf("Initializing cd4051b\n"); | |
chip_state_t *chip = malloc(sizeof(chip_state_t)); | |
chip->pin_comio = pin_init("COMIO", ANALOG); | |
chip->cio0_val = pin_init("CIO0", ANALOG); | |
chip->cio1_val = pin_init("CIO1", ANALOG); | |
chip->cio2_val = pin_init("CIO2", ANALOG); | |
chip->cio3_val = pin_init("CIO3", ANALOG); | |
chip->cio4_val = pin_init("CIO4", ANALOG); | |
chip->cio5_val = pin_init("CIO5", ANALOG); | |
chip->cio6_val = pin_init("CIO6", ANALOG); | |
chip->cio7_val = pin_init("CIO7", ANALOG); | |
chip->inh = pin_init("INH", INPUT_PULLDOWN); | |
chip->a = pin_init("A", INPUT); | |
chip->b = pin_init("B", INPUT); | |
chip->c = pin_init("C", INPUT); | |
pin_watch(chip->inh, BOTH); | |
pin_watch(chip->a, BOTH); | |
pin_watch(chip->b, BOTH); | |
pin_watch(chip->c, BOTH); | |
update_output(chip); | |
return chip; | |
} | |
bool EXPORT(chip_pin_change)(chip_state_t *chip, uint32_t pin, uint32_t value) { | |
update_output(chip); | |
printf("Output Value: %f\n", pin_adc_read(chip->pin_comio)); | |
return false; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "CD4051B", | |
"author": "Chris Schmidt", | |
"pins": [ | |
"CIO4", | |
"CIO6", | |
"COMIO", | |
"CIO7", | |
"CIO5", | |
"INH", | |
"VEE", | |
"VSS", | |
"C", | |
"B", | |
"A", | |
"CIO3", | |
"CIO0", | |
"CIO1", | |
"CIO2", | |
"VDD" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "chip-cd4051b", | |
"id": "ic1", | |
"top": 0, | |
"left": 0, | |
"attrs": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment