⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 f12x_adc0_externalinput_mux.c

📁 c8051f120_ad采样程序
💻 C
📖 第 1 页 / 共 2 页
字号:
// Parameters   : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.4   digital   push-pull     UART TX
// P0.5   digital   open-drain    UART RX
// P1.6   digital   push-pull     LED
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = CONFIG_PAGE;              // set SFR page

   XBR0     = 0x00;
   XBR1     = 0x00;
   XBR2     = 0x44;                    // Enable crossbar and weak pull-up
                                       // Enable UART1

   P0MDOUT |= 0x01;                    // Set TX1 pin to push-pull
   P1MDOUT |= 0x40;                    // Set P1.6(LED) to push-pull

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page
}

//-----------------------------------------------------------------------------
// UART1_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure the UART1 using Timer1, for <baudrate> and 8-N-1.
//
//-----------------------------------------------------------------------------
void UART1_Init (void)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = UART1_PAGE;
   SCON1   = 0x10;                     // SCON1: mode 0, 8-bit UART, enable RX

   SFRPAGE = TIMER01_PAGE;
   TMOD   &= ~0xF0;
   TMOD   |=  0x20;                    // TMOD: timer 1, mode 2, 8-bit reload


   if (SYSCLK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON |= 0x10;                   // T1M = 1; SCA1:0 = xx
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x13;                  // Clear all T1 related bits
      CKCON |=  0x01;                  // T1M = 0; SCA1:0 = 01
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x13;                  // T1M = 0; SCA1:0 = 00
   } else {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x13;                  // Clear all T1 related bits
      CKCON |=  0x02;                  // T1M = 0; SCA1:0 = 10
   }

   TL1 = TH1;                          // Initialize Timer1
   TR1 = 1;                            // Start Timer1

   SFRPAGE = UART1_PAGE;
   TI1 = 1;                            // Indicate TX1 ready

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page

}

//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure ADC0 to use Timer3 overflows as conversion source, to
// generate an interrupt on conversion complete, and to use right-justified
// output mode.  Enables ADC end of conversion interrupt. Leaves ADC disabled.
//
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = ADC0_PAGE;

   ADC0CN = 0x0C;                      // ADC0 disabled; normal tracking
                                       // mode; ADC0 conversions are initiated
                                       // on overflow of Timer2; ADC0 data is
                                       // right-justified, low power tracking 
                                       // mode

   REF0CN = 0x03;                      // Enable on-chip VREF and output buffer

   AMX0CF = 0x00;                      // AIN inputs are single-ended (default)

   AMX0SL = 0x00;                      // Select AIN0.0 pin as ADC mux input 
                                       // ISR will change this to step through 
                                       // inputs

   ADC0CF = (SYSCLK/SAR_CLK) << 3;     // ADC conversion clock = 2.5MHz
   ADC0CF |= 0x00;                     // PGA gain = 1 (default)

   EIE2 |= 0x02;                       // enable ADC interrupts

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page
}

//-----------------------------------------------------------------------------
// TIMER2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// Configure Timer2 to auto-reload at 20uS rate using SYSCLK as its timebase
//
//-----------------------------------------------------------------------------
void TIMER2_Init (void)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = TMR2_PAGE;

   TMR2CN = 0x00;                      // Stop Timer2; Clear TF2, select 
                                       // auto-reload;
   TMR2CF = 0x08;                      // use SYSCLK as timebase, count down
   RCAP2   = 65535 -(SYSCLK / 50000);  // Init reload values for 20uS
   TMR2    = RCAP2;                    // Set to reload immediately
   TR2     = 1;                        // start Timer2

   IE |= 0x20;                         // enable timer 2 interrupt 

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
//
// This ISR is called when the ADC0 completes a conversion.  Each value is 
// added to a running total <accumulator>, and the local decimation counter 
// <int_dec> decremented. When <int_dec> reaches zero, we post the decimated
// result in the global variable <Result[]>.
//
// The analog input is sampled, held, and converted on a Timer2 overflow.  To
// maximize input settling time, the analog mux is also advanced to the next
// input on the Timer2 overflow.  Two different indices are held globally:
//    amux_convert:  index of the analog input undergoing conversion
//    amux_input:    index of the analog input selected in the analog 
//                   multiplexer
// 
//
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 15
{

   static unsigned int_dec=INT_DEC;    // Integrate/decimate counter
                                       // we post a new result when
                                       // int_dec = 0

   static long accumulator[ANALOG_INPUTS] ={0L};       
                                       // Here's where we integrate the
                                       // ADC samples from input AIN0.0
   unsigned char i;

   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page
   
   SFRPAGE = ADC0_PAGE;
   AD0INT = 0;                         //clear ADC conversion complete overflow

   accumulator[amux_convert] += ADC0;  // Read ADC value and add to running
                                       // total

   if(amux_convert == (ANALOG_INPUTS-1))// reset input index if the last input 
                                       //was just read
      {
      int_dec--;                       // Update decimation counter
                                       // when last of the analog inputs 
                                       // sampled
      }

   if (int_dec == 0)                   // If zero, then post result
   {
      int_dec = INT_DEC;               // Reset counter

      for(i=0; i<ANALOG_INPUTS; i++)
         {
         Result[i] = accumulator[i] >> 8; //Copy decimated values into Result 
         accumulator[i] = 0L;          // Reset accumulators
         }
   }

   amux_convert = amux_input;          // now that conversion results are 
                                       // stored, advance index to the analog
                                       // input currently selected on the mux

   LED = 1;

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page
}

//-----------------------------------------------------------------------------
// TIMER2_ISR
//-----------------------------------------------------------------------------
//
// The timer2 overflow triggers the ADC0 conversion on the analog MUX input
// previously selected.  It is permissable to change the analog MUX
// input once conversion has started, as the ADC has an internal sample 
// and hold.
//
// This ISR routine will then select the next analog MUX input so as to 
// maximize the settling time.
//
//-----------------------------------------------------------------------------

void TIMER2_ISR(void) interrupt 5
{
   SFRPAGE = TMR2_PAGE;
   TF2 = 0;



   amux_input ++;                      // step to the next analog mux input
 
   if(amux_input == ANALOG_INPUTS)     // reset input index if the last input 
      {                                // was just read
      amux_input=0;                    // reset input index back to AIN0.0
      }

   SFRPAGE = ADC0_PAGE;
   AMX0SL = amux_input;                // select the next input on the analog 
                                       // multiplexer

   LED = 0;                        
}

//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Wait_MS
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters:
//   1) unsigned int ms - number of milliseconds of delay
//                        range is full range of integer: 0 to 65335
//
// This routine inserts a delay of <ms> milliseconds.
//
//-----------------------------------------------------------------------------
void Wait_MS(unsigned int ms)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = TMR3_PAGE;

   TMR3CN = 0x00;                      // Stop Timer3; Clear TF3;
   TMR3CF = 0x00;                      // use SYSCLK/12 as timebase

   RCAP3 = -(SYSCLK/1000/12);          // Timer 3 overflows at 1 kHz
   TMR3 = RCAP3;

   TR3 = 1;                            // Start Timer 3

   while(ms)
   {
      TF3 = 0;                         // Clear flag to initialize
      while(!TF3);                     // Wait until timer overflows
      ms--;                            // Decrement ms
   }

   TR3 = 0;                            // Stop Timer 3

   SFRPAGE = SFRPAGE_SAVE;             // Restore SFRPAGE
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -