-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathad983x.h
84 lines (71 loc) · 2.24 KB
/
ad983x.h
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef __ARDUINO_AD9838X
#define __ARDUINO_AD9838X
#include <Arduino.h>
#include <SPI.h>
enum SignOutput {
SIGN_OUTPUT_NONE = 0x0000,
SIGN_OUTPUT_MSB = 0x0028,
SIGN_OUTPUT_MSB_2 = 0x0020,
SIGN_OUTPUT_COMPARATOR = 0x0038,
};
enum OutputMode {
OUTPUT_MODE_SINE = 0x0000,
OUTPUT_MODE_TRIANGLE = 0x0002,
};
enum SleepMode {
SLEEP_MODE_NONE = 0x0000,
SLEEP_MODE_MCLK = 0x0080,
SLEEP_MODE_DAC = 0x0040,
SLEEP_MODE_ALL = 0x00C0,
};
class AD983X {
public:
AD983X(byte select_pin, int frequency_mhz);
void setFrequencyWord(byte reg, uint32_t frequency);
void setPhaseWord(byte reg, uint32_t phase);
void setSignOutput(SignOutput out);
void setOutputMode(OutputMode out);
void setSleep(SleepMode out);
inline uint32_t computeFrequencyWord(uint32_t frequency) {
// This is a manual expansion of (frequency * 2^28) / m_frequency_mhz
// Since it doesn't require 64 bit multiplies or divides, it results in
// substantially smaller code sizes.
uint32_t lval = ((frequency & 0xFF) << 22) / (15625l * m_frequency_mhz);
uint32_t mval = ((frequency & 0xFF00) << 14) / (15625l * m_frequency_mhz);
uint32_t hval = ((frequency & 0xFF0000) << 6) / (15625l * m_frequency_mhz);
return (hval << 16) + (mval << 8) + lval;
}
inline void setFrequency(byte reg, long int frequency) {
frequency = this->computeFrequencyWord(frequency);
this->setFrequencyWord(reg, frequency);
}
inline void setFrequency(byte reg, float frequency) {
this->setFrequencyWord(reg, (frequency * (1l << 28)) / (m_frequency_mhz * 1000000));
}
protected:
void init();
void writeReg(uint16_t value);
void writeReg2(uint16_t value1, uint16_t value2);
byte m_select_pin;
int m_frequency_mhz;
uint16_t m_reg;
volatile uint8_t *m_select_out;
uint8_t m_select_bit;
};
class AD983X_PIN : public AD983X {
private:
byte m_reset_pin;
public:
AD983X_PIN(byte select_pin, int frequency_mhz, byte reset_pin);
void reset(boolean in_reset);
void begin();
};
class AD983X_SW : public AD983X {
public:
AD983X_SW(byte select_pin, int frequency_mhz);
void reset(boolean in_reset);
void begin();
void selectFrequency(byte reg);
void selectPhase(byte reg);
};
#endif