Library for interfacing with AD9837, generate Sine/Triangular/Square wave
AD9837.cpp@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 "AD9837.h" |
nicolas97 | 0:fdfd54fe27dc | 2 | #include "mbed.h" |
nicolas97 | 0:fdfd54fe27dc | 3 | |
nicolas97 | 0:fdfd54fe27dc | 4 | #define REG_FREQ1 0x8000 |
nicolas97 | 0:fdfd54fe27dc | 5 | #define REG_FREQ0 0x4000 |
nicolas97 | 0:fdfd54fe27dc | 6 | #define REG_PHASE0 0xC000 |
nicolas97 | 0:fdfd54fe27dc | 7 | #define REG_PHASE1 0xE000 |
nicolas97 | 0:fdfd54fe27dc | 8 | |
nicolas97 | 0:fdfd54fe27dc | 9 | #define REG_B28 0x2000 |
nicolas97 | 0:fdfd54fe27dc | 10 | #define REG_HLB 0x1000 |
nicolas97 | 0:fdfd54fe27dc | 11 | #define REG_FSEL 0x0800 |
nicolas97 | 0:fdfd54fe27dc | 12 | #define REG_PSEL 0x0400 |
nicolas97 | 0:fdfd54fe27dc | 13 | #define REG_PINSW 0x0200 |
nicolas97 | 0:fdfd54fe27dc | 14 | #define REG_RESET 0x0100 |
nicolas97 | 0:fdfd54fe27dc | 15 | #define REG_SLEEP1 0x0080 |
nicolas97 | 0:fdfd54fe27dc | 16 | #define REG_SLEEP12 0x0040 |
nicolas97 | 0:fdfd54fe27dc | 17 | #define REG_OPBITEN 0x0020 |
nicolas97 | 0:fdfd54fe27dc | 18 | #define REG_SIGNPIB 0x0010 |
nicolas97 | 0:fdfd54fe27dc | 19 | #define REG_DIV2 0x0008 |
nicolas97 | 0:fdfd54fe27dc | 20 | #define REG_MODE 0x0002 |
nicolas97 | 0:fdfd54fe27dc | 21 | |
nicolas97 | 0:fdfd54fe27dc | 22 | #define SIGN_OUTPUT_MASK (REG_OPBITEN | REG_SIGNPIB | REG_DIV2 | REG_MODE) |
nicolas97 | 0:fdfd54fe27dc | 23 | |
nicolas97 | 0:fdfd54fe27dc | 24 | AD9837::AD9837(PinName mosi, PinName miso, PinName sclk, PinName cs) : _spi(mosi, miso, sclk), _cs(cs) { |
nicolas97 | 0:fdfd54fe27dc | 25 | |
nicolas97 | 0:fdfd54fe27dc | 26 | _spi.format(8, 2); |
nicolas97 | 0:fdfd54fe27dc | 27 | _spi.frequency(1000000); |
nicolas97 | 0:fdfd54fe27dc | 28 | } |
nicolas97 | 0:fdfd54fe27dc | 29 | |
nicolas97 | 0:fdfd54fe27dc | 30 | void AD9837::writeReg(uint16_t value) |
nicolas97 | 0:fdfd54fe27dc | 31 | { |
nicolas97 | 0:fdfd54fe27dc | 32 | _cs=0; |
nicolas97 | 0:fdfd54fe27dc | 33 | wait_us(10); |
nicolas97 | 0:fdfd54fe27dc | 34 | _spi.write(value >> 8); |
nicolas97 | 0:fdfd54fe27dc | 35 | _spi.write(value & 0xFF); |
nicolas97 | 0:fdfd54fe27dc | 36 | wait_us(10); |
nicolas97 | 0:fdfd54fe27dc | 37 | _cs=1; |
nicolas97 | 0:fdfd54fe27dc | 38 | } |
nicolas97 | 0:fdfd54fe27dc | 39 | |
nicolas97 | 0:fdfd54fe27dc | 40 | void AD9837::setFrequencyWord(unsigned int reg, uint32_t frequency) |
nicolas97 | 0:fdfd54fe27dc | 41 | { |
nicolas97 | 0:fdfd54fe27dc | 42 | writeReg((reg?REG_FREQ1:REG_FREQ0) | (frequency & 0x3FFF)); |
nicolas97 | 0:fdfd54fe27dc | 43 | writeReg((reg?REG_FREQ1:REG_FREQ0) | ((frequency >> 14) & 0x3FFF)); |
nicolas97 | 0:fdfd54fe27dc | 44 | } |
nicolas97 | 0:fdfd54fe27dc | 45 | |
nicolas97 | 0:fdfd54fe27dc | 46 | void AD9837::setPhaseWord(unsigned int reg, uint32_t phase) |
nicolas97 | 0:fdfd54fe27dc | 47 | { |
nicolas97 | 0:fdfd54fe27dc | 48 | writeReg((reg?REG_PHASE1:REG_PHASE0) | (phase & 0x0FFF)); |
nicolas97 | 0:fdfd54fe27dc | 49 | } |
nicolas97 | 0:fdfd54fe27dc | 50 | |
nicolas97 | 0:fdfd54fe27dc | 51 | void AD9837::setSignOutput(SignOutput out) |
nicolas97 | 0:fdfd54fe27dc | 52 | { |
nicolas97 | 0:fdfd54fe27dc | 53 | m_reg = (m_reg & ~SIGN_OUTPUT_MASK) | out; |
nicolas97 | 0:fdfd54fe27dc | 54 | writeReg(m_reg); |
nicolas97 | 0:fdfd54fe27dc | 55 | } |
nicolas97 | 0:fdfd54fe27dc | 56 | |
nicolas97 | 0:fdfd54fe27dc | 57 | void AD9837::setOutputMode(OutputMode out) |
nicolas97 | 0:fdfd54fe27dc | 58 | { |
nicolas97 | 0:fdfd54fe27dc | 59 | if(out == OUTPUT_MODE_TRIANGLE) { |
nicolas97 | 0:fdfd54fe27dc | 60 | m_reg = (m_reg & ~SIGN_OUTPUT_MASK) | out; |
nicolas97 | 0:fdfd54fe27dc | 61 | } if(out == OUTPUT_MODE_SINE) { |
nicolas97 | 0:fdfd54fe27dc | 62 | m_reg &= ~REG_MODE; |
nicolas97 | 0:fdfd54fe27dc | 63 | }else { |
nicolas97 | 0:fdfd54fe27dc | 64 | m_reg = 0x2A; |
nicolas97 | 0:fdfd54fe27dc | 65 | } |
nicolas97 | 0:fdfd54fe27dc | 66 | writeReg(m_reg); |
nicolas97 | 0:fdfd54fe27dc | 67 | } |