📄 mainprog.lst
字号:
404 void adc(unsigned char channel)
405 {
406 1 //设置p1口为纯输入状态,用于adc
407 1 ADC_CONTR=0xe8+channel; //启动AD转换,用最低转换速率
408 1 while ((ADC_CONTR & 0x10)==0) {}
409 1 ADC_CONTR=0xe0+channel; //停止AD转换
410 1 ADC_result_intr = ADC_HIGH*4;
411 1 ADC_result_intr +=ADC_LOW & 0x03; //adc结果
412 1 }
413
414
415 //ADC系数计算
416 //以各通道A/D值乘以系数所得值为毫伏值
417 void ADC_coefficient_measure(void)
418 {
419 1 unsigned int i;
420 1 unsigned long ADC_result_temp;
421 1 ADC_result_temp=0;
422 1 for (i=0;i<30000;i++){
423 2 adc(0); //0通道AD转换1次
424 2 ADC_result_temp+=ADC_result_intr;
425 2 }
426 1 ADC_result_intr=(unsigned int)(ADC_result_temp/i);
427 1 ADC_result_intr0=ADC_result_intr;
C51 COMPILER V8.06 MAINPROG 02/13/2009 09:29:07 PAGE 8
428 1 ADC0_coefficient=(float)2485/ADC_result_intr0; //最大5.3V
429 1 //实际51mV,运放输出5.1V,放大倍数为=0.01
430 1 //ADC1_coefficient=ADC0_coefficient/100; //实际电阻为100K/1K倍,最大0.053V
431 1 ADC1_coefficient=ADC0_coefficient/50; //实际电阻为50K/1K倍,最大0.106V
432 1 //实际1.468V,运放输出4.102V,放大倍数为=0.3579
433 1 ADC2_coefficient=ADC0_coefficient*3579/10000; //实际电阻为10K/3.3K倍,最大1.9
434 1 //实际30.59V,运放输出4.14V,放大倍数为=7.39
435 1 ADC3_coefficient=ADC0_coefficient*739/100; //实际电阻为2.6K/20K倍,最大40.77V
436 1 }
437
438
439
440 //带滤波AD转换
441 //全局变量ADC_result表示滤波后AD值
442 void ADC_filter(unsigned char channel)
443 {
444 1 adc(channel); //AD转换1次
445 1 ADC_result1=ADC_result_intr;
446 1 adc(channel); //AD转换1次
447 1 ADC_result2=ADC_result_intr;
448 1 adc(channel); //AD转换1次
449 1 ADC_result3=ADC_result_intr;
450 1 if (ADC_result1>=ADC_result2) {
451 2 if (ADC_result2>=ADC_result3) ADC_result_intr=ADC_result2; //ADC_result2为中间
452 2 else { //ADC_result2最小
453 3 if (ADC_result1>ADC_result3) ADC_result_intr=ADC_result3; //ADC_result3为中间
454 3 else ADC_result_intr=ADC_result1; //ADC_result1为中间
455 3 }
456 2 }
457 1 else { //ADC_result1<ADC_result2
458 2 if (ADC_result2<=ADC_result3) ADC_result_intr=ADC_result2; //ADC_result2为中间
459 2 else { //ADC_result2最大
460 3 if (ADC_result1>ADC_result3) ADC_result_intr=ADC_result1; //ADC_result1为中间
461 3 else ADC_result_intr=ADC_result3; //ADC_result3为中间
462 3 }
463 2 }
464 1 }
465
466 /*
467 //通道电压检测
468 //全局变量Votage表示电压转换后的电压或电流
469 void Channel_Votage(float ADC_result,unsigned char channel)
470 {
471 //printic(ADC_result);
472 if(channel==1) {
473 Votage=(ADC1_coefficient*ADC_result)/100; //实际电压,单位:10mV
474 }
475 else if(channel==2) {
476 Votage=(ADC2_coefficient*ADC_result)/100; //实际电压,单位:mV
477 }
478 else if(channel==3) {
479 Votage=(ADC3_coefficient*ADC_result)/10000; //实际电压,单位:10mV
480 }
481 }
482 //显示电压
483 void disp_votage(void)
484 {
485 //打印测量时间计数
486 disp_float_decimal(Mesure_counter_10ms);
487 //测量电极电压。全局变量Votage表示电压,如果电压低于3V,chip_connect表示是否正在接触芯片
488 ADC_filter(1); //测量电压
489 Channel_Votage(ADC_result,1); //单位毫伏
C51 COMPILER V8.06 MAINPROG 02/13/2009 09:29:07 PAGE 9
490 //以十进制方式显示电压,单位V.
491 disp_float_votage(Votage,2);
492 //测量电极电压。全局变量Votage表示电压,如果电压低于3V,chip_connect表示是否正在接触芯片
493 ADC_filter(2); //测量电压
494 Channel_Votage(ADC_result,2); //单位毫伏
495 //以十进制方式显示电压,单位V.
496 disp_float_votage(Votage,2);
497 //测量电极电压。全局变量Votage表示电压,如果电压低于3V,chip_connect表示是否正在接触芯片
498 ADC_filter(3); //测量电压
499 Channel_Votage(ADC_result,3); //单位毫伏
500 //以十进制方式显示电压,单位V.
501 disp_float_votage(Votage,2);
502 printh(0x0d);printh(0x0a);
503 }
504 */
505 void Capacitance_Mesure(void)
506 {
507 1 /*
508 1 float float_temp;
509 1 //float float_temp;
510 1 switch (Mesure_state) //侧量状态
511 1 {
512 1 case Capacitance_Mesure_start: //测量初始化
513 1 Mesure_counter_10ms=0; //侧量计数
514 1 break;
515 1 //28V电容充电电容测量开始
516 1 case charge_28V_cap_test_start:
517 1 //测量起始电压
518 1 ADC_filter(1); //测量电压
519 1 Channel_Votage(ADC_result,1); //单位毫伏
520 1 start_votage=Votage; //充电起始电压
521 1 Votage_record=Votage; //电压记录
522 1 //清累计电荷量
523 1 charge=0; //充电总电荷
524 1 charge_record=0; //充电电荷记录
525 1 ADC_counter=0; //累加次数
526 1 printh(0x0d);printh(0x0a);
527 1 print("28VC: ");
528 1 print("V:");
529 1 disp_float_votage(Votage,2); //显示当前电压
530 1 print("I:");
531 1 disp_float_votage(current,2); //显示当前电流
532 1 print("Counter:");
533 1 disp_float_decimal(ADC_counter); //显示累加次数
534 1 print("Q:");
535 1 disp_float_decimal(charge); //显示累加电荷量
536 1 Channel_Votage(float_temp,2); //折合成电压,单位:毫伏
537 1 printh(0x0d);printh(0x0a);
538 1 //进入测量状态
539 1 Mesure_state=charge_28V_cap_test; //开始测量
540 1 disp_flag=1;
541 1 break;
542 1
543 1 //28V电容充电电容测量
544 1 case charge_28V_cap_test:
545 1 if (flag_10ms_1==1) {
546 1 flag_10ms_1=0;
547 1 ADC_filter(2); //测量电流
548 1 charge+=ADC_result_intr; //电荷量累加
549 1 ADC_counter++; //累加次数
550 1 Channel_Votage(ADC_result_intr,2); //单位毫伏
551 1 current=Votage/25; //当前电流
C51 COMPILER V8.06 MAINPROG 02/13/2009 09:29:07 PAGE 10
552 1 if ((disp_flag==1) && (flag_1s_1==1) )
553 1 {
554 1 flag_1s_1=0;
555 1 print(" I:");
556 1 disp_float_votage(current,2); //显示当前电流
557 1 ADC_filter(1); //测量电压
558 1 print(" V:");
559 1 Channel_Votage(ADC_result_intr,1); //单位毫伏
560 1 disp_float_votage(Votage,2); //显示当前电压
561 1 }
562 1 }
563 1 ADC_filter(1); //测量电压
564 1 Channel_Votage(ADC_result_intr,1); //单位毫伏
565 1 Curent_Votage=Votage; //当前电压
566 1 //每达到1伏打印一次
567 1 if(((Votage/1000)>(Votage_record/1000))||(stop_flag==1)){ //打印电压
568 1 Votage_record=Votage;
569 1 float_temp=(charge-charge_record)/2500;
570 1 charge_record=charge; //记录本次电荷累计数量
571 1 printh(0x0d);printh(0x0a);
572 1 print("28VC: ");
573 1 print("V:");
574 1 disp_float_votage(Votage,2); //显示当前电压
575 1 print("I:");
576 1 disp_float_votage(current,2); //显示当前电流
577 1 print("Counter:");
578 1 disp_float_decimal(ADC_counter); //显示累加次数
579 1 print("Q:");
580 1 disp_float_decimal(charge); //显示累加电荷量
581 1 print("ΔQ:");
582 1 disp_float_decimal(charge-charge_record); //显示电荷量差
583 1 print("ΔC:");
584 1 Channel_Votage(charge-charge_record,2); //折合成电压,单位:毫伏
585 1 Votage=Votage/25000; //折合成电流(以25毫欧为采样电阻)
586 1 Votage=Votage*1000/(Curent_Votage-Votage_record); //折合成电容
587 1 disp_float_votage(Votage,2); //显示电容量
588 1 print("C:");
589 1 Channel_Votage(charge/1000,2); //折合成电压,单位:毫伏
590 1 //disp_float_decimal(Votage);
591 1 Votage=Votage/25; //折合成电流(以25毫欧为采样电阻)
592 1 //disp_float_decimal(Votage);
593 1 Votage=Votage*1000/(Curent_Votage-start_votage); //总电容
594 1 //disp_float_decimal(Curent_Votage);
595 1 //disp_float_decimal(start_votage);
596 1 disp_float_votage(Votage,2); //显示电容量
597 1 printh(0x0d);printh(0x0a);
598 1
599 1 charge_record=charge; //记录本次电荷累计数量
600 1 Votage_record=Curent_Votage; //记录当前电压
601 1 }
602 1 if (stop_flag==1) {
603 1 stop_flag=0;
604 1 disp_flag=0;
605 1 }
606 1 break;
607 1 //28V电容放电电容测量开始
608 1 case discharge_28V_cap_test_start:
609 1 //测量起始电压
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -