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

📄 analog.c

📁 cygnal上的数据采集的程序(带实时时钟)
💻 C
字号:
//-----------------------------------------------------------------------------
// Copyright (c) 2002 Jim Brady
// Do not use commercially without author's permission
// Last revised August 2002
// Net ANALOG.C
//
// This module handles the analog inputs which are external temperature
// sensor, the on-chip temperature sensor, and operating voltage.
//-----------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include "C8051f.h"
#include "serial1.h"
#include "analog.h"


sfr16 ADC0     = 0xbe;                 // ADC0 data

//--------------------------------------------------------------------------
// Initialize the A/D converter
// 
//--------------------------------------------------------------------------
void ADC_Init(void)
{
	ADC0CN = 0x81;                      // ADC0 enabled; normal tracking
     	                                // mode; ADC0 conversions are initiated 
                                       	// on write to AD0BUSY; ADC0 data is
                                       	// left-justified
	REF0CN = 0x07;                      // enable temp sensor, on-chip VREF,
                                       	// and VREF output buffer
	AMX0CF = 0x00;
	AMX0SL = MUX_CPU_TEMP; 	

    ADC0CF = (SYSCLK/2500000) << 3;     // ADC conversion clock = 2.5MHz
 	ADC0CF &= 0x0f8;                     // PGA gain = 2
	EIE2 &= ~0x02;                      // disable ADC0 EOC interrupt
	EIE1 &= ~0x04;                      // disable ADC0 window compare interrupt
}



//--------------------------------------------------------------------------
// This function is a little state machine which reads one analog
// inputs at a time, out of the 3 possible inputs
//  1. On-chip temperature
//  2. External air temperature
//  3. CPU operating voltage
//--------------------------------------------------------------------------
unsigned long Read_Analog_Inputs(unsigned char mux_select)
{
   	unsigned long idata temp_long;

	temp_long = 0;

	AMX0SL = mux_select;
	AD0INT = 0;                     
   	AD0BUSY = 1;                     
    while (AD0INT == 0);
	AD0INT = 0;                     
   	AD0BUSY = 1;                     
    while (AD0INT == 0);

	switch (mux_select)
	{
		case MUX_CPU_TEMP:
		{
     
			temp_long = ADC0 - 42380/2;
		    temp_long = (temp_long * 200L) / 156;

			break;
		}
		case MUX_AN0:
		{
			temp_long = ADC0;
			temp_long = 24*temp_long/655;
			break;
		}

		case MUX_AN1:
		{
			temp_long = ADC0;
			temp_long = 24*temp_long/655;
			break;
		}

		default:
		{
			return 0;
		}
  	}
	ADC0 = 0;
	return temp_long;
}
 

 

⌨️ 快捷键说明

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