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.
Dependencies: FATFileSystem SDFileSystem WIZnetInterface mbed
Fork of adc_dac by
main.cpp
00001 /* 00002 #include "mbed.h" 00003 AnalogIn battery(A3); 00004 Serial uart1(PA_13,PA_14); 00005 int main(){ 00006 while(1){ 00007 if(battery > 0.5){ 00008 uart1.printf("%02f\n",battery.read()*3.3); 00009 wait(1); 00010 } 00011 } 00012 } 00013 */ 00014 00015 /********uart1 demo******** 00016 #include "mbed.h" 00017 00018 Serial uart1(PA_13, PA_14); // tx, rx 00019 00020 int main() { 00021 uart1.printf("Please input something\n"); 00022 00023 while(1) { 00024 char c = uart1.getc(); 00025 uart1.printf("%s\n",c); 00026 } 00027 } 00028 */ 00029 00030 /********uart2 demo******** 00031 #include "mbed.h" 00032 00033 Serial uart2(PC_10, PC_11); // tx, rx 00034 00035 int main() { 00036 uart2.printf("Please input something\n"); 00037 00038 while(1) { 00039 char c = uart2.getc(); 00040 uart2.printf("%s\n",c); 00041 } 00042 } 00043 */ 00044 00045 /**********Digital demo********** 00046 #include "mbed.h" 00047 00048 DigitalOut num1(D10); 00049 DigitalOut num2(D11); 00050 DigitalOut num3(D12); 00051 DigitalOut num4(D2); 00052 DigitalOut num5(D3); 00053 DigitalOut num6(D5); 00054 DigitalOut num7(D6); 00055 00056 int main(){ 00057 while(1){ 00058 wait(1); 00059 num1 = 1; 00060 num2 = 1; 00061 num3 = 1; 00062 00063 wait(1); 00064 num1 = 0; 00065 num2 = 0; 00066 num3 = 0; 00067 00068 } 00069 } 00070 /* */ 00071 00072 /**********analog demo*********** 00073 #include "mbed.h" 00074 AnalogIn battery5(A0); 00075 AnalogIn battery4(PC_9); 00076 AnalogIn battery3(D5); 00077 Serial uart1(PA_13,PA_14); 00078 //Serial pc(USBTX,USBRX); 00079 int main(){ 00080 while(1){ 00081 //if(battery3 > 0.5){ 00082 // uart1.printf("%f\n",battery3.read()*3.3); 00083 // wait(0.2); 00084 // } 00085 //if(battery4 > 0.5){ 00086 // uart1.printf("%f\n",battery4.read()); 00087 //wait(0.2); 00088 //} 00089 if(battery5 > 0.5){ 00090 uart1.printf("%f\n",battery5.read()); 00091 wait(0.2); 00092 } 00093 } 00094 } 00095 */ 00096 00097 /* 00098 #include"mbed.h" 00099 AnalogIn battery(A2); 00100 Serial uart1(PA_13,PA_14); 00101 int main(){ 00102 while(1){ 00103 if(battery > 0.5){ 00104 uart1.printf("%f",battery.read()); 00105 wait(1); 00106 } 00107 } 00108 } 00109 */ 00110 /* 00111 #include "mbed.h" 00112 00113 Serial uart1(PA_13, PA_14); // tx, rx 00114 Serial uart2(PC_10, PC_11); // tx, rx 00115 DigitalOut myD1(D10); 00116 DigitalOut myD2(D11); 00117 DigitalOut myD3(D12); 00118 DigitalOut myD4(D2); 00119 AnalogIn myA1(A1); 00120 AnalogIn myA2(A2); 00121 AnalogIn myA3(A3); 00122 PwmOut PWM1(D3); 00123 PwmOut PWM2(D5); 00124 PwmOut PWM3(D6); 00125 00126 Timeout timeout; 00127 00128 void attimeout() 00129 { 00130 myD1 = 0; 00131 myD2 = 0; 00132 myD3 = 0; 00133 myD4 = 0; 00134 } 00135 00136 int main() { 00137 uart1.baud(115200); 00138 uart2.baud(115200); 00139 PWM1.period_us(500); 00140 PWM2.period_us(500); 00141 PWM3.period_us(500); 00142 PWM1 = 0.2; 00143 PWM2 = 0.5; 00144 PWM3 = 0.8; 00145 uart2.printf("uart2:for Loopback test,please input something\n"); 00146 uart1.printf("start testing uart1 digitalOut and PWM and analogIn\n"); 00147 while(1){ 00148 while(uart2.readable()){ 00149 uart2.printf("%c", uart2.getc()); 00150 } 00151 00152 timeout.attach(&attimeout,2); 00153 00154 myD1 = 1; 00155 myD2 = 1; 00156 myD3 = 1; 00157 myD4 = 1; 00158 wait(0.25); 00159 00160 uart1.printf("A1:%02f\n",myA1.read()*3.3); 00161 uart1.printf("A2:%02f\n",myA2.read()*3.3); 00162 uart1.printf("A3:%02f\n",myA3.read()*3.3); 00163 } 00164 } 00165 */ 00166 00167 #include "mbed.h" 00168 #include <Serial.h> 00169 #include "EthernetInterface.h" 00170 #include "SDFileSystem.h" 00171 #include <stdio.h> 00172 #include <string.h> 00173 00174 #define ADXL345_I2C_READ 0xA7 00175 #define ADXL345_I2C_WRITE 0xA6 00176 #define ADXL345_I2C_ADDRESS 0x53 00177 #define ADXL345_DEVID_REG 0x00 00178 #define ADXL345_DATAX0_REG 0x32 00179 #define ADXL345_POWER_CTL_REG 0x2D 00180 #define MeasurementMode 0x08 00181 00182 #define MAC "\x00\x08\xDC\x11\x34\x78" 00183 #define IP "192.168.1.19" 00184 #define MASK "255.255.255.0" 00185 #define GATEWAY "192.168.1.1" 00186 00187 #define HTTPD_SERVER_PORT 80 00188 #define HTTPD_MAX_REQ_LENGTH 1023 00189 #define HTTPD_MAX_HDR_LENGTH 255 00190 #define HTTPD_MAX_FNAME_LENGTH 127 00191 #define HTTPD_MAX_DNAME_LENGTH 127 00192 00193 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 00194 EthernetInterface eth; 00195 TCPSocketServer server; 00196 TCPSocketConnection client; 00197 char buffer[HTTPD_MAX_REQ_LENGTH+1]; 00198 char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; 00199 char fileName[HTTPD_MAX_FNAME_LENGTH+1]; 00200 char dirName[HTTPD_MAX_DNAME_LENGTH+1]; 00201 char *uristr; 00202 char *eou; 00203 char *qrystr; 00204 FILE *fp; 00205 int rdCnt; 00206 DigitalOut led1(D10); //server listning status 00207 DigitalOut led2(D11); //socket connecting status 00208 Ticker ledTick; 00209 00210 I2C adxl345(PA_10,PA_09); 00211 int16_t xyz[3] = {0, 0, 0}; 00212 00213 Serial uart0(PA_13, PA_14); // tx, rx 00214 Serial uart2(PC_10, PC_11); // tx, rx 00215 //DigitalOut myD1(D10); 00216 //DigitalOut myD2(D11); 00217 DigitalOut myD3(D12); 00218 DigitalOut myD4(D2); 00219 AnalogIn myA1(A1); 00220 AnalogIn myA2(A2); 00221 AnalogIn myA3(A3); 00222 PwmOut PWM1(D3); 00223 PwmOut PWM2(D5); 00224 PwmOut PWM3(D6); 00225 DigitalIn mybutton(PC_06); 00226 00227 Timeout digitalTimeout; 00228 Timeout analogTimeout; 00229 00230 00231 void attimeoutD() 00232 { 00233 static int pos = 0; 00234 if(pos == 0){ 00235 //myD1 = 0; 00236 //myD2 = 0; 00237 myD3 = 0; 00238 myD4 = 0; 00239 }else{ 00240 //myD1 = 1; 00241 //myD2 = 1; 00242 myD3 = 1; 00243 myD4 = 1; 00244 } 00245 pos = !pos; 00246 digitalTimeout.attach(&attimeoutD,0.5); 00247 } 00248 void attimeoutA() 00249 { 00250 uart0.printf("A1:%02fV ",myA1.read()*3.3); 00251 uart0.printf("A2:%02fV ",myA2.read()*3.3); 00252 uart0.printf("A3:%02fV \r\n",myA3.read()*3.3); 00253 analogTimeout.attach(&attimeoutA,1); 00254 } 00255 00256 void ledTickfunc() 00257 { 00258 led1 = !led1; 00259 } 00260 00261 void getFile(char* uri) 00262 { 00263 //uart0.printf("getFile %s\n", uri); 00264 char *lstchr = strrchr(uri, NULL) -1; 00265 if ('/' == *lstchr) { 00266 //uart0.printf("Open directory /sd%s\r\n", uri); 00267 *lstchr = 0; 00268 sprintf(fileName, "/sd%s", uri); 00269 DIR *d = opendir(fileName); 00270 if (d != NULL) { 00271 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); 00272 client.send(httpHeader,strlen(httpHeader)); 00273 sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri); 00274 client.send(httpHeader,strlen(httpHeader)); 00275 struct dirent *p; 00276 while((p = readdir(d)) != NULL) { 00277 sprintf(dirName, "%s/%s", fileName, p->d_name); 00278 //uart0.printf("%s\n", dirName); 00279 DIR *subDir = opendir(dirName); 00280 if (subDir != NULL) { 00281 sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name); 00282 } else { 00283 sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name); 00284 } 00285 client.send(httpHeader,strlen(httpHeader)); 00286 } 00287 } 00288 closedir(d); 00289 //uart0.printf("Directory closed\r\n"); 00290 sprintf(httpHeader,"</ul></body></html>"); 00291 client.send(httpHeader,strlen(httpHeader)); 00292 } else { 00293 sprintf(fileName, "/sd%s", uri); 00294 fp = fopen(fileName, "r"); 00295 if (fp == NULL) { 00296 //uart0.printf("File not found\r\n"); 00297 sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00298 client.send(httpHeader,strlen(httpHeader)); 00299 client.send(uri,strlen(uri)); 00300 } else { 00301 //uart0.printf("Sending: header"); 00302 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); 00303 client.send(httpHeader,strlen(httpHeader)); 00304 //uart0.printf(" file"); 00305 while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) { 00306 client.send(buffer, rdCnt); 00307 //uart0.printf("."); 00308 } 00309 client.send(buffer, rdCnt); 00310 fclose(fp); 00311 //uart0.printf("done\r\n"); 00312 } 00313 } 00314 } 00315 00316 int main() { 00317 uart0.baud(9600); 00318 uart2.baud(9600); 00319 PWM1.period_us(500); 00320 PWM2.period_us(500); 00321 PWM3.period_us(500); 00322 PWM1 = 0.2; 00323 PWM2 = 0.5; 00324 PWM3 = 0.8; 00325 uart2.printf("uart2:for Loopback test,please input something\n"); 00326 uart0.printf("start testing uart1 digitalOut and PWM and analogIn\r\n"); 00327 digitalTimeout.attach(&attimeoutD,1); 00328 analogTimeout.attach(&attimeoutA,1); 00329 /*iic*/ 00330 char config[2]; 00331 char data[6]; 00332 config[0] = ADXL345_DEVID_REG; 00333 char output; 00334 adxl345.write( ADXL345_I2C_WRITE , config, 1); 00335 adxl345.read( ADXL345_I2C_READ , &output, 1); 00336 uart0.printf("ADXL345 device id is 0x%x.\r\n",output); 00337 config[0] = ADXL345_POWER_CTL_REG; 00338 config[1] = MeasurementMode; 00339 adxl345.write( ADXL345_I2C_WRITE, config, 2); //set power 00340 00341 config[0]=ADXL345_DATAX0_REG; 00342 /*eth&sd*/ 00343 ledTick.attach(&ledTickfunc,0.5); 00344 uart0.printf("Initializing\r\n"); 00345 uart0.printf("Checking File System\r\n"); 00346 DIR *d = opendir("/sd/"); 00347 if (d != NULL) { 00348 uart0.printf("SD Card Present\r\n"); 00349 } else { 00350 uart0.printf("SD Card Root Directory Not Found\r\n"); 00351 } 00352 uart0.printf("Initializing Ethernet\r\n"); 00353 eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); 00354 uart0.printf("Connecting\r\n"); 00355 eth.connect(); 00356 uart0.printf("IP Address is %s\r\n", eth.getIPAddress()); 00357 server.bind(HTTPD_SERVER_PORT); 00358 server.listen(); 00359 uart0.printf("Server Listening\r\n"); 00360 00361 while(1) { 00362 //wait(0.1); 00363 /*eth&sd*/ 00364 //uart0.printf("\nWait for new connection...\r\n"); 00365 server.accept(client); 00366 00367 client.set_blocking(false, 1500); // Timeout after (1.5)s 00368 00369 //uart0.printf("Connection from: %s\r\n", client.get_address()); 00370 while (1) { 00371 /**/ 00372 if (mybutton == 0) { 00373 uart0.printf("USER Button is Pressed!!!\r\n"); 00374 } 00375 if(uart0.readable()){ 00376 char ch = uart0.getc(); 00377 uart0.printf("%c", ch); 00378 } 00379 if(uart2.readable()){ 00380 char ch2 = uart2.getc(); 00381 uart2.printf("%c", ch2); 00382 } 00383 /*iic*/ 00384 adxl345.write( ADXL345_I2C_WRITE, config, 1); 00385 adxl345.read( ADXL345_I2C_READ , data, 6); 00386 xyz[0]=(int)data[1]<<8|(int)data[0]; 00387 xyz[1]=(int)data[3]<<8|(int)data[2]; 00388 xyz[2]=(int)data[5]<<8|(int)data[4]; 00389 uart0.printf("%6i, %6i, %6i\r\n", xyz[0], xyz[1], xyz[2]); 00390 /**/ 00391 00392 led2 = true; 00393 int n = client.receive(buffer, sizeof(buffer)); 00394 if (n <= 0){ 00395 led2 = false; 00396 client.close(); 00397 break; 00398 } 00399 //uart0.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); 00400 if (n >= 1024) { 00401 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00402 client.send(httpHeader,strlen(httpHeader)); 00403 client.send(buffer,n); 00404 led2 = false; 00405 client.close(); 00406 break; 00407 } else { 00408 buffer[n]=0; 00409 } 00410 if (!strncmp(buffer, "GET ", 4)) { 00411 uristr = buffer + 4; 00412 eou = strstr(uristr, " "); 00413 if (eou == NULL) { 00414 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00415 client.send(httpHeader,strlen(httpHeader)); 00416 client.send(buffer,n); 00417 } else { 00418 *eou = 0; 00419 getFile(uristr); 00420 } 00421 } 00422 } 00423 00424 } 00425 }
Generated on Wed Jul 13 2022 19:03:26 by
1.7.2
