|
| 1 | +// |
| 2 | +// FILE: AD7367.cpp |
| 3 | +// AUTHOR: Rob Tillaart |
| 4 | +// VERSION: 0.1.0 |
| 5 | +// DATE: 2025-01-10 |
| 6 | +// PURPOSE: Arduino library for the AD7367, 2 channel simultaneous sampling 14 bit ADC. |
| 7 | + |
| 8 | + |
| 9 | +#include "AD7367.h" |
| 10 | + |
| 11 | + |
| 12 | +AD7367::AD7367(uint8_t select, uint8_t clock, uint8_t convert, uint8_t busy, uint8_t data0, uint8_t data1) |
| 13 | +{ |
| 14 | + _type = 67; |
| 15 | + _bits = 14; |
| 16 | + _select = select; |
| 17 | + _clock = clock; |
| 18 | + _convert = convert; |
| 19 | + _busy = busy; |
| 20 | + _data0 = data0; |
| 21 | + _data1 = data1; |
| 22 | +} |
| 23 | + |
| 24 | +// sets internal state |
| 25 | +void AD7367::begin() |
| 26 | +{ |
| 27 | + pinMode(_select, OUTPUT); |
| 28 | + pinMode(_clock, OUTPUT); |
| 29 | + pinMode(_convert, OUTPUT); |
| 30 | + pinMode(_busy, INPUT); |
| 31 | + pinMode(_data0, INPUT); |
| 32 | + pinMode(_data1, INPUT); |
| 33 | + |
| 34 | + digitalWrite(_select, HIGH); |
| 35 | + digitalWrite(_clock, HIGH); |
| 36 | + digitalWrite(_convert, HIGH); |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +uint8_t AD7367::getType() |
| 41 | +{ |
| 42 | + return _type; |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +int AD7367::read() |
| 47 | +{ |
| 48 | + // simulation |
| 49 | + // _value[0] = random(16384); // 14 bits = 16383, 12 bits = 4095 |
| 50 | + // _value[1] = random(16384); |
| 51 | + |
| 52 | + // datasheet page 07 + 20 |
| 53 | + // trigger conversion |
| 54 | + triggerConversion(); |
| 55 | + // wait for conversion to be done |
| 56 | + while (conversionBusy()); |
| 57 | + return readAsync(); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +void AD7367::triggerConversion() |
| 62 | +{ |
| 63 | + // Trigger conversion by 10 ns pulse. |
| 64 | + // might need a delayMicroseconds(1); on very fast processors. |
| 65 | + // needs a configuration flag, although checking the flag is already 10 ns. |
| 66 | + // Are NOP's portable? |
| 67 | + digitalWrite(_convert, LOW); |
| 68 | + // delayMicroseconds(1); |
| 69 | + digitalWrite(_convert, HIGH); |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +bool AD7367::conversionBusy() |
| 74 | +{ |
| 75 | + return digitalRead(_busy) == HIGH; |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +bool AD7367::conversionReady() |
| 80 | +{ |
| 81 | + return digitalRead(_busy) == LOW; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +int AD7367::readAsync() |
| 86 | +{ |
| 87 | + digitalWrite(_select, LOW); |
| 88 | + // use local variables for clocking |
| 89 | + int d0 = 0; |
| 90 | + int d1 = 0; |
| 91 | + // clock in the bits |
| 92 | + for (int i = 0; i < _bits; i++) |
| 93 | + { |
| 94 | + digitalWrite(_clock, LOW); |
| 95 | + digitalWrite(_clock, HIGH); |
| 96 | + d0 <<= 1; |
| 97 | + d0 |= digitalRead(_data0); |
| 98 | + d1 <<= 1; |
| 99 | + d1 |= digitalRead(_data1); |
| 100 | + } |
| 101 | + _value[0] = d0; |
| 102 | + _value[1] = d1; |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +int AD7367::getValue(uint8_t channel) |
| 108 | +{ |
| 109 | + return _value[channel]; |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +int AD7367::getBits() |
| 114 | +{ |
| 115 | + return _bits; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +////////////////////////////////////////////////////////////////// |
| 120 | +// |
| 121 | +// PROTECTED |
| 122 | +// |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +///////////////////////////////////////////////////////////////////////////// |
| 127 | +// |
| 128 | +// DERIVED CLASS AD7366 |
| 129 | +// |
| 130 | +AD7366::AD7366(uint8_t select, uint8_t clock, uint8_t convert, uint8_t busy, uint8_t data0, uint8_t data1) |
| 131 | + :AD7367(select, clock, convert, busy, data0, data1) |
| 132 | +{ |
| 133 | + _type = 66; |
| 134 | + _bits = 12; |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +// -- END OF FILE -- |
| 139 | + |
0 commit comments