This library provides a class to generate differents signale wave form. Note that the maximum update rate of 1 MHz, so Fmax = 1MHz / _num_pixels (see UM10360 - Chapter 30: LPC17xx Digital-to-Analog Converter (DAC).
SignalGenerator.h@1:d6cbee8595e0, 2010-11-22 (annotated)
- Committer:
- Yann
- Date:
- Mon Nov 22 08:14:22 2010 +0000
- Revision:
- 1:d6cbee8595e0
- Parent:
- 0:40051400cafe
V0.0.2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yann | 0:40051400cafe | 1 | /* mbed signal generator implementation using LPC1768 DAC |
Yann | 0:40051400cafe | 2 | * Copyright (c) 2010 ygarcia |
Yann | 0:40051400cafe | 3 | * |
Yann | 0:40051400cafe | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
Yann | 0:40051400cafe | 5 | * of this software and associated documentation files (the "Software"), to deal |
Yann | 0:40051400cafe | 6 | * in the Software without restriction, including without limitation the rights |
Yann | 0:40051400cafe | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
Yann | 0:40051400cafe | 8 | * copies of the Software, and to permit persons to whom the Software is |
Yann | 0:40051400cafe | 9 | * furnished to do so, subject to the following conditions: |
Yann | 0:40051400cafe | 10 | * |
Yann | 0:40051400cafe | 11 | * The above copyright notice and this permission notice shall be included in |
Yann | 0:40051400cafe | 12 | * all copies or substantial portions of the Software. |
Yann | 0:40051400cafe | 13 | * |
Yann | 0:40051400cafe | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Yann | 0:40051400cafe | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Yann | 0:40051400cafe | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Yann | 0:40051400cafe | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Yann | 0:40051400cafe | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Yann | 0:40051400cafe | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Yann | 0:40051400cafe | 20 | * THE SOFTWARE. |
Yann | 0:40051400cafe | 21 | */ |
Yann | 0:40051400cafe | 22 | |
Yann | 0:40051400cafe | 23 | #if !defined(__SIGNALGERNERATOR_H__) |
Yann | 0:40051400cafe | 24 | #define __SIGNALGERNERATOR_H__ |
Yann | 0:40051400cafe | 25 | |
Yann | 0:40051400cafe | 26 | #include "Debug.h" |
Yann | 0:40051400cafe | 27 | |
Yann | 1:d6cbee8595e0 | 28 | /** This class provides methods to generate differents signale wave form. V0.0.0.2 |
Yann | 0:40051400cafe | 29 | * |
Yann | 0:40051400cafe | 30 | * Note that the maximum update rate of 1 MHz, so Fmax = 1MHz / _num_pixels |
Yann | 0:40051400cafe | 31 | * |
Yann | 0:40051400cafe | 32 | * Note that Vout is given by the relation below (UM10360 - Table 537. D/A Pin Description): |
Yann | 0:40051400cafe | 33 | * Vout = VALUE * ((Vrefp - Vrefn) / 1024) + Vrefn |
Yann | 0:40051400cafe | 34 | * in which: |
Yann | 0:40051400cafe | 35 | * - Vrefp: tied to VDD (e.g. 3.3V) |
Yann | 0:40051400cafe | 36 | * - Vrefn: tied to Vss |
Yann | 0:40051400cafe | 37 | * @see UM10360 - Chapter 30: LPC17xx Digital-to-Analog Converter (DAC) |
Yann | 0:40051400cafe | 38 | * @remark This class was validated with Tektronix TDS2014 oscilloscope |
Yann | 0:40051400cafe | 39 | * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com) |
Yann | 0:40051400cafe | 40 | * References: |
Yann | 0:40051400cafe | 41 | * - http://fabrice.sincere.pagesperso-orange.fr/cm_electronique/projet_pic/gbf/gbf.htm |
Yann | 0:40051400cafe | 42 | * @todo 1) Enhance comments 2) Add support of user-defined signal in byte array format |
Yann | 0:40051400cafe | 43 | */ |
Yann | 0:40051400cafe | 44 | class SignalGenerator |
Yann | 0:40051400cafe | 45 | { |
Yann | 0:40051400cafe | 46 | public: |
Yann | 0:40051400cafe | 47 | /** List of supported signals |
Yann | 0:40051400cafe | 48 | */ |
Yann | 0:40051400cafe | 49 | enum SignalGeneratorType { |
Yann | 0:40051400cafe | 50 | SquareSignal = 0, //<! Square signal (50% duty cycle) |
Yann | 0:40051400cafe | 51 | TriangleSignal = 1, //<! Triangle signal |
Yann | 0:40051400cafe | 52 | SawtoothSignal = 2, //<! Sawtooth signal |
Yann | 0:40051400cafe | 53 | ReverseSawtoothSignal = 3, //<! Reversed sawtooth signal |
Yann | 0:40051400cafe | 54 | SinusSignal = 4 //<! Sinusoidal signal |
Yann | 0:40051400cafe | 55 | }; // End of SignalGeneratorType enum |
Yann | 0:40051400cafe | 56 | public: |
Yann | 0:40051400cafe | 57 | /** Default ctor |
Yann | 0:40051400cafe | 58 | * |
Yann | 0:40051400cafe | 59 | * Initialize the internal datas. Default values are: |
Yann | 0:40051400cafe | 60 | * - signal type: SquareSignal |
Yann | 0:40051400cafe | 61 | * - frequency: 1000Hz |
Yann | 0:40051400cafe | 62 | * - # of pixels is fixed, _twait is adjusted |
Yann | 0:40051400cafe | 63 | * |
Yann | 0:40051400cafe | 64 | * @param p_outPort: Pin name of the Analog out port (e.g. p18 for LPC1768) |
Yann | 0:40051400cafe | 65 | */ |
Yann | 0:40051400cafe | 66 | SignalGenerator(PinName p_outPort); |
Yann | 0:40051400cafe | 67 | /** Default dtor |
Yann | 0:40051400cafe | 68 | * |
Yann | 0:40051400cafe | 69 | * Free resources |
Yann | 0:40051400cafe | 70 | */ |
Yann | 0:40051400cafe | 71 | virtual ~SignalGenerator(); |
Yann | 0:40051400cafe | 72 | |
Yann | 0:40051400cafe | 73 | /** Set the frequency of the signal. |
Yann | 0:40051400cafe | 74 | * |
Yann | 0:40051400cafe | 75 | * @param p_signalType: Signal type. Default value is square signal |
Yann | 0:40051400cafe | 76 | * @param p_frequency: Signal frequency. Default value is 1000Hz |
Yann | 0:40051400cafe | 77 | * @param p_num_pixels: Number of pixels ber period. Default value is 64 |
Yann | 0:40051400cafe | 78 | * @see UM10360 - Chapter 30: LPC17xx Digital-to-Analog Converter (DAC) |
Yann | 0:40051400cafe | 79 | * @code |
Yann | 0:40051400cafe | 80 | * // Prepare a sinus signal at 500Hz |
Yann | 0:40051400cafe | 81 | * signal.SetSignalFrequency(SignalGenerator::SinusSignal,500); |
Yann | 0:40051400cafe | 82 | * // Launch execution |
Yann | 0:40051400cafe | 83 | * signal.BeginRunAsync(); |
Yann | 0:40051400cafe | 84 | * // Here signal generation is running. Use Led or oscilloscope to vizualize output |
Yann | 0:40051400cafe | 85 | * printf("Press any key to terminate"); |
Yann | 0:40051400cafe | 86 | * getchar(); |
Yann | 0:40051400cafe | 87 | * // Terminate execution |
Yann | 0:40051400cafe | 88 | * signal.EndRunAsync(); |
Yann | 0:40051400cafe | 89 | * @endcode |
Yann | 0:40051400cafe | 90 | */ |
Yann | 0:40051400cafe | 91 | void SetSignalFrequency(SignalGenerator::SignalGeneratorType p_signalType = SignalGenerator::SquareSignal, int p_frequency = 1000, int p_num_pixels = 64); |
Yann | 0:40051400cafe | 92 | /** Generate the signal in synchrnous mode |
Yann | 0:40051400cafe | 93 | * |
Yann | 0:40051400cafe | 94 | * @see Stop() method |
Yann | 0:40051400cafe | 95 | */ |
Yann | 0:40051400cafe | 96 | void Run(); |
Yann | 0:40051400cafe | 97 | /** Method called to terminate synchronous running mode |
Yann | 0:40051400cafe | 98 | */ |
Yann | 0:40051400cafe | 99 | void Stop(); |
Yann | 0:40051400cafe | 100 | /** Method called to prepare an asynchronous running mode |
Yann | 0:40051400cafe | 101 | * |
Yann | 0:40051400cafe | 102 | * @return 0 on success, -1 if _twait less that 7us. On error, you shall use synchronous method |
Yann | 0:40051400cafe | 103 | */ |
Yann | 0:40051400cafe | 104 | int BeginRunAsync(); |
Yann | 0:40051400cafe | 105 | /** Method called to launch the asynchronous running mode |
Yann | 0:40051400cafe | 106 | */ |
Yann | 0:40051400cafe | 107 | void RunAsync(); |
Yann | 0:40051400cafe | 108 | /** Method called to terminate asynchronous running mode |
Yann | 0:40051400cafe | 109 | */ |
Yann | 0:40051400cafe | 110 | void EndRunAsync(); |
Yann | 0:40051400cafe | 111 | |
Yann | 0:40051400cafe | 112 | SignalGenerator& operator =(const bool& p_mode); |
Yann | 0:40051400cafe | 113 | SignalGenerator& operator =(const int& p_frequency); |
Yann | 0:40051400cafe | 114 | SignalGenerator& operator =(const SignalGenerator::SignalGeneratorType& p_signalType); |
Yann | 0:40051400cafe | 115 | private: |
Yann | 0:40051400cafe | 116 | /** Set the signal type to generate and build the lookup table according to the signal frequency. |
Yann | 0:40051400cafe | 117 | */ |
Yann | 0:40051400cafe | 118 | void PrepareSignal(); |
Yann | 0:40051400cafe | 119 | /** Build the lookup table for a square signal according to the signal frequency. |
Yann | 0:40051400cafe | 120 | * |
Yann | 0:40051400cafe | 121 | * Note that the duty cycle is 50%. For different values, please refer to PwmOut class |
Yann | 0:40051400cafe | 122 | */ |
Yann | 0:40051400cafe | 123 | void PrepareSquareSignal(); |
Yann | 0:40051400cafe | 124 | /** Build the lookup table for a triangle signal according to the signal frequency. |
Yann | 0:40051400cafe | 125 | */ |
Yann | 0:40051400cafe | 126 | void PrepareTriangleSignal(); |
Yann | 0:40051400cafe | 127 | /** Build the lookup table for a reverse sawtooth signal according to the signal frequency. |
Yann | 0:40051400cafe | 128 | */ |
Yann | 0:40051400cafe | 129 | void PrepareSawtoothSignal(); |
Yann | 0:40051400cafe | 130 | /** Build the lookup table for a sawtooth sinal according to the signal frequency. |
Yann | 0:40051400cafe | 131 | */ |
Yann | 0:40051400cafe | 132 | void PrepareReverseSawtoothSignal(); |
Yann | 0:40051400cafe | 133 | /** Build the lookup table for a 'sinus' sinal according to the signal frequency. |
Yann | 0:40051400cafe | 134 | */ |
Yann | 0:40051400cafe | 135 | void PrepareSinusSignal(); |
Yann | 0:40051400cafe | 136 | private: |
Yann | 0:40051400cafe | 137 | SignalGenerator::SignalGeneratorType _signalType; //<! Signal selected - Default value is SquareSignal |
Yann | 0:40051400cafe | 138 | int _frequency; // Required frequency of the signal |
Yann | 0:40051400cafe | 139 | float *_values; //<! Pointer to the array of calculated values for the selected signal |
Yann | 0:40051400cafe | 140 | int _num_pixels; // <! Number of calculated points, this is the _values length |
Yann | 0:40051400cafe | 141 | float _twait; //<! Time between each changes of the analog output |
Yann | 0:40051400cafe | 142 | bool _mode; //<! # of pixels is fixed (128 pixels) |
Yann | 0:40051400cafe | 143 | AnalogOut _out; //<! Analog out pin of the LPC1768 |
Yann | 0:40051400cafe | 144 | bool _stop; //<! Set to tru to stop the signal generation |
Yann | 0:40051400cafe | 145 | int _asynckCounter; //<! Index on _values when asynchronous run is used |
Yann | 0:40051400cafe | 146 | Ticker _asyncTicker; //<! Ticker to ipdate DAC when asynchronous run is used |
Yann | 0:40051400cafe | 147 | DigitalOut _debugLed; //<! For debug with oscilloscope purpose |
Yann | 0:40051400cafe | 148 | }; // End of class SignalGenerator |
Yann | 0:40051400cafe | 149 | |
Yann | 0:40051400cafe | 150 | #endif // __SIGNALGERNERATOR_H__ |