|
| 1 | +/****************************************************************************** |
| 2 | +Library_Test.ino |
| 3 | +Library test file for the MiniGen library. |
| 4 | +Mike Hord @ SparkFun Electronics |
| 5 | +6 May 2014 |
| 6 | +https://github.com/sparkfun/MiniGen |
| 7 | +
|
| 8 | +This is a simple test/example file for the MiniGen board and library. |
| 9 | +
|
| 10 | +The MiniGen is a Pro Mini shield-type board (although it can be used as a |
| 11 | +standalone product as well) that generates sine, square and triangle waves up |
| 12 | +to a frequency of approximately 3MHz; above 3MHz, you'll start to see rolloff |
| 13 | +as the anti-aliasing filter on the output begins to affect the signal. Expect |
| 14 | +a peak-to-peak amplitude of about 1V and a DC offset of Vcc/2. |
| 15 | +
|
| 16 | +Resources: |
| 17 | +Uses the MiniGen library and the built-in SPI library. |
| 18 | +
|
| 19 | +Development environment specifics: |
| 20 | +Code developed in Arduino 1.0.5, on an Arduino Pro Mini 5V. |
| 21 | +
|
| 22 | +This code is beerware; if you see me (or any other SparkFun employee) at the |
| 23 | +local, and you've found our code helpful, please buy us a round! |
| 24 | +
|
| 25 | +Distributed as-is; no warranty is given. |
| 26 | +******************************************************************************/ |
| 27 | +// Due to limitations in the Arduino environment, SPI.h must be included both |
| 28 | +// in the library which uses it *and* any sketch using that library. |
1 | 29 | #include <SPI.h> |
2 | 30 | #include <MiniGen.h> |
3 | 31 |
|
| 32 | +// Create an instance of the MiniGen device; note that this has no provision for |
| 33 | +// alternate CLK and MOSI pins, but you *can* pass it a different CS (or FSYNC, |
| 34 | +// as it's referred to elsewhere) pin number. |
4 | 35 | MiniGen gen; |
5 | 36 |
|
6 | 37 | void setup() |
7 | 38 | { |
| 39 | + // Clear the registers in the AD9837 chip, so we're starting from a known |
| 40 | + // location. Note that since the AD9837 has no DOUT, we can't use the |
| 41 | + // read-modify-write method of control. At power up, the output frequency |
| 42 | + // will be 100Hz. |
8 | 43 | gen.reset(); |
9 | 44 | delay(2000); |
| 45 | + |
| 46 | + // SQUARE is, as it suggests, a square wave. SQUARE is at the current output |
| 47 | + // frequency. |
10 | 48 | gen.setMode(MiniGen::SQUARE); |
11 | 49 | delay(3000); |
| 50 | + |
| 51 | + // SQUARE_2 is at half the normal output frequency. |
12 | 52 | gen.setMode(MiniGen::SQUARE_2); |
13 | 53 | delay(3000); |
| 54 | + |
| 55 | + // Exactly what you think it is. |
14 | 56 | gen.setMode(MiniGen::TRIANGLE); |
15 | 57 | delay(3000); |
| 58 | + |
| 59 | + // Yep. |
16 | 60 | gen.setMode(MiniGen::SINE); |
| 61 | + |
| 62 | + // This needs a little explanation. The choices are FULL, COARSE, and FINE. |
| 63 | + // a FULL write takes longer but writes the entire frequency word, so you |
| 64 | + // can change from any frequency to any other frequency. COARSE only allows |
| 65 | + // you to change the upper bits; the lower bits remain unchanged, so you |
| 66 | + // can do a fast write of a large step size. FINE is the opposite; quick |
| 67 | + // writes but smaller steps. |
17 | 68 | gen.setFreqAdjustMode(MiniGen::FULL); |
18 | 69 | } |
19 | 70 |
|
20 | 71 | void loop() |
21 | 72 | { |
| 73 | + // Loop is going to increase the frequency in steps of 10Hz, basically |
| 74 | + // forever. Since the upper limit is 3MHz, you'll probably not sit |
| 75 | + // around long enough for frequency to overflow. |
22 | 76 | static float frequency = 10.0; |
| 77 | + |
| 78 | + // freqCalc() makes a useful 32-bit value out of the frequency value (in |
| 79 | + // Hz) passed to it. |
23 | 80 | unsigned long freqReg = gen.freqCalc(frequency); |
| 81 | + |
| 82 | + // Adjust the frequency. This is a full 32-bit write. |
24 | 83 | gen.adjustFreq(MiniGen::FREQ0, freqReg); |
25 | 84 | delay(100); |
26 | 85 | frequency += 10.0; |
|
0 commit comments