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

📄 adc.c

📁 Infineon单片机XC164CMADC模数转换模块和定时器中断例程
💻 C
字号:
//****************************************************************************
// @Module        Analog / Digital Converter (ADC)
// @Filename      ADC.C
// @Project       164.dav
//----------------------------------------------------------------------------
// @Controller    Infineon XC164CM-4F40
//
// @Compiler      Keil
//
// @Codegenerator 1.1
//
// @Description   This file contains functions that use the ADC module.
//
//----------------------------------------------------------------------------
// @Date          2007-5-4 22:13:47
//
//****************************************************************************

// USER CODE BEGIN (ADC_General,1)

// USER CODE END



//****************************************************************************
// @Project Includes
//****************************************************************************

#include "MAIN.H"

// USER CODE BEGIN (ADC_General,2)

// USER CODE END


//****************************************************************************
// @Macros
//****************************************************************************

// USER CODE BEGIN (ADC_General,3)

// USER CODE END


//****************************************************************************
// @Defines
//****************************************************************************

// USER CODE BEGIN (ADC_General,4)

// USER CODE END


//****************************************************************************
// @Typedefs
//****************************************************************************

// USER CODE BEGIN (ADC_General,5)

// USER CODE END


//****************************************************************************
// @Imported Global Variables
//****************************************************************************

// USER CODE BEGIN (ADC_General,6)

// USER CODE END


//****************************************************************************
// @Global Variables
//****************************************************************************

// USER CODE BEGIN (ADC_General,7)

// USER CODE END


//****************************************************************************
// @External Prototypes
//****************************************************************************

// USER CODE BEGIN (ADC_General,8)

// USER CODE END


//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************

// USER CODE BEGIN (ADC_General,9)

// USER CODE END


//****************************************************************************
// @Function      void ADC_vInit(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the initialization function of the ADC function 
//                library. It is assumed that the SFRs used by this library 
//                are in reset state. 
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          2007-5-4
//
//****************************************************************************

// USER CODE BEGIN (Init,1)

// USER CODE END

void ADC_vInit(void)
{
  // USER CODE BEGIN (Init,2)

  // USER CODE END

  ///  - enhanced mode is selected
  ///  - fixed channel single conversion mode is selected
  ///  - 8-bit resolution
  ///  - once converts channel 0
  ///  - ADC start bit is reset
  ///  - wait for ADDAT read mode is disabled
  ///  - converter basic clock tbc is fpd / 1
  ///  - sample time tsc is tbc * 8
  ///  - post calibration is disabled
  ///  - conversion time tc = 1.350 usec
  ///  - channel injection is disabled
  ADC_CTR0       =  0x8000;      // load ADC control register 0

  ADC_CTR2       =  0x1000;      // load ADC control register 2



  ///  -----------------------------------------------------------------------
  ///  Configuration of the used ADC Interrupts:
  ///  -----------------------------------------------------------------------

  ///  -----------------------------------------------------------------------
  ///  Configuration of the used ADC Port Pins:
  ///  -----------------------------------------------------------------------
  ///  - P5.0 is used for Analog Input Pin (AN0)
  ///  - P5.1 is used for Analog Input Pin (AN1)
  ///  - P5.2 is used for Analog Input Pin (AN2)
  ///  - P5.3 is used for Analog Input Pin (AN3)
  ///  - P5.4 is used for Analog Input Pin (AN4)
  ///  - P5.5 is used for Analog Input Pin (AN5)
  ///  - P5.6 is used for Analog Input Pin (AN6)
  ///  - P5.7 is used for Analog Input Pin (AN7)
  ///  - P5.10 is used for Analog Input Pin (AN10)
  ///  - P5.11 is used for Analog Input Pin (AN11)
  ///  - P5.12 is used for Analog Input Pin (AN12)
  ///  - P5.13 is used for Analog Input Pin (AN13)

  ///  - digital input stage is disconnected from port line P5.0
  ///  - digital input stage is disconnected from port line P5.1
  ///  - digital input stage is disconnected from port line P5.2
  ///  - digital input stage is disconnected from port line P5.3
  ///  - digital input stage is disconnected from port line P5.4
  ///  - digital input stage is disconnected from port line P5.5
  ///  - digital input stage is disconnected from port line P5.6
  ///  - digital input stage is disconnected from port line P5.7
  ///  - digital input stage is disconnected from port line P5.10
  ///  - digital input stage is disconnected from port line P5.11
  ///  - digital input stage is disconnected from port line P5.12
  ///  - digital input stage is disconnected from port line P5.13

  P5DIDIS        =  0x3CFF;      // load Port 5 digital input disable register



  // USER CODE BEGIN (Init,3)

  // USER CODE END

} //  End of function ADC_vInit


//****************************************************************************
// @Function      void ADC_vConfConv(ubyte ubMode, ubyte ubChannel) 
//
//----------------------------------------------------------------------------
// @Description   This function configures a new A/D conversion. In fixed 
//                channel single conversion mode (ADC_FIXED) the forwarded 
//                channel is converted once. In fixed channel continuous 
//                conversion mode (ADC_FIXED_CONTI) the forwarded channel is 
//                converted continuously. In auto scan single conversion mode 
//                (ADC_SCAN) a sequence beginning with the forwarded channel 
//                and ending with channel 0 is converted. In auto scan 
//                continuous conversion mode (ADC_SCAN_CONTI) the sequence is 
//                converted continuously. To start the conversion it is 
//                necessary to call ADC_vStartConv().
//                Note: 
//                While a conversion is in progress, the mode and the channel 
//                may be changed. The new mode will be evaluated after the 
//                current conversion. The new channel will be evaluated after 
//                the current conversion (fixed channel modes) or after the 
//                current conversion sequence (auto scan modes).  The 
//                following constants are available for ubMode: 
//                ADC_FIXED 
//                ADC_FIXED_CONTI 
//                ADC_SCAN 
//                ADC_SCAN_CONTI 
//                 
//                The following constants are available for ubChannel: 
//                ADC_ANA_0 .. ADC_ANA_7 and 
//                ADC_ANA_10 .. ADC_ANA_15 (see @Defines in header file) 
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    ubMode: 
//                Conversion mode
// @Parameters    ubChannel: 
//                Channel number
//
//----------------------------------------------------------------------------
// @Date          2007-5-4
//
//****************************************************************************

// USER CODE BEGIN (ConfConv,1)

// USER CODE END

void ADC_vConfConv(ubyte ubMode, ubyte ubChannel)
{

  ADC_CTR0 = (ADC_CTR0 & 0xFF80) | (ubMode << 5) | (ubChannel);

} //  End of function ADC_vConfConv

void ADC_vConfConvSingleMode(ubyte ubChannel)
{
  ADC_CTR0       =  ADC_CTR0&0xfff0;      // load ADC control register 0
  ADC_CTR0 = (ADC_CTR0)| (ubChannel);

} //  End of function ADC_vConfConv
//****************************************************************************
// @Function      uword ADC_uwReadConv(void) 
//
//----------------------------------------------------------------------------
// @Description   This function returns the result of a conversion. This 
//                function must be called after each conversion. The lower 10 
//                bits contain the conversion result while the upper 4 bits 
//                identify the converted analog channel. 
//
//----------------------------------------------------------------------------
// @Returnvalue   Conversion result
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          2007-5-4
//
//****************************************************************************

// USER CODE BEGIN (ReadConv,1)

// USER CODE END

uword ADC_uwReadConv(void)
{

  ADC_CIC_IR = 0;
  return(ADC_DAT);

} //  End of function ADC_uwReadConv


void ADC_uwReadConvtest(void)
{
	unsigned char i;
	unsigned int testdata;
	ADC_CIC_IR=0;
	for(i=0;i<8;i++)
	{
		ADC_vConfConvSingleMode(i);
		ADC_vStartConv();
		while(!ADC_CIC_IR);
			testdata=ADC_DAT;
		ADC_CIC_IR=0;
	}
  	for(i=10;i<14;i++)
	{
		ADC_vConfConvSingleMode(i);
		ADC_vStartConv();
		while(!ADC_CIC_IR);
			testdata=ADC_DAT;
			testdata=testdata&0x0ff0;
			testdata=testdata>>4;
		ADC_CIC_IR=0;
	}

} //  End of functi

// USER CODE BEGIN (ADC_General,10)

// USER CODE END

⌨️ 快捷键说明

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