jisakson3

Dependencies:   MODDMA mbed

Committer:
jisakson3
Date:
Mon Dec 05 02:01:53 2022 +0000
Revision:
0:c1a4c1e9618c
V1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jisakson3 0:c1a4c1e9618c 1 //
jisakson3 0:c1a4c1e9618c 2 // Signal Generator DAC Driver
jisakson3 0:c1a4c1e9618c 3 //
jisakson3 0:c1a4c1e9618c 4 // Derived from AN10917: Memory to DAC data transfers using the LPC1700's DMA
jisakson3 0:c1a4c1e9618c 5 //
jisakson3 0:c1a4c1e9618c 6 //
jisakson3 0:c1a4c1e9618c 7 #ifndef SIGNALGENDAC_H
jisakson3 0:c1a4c1e9618c 8 #define SIGNALGENDAC_H
jisakson3 0:c1a4c1e9618c 9
jisakson3 0:c1a4c1e9618c 10 #include "mbed.h"
jisakson3 0:c1a4c1e9618c 11
jisakson3 0:c1a4c1e9618c 12 #include "SignalGenDefs.h" // access the waveform mode data type
jisakson3 0:c1a4c1e9618c 13
jisakson3 0:c1a4c1e9618c 14
jisakson3 0:c1a4c1e9618c 15 #define SIGNAL_MEM_ENTRIES 2048 // size of the DAC buffer
jisakson3 0:c1a4c1e9618c 16
jisakson3 0:c1a4c1e9618c 17 /// The Signal Generator DAC Driver
jisakson3 0:c1a4c1e9618c 18 ///
jisakson3 0:c1a4c1e9618c 19 /// This class provides the interface to first configure the DAC hardware characteristics,
jisakson3 0:c1a4c1e9618c 20 /// and then to define and control the DAC output.
jisakson3 0:c1a4c1e9618c 21 ///
jisakson3 0:c1a4c1e9618c 22 /// A choice of waveforms is available (Sine, Square, Triangle, Sawtooth, and User Defined.
jisakson3 0:c1a4c1e9618c 23 ///
jisakson3 0:c1a4c1e9618c 24 /// @todo add support for User Defined waveform.
jisakson3 0:c1a4c1e9618c 25 ///
jisakson3 0:c1a4c1e9618c 26 /// @code
jisakson3 0:c1a4c1e9618c 27 /// SignalGenDAC g_signal; // defaults to LPC1768 mbed module (p18 and 3.3v)
jisakson3 0:c1a4c1e9618c 28 ///
jisakson3 0:c1a4c1e9618c 29 /// g_signal.PrepareWaveform(SG_SINE, 1000, 50, 2.2, 1.5);
jisakson3 0:c1a4c1e9618c 30 /// g_signal.Start();
jisakson3 0:c1a4c1e9618c 31 /// wait_ms(1000);
jisakson3 0:c1a4c1e9618c 32 /// g_signal.Stop();
jisakson3 0:c1a4c1e9618c 33 /// @endcode
jisakson3 0:c1a4c1e9618c 34 ///
jisakson3 0:c1a4c1e9618c 35 class SignalGenDAC {
jisakson3 0:c1a4c1e9618c 36
jisakson3 0:c1a4c1e9618c 37 public:
jisakson3 0:c1a4c1e9618c 38
jisakson3 0:c1a4c1e9618c 39 /// Constructor, which is used to define the hardware
jisakson3 0:c1a4c1e9618c 40 ///
jisakson3 0:c1a4c1e9618c 41 /// The default parameters are based on the mbed LPC1768 micro, which has
jisakson3 0:c1a4c1e9618c 42 /// AnalogOut on p18 and uses a 3.3v supply for the A/D reference.
jisakson3 0:c1a4c1e9618c 43 ///
jisakson3 0:c1a4c1e9618c 44 /// @param[in] aout is the analog output pin
jisakson3 0:c1a4c1e9618c 45 /// @param[in] minV is based on the A/D low reference voltage (default 0.0)
jisakson3 0:c1a4c1e9618c 46 /// @param[in] maxV is based on the A/D high reference voltage (default 3.3)
jisakson3 0:c1a4c1e9618c 47 ///
jisakson3 0:c1a4c1e9618c 48 SignalGenDAC(PinName aout = p18, float minV = 0.0, float maxV = 3.3);
jisakson3 0:c1a4c1e9618c 49
jisakson3 0:c1a4c1e9618c 50 /// Destructor
jisakson3 0:c1a4c1e9618c 51 ///
jisakson3 0:c1a4c1e9618c 52 ~SignalGenDAC();
jisakson3 0:c1a4c1e9618c 53
jisakson3 0:c1a4c1e9618c 54 /// Create the waveform in the private memory buffer that is used to DMA to the DAC
jisakson3 0:c1a4c1e9618c 55 ///
jisakson3 0:c1a4c1e9618c 56 /// @param[in] mode defines the waveform: Sine, Square, Triangle, Sawtooth, User
jisakson3 0:c1a4c1e9618c 57 /// @param[in] frequency defines the desired frequency
jisakson3 0:c1a4c1e9618c 58 /// @param[in] dutycycle defined the duty cycle of the waveform to be created. The value
jisakson3 0:c1a4c1e9618c 59 /// is range limited to 5 to 95 (representing 5 to 95 %).
jisakson3 0:c1a4c1e9618c 60 /// @param[in] voltage is the peak-to-peak voltage, and it range limited to 0 to 3.0.
jisakson3 0:c1a4c1e9618c 61 /// @param[in] offset is the offset voltage, and is range limited to 0 to 3.0.
jisakson3 0:c1a4c1e9618c 62 ///
jisakson3 0:c1a4c1e9618c 63 void PrepareWaveform(SG_Waveform mode, float frequency, float dutycycle, float voltage, float offset);
jisakson3 0:c1a4c1e9618c 64
jisakson3 0:c1a4c1e9618c 65 /// Start the signal, in either a oneshot, or continuous mode.
jisakson3 0:c1a4c1e9618c 66 ///
jisakson3 0:c1a4c1e9618c 67 /// @param[in] oneShot defaults false, which causes continuous mode.
jisakson3 0:c1a4c1e9618c 68 /// When set true, one cycle is produced.
jisakson3 0:c1a4c1e9618c 69 ///
jisakson3 0:c1a4c1e9618c 70 void Start(bool oneShot = false);
jisakson3 0:c1a4c1e9618c 71
jisakson3 0:c1a4c1e9618c 72 /// Stop the signal, if it is running.
jisakson3 0:c1a4c1e9618c 73 ///
jisakson3 0:c1a4c1e9618c 74 void Stop(void);
jisakson3 0:c1a4c1e9618c 75
jisakson3 0:c1a4c1e9618c 76 /// Determine if the signal is running.
jisakson3 0:c1a4c1e9618c 77 ///
jisakson3 0:c1a4c1e9618c 78 /// @returns true if the signal is running.
jisakson3 0:c1a4c1e9618c 79 ///
jisakson3 0:c1a4c1e9618c 80 bool isRunning(void) { return isOn; }
jisakson3 0:c1a4c1e9618c 81
jisakson3 0:c1a4c1e9618c 82 private:
jisakson3 0:c1a4c1e9618c 83 bool isOn; // tracks whether the signal is on or off
jisakson3 0:c1a4c1e9618c 84 AnalogOut * aout;
jisakson3 0:c1a4c1e9618c 85 float frequency; // signal parameters
jisakson3 0:c1a4c1e9618c 86 float dutycycle;
jisakson3 0:c1a4c1e9618c 87 float voltage;
jisakson3 0:c1a4c1e9618c 88 float offset;
jisakson3 0:c1a4c1e9618c 89 float minV; // Based on the A/D hardware
jisakson3 0:c1a4c1e9618c 90 float maxV; // Based on the A/D hardware
jisakson3 0:c1a4c1e9618c 91 /// range limit a value.
jisakson3 0:c1a4c1e9618c 92 float rangelimit(float value, float min, float max);
jisakson3 0:c1a4c1e9618c 93 int numSamples; // private container for number of samples
jisakson3 0:c1a4c1e9618c 94 };
jisakson3 0:c1a4c1e9618c 95
jisakson3 0:c1a4c1e9618c 96 #endif // SIGNALGENDAC_H