Yet another implementation of wave function generator

Dependencies:   mbed vt100

A function generator implemented using DAC of KL25Z on FRDM-KL25Z
On 13-Dec-2015 Support for FRDM-K64F added.

Kinetis の DAC を使用したファンクションジェネレータ(以下funcgen)です。
13-Dec-2015 FRDM-K64F のサポートも追加しました。

/media/uploads/Rhyme/funcgen_top.jpg

Generates a 1KHz sin wave of 3.0V (p-p) when started.

The analog signal output is analogOut (PTE30), for the GND I was using GND of J2.

起動直後には 1KHz 3V(p-p) のサイン波を生成しています。

アナログ信号は PTE30 から、 GNDについては、J2 の GNDを使用しています。

This funcgen can generate multiple sin, saw, sqr, tri waves in different freq, amp simultaneously.
All the waves are summed to form the output.

この funcgen では 複数の異なる周期、振幅の正弦波、のこぎり波、矩形波、三角波を同時に生成することが可能です。
生成された波形はすべて加算される形で出力波形となります。

For example following wave form was generated with
sin amp = 0.5V freq = 1000 Hz
sin amp = 0.5V freq = 123 Hz
sqr amp = 0.5V freq = 20 Hz
saw amp = 1.0V freq = 3Hz

たとえば下図の波形は
サイン波 0.5V 1000Hz
サイン波 0.5V 123 Hz
矩形波 0.5V 20Hz
のこぎり波 1.0V 3Hz
を合成して得られたものです。

/media/uploads/Rhyme/summed_output.jpg

Some tutorial (簡単な使い方)

When you connect a terminal program with 115200 baud, 8bit, no parity,
after reset command menu will be presented.
You may also want to enable local echo, so that you can see what you are typing.

ターミナルプログラムで 115200 baud 8bit ノーパリティで接続していると、
リセット後に下記のようなコマンドメニューが表示されます。
入力している文字が表示されるように Local Echo を有効にしておきます。

/media/uploads/Rhyme/startup_menus.jpg

If you type "list" to the prompt, list of current wave(s) will be shown.

プロンプトに list と入力すると、現在の波形リストが表示されます。

/media/uploads/Rhyme/list_cmds.jpg

So we can tell that 1 wave of sine whose amplitude is 3.0V
and Frequency is 1000Hz is currently running.
Please ignore duty for sin and saw waves ;-) 
The "*" letter in the left shows that this signal is currently selected for operation.

これから、1KHz 3V(p-p)の正弦波が生成されていることがわかります。
デューティー比も表示されていますが、正弦波(sin)とのごきり波(saw)に関しては、
これは無視してください。 
また、左側についている“*”マークが、現在この信号がコマンドの対象になっていることを示しています。

At this time the wave form would look like

この時点で波形は下記のようになっています

/media/uploads/Rhyme/sin_3v_1khz.jpg

If you type "amp 1" the wave form will be

ここで amp 1 と入力すると波形は下記のようになります

/media/uploads/Rhyme/sin_1v_1khz.jpg

Next let's play with a square wave!
To remove current wave type "delete", then type "sqr 3 1000 50"
which means first delete the sin wave then add a square wave of 3V, 1KHz and duty 50%.

それでは矩形波で遊んでみましょう!
まず正弦波を消すのに“delete”とタイプして、次に“sqr 3 1000 50” とタイプしてください。
これは3V 1KHz デューティ50%の正弦波の追加することになります。

/media/uploads/Rhyme/add_sqr.jpg

The wave form of Oscilloscope will be something like

オシロの波形は下記のような感じになります

/media/uploads/Rhyme/sqr_3v_1khz_50.jpg

Now if you type "duty 0" you will get

ここで “duty 0” と入力すると

/media/uploads/Rhyme/sqr_3v_1khz_0.jpg

For "duty 20" you will get

“duty 20”では

/media/uploads/Rhyme/sqr_3v_1khz_20.jpg

For "duty 80" you will get

"duty 80" では

/media/uploads/Rhyme/sqr_3v_1khz_80.jpg

And finally if you specify "duty 100", you will get

最後に“duty 100”を指定すると

/media/uploads/Rhyme/sqr_3v_1khz_100.jpg

This way, you can also get DC bias of this sqr wave's amplitude.

この方法で、この矩形波の振幅に等しいDC入力とすることもできます。

Next, let's try "tri wave", again type "delete" and "tri 3 1000 50", you will get

次に三角波行ってみましょう、再び“delete”、“tri 3 1000 50”と入力しますと

/media/uploads/Rhyme/tri_3_1khz_50.jpg

Again if we try, "duty 0", we will get

“duty 0”とすると

/media/uploads/Rhyme/tri_3_1khz_0.jpg

For "duty 20"

"duty 20" では

/media/uploads/Rhyme/tri_3_1khz_20.jpg

For "duty 80"

"duty 80" では

/media/uploads/Rhyme/tri_3_1khz_80.jpg

And finally "duty 100"

最後に "duty 100"

/media/uploads/Rhyme/tri_3_1khz_100.jpg

Please note that for the "duty 100",
the triangle wave is identical with "saw" wave,
which I noticed after implementing both of them orz

“duty 100”の三角波はのこぎり波と同じものになります。 
気が付いたのは両方の波形をプログラムしちゃってからでした・・・ orz

Multiple waves sample  複数波形の合成例

Now let's try multiple wave composition!

それでは複数波形の合成を試してみましょう!

First let's delete the previous wave, by "delete"

最初に先の信号を"delete" で消しておきます。

Then type, "sqr 1 100 50", "sin 1 700", you will get

次に "sqr 1 100 50", "sin 1 700" とすると

/media/uploads/Rhyme/sqr_1_sin_1.jpg

/media/uploads/Rhyme/sqr_1_sin_1_wave.jpg

And adding a saw wave by "saw 1 10" will give us

最後にのこぎり波を "saw 1 10" で追加すると

/media/uploads/Rhyme/sqr_1_sin_1_saw_1_wave.jpg

If we enter "list" command we will see

ここで list コマンドを入力すると

/media/uploads/Rhyme/list_waves.jpg

Note the "*" letter specify the selected wave,
so if you enter "amp", "freq", "duty", "delete" command,
it will be applied to this wave.
If you want to manipulate other wave,
use "select #" command to specify the wave you'd like to modify.
For example, if you enter "select 0" then the sqr wave at No.0 will be selected.

左側の"*"マークが現在選択されている波形を示しています、
現在"amp", "freq", "duty", "delete" などのコマンドを入力した場合、
その操作はこの波形に対して行われます。
他の波形をモディファイしたい場合には"select #" コマンドを使用してその波形を選択してください。
たとえば "select 0" と入力すると 矩形波(sqr)が選択されることになります。

/media/uploads/Rhyme/ojizo_and.jpg

Committer:
Rhyme
Date:
Sun Dec 13 07:27:43 2015 +0000
Revision:
1:b4f26ccaf417
Parent:
0:46a79748aa81
Support for FRDM-K64F added;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:46a79748aa81 1 /** mbed wave.h general wave form class for function generator
Rhyme 0:46a79748aa81 2 * Copyright (c) 2014, 2015 Motoo Tanaka @ Design Methodology Lab
Rhyme 0:46a79748aa81 3 *
Rhyme 0:46a79748aa81 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Rhyme 0:46a79748aa81 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Rhyme 0:46a79748aa81 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Rhyme 0:46a79748aa81 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Rhyme 0:46a79748aa81 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Rhyme 0:46a79748aa81 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Rhyme 0:46a79748aa81 10 * THE SOFTWARE.
Rhyme 0:46a79748aa81 11 */
Rhyme 0:46a79748aa81 12
Rhyme 0:46a79748aa81 13 #ifndef _WAVE_H_
Rhyme 0:46a79748aa81 14 #define _WAVE_H_
Rhyme 0:46a79748aa81 15
Rhyme 0:46a79748aa81 16 /** wave class basic class of wave form generator
Rhyme 0:46a79748aa81 17 *
Rhyme 0:46a79748aa81 18 */
Rhyme 0:46a79748aa81 19 class wave {
Rhyme 0:46a79748aa81 20 public:
Rhyme 0:46a79748aa81 21
Rhyme 0:46a79748aa81 22 /** constructor
Rhyme 0:46a79748aa81 23 *
Rhyme 0:46a79748aa81 24 * @param float v amplitude in volt 0.0 ~ 3.28 for FRDM-KL25Z
Rhyme 0:46a79748aa81 25 * @param int f Frequency in Hz (1~10000Hz)
Rhyme 0:46a79748aa81 26 * @param int duty Duty in percent (0 ~ 100%)
Rhyme 0:46a79748aa81 27 * @param int p Phase in degree (0 ~ 359 degree)
Rhyme 0:46a79748aa81 28 */
Rhyme 0:46a79748aa81 29 wave(float v, int f, int duty = 50, int p = 0) ; // volt, freq, duty, phase
Rhyme 0:46a79748aa81 30
Rhyme 0:46a79748aa81 31 /** destructor */
Rhyme 0:46a79748aa81 32 ~wave(void) ;
Rhyme 0:46a79748aa81 33
Rhyme 0:46a79748aa81 34 /** mutator for _amp but in float voltage
Rhyme 0:46a79748aa81 35 *
Rhyme 0:46a79748aa81 36 * @param float newvalue vlotage to assign (0.0 ~ 3.28V for FRDM-KL25Z)
Rhyme 0:46a79748aa81 37 */
Rhyme 0:46a79748aa81 38 void volt(float newvalue) ; // set voltage
Rhyme 0:46a79748aa81 39
Rhyme 0:46a79748aa81 40 /** inspector for _amp but in float voltage
Rhyme 0:46a79748aa81 41 *
Rhyme 0:46a79748aa81 42 * @return float amplitude in vlot (0.0 ~ 3.28V for FRDM-KL25Z)
Rhyme 0:46a79748aa81 43 */
Rhyme 0:46a79748aa81 44 float volt(void) ; // get voltage
Rhyme 0:46a79748aa81 45
Rhyme 0:46a79748aa81 46 /** mutator for _amp in int value
Rhyme 0:46a79748aa81 47 *
Rhyme 0:46a79748aa81 48 * @param int newvalue value for amplitude (0x0000 ~ 0xFFFF)
Rhyme 0:46a79748aa81 49 */
Rhyme 0:46a79748aa81 50 void amp(int newvalue) ; // set amp
Rhyme 0:46a79748aa81 51
Rhyme 0:46a79748aa81 52 /** inspector for _amp
Rhyme 0:46a79748aa81 53 *
Rhyme 0:46a79748aa81 54 * @returns amplitude in unsigned short (0x0000 ~ 0xFFFF)
Rhyme 0:46a79748aa81 55 */
Rhyme 0:46a79748aa81 56 int amp(void) ; // get amp
Rhyme 0:46a79748aa81 57
Rhyme 0:46a79748aa81 58 /** mutator for _cycle
Rhyme 0:46a79748aa81 59 *
Rhyme 0:46a79748aa81 60 * @param int newvalue length of 1 cycle of the wave
Rhyme 0:46a79748aa81 61 */
Rhyme 0:46a79748aa81 62 void cycle(int newvalue) ; // set cycle
Rhyme 0:46a79748aa81 63
Rhyme 0:46a79748aa81 64 /** inspector for _cycle
Rhyme 0:46a79748aa81 65 *
Rhyme 0:46a79748aa81 66 * @returns int length of 1 cycle in sample time unit
Rhyme 0:46a79748aa81 67 */
Rhyme 0:46a79748aa81 68 int cycle(void) ; // get cycle
Rhyme 0:46a79748aa81 69
Rhyme 0:46a79748aa81 70 /** mutator for _phase
Rhyme 0:46a79748aa81 71 *
Rhyme 0:46a79748aa81 72 * @param int newvalue value for _phase in degree (0 ~ 359 degree)
Rhyme 0:46a79748aa81 73 */
Rhyme 0:46a79748aa81 74 void phase(int newvalue) ; // set phase
Rhyme 0:46a79748aa81 75
Rhyme 0:46a79748aa81 76 /** inspector for _phase
Rhyme 0:46a79748aa81 77 *
Rhyme 0:46a79748aa81 78 * @return int phase in degree (0 ~ 359 degree)
Rhyme 0:46a79748aa81 79 */
Rhyme 0:46a79748aa81 80 int phase(void) ; // get phase
Rhyme 0:46a79748aa81 81
Rhyme 0:46a79748aa81 82 /** mutator for _cycle but in frequency (Hz)
Rhyme 0:46a79748aa81 83 *
Rhyme 0:46a79748aa81 84 * @param int newvalue frequcney (1 ~ 10000Hz)
Rhyme 0:46a79748aa81 85 */
Rhyme 0:46a79748aa81 86 void freq(int newvalue) ; // set freq
Rhyme 0:46a79748aa81 87
Rhyme 0:46a79748aa81 88 /** inspector for _cycle but in frequency (Hz)
Rhyme 0:46a79748aa81 89 *
Rhyme 0:46a79748aa81 90 * @returns int current frequency in Hz (1~10000Hz)
Rhyme 0:46a79748aa81 91 */
Rhyme 0:46a79748aa81 92 int freq(void) ; // get freq
Rhyme 0:46a79748aa81 93
Rhyme 0:46a79748aa81 94 /** mutator for _pos current positon in _cycle
Rhyme 0:46a79748aa81 95 *
Rhyme 0:46a79748aa81 96 * @param int newvalue (0 ~ _cycle-1)
Rhyme 0:46a79748aa81 97 */
Rhyme 0:46a79748aa81 98 void pos(int newvalue) ; // set position
Rhyme 0:46a79748aa81 99
Rhyme 0:46a79748aa81 100 /** insepector for _pos
Rhyme 0:46a79748aa81 101 *
Rhyme 0:46a79748aa81 102 * @returns int _pos current position in the _cycle
Rhyme 0:46a79748aa81 103 */
Rhyme 0:46a79748aa81 104 int pos(void) ; // get position
Rhyme 0:46a79748aa81 105
Rhyme 0:46a79748aa81 106 /** mutator for _duty the percentage of positive value part
Rhyme 0:46a79748aa81 107 *
Rhyme 0:46a79748aa81 108 * @param int newvalue new duty value in percent (0~100%)
Rhyme 0:46a79748aa81 109 */
Rhyme 0:46a79748aa81 110 virtual void duty(int newvalue) ; // set duty
Rhyme 0:46a79748aa81 111
Rhyme 0:46a79748aa81 112 /** inspector for _duty
Rhyme 0:46a79748aa81 113 *
Rhyme 0:46a79748aa81 114 * @returns int duty (0~100%)
Rhyme 0:46a79748aa81 115 */
Rhyme 0:46a79748aa81 116 virtual int duty(void) ; // get duty
Rhyme 0:46a79748aa81 117
Rhyme 0:46a79748aa81 118 /** update internal value consistancy
Rhyme 0:46a79748aa81 119 */
Rhyme 0:46a79748aa81 120 virtual void update(void) ; // adjust for time unit change
Rhyme 0:46a79748aa81 121
Rhyme 0:46a79748aa81 122 /** advance position to be accessed through value()
Rhyme 0:46a79748aa81 123 *
Rhyme 0:46a79748aa81 124 * @param int s steps to advance, 1 for default
Rhyme 0:46a79748aa81 125 */
Rhyme 0:46a79748aa81 126 virtual void advance(int s = 1) ; // advance 1 tu
Rhyme 0:46a79748aa81 127
Rhyme 0:46a79748aa81 128 /** inspector for the value at _pos
Rhyme 0:46a79748aa81 129 *
Rhyme 0:46a79748aa81 130 * @returns the value at _pos
Rhyme 0:46a79748aa81 131 */
Rhyme 0:46a79748aa81 132 virtual int value(void) ; // returns current value
Rhyme 0:46a79748aa81 133
Rhyme 0:46a79748aa81 134 /** inspector to return class name
Rhyme 0:46a79748aa81 135 */
Rhyme 0:46a79748aa81 136 virtual const char *name(void) ;
Rhyme 0:46a79748aa81 137
Rhyme 0:46a79748aa81 138 protected:
Rhyme 0:46a79748aa81 139 /** utility function to convert phase to position
Rhyme 0:46a79748aa81 140 *
Rhyme 0:46a79748aa81 141 * @param p phase value to be converted to the position
Rhyme 0:46a79748aa81 142 * @returns position of specified phase
Rhyme 0:46a79748aa81 143 */
Rhyme 0:46a79748aa81 144 inline int phase2pos(int p) ;
Rhyme 0:46a79748aa81 145
Rhyme 0:46a79748aa81 146 /** utility function to convert position to phase
Rhyme 0:46a79748aa81 147 *
Rhyme 0:46a79748aa81 148 * @param p position to be converted to the phase
Rhyme 0:46a79748aa81 149 * @returns phase of specified position
Rhyme 0:46a79748aa81 150 */
Rhyme 0:46a79748aa81 151 inline int pos2phase(int p) ;
Rhyme 0:46a79748aa81 152
Rhyme 0:46a79748aa81 153 int _amp ; /* 0~0xFFFF (12bit for Kinetis) */
Rhyme 0:46a79748aa81 154 int _cycle ; /* 2~20000 10kHz ~ 1Hz */
Rhyme 0:46a79748aa81 155 int _phase ; /* 0-360 degree */
Rhyme 0:46a79748aa81 156 int _freq ; /* 1/cycle 50kHz ~ 1Hz */
Rhyme 0:46a79748aa81 157 int _pos ; /* position in cycle */
Rhyme 0:46a79748aa81 158 int _duty ; /* 0-100 % */
Rhyme 0:46a79748aa81 159 char *_name ;
Rhyme 0:46a79748aa81 160 private:
Rhyme 0:46a79748aa81 161 } ;
Rhyme 0:46a79748aa81 162
Rhyme 0:46a79748aa81 163 extern int sample_time ; // sampling interval in us
Rhyme 0:46a79748aa81 164 extern float vref ; // voltage at 0xFFFF
Rhyme 0:46a79748aa81 165
Rhyme 0:46a79748aa81 166 #endif