📄 adc.c
字号:
#ifndef _ADC_C_
#define _ADC_C_
#include "c8051f000.h"
#include "adc.h"
#include "system.h"
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
//
// Configure ADC0 to use Timer3 overflows as conversion source, to
// generate an interrupt on conversion complete, and to use left-justified
// output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled.
//
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
//
// Configure ADC0 to use AD0BUSY as conversion source, to use left-justified
// output mode, to use normal tracking mode, and to measure the output of
// the on-chip temperature sensor. Disables ADC0 end of conversion interrupt
// and ADC0 window compare interrupt.
//
void fADC0_Init (void)
{
ADC0CN = 0x80; // ADC0 enabled; normal tracking
// mode; ADC0 conversions are initiated
// on write to AD0BUSY; ADC0 data is
// left-justified
REF0CN = 0x03; // disable temp sensor, on-chip VREF,
// and VREF output buffer
AMX0SL = 0x00; // Select channel 0 as ADC mux output
ADC0CF = 0x80; //
EIE2 &= ~0x02; // disable ADC0 EOC interrupt
EIE1 &= ~0x04; // disable ADC0 window compare interrupt
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
//
// ADC0 end-of-f ISR
// Here we take the ADC0 sample, add it to a running total <accumulator>, and
// decrement our local decimation counter <int_dec>. When <int_dec> reaches
// zero, we post the decimated result in the global variable <result>.
//
void fADC0_ISR (void) interrupt 15
{
static unsigned int_dec=INT_DEC; // integrate/decimate counter
// we post a new result when
// int_dec = 0
static long accumulator=0L; // here's where we integrate the
// ADC samples
ADCINT = 0; // clear ADC conversion complete
// indicator
accumulator += ADC0; // read ADC value and add to running
// total
int_dec--; // update decimation counter
if (int_dec == 0) { // if zero, then post result
int_dec = INT_DEC; // reset counter
result = accumulator >> 12;
accumulator = 0L; // reset accumulator
}
}
unsigned int fADConverter(unsigned char channel)
{
unsigned int adc_result = 0,temp;
unsigned char i;
// AMX0SL = channel;
for(i=0;i<16;i++)
{
ADCINT = 0; // clear conversion complete indicator
ADBUSY = 1; // initiate conversion
while (ADCINT == 0); // wait for conversion complete
adc_result = adc_result + ADC0L; // read ADC0 data
temp = ADC0H;
temp = temp <<8;
adc_result = adc_result + temp ;
}
adc_result = adc_result>>4;
// adc_result = i-16;
return(adc_result);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -