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

📄 f350_adc_buffered_2.c

📁 性价比突出的C8051F350单片机各种例子程序
💻 C
字号:
//插上短路环J4,J7,J8的2,3脚,J9的2,3脚,J15,连上232串口线。
//AIN1与AGND连接,在AIN0输入0-2.5V之间的电压。

// Includes
//-----------------------------------------------------------------------------
#include <c8051f350.h>                    // SFR declarations
#include <stdio.h>                        // Standard I/O Library
#include <math.h>

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F35x
//-----------------------------------------------------------------------------

sfr16 DP       = 0x82;                    // data pointer
sfr16 TMR3RL   = 0x92;                    // Timer3 reload value
sfr16 TMR3     = 0x94;                    // Timer3 counter
sfr16 ADC0DEC  = 0x9a;
sfr16 TMR2RL   = 0xca;                    // Timer2 reload value
sfr16 TMR2     = 0xcc;                    // Timer2 counter
sfr16 PCA0CP0  = 0xe9;                    // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP1  = 0xeb;                    // PCA0 Module 2 Capture/Compare
sfr16 PCA0CP2  = 0xed;                    // PCA0 Module 2 Capture/Compare
sfr16 PCA0     = 0xf9;                    // PCA0 counter

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK       49000000             // SYSCLK frequency (Hz)
#define BAUDRATE     115200               // UART0 Baudrate (bps)

#define MDCLK        2457600              // Modulator Clock (Hz)
#define OWR          10                   // desired Output Word Rate in Hz

#define VREF         2530                 // External VREF (x 10^-2 V)
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
long xdata sample_array[128];
long ADC_OutputVal=0;         // Concatenated ADC output value
int   cyz;
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT7_Init (void);
void ADC0_Init (void);
void IDA0_Init (void);
void UART0_Init (void);
void YJ_Init(void);
void Write_CHAR(unsigned char yjchar);

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {



		   unsigned int i,g;
		   float average;
		   unsigned char e11,e22,e33,e44;
		   unsigned int e1,e2,e3,e4; 

   
		   PCA0MD &= ~0x40;                      // disable watchdog timer 

		   SYSCLK_Init();                         // Initialize system clock to 49 MHz

		   PORT7_Init();                           // Initialize crossbar and GPIO

  
   	     for(i=0;i<128;i++)
	 	  {
	      sample_array[i]=0;
	  
	 	  }

		   ADC0_Init();                           // 初始化ADC0

		   UART0_Init();                          // 初始化UART0

		   EA = 1;                                // 使能总中断

		   printf("\nMeasurements using the 24-bit ADC in C8051F350\n");
		   printf("\nCalibrating ...\n");

		   EIE1 &= ~0x08;                         // 不使能 ADC0 中断

		   ADC0MD |= 0x01;                        // 完全内部校准

		   while (!AD0CALC);                      // 等待转换完成

		   ADC0MD &= ~0x07;                       //  ADC0 为理想模式 
                                         

		   printf("Calibration complete\n\n");

		   AD0INT = 0;                            // clear pending sample indication
		   ADC0MD |= 0x83;                         // 启动连续转换

   while(1)                                        //处理采样值
	  		{

      // 捕捉128次采样
      		printf ("Collecting 128 samples...\n");
      
     	  for (i = 0; i < 128; i++) 
      	   {
         	while(!AD0INT);                     //等待转换完成
        	AD0INT = 0;                         //清 AD0 中断标志

         // concatenate ADC0 data bytes to form the 24-bit value
 
 			ADC_OutputVal = ADC0H;
         	ADC_OutputVal =ADC_OutputVal<< 16;
         	ADC_OutputVal += (long)ADC0L + ((long)ADC0M << 8);
         	sample_array[i] = ADC_OutputVal;
		 
     	 	}
	  	 
     	  for(i=0;i<128;i++)
	 		{
	 	  average=average+(float)sample_array[i];
	  
	 		}
	      average=average/128;
		  average=average/256/256/256;
		  average=average*VREF;
		  average=average*2;
	 	  average=average;

 		  printf("ADC Vin = %6d mV",(int)average);
      
	      cyz=(int)average;
		  e1=cyz/1000;
		  e2=(cyz-1000*e1)/100;
		  e3=(cyz-1000*e1-100*e2)/10;
		  e4=cyz-1000*e1-100*e2-10*e3;
		  g=0;
		  do
		  {
       
		    if(g==e1)e11=0x30+g;
		            
			if(g==e2)e22=0x30+g;
		           
	        if(g==e3)e33=0x30+g;
		           
			if(g==e4)e44=0x30+g;
		            
		    g++;
		   }while(g<10);
 	
	     YJ_Init();                                      //液晶显示采样值
	     for(i=0;i<4000;i++);
	     for(i=0;i<4000;i++);
	     for(i=0;i<4000;i++);
	 	 Write_CHAR(e11);
	   	 Write_CHAR(0x2e);
		 Write_CHAR(e22);
		 Write_CHAR(e33);
		 Write_CHAR(e44);
		 Write_CHAR(0x56);
		 for(i=0;i<4000;i++);
	     for(i=0;i<4000;i++);
	     for(i=0;i<4000;i++);
		 }

}


//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal 24.5MHz
// oscillator as its clock source, with x 2 multiply for
// 49 MHz operation. Also enables missing clock detector reset.
//
void SYSCLK_Init (void)
{
   		unsigned i;

  		 OSCICN = 0x80;                         // enable intosc
  		 CLKSEL = 0x00;                         // select intosc as sysclk source

// INTOSC configure

   		OSCICN = 0x83;

// PLL configure

  	 	CLKMUL = 0x00;                         // Reset Clock Multiplier

   		CLKMUL &= ~0x03;                       // select INTOSC / 2 as PLL source

   		CLKMUL |= 0x80;                        // Enable 4x Multipler (MULEN = 1)

   		for (i = 0; i < 125; i++);             // Delay for at least 5us

   		CLKMUL |= 0xC0;                        // Initialize Multiplier

   		while (!(CLKMUL & 0x20));              // Poll for Multiply Ready

// SYSCLK configure

   		VDM0CN = 0x80;                         // enable VDD monitor
   		RSTSRC = 0x06;                         // enable missing clock detector
                                          // and VDD monitor reset sources
   		CLKSEL = 0x02;                         // select PLL as clock source
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P0.4 - TX0 (push-pull)
// P0.5 - RX0

void PORT7_Init (void)
{
   		XBR0     |= 0x01;                       // UART0 Selected
   		XBR1     |= 0x40;                       // Enable crossbar and weak pull-ups
   		P0MDOUT |= 0xD0;                       // TX, LEDs = Push-pull
}

//-----------------------------------------------------------------------------
// ADC0_Init extVREF Bipolar AIN0.1-AIN0.0
//-----------------------------------------------------------------------------
//
// This function initializes the ADC to measure across AIN0.1 and AIN0.0 
// on the Target Board (Differential measurements, Bipolar codes)

void ADC0_Init (void)
{
   		unsigned ADC0_decimation;

     	REF0CN &= ~0x01;                       // disable internal vref

//   	REF0CN |= 0x01;                        // (enable if using internal vref)


//   	ADC0CN = 0x00;                         // unipolar output codes, GAIN=1
	 	ADC0CN = 0x10;                         // Bipolar output codes, GAIN=1

//   	ADC0CF = 0x00;                         // interrupts upon SINC3 filter output
                                          // and uses internal VREF

    	ADC0CF = 0x04;                         // interrupts upon SINC3 filter output
                                          // and uses external VREF

   
   		ADC0CLK = (SYSCLK/MDCLK)-1;            // Ideally, MDCLK = 2.4576 MHz
                                          
                                          // Generate MDCLK for modulator.
                               


   // program decimation rate for desired OWR

   		ADC0_decimation = (unsigned long) SYSCLK/ (unsigned long) OWR / 
                     (unsigned long) (ADC0CLK+1)/(unsigned long)128;

   		ADC0_decimation--;

  	    ADC0DEC = ADC0_decimation;


  	    ADC0BUF = 0x00;                        // 关闭输入缓冲


    	ADC0MUX = 0x01;                       // 差分输入
                                          // AIN+ => AIN0.0
                                          // AIN- => AIN0.1 
                                          




     	ADC0MD |= 0x80;                      // 使能(IDLE Mode)
}

//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
void UART0_Init (void)
{
   		SCON0 = 0x10;                          // 8-bit variable bit rate
                                          // level of STOP bit is ignored
                                          // RX enabled
                                          // ninth bits are zeros
                                          // clear RI0 and TI0 bits
   		if (SYSCLK/BAUDRATE/2/256 < 1)
    	{
      	TH1 = -(SYSCLK/BAUDRATE/2);
     	 CKCON |=  0x08;                     // T1M = 1; SCA1:0 = xx
    	} 
   		else if (SYSCLK/BAUDRATE/2/256 < 4)
		{
      	TH1 = -(SYSCLK/BAUDRATE/2/4);
      	CKCON &= ~0x0B;                     // T1M = 0; SCA1:0 = 01                  
      	CKCON |=  0x01;
  		} 
		else if (SYSCLK/BAUDRATE/2/256 < 12) 
		{
      	TH1 = -(SYSCLK/BAUDRATE/2/12);
      	CKCON &= ~0x0B;                     // T1M = 0; SCA1:0 = 00
   		} 
		else 
		{
      	TH1 = -(SYSCLK/BAUDRATE/2/48);
      	CKCON &= ~0x0B;                     // T1M = 0; SCA1:0 = 10
      	CKCON |=  0x02;
  		 }

   		TL1 = TH1;                             // init Timer1
   		TMOD &= ~0xf0;                         // TMOD: timer 1 in 8-bit autoreload
   		TMOD |=  0x20;                       
   		TR1 = 1;                               // START Timer1
  		TI0 = 1;                               // Indicate TX0 ready
}

⌨️ 快捷键说明

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