📄 f06x_adc2_externalinput_mux.lst
字号:
398 //-----------------------------------------------------------------------------
399 void TIMER2_Init (void)
400 {
401 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
402 1
403 1 SFRPAGE = TMR2_PAGE;
404 1
405 1 TMR2CN = 0x00; // Stop Timer2; Clear TF2, select
406 1 // auto-reload;
407 1 TMR2CF = 0x08; // use SYSCLK as timebase, count down
408 1 RCAP2 = 65536 -(SYSCLK / 24500); // Init reload values for 20uS
409 1 TMR2 = RCAP2; // Set to reload immediately
410 1 TR2 = 1; // start Timer2
411 1 ET2 = 1; // enable timer 2 interrupt
412 1 //IE |= 0x20; // enable timer 2 interrupt
413 1
414 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
415 1 }
416
417
418 //-----------------------------------------------------------------------------
419 // Interrupt Service Routines
420 //-----------------------------------------------------------------------------
421
422 //-----------------------------------------------------------------------------
423 // ADC2_ISR
424 //-----------------------------------------------------------------------------
425 //
426 // This ISR is called when the ADC0 completes a conversion. Each value is
427 // added to a running total <accumulator>, and the local decimation counter
C51 COMPILER V8.08 F06X_ADC2_EXTERNALINPUT_MUX 02/14/2008 15:32:09 PAGE 8
428 // <int_dec> decremented. When <int_dec> reaches zero, we post the decimated
429 // result in the global variable <Result[]>.
430 //
431 // The analog input is sampled, held, and converted on a Timer2 overflow. To
432 // maximize input settling time, the analog mux is also advanced to the next
433 // input on the Timer2 overflow. Two different indices are held globally:
434 // amux_convert: index of the analog input undergoing conversion
435 // amux_input: index of the analog input selected in the analog
436 // multiplexer
437 //
438 //
439 //-----------------------------------------------------------------------------
440 void ADC2_ISR (void) interrupt 18
441 {
442 1
443 1 static unsigned int_dec=INT_DEC; // Integrate/decimate counter
444 1 // we post a new result when
445 1 // int_dec = 0
446 1
447 1 static long accumulator[ANALOG_INPUTS] ={0L};
448 1 // Here's where we integrate the
449 1 // ADC samples from input AIN2.0
450 1 unsigned char i;
451 1
452 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
453 1
454 1 SFRPAGE = ADC2_PAGE;
455 1 AD2INT = 0; //clear ADC conversion complete overflow
456 1
457 1 accumulator[amux_convert] += ADC2; // Read ADC value and add to running
458 1 // total
459 1
460 1 if(amux_convert == (ANALOG_INPUTS-1))// reset input index if the last input
461 1 //was just read
462 1 {
463 2 int_dec--; // Update decimation counter
464 2 // when last of the analog inputs
465 2 // sampled
466 2 }
467 1
468 1 if (int_dec == 0) // If zero, then post result
469 1 {
470 2 int_dec = INT_DEC; // Reset counter
471 2
472 2 for(i=0; i<ANALOG_INPUTS; i++)
473 2 {
474 3 Result[i] = accumulator[i] >> 8; //Copy decimated values into Result
475 3 accumulator[i] = 0L; // Reset accumulators
476 3 }
477 2 }
478 1
479 1 amux_convert = amux_input; // now that conversion results are
480 1 // stored, advance index to the analog
481 1 // input currently selected on the mux
482 1
483 1 LED = 1;
484 1
485 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
486 1 }
487
488 //-----------------------------------------------------------------------------
489 // TIMER2_ISR
C51 COMPILER V8.08 F06X_ADC2_EXTERNALINPUT_MUX 02/14/2008 15:32:09 PAGE 9
490 //-----------------------------------------------------------------------------
491 //
492 // The timer2 overflow triggers the ADC0 conversion on the analog MUX input
493 // previously selected. It is permissable to change the analog MUX
494 // input once conversion has started, as the ADC has an internal sample
495 // and hold.
496 //
497 // This ISR routine will then select the next analog MUX input so as to
498 // maximize the settling time.
499 //
500 //-----------------------------------------------------------------------------
501
502 void TIMER2_ISR(void) interrupt 5
503 {
504 1 SFRPAGE = TMR2_PAGE;
505 1 TF2 = 0;
506 1
507 1
508 1
509 1 amux_input ++; // step to the next analog mux input
510 1
511 1 if(amux_input == ANALOG_INPUTS) // reset input index if the last input
512 1 { // was just read
513 2 amux_input=0; // reset input index back to AIN2.0
514 2 }
515 1
516 1 SFRPAGE = ADC2_PAGE;
517 1 AMX2SL = amux_input; // select the next input on the analog
518 1 // multiplexer
519 1
520 1 LED = 0;
521 1 }
522
523 //-----------------------------------------------------------------------------
524 // Support Subroutines
525 //-----------------------------------------------------------------------------
526
527 //-----------------------------------------------------------------------------
528 // wait_ms
529 //-----------------------------------------------------------------------------
530 //
531 // This routine inserts a delay of <ms> milliseconds.
532 //
533 void Wait_MS(unsigned int ms)
534 {
535 1 char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
536 1
537 1 SFRPAGE = TMR3_PAGE;
538 1
539 1 TMR3CN = 0x00; // Stop Timer3; Clear TF3;
540 1 TMR3CF = 0x00; // use SYSCLK/12 as timebase
541 1
542 1 RCAP3 = -(SYSCLK/1000/12); // Timer 3 overflows at 1 kHz
543 1 TMR3 = RCAP3;
544 1
545 1 TR3 = 1; // Start Timer 3
546 1
547 1 while(ms)
548 1 {
549 2 TF3 = 0; // Clear flag to initialize
550 2 while(!TF3); // Wait until timer overflows
551 2 ms--; // Decrement ms
C51 COMPILER V8.08 F06X_ADC2_EXTERNALINPUT_MUX 02/14/2008 15:32:09 PAGE 10
552 2 }
553 1
554 1 TR3 = 0; // Stop Timer 3
555 1
556 1 SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
557 1 }
558
559 //-----------------------------------------------------------------------------
560 // End Of File
561 //-----------------------------------------------------------------------------
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 509 ----
CONSTANT SIZE = 28 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 68 6
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -