0% found this document useful (0 votes)
111 views

ADC

This document defines several special function registers for a microcontroller including registers for a data pointer, timers, analog-to-digital converter, digital-to-analog converters. It also includes function prototypes for initializing the ADC and an ADC interrupt service routine. The main routine initializes the ADC, enables interrupts, and prints the temperature measured by the ADC in degrees Celsius.

Uploaded by

francissimo185
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

ADC

This document defines several special function registers for a microcontroller including registers for a data pointer, timers, analog-to-digital converter, digital-to-analog converters. It also includes function prototypes for initializing the ADC and an ADC interrupt service routine. The main routine initializes the ADC, enables interrupts, and prints the temperature measured by the ADC in degrees Celsius.

Uploaded by

francissimo185
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//-----------------------------------------------------------------------------

// 16-bit SFR Definitions for F00x, F01x


//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
//-----------------------------------------------------------------------------
//*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void ADC_Init (void);
void ADC_ISR (void);
//-----------------------------------------------------------------------------
//*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
// long temp_copy;
// int temp_int; // integer portion of temperature
// int temp_frac; // fractional portion of temperature (i
n
// hundredths of a degree)
ADC_Init (); // init ADC
ADCEN = 1; // enable ADC
// result = 0L; // initialize temperature variable
EA = 1; // Enable global interrupts
while (1) {
temp_copy = result; // Get most recent sample to convert
// the ADC code to a temperature
temp_copy -= 0xa381; // correct offset to 0deg, 0V
temp_copy *= 0x01a9; // 2.86mV/degree C
temp_copy *= 100; // convert result to 100ths of a degree C
temp_copy = temp_copy >> 16; // divide by 2^16
temp_int = temp_copy / 100; // Seperate integer and fractional compon
ents
temp_frac = temp_copy - (100 * temp_int);
printf ("Temperature is %d.%d\n", (int) temp_int, (int) temp_fra
c);
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*
//-----------------------------------------------------------------------------
// ADC_Init
//-----------------------------------------------------------------------------
//
// Configure A/D converter to use Timer3 overflows as conversion source, to
// generate an interrupt on conversion complete, and to use right-justified
// output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled.
//
void ADC_Init (void)
{
ADC0CN = 0x04; // ADC disabled; normal tracking mode;
// ADC conversions are initiated on overflow of Timer3;
// ADC data is right-justified
AMX0SL = 0x0f; // Select TEMP sens as ADC mux output
ADC0CF = 0x61; // ADC conversion clock = sysclk/8
EIE2 |= 0x02; // Enable ADC interrupts
}
//-----------------------------------------------------------------------------
//*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// ADC_ISR
//-----------------------------------------------------------------------------
//
// ADC end-of-conversion ISR
// Here we take the ADC sample, add it to a running total <accumulator>, and
// decrement our local decimation counter <int_dec>. When <int_dec> reaches
// zero, we calculate the new value of the global variable <result>,
// which stores the accumulated ADC result.
//
void ADC_isr (void) interrupt 15
{
static unsigned int_dec=256; // Integrate/Decimate counter, new result when int
_dec = 0
static long accumulator=0L; // ADC samples Integration result
ADCINT = 0; // Clear ADC conversion complete indicator
accumulator += ADC0; // Add ADC to the running total
int_dec--; // update decimation counter
if (int_dec == 0) { // if zero, then decimate
int_dec = 256; // reset counter
result = accumulator >> 4;// Shift to perform the divide operation
accumulator = 0L; // dump accumulator
}
}

You might also like