-
Notifications
You must be signed in to change notification settings - Fork 1
/
analogMux.c
50 lines (44 loc) · 1.61 KB
/
analogMux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* From https://github.com/rudolfbyker/analog-multiplexer
*/
#include <stdint.h>
#include "analogMux.h"
void analogMux8_init(const struct AnalogMux8 *const self) {
pinMode(self->selectorPin0, 1); // OUTPUT
pinMode(self->selectorPin1, 1); // OUTPUT
pinMode(self->selectorPin2, 1); // OUTPUT
pinMode(self->analogPin, self->analogPinDirection);
}
uint32_t analogMux8_selectAndRead(const struct AnalogMux8 *const self, uint8_t selector) {
analogMux8_select(self, selector);
delay(self->delayAfterSelection);
return analogRead(self->analogPin);
}
void analogMux8_selectAndWrite(const struct AnalogMux8 *const self, uint8_t selector, uint32_t value) {
analogMux8_select(self, selector);
delay(self->delayAfterSelection);
return analogWrite(self->analogPin, value);
}
void analogMux8_select(const struct AnalogMux8 *const self, uint8_t selector) {
digitalWrite(self->selectorPin0, (selector & (1 << 0)) >> 0);
digitalWrite(self->selectorPin1, (selector & (1 << 1)) >> 1);
digitalWrite(self->selectorPin2, (selector & (1 << 2)) >> 2);
}
void createConsecutiveMuxesInArray(
uint8_t firstSelectorPin,
uint8_t firstAnalogPin,
uint8_t analogPinDirection,
uint16_t delayAfterSelection,
uint8_t numMuxes,
struct AnalogMux8 *const muxes
) {
uint8_t i;
for (i=0; i<numMuxes; i++) {
muxes[i].selectorPin0 = firstSelectorPin + 3 * i;
muxes[i].selectorPin1 = firstSelectorPin + 3 * i + 1;
muxes[i].selectorPin2 = firstSelectorPin + 3 * i + 2;
muxes[i].analogPin = firstAnalogPin + i;
muxes[i].analogPinDirection = analogPinDirection;
muxes[i].delayAfterSelection = delayAfterSelection;
}
}