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 mbed_S9132_stream04 00003 Read out data from Hamamatsu S9132 Profile sensor 00004 and stream data to computer through serial usb 00005 Visarute Pinrod 00006 SonicMEMS Laboratory, ECE, Cornell University, Ithaca, NY, USA 00007 August 20, 2015 00008 ============================================================================*/ 00009 00010 #include "mbed.h" 00011 #include "FastIO.h" 00012 00013 FastOut<p23> stC; 00014 FastOut<p24> clkC; 00015 FastIn<p25> eosX; 00016 FastIn<p26> trigX; 00017 FastIn<p27> eocX; 00018 FastIn<p28> doY; 00019 FastIn<p29> doX; 00020 FastIn<p30> trigY; 00021 00022 //DigitalOut stC(p23); 00023 //DigitalOut clkC(p24); 00024 //DigitalIn eosX(p25); 00025 //DigitalIn trigX(p26); 00026 //DigitalIn eocX(p27); 00027 //DigitalIn doY(p28); 00028 //DigitalIn doX(p29); 00029 //DigitalIn trigY(p30); 00030 // DigitalOut myled(LED1); 00031 00032 // Object 00033 Serial pc(USBTX, USBRX); // tx, rx 00034 00035 void initial(){ 00036 pc.baud(460800); 00037 //pc.baud(921600); 00038 stC=1; 00039 clkC= 0; 00040 } 00041 00042 void readS9132() { 00043 short i=0; 00044 short x[256] = {0}; 00045 short y[256] = {0}; 00046 for(i=0;i<256;i++){ 00047 x[i]=0xEA6; 00048 y[i]=0xEA6; 00049 } 00050 char eoc=2; 00051 char bit=11; 00052 short xp = 0; 00053 short yp = 0; 00054 char xo = 0; 00055 char yo = 0; 00056 short pxi=0; 00057 stC=1; 00058 clkC= 0; 00059 stC=0; 00060 wait_us(1); 00061 clkC= 1; 00062 wait_us(1); 00063 clkC= 0; 00064 wait_us(1); 00065 clkC= 1; 00066 wait_us(1); 00067 clkC= 0; 00068 wait_us(1); 00069 00070 wait_us(1); 00071 stC=1; 00072 //pc.printf("\n\n ========= Restart MBED ========= \n\n"); 00073 for(i=0;i<3200;i++) { 00074 //pc.printf(" i = %d ",i); 00075 clkC= 1; 00076 wait_us(1); 00077 /*pc.printf(" trigX "); 00078 if (trigX.read() == 1){ 00079 pc.printf("1"); 00080 }else{ 00081 pc.printf("0"); 00082 } 00083 pc.printf(" trigY "); 00084 if (trigY.read() == 1){ 00085 pc.printf("1"); 00086 }else{ 00087 pc.printf("0"); 00088 }*/ 00089 clkC= 0; 00090 wait_us(1); 00091 //pc.printf(" eoc "); 00092 if (eocX.read() == 1){ 00093 //pc.printf("1"); 00094 eoc=1; 00095 }else{ 00096 //pc.printf("0"); 00097 eoc=0; 00098 } 00099 /*pc.printf(" eos "); 00100 if (eosX.read() == 1){ 00101 pc.printf("1"); 00102 }else{ 00103 pc.printf("0"); 00104 } 00105 pc.printf(" doX ");*/ 00106 if (doX.read() == 1){ 00107 //pc.printf("1"); 00108 xo=1; 00109 }else{ 00110 //pc.printf("0"); 00111 xo=0; 00112 } 00113 //pc.printf(" doY "); 00114 if (doY.read() == 1){ 00115 //pc.printf("1"); 00116 yo=1; 00117 }else{ 00118 //pc.printf("0"); 00119 yo=0; 00120 } 00121 00122 /*pc.printf(" trigX "); 00123 if (trigX.read() == 1){ 00124 pc.printf("1"); 00125 }else{ 00126 pc.printf("0"); 00127 } 00128 pc.printf(" trigY "); 00129 if (trigY.read() == 1){ 00130 pc.printf("1"); 00131 }else{ 00132 pc.printf("0"); 00133 } 00134 pc.printf("\n"); 00135 */ 00136 00137 if(eoc==0){ 00138 bit--; 00139 if(bit<10){ 00140 xp=xp<<1|xo; 00141 yp=yp<<1|yo; 00142 } 00143 if(bit==0){ 00144 bit=11; 00145 x[pxi]=xp; 00146 y[pxi]=yp; 00147 pxi++; 00148 //pc.printf("pxi= %d, x = %03X, y = %03X\n",pxi,xp,yp); 00149 wait_us(1); 00150 xp=0; 00151 yp=0; 00152 00153 } 00154 } 00155 } 00156 pc.printf("[CAM_READ_ST]"); 00157 for(i=0;i<256;i++){ 00158 pc.printf("%03X%03X",x[i],y[i]); 00159 //pc.printf(" i= %d, x = %03X, y = %03X\n",i,x[i],y[i]); 00160 } 00161 pc.printf("[CAM_READ_ED]\n"); 00162 } 00163 00164 void flushS9132() { 00165 short i=0; 00166 short x[256] = {0}; 00167 short y[256] = {0}; 00168 for(i=0;i<256;i++){ 00169 x[i]=0xEA6; 00170 y[i]=0xEA6; 00171 } 00172 char eoc=2; 00173 char bit=11; 00174 short xp = 0; 00175 short yp = 0; 00176 char xo = 0; 00177 char yo = 0; 00178 short pxi=0; 00179 stC=1; 00180 clkC= 0; 00181 stC=0; 00182 wait_us(1); 00183 clkC= 1; 00184 wait_us(1); 00185 clkC= 0; 00186 wait_us(1); 00187 clkC= 1; 00188 wait_us(1); 00189 clkC= 0; 00190 wait_us(1); 00191 00192 wait_us(1); 00193 stC=1; 00194 //pc.printf("\n\n ========= Restart MBED ========= \n\n"); 00195 for(i=0;i<3200;i++) { 00196 //pc.printf(" i = %d ",i); 00197 clkC= 1; 00198 wait_us(1); 00199 /*pc.printf(" trigX "); 00200 if (trigX.read() == 1){ 00201 pc.printf("1"); 00202 }else{ 00203 pc.printf("0"); 00204 } 00205 pc.printf(" trigY "); 00206 if (trigY.read() == 1){ 00207 pc.printf("1"); 00208 }else{ 00209 pc.printf("0"); 00210 }*/ 00211 clkC= 0; 00212 wait_us(1); 00213 //pc.printf(" eoc "); 00214 if (eocX.read() == 1){ 00215 //pc.printf("1"); 00216 eoc=1; 00217 }else{ 00218 //pc.printf("0"); 00219 eoc=0; 00220 } 00221 /*pc.printf(" eos "); 00222 if (eosX.read() == 1){ 00223 pc.printf("1"); 00224 }else{ 00225 pc.printf("0"); 00226 } 00227 pc.printf(" doX ");*/ 00228 if (doX.read() == 1){ 00229 //pc.printf("1"); 00230 xo=1; 00231 }else{ 00232 //pc.printf("0"); 00233 xo=0; 00234 } 00235 //pc.printf(" doY "); 00236 if (doY.read() == 1){ 00237 //pc.printf("1"); 00238 yo=1; 00239 }else{ 00240 //pc.printf("0"); 00241 yo=0; 00242 } 00243 00244 /*pc.printf(" trigX "); 00245 if (trigX.read() == 1){ 00246 pc.printf("1"); 00247 }else{ 00248 pc.printf("0"); 00249 } 00250 pc.printf(" trigY "); 00251 if (trigY.read() == 1){ 00252 pc.printf("1"); 00253 }else{ 00254 pc.printf("0"); 00255 } 00256 pc.printf("\n"); 00257 */ 00258 00259 if(eoc==0){ 00260 bit--; 00261 if(bit<10){ 00262 xp=xp<<1|xo; 00263 yp=yp<<1|yo; 00264 } 00265 if(bit==0){ 00266 bit=11; 00267 x[pxi]=xp; 00268 y[pxi]=yp; 00269 pxi++; 00270 //pc.printf("pxi= %d, x = %03X, y = %03X\n",pxi,xp,yp); 00271 wait_us(1); 00272 xp=0; 00273 yp=0; 00274 00275 } 00276 } 00277 } 00278 } 00279 00280 int main(){ 00281 // Variable for main function 00282 char buf[16]; // Buffer for the command from PC 00283 initial(); 00284 while(1) { 00285 flushS9132(); 00286 if (pc.readable()) { 00287 //Command format: Start Mode Submode VALUe enD S F F FFFF D 00288 pc.gets(buf, 16); 00289 //pc.printf("Get buf: %s\n",buf); 00290 if ((buf[0] == 'S')&&(buf[7] == 'D')) { 00291 //Mode switch 00292 switch(buf[1]) { 00293 //read all axis mode 00294 case 'c': 00295 // camera 00296 switch(buf[2]) { 00297 case 'o': 00298 readS9132(); 00299 break; 00300 default: 00301 break; 00302 } 00303 default: 00304 break; 00305 } 00306 } 00307 } 00308 } 00309 return 0; 00310 }
Generated on Thu Jul 14 2022 01:46:22 by
1.7.2