adc_internal.c File Reference

Module for using the internal analog to digital converters on the LPC2194/01 microprocessor. More...

Go to the source code of this file.

Functions

void adci_convert_all (void)
 Begins the conversions of all the channels.
void adci_convert_next (void)
 Begins conversion of the next channel in the list.
void adci_efilter_add (volatile ADCI_FILTER *f, short num)
 Adds a new value to the filter.
volatile ADCI_FILTERadci_get_filter (ADCI_CHAN channel)
 Returns a pointer to the filter struct for the given channel.
int adci_get_raw (ADCI_CHAN chan)
 Returns the raw value in the filter for the given channel.
int adci_get_result (ADCI_CHAN channel)
 Returns the value of the filtered data for the given channel.
void adci_init (int coeff0, int coeff1, int coeff2, int coeff3)
 Initializes the adc_internal module.
static void adci_init_channel (ADCI_CHAN ch, int coeff)
 Initializes the given channel with the filter coefficient.
void adci_isr (void)
 This function is called by the interrupt upon completion of a channel conversion.

Variables

static volatile ADCI_FILTER adci_filters [4]
 The filters for the incoming data.
static ADCI_CHAN adci_sched [4]
 The list of channels to convert.
static int adci_sched_index = -1
 Index of the channel list.
static int adci_sched_size = 0
 Size of the filled portion of the channel array.
static ADCI_STATUS adci_status = ADCI_DONE
 The state of the adc conversion.

Detailed Description

Module for using the internal analog to digital converters on the LPC2194/01 microprocessor.

Example hardware and register setup:

  // *******************************************************************************
  // Initialize Internal ADC 
  // *******************************************************************************
  //Burst mode setup (old)
  //ADCR = 0x0021C700|(ADCI_CH0_READ)|(ADCI_CH1_READ << 1)|(ADCI_CH2_READ << 2)|(ADCI_CH3_READ << 3);
  //Interrupt driven setup - CLKDIV = 199; bit21 is ADC enabled; 
  //Which channels to read from; if you want to read from a channel, set its value to 1
  #define ADCI_CH0_READ 0
  #define ADCI_CH1_READ 0
  #define ADCI_CH2_READ 0
  #define ADCI_CH3_READ 0
  ADCR = (1<<21)|(199<<8) & ~ADCR_RB;
  //Set Pins to be AINx if reading from that channel
  if (ADCI_CH0_READ) {
    PINSEL1 &= ~(3<<22);
    PINSEL1 |= (1<<22);
  }  //AIN0 on pin P0.27
  if (ADCI_CH1_READ) {
    PINSEL1 &= ~(3<<24);
    PINSEL1 |= (1<<24);
  }  //AIN1 on pin P0.28 
  if (ADCI_CH2_READ) {
    PINSEL1 &= ~(3<<26);
    PINSEL1 |= (1<<26);
  }  //AIN2 on pin P0.29
  if (ADCI_CH3_READ) {
    PINSEL1 &= ~(3<<28);
    PINSEL1 |= (1<<28);
  }  //AIN3 on pin P0.30  
  //adci interrupts for conversion completions
  ADINTEN = (1<<8)//(ADCI_CH0_READ)|(ADCI_CH1_READ << 1)|(ADCI_CH2_READ << 2)|(ADCI_CH3_READ << 3)
      & ~ADINTEN_RB;
Author:
Nicolas Williamson
Date:
March 2009; Rewritten November 2009
Version:
0.2

Definition in file adc_internal.c.


Function Documentation

void adci_convert_all ( void   ) 

Begins the conversions of all the channels.

The converter will interrupt upon completion of a channel and signal the next channel to begin converting.

Definition at line 77 of file adc_internal.c.

References adci_convert_next(), adci_status, and error_occurred().

00078 {
00079   if (adci_status == ADCI_NOT_DONE) { //previous conversion did not finish
00080     error_occurred(ERROR_ADCI_DNF);
00081   }
00082   adci_convert_next();
00083 }

void adci_efilter_add ( volatile ADCI_FILTER f,
short  num 
)

Adds a new value to the filter.

Maximum value accepted is 2^16.

Note:
Filter average will hold (your value)*2^14 -> bit shift back if you'd like to reduce resolution or to get out values that you expect.
Parameters:
f The filter to add the new value to.
num The new value to add to the filter.

Definition at line 180 of file adc_internal.c.

References ADCI_FILTER::average, ADCI_FILTER::coeff, ADCI_FILTER::count, and error_occurred().

Referenced by adci_isr().

00180                                                          {
00181   if (num > (1<<16)){
00182     error_occurred(ERROR_ADCI_FILT_OOB);
00183   }
00184   if (f->average == 0)
00185     f->average = num << 14;
00186   else
00187     f->average = f->average - (f->average >> f->coeff) + (((int)num) << (14 - f->coeff));
00188   f->count = f->count + 1;
00189 } 

volatile ADCI_FILTER* adci_get_filter ( ADCI_CHAN  channel  ) 

Returns a pointer to the filter struct for the given channel.

Parameters:
channel The channel to get the filter from.
Returns:
A pointer to the filter.
See also:
ADCI_FILTER

Definition at line 167 of file adc_internal.c.

00168 {
00169   return &adci_filters[channel];
00170 }

int adci_get_raw ( ADCI_CHAN  chan  ) 

Returns the raw value in the filter for the given channel.

Because of the exponential filter, this value will be (data << 14), or data * 2^14.

Parameters:
chan The channel to read from.
Returns:
The raw adc value from the filter.

Definition at line 141 of file adc_internal.c.

References ADCI_FILTER::average.

00142 {
00143   return adci_filters[chan].average;
00144 }

int adci_get_result ( ADCI_CHAN  channel  ) 

Returns the value of the filtered data for the given channel.

Parameters:
channel The channel to read from.
Returns:
The filtered adc value.

Definition at line 129 of file adc_internal.c.

References ADCI_FILTER::average.

00130 {
00131   return adci_filters[channel].average>>14;
00132 }

void adci_init ( int  coeff0,
int  coeff1,
int  coeff2,
int  coeff3 
)

Initializes the adc_internal module.

Coeffs are a value between -1 and 14 (inclusive) and are the coeffecients for the exponential filter. Passing -1 (ADCI_NULL) means don't use that channel. Passing 0 (ADCI_NO_FILTER) means no filtering on that channel.

Parameters:
coeff0 The coefficient for channel 0.
coeff1 The coefficient for channel 1.
coeff2 The coefficient for channel 2.
coeff3 The coefficient for channel 3.

Definition at line 65 of file adc_internal.c.

References adci_init_channel().

00065                                                               {
00066   adci_init_channel(ADCI_CH_0, coeff0);
00067   adci_init_channel(ADCI_CH_1, coeff1);
00068   adci_init_channel(ADCI_CH_2, coeff2);
00069   adci_init_channel(ADCI_CH_3, coeff3);
00070 }

static void adci_init_channel ( ADCI_CHAN  ch,
int  coeff 
) [static]

Initializes the given channel with the filter coefficient.

Parameters:
ch The channel to initialize.
coeff The coefficient for the filter.

Definition at line 151 of file adc_internal.c.

References adci_sched, adci_sched_size, ADCI_FILTER::average, ADCI_FILTER::coeff, and ADCI_FILTER::count.

Referenced by adci_init().

00152 {
00153   if (coeff >= 0){
00154     adci_filters[ch].average = 0; //no initial value
00155     adci_filters[ch].count = 0;   //no values yet
00156     adci_filters[ch].coeff = coeff;   //filter coeff
00157     adci_sched[adci_sched_size] = ch;
00158     adci_sched_size++;
00159   }
00160 }

void adci_isr ( void   ) 

This function is called by the interrupt upon completion of a channel conversion.

Adds the value to the filter.

Definition at line 109 of file adc_internal.c.

References adci_convert_next(), and adci_efilter_add().

00110 {
00111   int reg = ADGDR;
00112   int chan = (reg>>24) & (0x7);
00113   int result = (reg>>6) & (0x3FF);
00114 
00115   adci_efilter_add(&(adci_filters[chan]), result); 
00116   
00117 
00118 //  printf("chan: %i\n",chan);
00119   VICVectAddr = 0; //Interrupt Acknowledged
00120   adci_convert_next(); 
00121 
00122 }


Variable Documentation

volatile ADCI_FILTER adci_filters[4] [static]

The filters for the incoming data.

Definition at line 49 of file adc_internal.c.

ADCI_CHAN adci_sched[4] [static]

The list of channels to convert.

Definition at line 50 of file adc_internal.c.

Referenced by adci_convert_next(), and adci_init_channel().

int adci_sched_index = -1 [static]

Index of the channel list.

Definition at line 52 of file adc_internal.c.

Referenced by adci_convert_next().

int adci_sched_size = 0 [static]

Size of the filled portion of the channel array.

Definition at line 53 of file adc_internal.c.

Referenced by adci_convert_next(), and adci_init_channel().

ADCI_STATUS adci_status = ADCI_DONE [static]

The state of the adc conversion.

Definition at line 51 of file adc_internal.c.

Referenced by adci_convert_all(), and adci_convert_next().

Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3