Library for interfacing with AD9837, generate Sine/Triangular/Square wave
AD9837.h@0:fdfd54fe27dc, 2015-06-30 (annotated)
- Committer:
- nicolas97
- Date:
- Tue Jun 30 08:25:56 2015 +0000
- Revision:
- 0:fdfd54fe27dc
Library to interface with AD9837 (function generator)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nicolas97 | 0:fdfd54fe27dc | 1 | #include "mbed.h" |
nicolas97 | 0:fdfd54fe27dc | 2 | |
nicolas97 | 0:fdfd54fe27dc | 3 | enum SignOutput { |
nicolas97 | 0:fdfd54fe27dc | 4 | SIGN_OUTPUT_NONE = 0x0000, |
nicolas97 | 0:fdfd54fe27dc | 5 | SIGN_OUTPUT_MSB = 0x0028, |
nicolas97 | 0:fdfd54fe27dc | 6 | SIGN_OUTPUT_MSB_2 = 0x0020, |
nicolas97 | 0:fdfd54fe27dc | 7 | SIGN_OUTPUT_COMPARATOR = 0x0038, |
nicolas97 | 0:fdfd54fe27dc | 8 | }; |
nicolas97 | 0:fdfd54fe27dc | 9 | |
nicolas97 | 0:fdfd54fe27dc | 10 | enum OutputMode { |
nicolas97 | 0:fdfd54fe27dc | 11 | OUTPUT_MODE_SINE = 0x0000, |
nicolas97 | 0:fdfd54fe27dc | 12 | OUTPUT_MODE_TRIANGLE = 0x0002, |
nicolas97 | 0:fdfd54fe27dc | 13 | OUTPUT_MODE_SQUARE = 0x0004, |
nicolas97 | 0:fdfd54fe27dc | 14 | }; |
nicolas97 | 0:fdfd54fe27dc | 15 | |
nicolas97 | 0:fdfd54fe27dc | 16 | class AD9837 |
nicolas97 | 0:fdfd54fe27dc | 17 | { |
nicolas97 | 0:fdfd54fe27dc | 18 | public: |
nicolas97 | 0:fdfd54fe27dc | 19 | AD9837(PinName mosi, PinName miso, PinName sclk, PinName cs); |
nicolas97 | 0:fdfd54fe27dc | 20 | void setFrequencyWord(unsigned int reg, uint32_t frequency); |
nicolas97 | 0:fdfd54fe27dc | 21 | void setPhaseWord(unsigned int reg, uint32_t phase); |
nicolas97 | 0:fdfd54fe27dc | 22 | void setSignOutput(SignOutput out); |
nicolas97 | 0:fdfd54fe27dc | 23 | void setOutputMode(OutputMode out); |
nicolas97 | 0:fdfd54fe27dc | 24 | |
nicolas97 | 0:fdfd54fe27dc | 25 | inline uint32_t computeFrequencyWord(uint32_t frequency) { |
nicolas97 | 0:fdfd54fe27dc | 26 | // This is a manual expansion of (frequency * 2^28) / m_frequency_mhz |
nicolas97 | 0:fdfd54fe27dc | 27 | // Since it doesn't require 64 bit multiplies or divides, it results in |
nicolas97 | 0:fdfd54fe27dc | 28 | // substantially smaller code sizes. |
nicolas97 | 0:fdfd54fe27dc | 29 | uint32_t lval = ((frequency & 0xFF) << 22) / (15625l * m_frequency_mhz); |
nicolas97 | 0:fdfd54fe27dc | 30 | uint32_t mval = ((frequency & 0xFF00) << 14) / (15625l * m_frequency_mhz); |
nicolas97 | 0:fdfd54fe27dc | 31 | uint32_t hval = ((frequency & 0xFF0000) << 6) / (15625l * m_frequency_mhz); |
nicolas97 | 0:fdfd54fe27dc | 32 | return (hval << 16) + (mval << 8) + lval; |
nicolas97 | 0:fdfd54fe27dc | 33 | } |
nicolas97 | 0:fdfd54fe27dc | 34 | |
nicolas97 | 0:fdfd54fe27dc | 35 | inline void setFrequency(unsigned char reg, long int frequency) { |
nicolas97 | 0:fdfd54fe27dc | 36 | frequency = this->computeFrequencyWord(frequency); |
nicolas97 | 0:fdfd54fe27dc | 37 | this->setFrequencyWord(reg, frequency); |
nicolas97 | 0:fdfd54fe27dc | 38 | } |
nicolas97 | 0:fdfd54fe27dc | 39 | |
nicolas97 | 0:fdfd54fe27dc | 40 | inline void setFrequency(unsigned char reg, float frequency) { |
nicolas97 | 0:fdfd54fe27dc | 41 | this->setFrequencyWord(reg, (frequency * (1l << 28)) / (m_frequency_mhz * 1000000)); |
nicolas97 | 0:fdfd54fe27dc | 42 | } |
nicolas97 | 0:fdfd54fe27dc | 43 | |
nicolas97 | 0:fdfd54fe27dc | 44 | protected: |
nicolas97 | 0:fdfd54fe27dc | 45 | void writeReg(uint16_t value); |
nicolas97 | 0:fdfd54fe27dc | 46 | int m_frequency_mhz; |
nicolas97 | 0:fdfd54fe27dc | 47 | uint16_t m_reg; |
nicolas97 | 0:fdfd54fe27dc | 48 | |
nicolas97 | 0:fdfd54fe27dc | 49 | private: |
nicolas97 | 0:fdfd54fe27dc | 50 | SPI _spi; |
nicolas97 | 0:fdfd54fe27dc | 51 | DigitalOut _cs; |
nicolas97 | 0:fdfd54fe27dc | 52 | }; |