Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /************************** 00002 For high-voltage switching circuit test 00003 2016 Sep. 2- 00004 Hiroyuki Kajimoto 00005 *************************** 00006 2020/08/02 00007 編集者:福田哲生 00008 変更点: 00009 以前のStandAlone_demoでは常にひげのジョリジョリ感を 00010 570Vで出し続けるプログラムであった。 00011 00012 今回は3Dプリンタによるハードケースおよび、 00013 モバイルバッテリーと昇圧ケーブルによる 00014 ポータブル化がすすめられたため 00015 ハードケースに付属させたスイッチによって 00016 刺激を変更できるようにした。 00017 00018 ハードケースをつけるとmbed本体LEDは見えないが 00019 電源スイッチオン(初期状態)で無灯火、刺激なし 00020 の状態から切り替えスイッチを1回押すごとに 00021 LED1点灯、sine波 00022 LED2点灯、ひげジョリジョリ 00023 LED3点灯、羊角ゴツゴツ 00024 LED4点灯、羊体モフモフ 00025 LED1,2点灯、犬サラモフ 00026 LED1,3点灯、炭酸シュワシュワ 00027 と、LED表示および刺激が遷移していく。 00028 00029 なお赤い基板にあるp16の部分と3.3V,GNDをスイッチに配線し 00030 p16がHighになると切り換わるようにした。 00031 チャタリング対策に200msの待機時間を設けた。 00032 00033 ***************************/ 00034 00035 #include "mbed.h" 00036 #include <stdio.h> 00037 #include <stdlib.h> 00038 00039 DigitalOut SN74LV595_DIN(p14); 00040 DigitalOut SN74LV595_RCLK(p13); 00041 DigitalOut SN74LV595_CLR(p12); 00042 DigitalOut SN74LV595_CLK(p11); 00043 DigitalIn SN74LV595_DOUT(p10); //Shift register output, so normally it is not connected. 00044 00045 DigitalIn Switch(p16);//スイッチ用ピン 00046 00047 //DAAD 00048 SPI spiDAAD(p5, p6, p7); // mosi(master output slave input, miso(not connected), clock signal 00049 DigitalOut DA_sync(p8); //chip select for AD5452 00050 DigitalOut AD_cs(p9); //chip select for AD7276 00051 00052 //Other I/O 00053 BusOut myleds(LED1, LED2, LED3, LED4); 00054 00055 //LED1からLED4をled1からled4に割り当てる。 00056 DigitalOut led1(LED1); 00057 DigitalOut led2(LED2); 00058 DigitalOut led3(LED3); 00059 DigitalOut led4(LED4); 00060 00061 00062 // stimulation mode 00063 #define DA_TEST 0xFA //sinusoidal wave mode to test DA 00064 #define MIN_TIME_DIFF 200//チャタリング用待機時間 00065 00066 const int ELECTRODE_NUM = 16; 00067 short stim_pattern[ELECTRODE_NUM] = { 0 }; 00068 short impedance[ELECTRODE_NUM] = { 0 }; 00069 00070 Timer timer; 00071 Timer mytimer; 00072 00073 bool AccessDeny = false; 00074 00075 int i; 00076 int Freq = 100;//正弦波、ひげの周波数 00077 int trigger = 0;//三角波用トリガー 00078 double LowFreq = 0; 00079 int TIME = 0;//炭酸用 00080 00081 int swflag = 0;//スイッチ用フラグ 00082 00083 00084 /******************************************************************************/ 00085 /* 00086 SN74LV595 Data Transfer 00087 2 bits are required for 1 electrode. 00088 00 OPEN 00089 10 GND 00090 01 HIGH 00091 11 SHORT 00092 */ 00093 /******************************************************************************/ 00094 void SN74LV595FastScan(int usWhichPin) 00095 { 00096 int ii, pin; 00097 static int pos; 00098 00099 SN74LV595_RCLK = 0; 00100 if (usWhichPin == 0) { //set 01(High) 00101 SN74LV595_DIN = 0; 00102 SN74LV595_CLK = 1; 00103 SN74LV595_CLK = 0; 00104 SN74LV595_DIN = 1; 00105 SN74LV595_CLK = 1; 00106 SN74LV595_CLK = 0; 00107 pos = 0; 00108 } 00109 else { 00110 pin = usWhichPin - pos; 00111 for (ii = 0; ii < pin; ii++) {//set 10 (GND) 00112 SN74LV595_DIN = 1; 00113 SN74LV595_CLK = 1; 00114 SN74LV595_CLK = 0; 00115 SN74LV595_DIN = 0; 00116 SN74LV595_CLK = 1; 00117 SN74LV595_CLK = 0; 00118 } 00119 pos = usWhichPin; 00120 } 00121 //Load S/R 00122 SN74LV595_RCLK = 1; 00123 SN74LV595_RCLK = 0; 00124 } 00125 00126 /******************************************************************************/ 00127 /* 00128 SN74LV595 init & Clear 00129 */ 00130 /******************************************************************************/ 00131 void SN74LV595Clear() 00132 { 00133 SN74LV595_CLR = 0; 00134 SN74LV595_RCLK = 0; 00135 SN74LV595_CLK = 0; 00136 SN74LV595_CLK = 1; 00137 SN74LV595_CLK = 0; 00138 SN74LV595_CLR = 1; 00139 } 00140 00141 void SN74LV595Init(int TotalPin) 00142 { 00143 int ii; 00144 00145 SN74LV595_CLR = 1; 00146 SN74LV595_CLK = 0; 00147 SN74LV595_RCLK = 0; 00148 for (ii = 0; ii < TotalPin; ii++) { 00149 SN74LV595_DIN = 1; 00150 SN74LV595_CLK = 1; 00151 SN74LV595_CLK = 0; 00152 SN74LV595_DIN = 0; 00153 SN74LV595_CLK = 1; 00154 SN74LV595_CLK = 0; 00155 } 00156 //Load S/R 00157 SN74LV595_RCLK = 1; 00158 SN74LV595_RCLK = 0; 00159 } 00160 00161 00162 /******************************************************************************/ 00163 /* 00164 DA&AD at the same time, using the same SPI clock. 00165 DA output by AD5452(SPI) 00166 AD input by AD7276(SPI) 00167 */ 00168 /******************************************************************************/ 00169 00170 short DAAD(short DA) 00171 { 00172 short AD; 00173 00174 //enable 00175 DA_sync = 0; 00176 AD_cs = 0; 00177 //simultaneous DA and AD 00178 AD = spiDAAD.write(DA << 2); 00179 //disable 00180 DA_sync = 1; 00181 AD_cs = 1; 00182 00183 return AD >> 2;//bottom 2bits are unnecessary 00184 } 00185 00186 void DAADinit() 00187 { 00188 //Setup SPI, 16bit, falling edge, 48MHz clock 00189 spiDAAD.format(16, 2); 00190 spiDAAD.frequency(48000000); 00191 } 00192 00193 00194 int main() 00195 { 00196 double t=0.0; 00197 short AD; 00198 int Randnum[10] = {400,600,200,500,700,200,600,300,600,500};//待機時間の配列 00199 int timediff = 0; 00200 Switch.mode(PullDown); 00201 00202 DAADinit(); 00203 SN74LV595Init(ELECTRODE_NUM); 00204 timer.start(); 00205 mytimer.start(); 00206 00207 while (1) { 00208 00209 if(Switch.read() == 1){ 00210 timediff = mytimer.read_ms(); 00211 if(timediff > MIN_TIME_DIFF) { 00212 swflag = swflag + 1; 00213 } 00214 mytimer.reset(); 00215 } 00216 if(swflag == 7){ 00217 swflag = 1; 00218 } 00219 if (swflag == 0){ 00220 myleds = 0; 00221 } 00222 if (swflag == 1) { //sin波を出力します 00223 t = (double)timer.read_us() * 0.000001; 00224 AD = DAAD((short)285 + (285 * (sin(2.0 * 3.1415926 * Freq * t)))); //SinWave 00225 led1 = 1; 00226 led2 = 0; 00227 led3 = 0; 00228 led4 = 0; 00229 } 00230 else if (swflag == 2) { //ひげのジョリジョリ(矩形波)を出力します 00231 t = (double)timer.read_us() * 0.000001; 00232 for(i =0; i<10; i++){ 00233 double Freqdevided1 = 200 / Freq;//出力時間 00234 double Freqdevided2 = Randnum[i] / Freq;//待機時間は配列でランダム(?)化 00235 double Freqint1 = floor(Freqdevided1);//整数化 00236 double Freqint2 = floor(Freqdevided2); 00237 AD = DAAD(570); 00238 wait_ms(Freqint1); 00239 DAAD(0); 00240 wait_ms(Freqint2); 00241 AD = DAAD(400); 00242 wait_ms(Freqint1); 00243 DAAD(0); 00244 wait_ms(Freqint2); 00245 led1 = 0; 00246 led2 = 1; 00247 led3 = 0; 00248 led4 = 0; 00249 } 00250 } 00251 else if (swflag == 3) {//羊の角のゴツゴツ感(三角波)を出力します 00252 for(int j = 0; j<10; j++){ 00253 t = (double)timer.read_us() * 0.000001; 00254 double sigma = 0;//フーリエ級数のシグマ 00255 for(i = 1; i < 4; i++){ 00256 sigma += sin(i * 3.1415926 / 2) * sin(i * t * 6.29 * 40)/(i * i);//三角波をフーリエ級数変換したやつ 00257 } 00258 AD = DAAD((short) 300 + (8 * 300 * sigma / (3.1415926 * 3.1415926))); 00259 if(AD < 100 && trigger == 1){//トリガーが1だといくらか待ちます 00260 wait_ms(Randnum[j] / 20); 00261 trigger = 0; 00262 } 00263 if(AD > 100 && trigger == 0){//トリガーが0の時は普通に三角波を出力してトリガーを1に変更 00264 trigger = 1; 00265 } 00266 } 00267 led1 = 0; 00268 led2 = 0; 00269 led3 = 1; 00270 led4 = 0; 00271 } 00272 else if (swflag == 4){//羊の体のモフモフ感(エンベロープ)を出力します 00273 t = (double)timer.read_us() * 0.000001; 00274 LowFreq = (short)(144 * (1.0 + sin(2.0 * 3.1415926 * 7 * t))); //低周波の方 00275 AD = DAAD((short)(LowFreq * ((1.0 + sin(2.0 * 3.1415926 * 200 * (t+5000000)))))); 00276 led1 = 0; 00277 led2 = 0; 00278 led3 = 0; 00279 led4 = 1; 00280 } 00281 else if (swflag == 5) {//犬の体のサラサラ感(エンベロープ)を出力します。 00282 t = (double)timer.read_us() * 0.000001; 00283 LowFreq = (short)(200 * (1.0 + sin(2.0 * 3.1415926 * 80 * t))); //低周波の方 00284 AD = DAAD((short)(LowFreq / 2 * ((1.0 + sin(2.0 * 3.1415926 * 320 * (t+5000000))) + (1.0 + sin(2.0 * 3.1415926 * 240 * (t+5000000))))));//低周波の出力を振幅にして合成波を出力します 00285 led1 = 1; 00286 led2 = 1; 00287 led3 = 0; 00288 led4 = 0; 00289 } 00290 else if (swflag == 6) {//炭酸のシュワシュワ感を出力します。 00291 t = (double)timer.read_us() * 0.000001; 00292 AD = DAAD((short)(570 * (1.0 + sin(2.0 * 3.1415926 * Freq * t)))); 00293 TIME = TIME +1; 00294 if(TIME>1000){ 00295 double Freqdevided1 = 100*(rand()%3)/ Freq;//出力時間はランダム化 00296 double Freqdevided2 = 10*(rand()%10);//待機時間もランダム化 00297 double Freqint1 = floor(Freqdevided1);//整数化 00298 double Freqint2 = floor(Freqdevided2); 00299 AD = DAAD(100*(3+rand()%3)); 00300 wait_ms(Freqint1); 00301 AD = DAAD(0); 00302 wait_ms(Freqint2); 00303 } 00304 led1 = 1; 00305 led2 = 0; 00306 led3 = 1; 00307 led4 = 0; 00308 } 00309 } 00310 }
Generated on Tue Aug 9 2022 20:53:09 by
 1.7.2
 1.7.2