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

📄 adcmain.c

📁 这本书里共11章
💻 C
字号:
/*--------------------------------------------------------------------------
adcMain.c

Contains a sample program that implements the AD converter for the 
Amtel Wirless T89C51CC01 and T89C51CC02.

Copyright (c) 1988-2001 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#include <stdio.h>
          
sfr  ADCON   =   0xF3;
sfr  ADCF    =   0xF6;
sfr  ADCLK   =   0xF2;
sfr  ADDH    =   0xF5;
sfr  ADDL    =   0xF4;

sfr  SCON    =   0x98;
sfr  TMOD    =   0x89;
sfr  TH1     =   0x8D;
sbit  TR1    =   0x88^6;
sbit  TI     =   0x98^1;

/*=============================================================================
=============================================================================*/

/*=============================================================================
This function initializes the A/D Converter on the Atmel WM CC01 using
10-bits.
=============================================================================*/
void init_ADC (void)
{
	ADCF = 0xFF;   // Setup P1 as 8 ADC inputs

	ADCLK = 0x1F;  // Set AD Clock divisor to 32 (the maximum)

	ADCON |= 0x20; // Enable the A/D Converter
}

/*=============================================================================
This function sets the A/D channel to convert and starts a conversion.
The 10-bit value is returned.
=============================================================================*/
unsigned read_ADC (unsigned char channel)
{
	ADCON &= ~0x07;               // Clear channel bits
	ADCON |= 0x07 & channel;      // Set channel to convert

	ADCON |= 0x08;                // Start a Conversion

	while( ADCON & 0x8 );         //Wait for conversion to end

	return (((unsigned) ADDH << 2) | (ADDL & 3));
}

/*=============================================================================
=============================================================================*/
void main(void)
{
	unsigned char i;

/*-----------------------------------------------
Setup the serial port.
-----------------------------------------------*/
	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    */

/*-----------------------------------------------
Initialize the A/D Converter
-----------------------------------------------*/
	init_ADC ();

/*-----------------------------------------------
Loop thru each channel and output the converted
value.
-----------------------------------------------*/
	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 + -