📄 main.lst
字号:
278 1
279 1 // 关闭看门狗
280 1 WDTCN = 0xde;
281 1 WDTCN = 0xad;
282 1
283 1 // 初始化硬件
284 1 EMIF_Init();
285 1 PORT_Init();
286 1 SYSCLK_Init();
287 1
288 1 Timer_Init();
289 1 UART0_Init();
290 1
291 1 // 初始化模拟部分
292 1 InitVariable();
293 1
294 1 ADC_Init();
295 1 Voltage_Reference_Init();
296 1 DAC_Init();
297 1
298 1 // 系统启动
299 1 SendString("CGI Test System!\r\n");
300 1
301 1
302 1 P_LED2 = 0;
303 1
C51 COMPILER V8.02 MAIN 01/16/2008 21:37:44 PAGE 6
304 1 // 首先进行一次温度采集,初始化温度值
305 1 SampleTemperature();
306 1
307 1 EIE2|=0x02; //使能AD中断
308 1
309 1 AMX0SL = g_ADChannel;
310 1 AD0BUSY = 1; // 启动转换
311 1
312 1
313 1 while(1)
314 1 {
315 2
316 2 // 此处不使用mn_init函数而改用mn_init_m函数
317 2 // 以解决原库函数不能脱离调试器运行的问题
318 2 // 用户应该根据程序实际使用的协议来调整该函数
319 2 // 具体参加该函数
320 2 while((retval=mn_init_m()) < 0)
321 2 {
322 3 // If code execution enters this while(1) loop, the stack failed to initialize.
323 3 // Verify that all boards are connected and powered properly.
324 3
325 3 P_LED2 = !P_LED2;
326 3
327 3 SendString(HTML_BUFFER);
328 3
329 3 Dlyms(1000);
330 3 }
331 2
332 2
333 2
334 2 P_LED2 = 1;
335 2
336 2 // Connect to the network
337 2 establish_network_connection();
338 2
339 2 // Add web page to virtual file system.
340 2 // The main page MUST be called index.htm or index.html.
341 2 mn_vf_set_entry((byte *)"index.html", INDEX_SIZE, index_html, VF_PTYPE_FLASH);
342 2
343 2
344 2 mn_pf_set_entry(
345 2 (byte*)"get_data", // Script Name (ASCII)
346 2 get_data // Function Pointer
347 2 );
348 2 mn_pf_set_entry(
349 2 (byte*)"set_led", // Script Name (ASCII)
350 2 set_led // Function Pointer
351 2 );
352 2
353 2
354 2 // Start the Application Layer Services
355 2 // If this routine exits, check the return value for an error code.
356 2 retval = mn_server();
357 2
358 2 }
359 1 }
360
361 //-----------------------------------------------------------------------------
362 // establish_network_connection
363 //-----------------------------------------------------------------------------
364 //
365 // This function calls mn_ether_init() to initialize the CP2200 and attach to
C51 COMPILER V8.02 MAIN 01/16/2008 21:37:44 PAGE 7
366 // the network.
367 //
368 // If there is a network connection, the function returns 1.
369 //
370 // In the call to mn_ether_init(), NUM_AUTONEG_ATTEMPTS is set to 0, so the
371 // function will not return until it successfully auto-negotiates.
372 //
373 // mn_ether_init() will not be a blocking call if NUM_AUTONEG_ATTEMPTS is set
374 // to a value greater than 0.
375 //
376 int establish_network_connection()
377 {
378 1 int retval;
379 1
380 1 do
381 1 {
382 2 // mn_ether_init() initializes the Ethernet controller.
383 2 // AUTO_NEG indicates that the controller will auto-negotiate.
384 2 retval = mn_ether_init(AUTO_NEG, 0, 0);
385 2
386 2 // If there is no link, poll link_status until it sets or the
387 2 // CP2200 resets and then call mn_ether_init() again.
388 2 if (retval == LINK_FAIL)
389 2 {
390 3 while(!link_status && !ether_reset);
391 3 }
392 2
393 2 // If retval is less than zero and is not LINK_FAIL, there is a
394 2 // hardware error.
395 2 else if (retval < 0)
396 2 {
397 3 // Verify that the Ethernet controller is connected and powered properly.
398 3 // Verity that the EMIF has been configured at a speed compatible with the
399 3 // Ethernet controller.
400 3 while(1);
401 3 }
402 2
403 2 }while(retval < 0);
404 1
405 1 return (1);
406 1
407 1 }
408
409
410
411 //-----------------------------------------------------------------------------
412 // ether_reset_low
413 //-----------------------------------------------------------------------------
414 //
415 // This routine drives the reset pin of the ethernet controller low.
416 //
417 void ether_reset_low()
418 {
419 1
420 1 P_RST = 0;
421 1
422 1 }
423
424 //-----------------------------------------------------------------------------
425 // ether_reset_high
426 //-----------------------------------------------------------------------------
427 //
C51 COMPILER V8.02 MAIN 01/16/2008 21:37:44 PAGE 8
428 // This routine places the reset pin in High-Z allowing it to be pulled up
429 // using the external pull-up resistor.
430 //
431 // Additionally, this routine waits for the reset pin to read high before
432 // exiting.
433 //
434 void ether_reset_high (void)
435 {
436 1
437 1 P_RST = 1;
438 1 while(P_RST==0);
439 1 }
440
441 //-----------------------------------------------------------------------------
442 // CGI Script: get_data
443 // 读取voltage&temperature状态 CGI程序
444 // 该函数返回电路板测试的电压和温度值
445 // 当使用CGI Script时调用该接口
446 //-----------------------------------------------------------------------------
447
448
449 void get_data (PSOCKET_INFO socket_ptr)
450 {
451 1 bit field_name_found;
452 1 unsigned int high,low;
453 1
454 1
455 1 xdata byte msg_buff[75];
456 1
457 1
458 1 // Search the URL for field-name = "type" and copy field-value to <msg_buff>
459 1 field_name_found = mn_http_find_value(BODYptr,(byte *)"type",msg_buff);
460 1 if(field_name_found)
461 1 {
462 2 // Type Check #1
463 2 // Check if the requested data type is "Temperature"
464 2 if( toupper(msg_buff[0]) == 'T')
465 2 {
466 3
467 3 // Search the URL for field-name = "format" and copy field-value to <msg_buff>
468 3 field_name_found = mn_http_find_value(BODYptr,(byte *)"format",msg_buff);
469 3
470 3 // Check if the requested format is HTML
471 3 if(field_name_found && toupper(msg_buff[0]) == 'H')
472 3 {
473 4
474 4 // User has requested temperature data in HTML format:
475 4 EA = 0;
476 4 g_Temperature*=100;
477 4 high = g_Temperature/286;
478 4 low = g_Temperature-high*286;
479 4 sprintf(HTML_BUFFER, "<html><body bgcolor=blue text=yellow><center><span style=\"font-family: sans-ser
-if; font-size: 28pt; font-weight: bold;\">%d.%03d</span></center></body></html>",
480 4 high,low);
481 4 EA = 1;
482 4
483 4 SendString(HTML_BUFFER);
484 4
485 4 socket_ptr->send_ptr = (byte *)HTML_BUFFER;
486 4 socket_ptr->send_len = strlen(HTML_BUFFER);
487 4
488 4 }
C51 COMPILER V8.02 MAIN 01/16/2008 21:37:44 PAGE 9
489 3
490 3 }
491 2 else
492 2 {
493 3
494 3 if( toupper(msg_buff[0]) == 'V')
495 3 {
496 4
497 4 // Search the URL for field-name = "format" and copy field-value to <msg_buff>
498 4 field_name_found = mn_http_find_value(BODYptr,(byte *)"format",msg_buff);
499 4
500 4 // Check if the requested format is HTML
501 4 if(field_name_found && toupper(msg_buff[0]) == 'H')
502 4 {
503 5
504 5 // User has requested light sensor data in HTML format
505 5 // Build the following page:
506 5 // <html>
507 5 // <head><meta http-equiv="refresh" content="2"></head>
508 5 // <body bgcolor="#757575" text="#505050">
509 5 // <b>50%</b>
510 5 // </body>
511 5 // </html>
512 5 //
513 5 //
514 5 EA = 0;
515 5 high=g_Voltage/1000;
516 5 low = g_Voltage-high*1000;
517 5 sprintf(HTML_BUFFER, "<html><head></head><body bgcolor=black text=yellow><center><span style=\"font-f
-amily: sans-serif; font-size: 28pt; font-weight: bold;\">%u.%03d</span></center></body></html>",
518 5 high,low );
519 5 EA = 1;
520 5
521 5 socket_ptr->send_ptr = (byte *)HTML_BUFFER;
522 5 socket_ptr->send_len = strlen(HTML_BUFFER);
523 5
524 5 }
525 4 }
526 3 }
527 2 }
528 1
529 1 }
530
531 //-----------------------------------------------------------------------------
532 // CGI程序
533 // 控制LED状态
534 //
535 //-----------------------------------------------------------------------------
536 void set_led(PSOCKET_INFO socket_ptr)
537 {
538 1 static byte msg_buff1[52];
539 1
540 1 int status1;
541 1
542 1
543 1 // Search for the “type” field and store the
544 1 // result in <msg_buff1>.
545 1 status1 = mn_http_find_value (BODYptr,(byte*)"led", msg_buff1);
546 1
547 1
548 1 // Check status1 and status2 to determine if
549 1 // msg_buff1 and msg_buff2 are valid.
C51 COMPILER V8.02 MAIN 01/16/2008 21:37:44 PAGE 10
550 1 if(status1)
551 1 {
552 2 if(strcmp(msg_buff1,"0")==0)
553 2 {
554 3 P_LED2 = 1;
555 3
556 3 strcpy(msg_ans,"LED ON");
557 3 }
558 2 else if(strcmp(msg_buff1,"1")==0)
559 2 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -