Transistor Gijutsu, October 2014, Special Features Chapter 9, Software of the Function Generator トランジスタ技術2014年10月号 特集第9章のソフトウェア わがまま波形発生器のソフトウェア
Information
tg_201410s8_AD7714 トランジスタ技術 2014年 10月号 第9章のソフトウェア
Program for Section 9 in October. 2014 issue of the Transistor Gijutsu
(Japanese electronics magazine)
概要
このプログラムは、ソフトウエアDDSにより、任意の波形を出力(2ch)します。 特徴は次のとおりです。
- PWM出力をDAコンバータとして利用します。
- 周波数や波形、バースト条件などを個別に設定できる独立した出力を2チャネル持っています。
- 周波数分解能0.023mHz
- 周波数範囲0.023mHz~10kHz
- 各チャネルにそれぞれ、波形の先頭で出力されるトリガ出力があります。
- 出力波形を関数で定義できます。
- 休止波数、出力波数、を設定することでバースト波形が出力できます。
ファイル
このソフトウエアは、次のファイルから構成されています。
- DDS.cpp - DDSによる波形発生
- main.cpp - main()関数
詳細については、10月号の記事および上記ファイル中のコメントを参照してください。
DDS/DDS.cpp@0:f1ecca559ec3, 2014-08-29 (annotated)
- Committer:
- Dance
- Date:
- Fri Aug 29 08:33:17 2014 +0000
- Revision:
- 0:f1ecca559ec3
Transistor Gijutsu, October 2014, Special Features Chapter 9; ????????2014?10??????9????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dance | 0:f1ecca559ec3 | 1 | #include "mbed.h" |
Dance | 0:f1ecca559ec3 | 2 | #include "USBSerial.h" |
Dance | 0:f1ecca559ec3 | 3 | #include "DDS.h" |
Dance | 0:f1ecca559ec3 | 4 | #include "DigitalOut.h" |
Dance | 0:f1ecca559ec3 | 5 | #include "math.h" |
Dance | 0:f1ecca559ec3 | 6 | |
Dance | 0:f1ecca559ec3 | 7 | DigitalOut out1(P0_2); // osc1のトリガ出力ピン |
Dance | 0:f1ecca559ec3 | 8 | DigitalOut out2(P0_23); // osc2のトリガ出力ピン |
Dance | 0:f1ecca559ec3 | 9 | |
Dance | 0:f1ecca559ec3 | 10 | // 最適化指定。DDS基準周波数100kHzに必要。 |
Dance | 0:f1ecca559ec3 | 11 | #pragma O3 |
Dance | 0:f1ecca559ec3 | 12 | |
Dance | 0:f1ecca559ec3 | 13 | // スタティック変数の領域確保 |
Dance | 0:f1ecca559ec3 | 14 | int16_t DDS::waveForm1[DDS::TBLSIZE]; |
Dance | 0:f1ecca559ec3 | 15 | unsigned int DDS::phaseAcc1 = 0; |
Dance | 0:f1ecca559ec3 | 16 | unsigned int DDS::phaseInc1 = 0; |
Dance | 0:f1ecca559ec3 | 17 | unsigned int DDS::phaseShift1 = 0; |
Dance | 0:f1ecca559ec3 | 18 | int DDS::bstBgn1; |
Dance | 0:f1ecca559ec3 | 19 | int DDS::bstEnd1; |
Dance | 0:f1ecca559ec3 | 20 | int DDS::bstLen1; |
Dance | 0:f1ecca559ec3 | 21 | int DDS::bstDcL1; |
Dance | 0:f1ecca559ec3 | 22 | int DDS::trigger1; |
Dance | 0:f1ecca559ec3 | 23 | int DDS::wc1; |
Dance | 0:f1ecca559ec3 | 24 | int DDS::lp1; |
Dance | 0:f1ecca559ec3 | 25 | |
Dance | 0:f1ecca559ec3 | 26 | int16_t DDS::waveForm2[DDS::TBLSIZE]; |
Dance | 0:f1ecca559ec3 | 27 | unsigned int DDS::phaseAcc2 = 0; |
Dance | 0:f1ecca559ec3 | 28 | unsigned int DDS::phaseInc2 = 0; |
Dance | 0:f1ecca559ec3 | 29 | unsigned int DDS::phaseShift2 = 0; |
Dance | 0:f1ecca559ec3 | 30 | int DDS::bstBgn2; |
Dance | 0:f1ecca559ec3 | 31 | int DDS::bstEnd2; |
Dance | 0:f1ecca559ec3 | 32 | int DDS::bstLen2; |
Dance | 0:f1ecca559ec3 | 33 | int DDS::bstDcL2; |
Dance | 0:f1ecca559ec3 | 34 | int DDS::trigger2; |
Dance | 0:f1ecca559ec3 | 35 | int DDS::wc2; |
Dance | 0:f1ecca559ec3 | 36 | int DDS::lp2; |
Dance | 0:f1ecca559ec3 | 37 | |
Dance | 0:f1ecca559ec3 | 38 | |
Dance | 0:f1ecca559ec3 | 39 | // 割り込みハンドラ |
Dance | 0:f1ecca559ec3 | 40 | void PWM1_IRQHandler (void) |
Dance | 0:f1ecca559ec3 | 41 | { |
Dance | 0:f1ecca559ec3 | 42 | static int pw1 = 1; |
Dance | 0:f1ecca559ec3 | 43 | static int pw2 = 1; |
Dance | 0:f1ecca559ec3 | 44 | LPC_CT32B1->IR |= 0x01; // 割り込み要因のリセット |
Dance | 0:f1ecca559ec3 | 45 | LPC_CT32B1->MR1 = pw1; // PWM1パルス幅変更 |
Dance | 0:f1ecca559ec3 | 46 | LPC_CT32B1->MR2 = pw2; // PWM2パルス幅変更 |
Dance | 0:f1ecca559ec3 | 47 | out1 = DDS::trigger1; // トリガパルス出力 |
Dance | 0:f1ecca559ec3 | 48 | out2 = DDS::trigger2; |
Dance | 0:f1ecca559ec3 | 49 | DDS::trigger1 = 0; // トリガ要因リセット |
Dance | 0:f1ecca559ec3 | 50 | DDS::trigger2 = 0; |
Dance | 0:f1ecca559ec3 | 51 | |
Dance | 0:f1ecca559ec3 | 52 | // 次のPWM幅とトリガ要因を計算 |
Dance | 0:f1ecca559ec3 | 53 | pw1 = DDS::getNextPw1(); |
Dance | 0:f1ecca559ec3 | 54 | pw2 = DDS::getNextPw2(); |
Dance | 0:f1ecca559ec3 | 55 | // トリガパルス終了。パルス幅はPWM計算時間。 |
Dance | 0:f1ecca559ec3 | 56 | out1 = 0; |
Dance | 0:f1ecca559ec3 | 57 | out2 = 0; |
Dance | 0:f1ecca559ec3 | 58 | } |
Dance | 0:f1ecca559ec3 | 59 | |
Dance | 0:f1ecca559ec3 | 60 | // osc1次のパルス幅を得る |
Dance | 0:f1ecca559ec3 | 61 | inline int DDS::getNextPw1(void) |
Dance | 0:f1ecca559ec3 | 62 | { |
Dance | 0:f1ecca559ec3 | 63 | static int ret = 0; |
Dance | 0:f1ecca559ec3 | 64 | |
Dance | 0:f1ecca559ec3 | 65 | // 位相の更新 |
Dance | 0:f1ecca559ec3 | 66 | phaseAcc1 += phaseInc1; |
Dance | 0:f1ecca559ec3 | 67 | // バースト信号処理 |
Dance | 0:f1ecca559ec3 | 68 | if(bstBgn1 <= wc1 && wc1 <= bstEnd1){ |
Dance | 0:f1ecca559ec3 | 69 | ret = waveForm1[(phaseAcc1 + phaseShift1)>> WFRES]; |
Dance | 0:f1ecca559ec3 | 70 | }else{ |
Dance | 0:f1ecca559ec3 | 71 | ret = bstDcL1; |
Dance | 0:f1ecca559ec3 | 72 | } |
Dance | 0:f1ecca559ec3 | 73 | if(lp1 > phaseAcc1){ |
Dance | 0:f1ecca559ec3 | 74 | // バースト出力の先頭でトリガパルスを発生する |
Dance | 0:f1ecca559ec3 | 75 | wc1++; |
Dance | 0:f1ecca559ec3 | 76 | if(wc1 > bstLen1){ |
Dance | 0:f1ecca559ec3 | 77 | wc1 = 1; |
Dance | 0:f1ecca559ec3 | 78 | trigger1 = 1; |
Dance | 0:f1ecca559ec3 | 79 | } |
Dance | 0:f1ecca559ec3 | 80 | } |
Dance | 0:f1ecca559ec3 | 81 | lp1 = phaseAcc1; |
Dance | 0:f1ecca559ec3 | 82 | |
Dance | 0:f1ecca559ec3 | 83 | return ret; |
Dance | 0:f1ecca559ec3 | 84 | }; |
Dance | 0:f1ecca559ec3 | 85 | |
Dance | 0:f1ecca559ec3 | 86 | // osc2次のパルス幅を得る |
Dance | 0:f1ecca559ec3 | 87 | inline int DDS::getNextPw2(void) |
Dance | 0:f1ecca559ec3 | 88 | { |
Dance | 0:f1ecca559ec3 | 89 | static int ret = 0; |
Dance | 0:f1ecca559ec3 | 90 | |
Dance | 0:f1ecca559ec3 | 91 | phaseAcc2 += phaseInc2; |
Dance | 0:f1ecca559ec3 | 92 | if(bstBgn2 <= wc2 && wc2 <= bstEnd2){ |
Dance | 0:f1ecca559ec3 | 93 | ret = waveForm2[(phaseAcc2 + phaseShift2) >> WFRES]; |
Dance | 0:f1ecca559ec3 | 94 | }else{ |
Dance | 0:f1ecca559ec3 | 95 | ret = bstDcL2; |
Dance | 0:f1ecca559ec3 | 96 | } |
Dance | 0:f1ecca559ec3 | 97 | if(lp2 > phaseAcc2){ |
Dance | 0:f1ecca559ec3 | 98 | wc2++; |
Dance | 0:f1ecca559ec3 | 99 | if(wc2 > bstLen2){ |
Dance | 0:f1ecca559ec3 | 100 | wc2 = 1; |
Dance | 0:f1ecca559ec3 | 101 | trigger2 = 1; |
Dance | 0:f1ecca559ec3 | 102 | } |
Dance | 0:f1ecca559ec3 | 103 | } |
Dance | 0:f1ecca559ec3 | 104 | lp2 = phaseAcc2; |
Dance | 0:f1ecca559ec3 | 105 | return ret; |
Dance | 0:f1ecca559ec3 | 106 | }; |
Dance | 0:f1ecca559ec3 | 107 | |
Dance | 0:f1ecca559ec3 | 108 | void DDS::reset(void) |
Dance | 0:f1ecca559ec3 | 109 | { |
Dance | 0:f1ecca559ec3 | 110 | // 割り込みを停止してから変数を初期化 |
Dance | 0:f1ecca559ec3 | 111 | NVIC_DisableIRQ(TIMER_32_1_IRQn); |
Dance | 0:f1ecca559ec3 | 112 | phaseAcc1 = phaseAcc2 = 0; |
Dance | 0:f1ecca559ec3 | 113 | wc1 = wc2 = 1; |
Dance | 0:f1ecca559ec3 | 114 | lp1 = lp2 = 0xffffffff; |
Dance | 0:f1ecca559ec3 | 115 | NVIC_EnableIRQ(TIMER_32_1_IRQn); |
Dance | 0:f1ecca559ec3 | 116 | } |
Dance | 0:f1ecca559ec3 | 117 | |
Dance | 0:f1ecca559ec3 | 118 | // osc1バースト波形の設定 |
Dance | 0:f1ecca559ec3 | 119 | void DDS::burst1(int begin, int end, int length, float dcLevel) |
Dance | 0:f1ecca559ec3 | 120 | { |
Dance | 0:f1ecca559ec3 | 121 | bstBgn1 = begin; |
Dance | 0:f1ecca559ec3 | 122 | bstEnd1 = end; |
Dance | 0:f1ecca559ec3 | 123 | bstLen1 = length; |
Dance | 0:f1ecca559ec3 | 124 | bstDcL1 = WFVSPN - WFMAG * dcLevel * WFVSPN; |
Dance | 0:f1ecca559ec3 | 125 | } |
Dance | 0:f1ecca559ec3 | 126 | |
Dance | 0:f1ecca559ec3 | 127 | // osc1バースト波形の設定 |
Dance | 0:f1ecca559ec3 | 128 | void DDS::burst2(int begin, int end, int length, float dcLevel) |
Dance | 0:f1ecca559ec3 | 129 | { |
Dance | 0:f1ecca559ec3 | 130 | bstBgn2 = begin; |
Dance | 0:f1ecca559ec3 | 131 | bstEnd2 = end; |
Dance | 0:f1ecca559ec3 | 132 | bstLen2 = length; |
Dance | 0:f1ecca559ec3 | 133 | bstDcL2 = WFVSPN - WFMAG * dcLevel * WFVSPN; |
Dance | 0:f1ecca559ec3 | 134 | } |
Dance | 0:f1ecca559ec3 | 135 | |
Dance | 0:f1ecca559ec3 | 136 | void DDS::fillTable(int16_t wftbl[], float level, float ofst, float (*wf)(float x)) |
Dance | 0:f1ecca559ec3 | 137 | { |
Dance | 0:f1ecca559ec3 | 138 | level = (level < -2.0) ? -2.0 : level; |
Dance | 0:f1ecca559ec3 | 139 | level = (level > 2.0) ? 2.0 : level; |
Dance | 0:f1ecca559ec3 | 140 | |
Dance | 0:f1ecca559ec3 | 141 | for(int a = 0; a < TBLSIZE; a++){ |
Dance | 0:f1ecca559ec3 | 142 | // ラジアンを引数として関数値を計算 |
Dance | 0:f1ecca559ec3 | 143 | float val = wf(2.0 * PI * a / TBLSIZE); |
Dance | 0:f1ecca559ec3 | 144 | val = (val < -1.0) ? -1.0 : val; |
Dance | 0:f1ecca559ec3 | 145 | val = (val > 1.0) ? 1.0 : val; |
Dance | 0:f1ecca559ec3 | 146 | // PWMの極性に合わせるためvalの符号を反転し、PWM値に変換 |
Dance | 0:f1ecca559ec3 | 147 | //val = (level * val + ofst) * WFMAG * WFVSPN; |
Dance | 0:f1ecca559ec3 | 148 | val = (level * val + ofst) * WFMAG * WFVSPN; |
Dance | 0:f1ecca559ec3 | 149 | wftbl[a] = WFVSPN - val; |
Dance | 0:f1ecca559ec3 | 150 | } |
Dance | 0:f1ecca559ec3 | 151 | } |
Dance | 0:f1ecca559ec3 | 152 | |
Dance | 0:f1ecca559ec3 | 153 | /// OSC1の波形定義 |
Dance | 0:f1ecca559ec3 | 154 | void DDS::osc1(float f, float p, float level, float ofst, float (*wf)(float x)) |
Dance | 0:f1ecca559ec3 | 155 | { |
Dance | 0:f1ecca559ec3 | 156 | phaseInc1 = (PACCLEN / DDSCLK) * f; |
Dance | 0:f1ecca559ec3 | 157 | if(p < 0){ |
Dance | 0:f1ecca559ec3 | 158 | p = 360 - p; |
Dance | 0:f1ecca559ec3 | 159 | } |
Dance | 0:f1ecca559ec3 | 160 | phaseShift1 = PACCLEN * p / 360.0; |
Dance | 0:f1ecca559ec3 | 161 | fillTable(waveForm1, level, ofst, wf); |
Dance | 0:f1ecca559ec3 | 162 | } |
Dance | 0:f1ecca559ec3 | 163 | |
Dance | 0:f1ecca559ec3 | 164 | /// OSC2の波形定義 |
Dance | 0:f1ecca559ec3 | 165 | void DDS::osc2(float f, float p, float level, float ofst, float (*wf)(float x)) |
Dance | 0:f1ecca559ec3 | 166 | { |
Dance | 0:f1ecca559ec3 | 167 | // 位相増分 |
Dance | 0:f1ecca559ec3 | 168 | phaseInc2 = (PACCLEN / DDSCLK) * f; |
Dance | 0:f1ecca559ec3 | 169 | if(p < 0){ |
Dance | 0:f1ecca559ec3 | 170 | p = 360 - p; |
Dance | 0:f1ecca559ec3 | 171 | } |
Dance | 0:f1ecca559ec3 | 172 | phaseShift2 = PACCLEN * p / 360.0; |
Dance | 0:f1ecca559ec3 | 173 | fillTable(waveForm2, level, ofst, wf); |
Dance | 0:f1ecca559ec3 | 174 | } |
Dance | 0:f1ecca559ec3 | 175 | |
Dance | 0:f1ecca559ec3 | 176 | void DDS::osc1(float freq, float phase) |
Dance | 0:f1ecca559ec3 | 177 | { |
Dance | 0:f1ecca559ec3 | 178 | phaseInc1 = (PACCLEN / DDSCLK) * freq; |
Dance | 0:f1ecca559ec3 | 179 | phaseShift1 = PACCLEN * phase / 360.0; |
Dance | 0:f1ecca559ec3 | 180 | } |
Dance | 0:f1ecca559ec3 | 181 | |
Dance | 0:f1ecca559ec3 | 182 | void DDS::osc2(float freq, float phase) |
Dance | 0:f1ecca559ec3 | 183 | { |
Dance | 0:f1ecca559ec3 | 184 | phaseInc2 = (PACCLEN / DDSCLK) * freq; |
Dance | 0:f1ecca559ec3 | 185 | phaseShift2 = PACCLEN * phase / 360.0; |
Dance | 0:f1ecca559ec3 | 186 | } |
Dance | 0:f1ecca559ec3 | 187 | |
Dance | 0:f1ecca559ec3 | 188 | // 三角波 |
Dance | 0:f1ecca559ec3 | 189 | float triangle(float x) |
Dance | 0:f1ecca559ec3 | 190 | { |
Dance | 0:f1ecca559ec3 | 191 | if(x < PI){ |
Dance | 0:f1ecca559ec3 | 192 | return 2.0 * x / PI - 1.0; |
Dance | 0:f1ecca559ec3 | 193 | }else{ |
Dance | 0:f1ecca559ec3 | 194 | return 2.0 * (2 * PI - x) / PI - 1.0; |
Dance | 0:f1ecca559ec3 | 195 | } |
Dance | 0:f1ecca559ec3 | 196 | } |
Dance | 0:f1ecca559ec3 | 197 | |
Dance | 0:f1ecca559ec3 | 198 | // 三角関数の合成による三角波 |
Dance | 0:f1ecca559ec3 | 199 | float triangleSin(float x) |
Dance | 0:f1ecca559ec3 | 200 | { |
Dance | 0:f1ecca559ec3 | 201 | x -= PI / 2.0; |
Dance | 0:f1ecca559ec3 | 202 | return 8.0 / PI / PI * (sin(x) - sin(3.0 * x) / 9.0 + sin(5.0 * x) / 25.0); |
Dance | 0:f1ecca559ec3 | 203 | //return 8.0 / PI / PI * (sin(x) - sin(3.0 * x) / 9.0 + sin(5.0 * x) / 25.0 - sin(7.0 * x) / 49.0 + sin(9.0 * x)/81.0); |
Dance | 0:f1ecca559ec3 | 204 | } |
Dance | 0:f1ecca559ec3 | 205 | |
Dance | 0:f1ecca559ec3 | 206 | // 方形波 |
Dance | 0:f1ecca559ec3 | 207 | float square(float x) |
Dance | 0:f1ecca559ec3 | 208 | { |
Dance | 0:f1ecca559ec3 | 209 | if(x < PI){ |
Dance | 0:f1ecca559ec3 | 210 | return 1.0; |
Dance | 0:f1ecca559ec3 | 211 | }else{ |
Dance | 0:f1ecca559ec3 | 212 | return -1.0; |
Dance | 0:f1ecca559ec3 | 213 | } |
Dance | 0:f1ecca559ec3 | 214 | } |
Dance | 0:f1ecca559ec3 | 215 | |
Dance | 0:f1ecca559ec3 | 216 | // コンソトラクタ、ハードウエアの初期化 |
Dance | 0:f1ecca559ec3 | 217 | DDS::DDS(void) |
Dance | 0:f1ecca559ec3 | 218 | { |
Dance | 0:f1ecca559ec3 | 219 | LPC_IOCON->TRST_PIO0_14 = 0x83; // P0_14 -> CT32B1_MAT1 |
Dance | 0:f1ecca559ec3 | 220 | LPC_IOCON->SWDIO_PIO0_15 = 0x83; // P0_15 -> CT32B1_MAT2 |
Dance | 0:f1ecca559ec3 | 221 | LPC_SYSCON->SYSAHBCLKCTRL |= 0x1 << 10; // Enable clock CT32B1 |
Dance | 0:f1ecca559ec3 | 222 | LPC_CT32B1->PWMC = 0xf; // Enable MAT0..3 |
Dance | 0:f1ecca559ec3 | 223 | LPC_CT32B1->MCR = 0x03; // MR0 reset & interrupt |
Dance | 0:f1ecca559ec3 | 224 | |
Dance | 0:f1ecca559ec3 | 225 | LPC_CT32B1->PR = 0; |
Dance | 0:f1ecca559ec3 | 226 | LPC_CT32B1->MR0 = WFVSPN - 1; // PWM周期 |
Dance | 0:f1ecca559ec3 | 227 | LPC_CT32B1->MR1 = WFVSPN / 2; // duty 50% |
Dance | 0:f1ecca559ec3 | 228 | LPC_CT32B1->MR2 = WFVSPN / 2; |
Dance | 0:f1ecca559ec3 | 229 | LPC_CT32B1->TCR = 0x02; // counter reset |
Dance | 0:f1ecca559ec3 | 230 | |
Dance | 0:f1ecca559ec3 | 231 | // デフォルトのDDS波形は1kHz連続正弦波 |
Dance | 0:f1ecca559ec3 | 232 | trigger1 = 0; |
Dance | 0:f1ecca559ec3 | 233 | burst1(1, 1, 1, 1.0); |
Dance | 0:f1ecca559ec3 | 234 | osc1(1000.0, 0.0, 0.5, 1.0, sin); |
Dance | 0:f1ecca559ec3 | 235 | trigger2 = 0; |
Dance | 0:f1ecca559ec3 | 236 | burst2(1, 1, 1, 1.0); |
Dance | 0:f1ecca559ec3 | 237 | osc2(2000.0, 0.0, 0.5, 1.0, sin); |
Dance | 0:f1ecca559ec3 | 238 | reset(); |
Dance | 0:f1ecca559ec3 | 239 | |
Dance | 0:f1ecca559ec3 | 240 | // USB割り込みの優先順位を下げる。 |
Dance | 0:f1ecca559ec3 | 241 | NVIC_SetPriority(USB_IRQn, 3); |
Dance | 0:f1ecca559ec3 | 242 | NVIC_SetPriority(USB_FIQn, 3); |
Dance | 0:f1ecca559ec3 | 243 | NVIC_SetPriority(TIMER_32_1_IRQn, 0); |
Dance | 0:f1ecca559ec3 | 244 | |
Dance | 0:f1ecca559ec3 | 245 | // 割り込み処理の登録と許可 |
Dance | 0:f1ecca559ec3 | 246 | NVIC_SetVector(TIMER_32_1_IRQn, (unsigned int)&PWM1_IRQHandler); |
Dance | 0:f1ecca559ec3 | 247 | NVIC_EnableIRQ(TIMER_32_1_IRQn); |
Dance | 0:f1ecca559ec3 | 248 | } |
Dance | 0:f1ecca559ec3 | 249 | |
Dance | 0:f1ecca559ec3 | 250 | |
Dance | 0:f1ecca559ec3 | 251 | void DDS::start(void) |
Dance | 0:f1ecca559ec3 | 252 | { |
Dance | 0:f1ecca559ec3 | 253 | LPC_CT32B1->TCR = 0x01; // counter enable |
Dance | 0:f1ecca559ec3 | 254 | } |
Dance | 0:f1ecca559ec3 | 255 | |
Dance | 0:f1ecca559ec3 | 256 | void DDS::stop(void) |
Dance | 0:f1ecca559ec3 | 257 | { |
Dance | 0:f1ecca559ec3 | 258 | LPC_CT32B1->TCR = 0x02; // counter reset |
Dance | 0:f1ecca559ec3 | 259 | } |