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

📄 adcmain.c

📁 这是单片机C51典型应用设计代码
💻 C
字号:
/*--------------------------------------------------------------------------
adcMain.c

Contains a sample program that implements the AD converter for the 
Amtel Wirless T87C5111 and T87C5112.

Copyright (c) 1988-2001 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/

#include <stdio.h>

sfr  ADCLK   =   0xF2;	
sfr  ADCON   =   0xF3;			 
sfr  ADDL    =   0xF4;			 
sfr  ADDH    =   0xF5;			 
sfr  ADCF    =   0xF6;

//sfr  IE1     =   0xB1;
//sbit  EA     =   0xA8^7;

sfr  SCON    =   0x98;
sbit  TI     =   SCON^1;

sfr  TMOD    =   0x89;
sfr  TH1     =   0x8D;

sbit  TR1    =   0x88^6;

/******************************************************************************
This function is called when the AD conversion is finished by the CPU.  This 
is only used when in Accurate or Precision mode. 
******************************************************************************/
//void ADC_INT( void ) interrupt 9
//{
//	ADCON &= ~0x10;
//}


/******************************************************************************
This function Initializes all values required to run the AD converter.
******************************************************************************/

void Init_ADC( void )
{
	ADCF |= 0xFF;		//Sets all channels for available input
	ADCLK |= 0x7F;		//Sets clock to second slowest value
	ADCON |= 0x20;		//Enables the ADC
	//ADCON |= 0x80;      //Sets QUIETM bit for Accurate conversion (10bit)
	//ADCON |=0x40;		  //Sets PSIDLE bit for Precision conversion (9bit)

	//IE1 |= 0x20;		//Enable ADC Interrupt (used in 9-10bit conversion)
	//EA  = 1;			//Enable Global Interrupts
}


/******************************************************************************
This function reads a channel and starts the AD conversion
******************************************************************************/
unsigned Read_ADC( unsigned char channel )
{
	ADCON &= ~0x07;                 //Clear channel bits
	ADCON |= 0x07 & channel;		//Sets channel to be read in ADCON

	ADCON |= 0x8;					//Start Conversion
	while( ADCON & 0x8 );			//Waiting until end of conversion.
	return( ((unsigned) ADDH << 2) | (ADDL & 3) );
}
/******************************************************************************
Main Function.  Read each channel and outputs the converted value
******************************************************************************/

void main( void )
{
	unsigned char i;


/*------------------------------------------------
Setup the serial port for 1200 baud at 16MHz.
------------------------------------------------*/
    SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr      */
    TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload        */
    TH1   = 221;                /* TH1:  reload value for 1200 baud @ 16MHz   */
    TR1   = 1;                  /* TR1:  timer 1 run                          */
    TI    = 1;                  /* TI:   set TI to send first char of UART    */

	Init_ADC();

   	while(1)
   	{
		for( i = 0; i <8; i++ )
		{
			printf("Channel %u = %4u\n", (unsigned)i,Read_ADC( i ));
		}
	}
}

⌨️ 快捷键说明

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