C8051F912怎么切换ADC通道
时间:10-02
整理:3721RD
点击:
我现在只会用一个模拟输入的,程序如下,现在想加入两个模拟输入,程序怎么处理啊,端口初始化怎么加,怎么分辨对应的数据
//-----------------------------------------------------------------------------
void Timer2_Init (void)
{
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// use SYSCLK as timebase, 16-bit
// auto-reload
CKCON |= 0x10; // select SYSCLK for timer 2 source
TMR2RL = 65535 - (SYSCLK / 10000); // init reload value for 10uS
TMR2 = 0xffff; // set to reload immediately
TR2 = 1; // start Timer2
}
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0x02; // ADC0 disabled, normal tracking,
// conversion triggered on TMR2 overflow
REF0CN = 0x01; // Select internal high speed voltage
// reference
ADC0MX = 0x0e; // Select P1.6 as the ADC input pin
ADC0CF = ((SYSCLK/8300000)-1)<<3; // Set SAR clock to 8.3MHz
ADC0CF |= 0x00; // Select Gain of 0.5
ADC0AC = 0x00; // Right-justify results, no shifting
// applied
EIE1 |= 0x08; // Enable ADC0 conversion complete int.
AD0EN = 1; // Enable ADC0
}
//------------------------------------------------------------------
INTERRUPT (ADC_ISR, INTERRUPT_ADC0_EOC)
{
static unsigned long accumulator = 0; // accumulator for averaging
static unsigned int measurements = 2048; // measurement counter
unsigned long result=0;
AD0INT = 0; // clear ADC0 conv. complete flag
accumulator += ADC0;
measurements--;
if(measurements == 0)
{
measurements = 2048;
result = accumulator / 2048;
accumulator=0;
// The 10-bit ADC value is averaged across 2048 measurements.
// The measured voltage applied to P1.4 is then:
//
// Vref (mV)
// measurement (mV) = --------------- * result (bits)
// (2^10)-1 (bits)
mV = result * 3360 / 1023;
//-----------------------------------------------------------------------------
void Timer2_Init (void)
{
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// use SYSCLK as timebase, 16-bit
// auto-reload
CKCON |= 0x10; // select SYSCLK for timer 2 source
TMR2RL = 65535 - (SYSCLK / 10000); // init reload value for 10uS
TMR2 = 0xffff; // set to reload immediately
TR2 = 1; // start Timer2
}
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0x02; // ADC0 disabled, normal tracking,
// conversion triggered on TMR2 overflow
REF0CN = 0x01; // Select internal high speed voltage
// reference
ADC0MX = 0x0e; // Select P1.6 as the ADC input pin
ADC0CF = ((SYSCLK/8300000)-1)<<3; // Set SAR clock to 8.3MHz
ADC0CF |= 0x00; // Select Gain of 0.5
ADC0AC = 0x00; // Right-justify results, no shifting
// applied
EIE1 |= 0x08; // Enable ADC0 conversion complete int.
AD0EN = 1; // Enable ADC0
}
//------------------------------------------------------------------
INTERRUPT (ADC_ISR, INTERRUPT_ADC0_EOC)
{
static unsigned long accumulator = 0; // accumulator for averaging
static unsigned int measurements = 2048; // measurement counter
unsigned long result=0;
AD0INT = 0; // clear ADC0 conv. complete flag
accumulator += ADC0;
measurements--;
if(measurements == 0)
{
measurements = 2048;
result = accumulator / 2048;
accumulator=0;
// The 10-bit ADC value is averaged across 2048 measurements.
// The measured voltage applied to P1.4 is then:
//
// Vref (mV)
// measurement (mV) = --------------- * result (bits)
// (2^10)-1 (bits)
mV = result * 3360 / 1023;
同时这也是多年来经历风雨坚韧乐观的生活态度
程序里p1.6是模拟输入,我想再加两个模拟输入,初始化和中断怎么处理
不要悄无生息的沉下去啊,求大神们指导一下
怎么没人帮忙