adc.c File Reference

#include "adc.h"

Functions

void adc_select_channel (uint8_t channel)
 select "channel"
void adc_LED (void)
 handels the LED output
void adc_off (void)
 switches the ADC off
void adc_set_nr_channels (int8_t nr)
 sets the nr of active channels
void adc_next_channel (void)
 switches to the next channel
void adc_set_presc (int8_t presc)
 sets the prescaler
void adc_single_channel (void)
void adc_multi_channels (void)
void adc_init (uint8_t channel)
 initialise ADC with "channel"
void adc_stop (void)
 stops the ADC
int8_t adc_stopped (void)
 checks, whether the ADC has stopped

Variables

volatile uint8_t adc_prescaler = 1
 the current prescaler
volatile uint16_t adc_count = 0
volatile int8_t adc_stop_flag = 1
 still needed ???
volatile uint32_t adc_starttime = 0
volatile double adc_duration = 0
volatile uint8_t adc_channels = 1
 nr of active channels
volatile uint8_t adc_current_channel = 0

Function Documentation

void adc_init uint8_t  channel  ) 
 

initialise ADC with "channel"

00104                                {
00105     adc_count = 0;
00106     adc_stop_flag = 0;
00107 
00108     adc_select_channel(channel);    //set the channel
00109     adc_current_channel = channel;  //save the number of the current channel
00110     adc_LED();
00111 
00112     ADCSRA = adc_prescaler & 7;     //& 7: only assign first three bits
00113     //AD Enable, AD Start Conversion, AD Free Running, AD Interrupt Enable
00114     adc_starttime = us_time_get();
00115     ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADFR);//interrupt method: | (1<<ADIE);
00116 
00117     if(adc_channels == 1) adc_single_channel();
00118     else adc_multi_channels();
00119 
00120     //stop adc again
00121     adc_stop();
00122 }

void adc_LED void   ) 
 

handels the LED output

00039                   {
00040     if(adc_channels == 1) PORTA = (PORTA & 15) + (8 << 4); //& 15: assign only first 4 bits
00041     if(adc_channels == 2) PORTA = (PORTA & 15) + (12 << 4); //& 15: assign only first 4 bits
00042     if(adc_channels == 3) PORTA = (PORTA & 15) + (14 << 4); //& 15: assign only first 4 bits
00043     if(adc_channels == 4) PORTA = (PORTA & 15) + (15 << 4); //& 15: assign only first 4 bits
00044 }

void adc_multi_channels void   ) 
 

00085                              {
00086     //fast measurement
00087     loop_until_bit_is_set(ADCSRA,ADIF);
00088     ADCSRA |= (1<<ADIF);
00089     adc[0] = ADCH;
00090     for(uint16_t x=0; x < ADC_BUF_SIZE; x++){
00091         adc_next_channel();
00092         loop_until_bit_is_set(ADCSRA,ADIF);
00093         adc[x] = ADCH;
00094         ADCSRA |= (1<<ADIF);
00095 
00096         loop_until_bit_is_set(ADCSRA,ADIF);
00097         ADCSRA |= (1<<ADIF);
00098         loop_until_bit_is_set(ADCSRA,ADIF);
00099         ADCSRA |= (1<<ADIF);
00100     }
00101 }

void adc_next_channel void   ) 
 

switches to the next channel

00057                            {
00058     uint8_t new = adc_current_channel;
00059     
00060     new++;
00061     if(new > adc_channels) new = 1;
00062     adc_select_channel(new);
00063 }

void adc_off void   ) 
 

switches the ADC off

00047                   {
00048     PORTA = PORTA & 15; //sets the upper 4 bits to zero
00049 
00050     ADCSRA = 0;
00051 }

void adc_select_channel uint8_t  channel  ) 
 

select "channel"

00029                                         {
00030     if(channel == adc_current_channel) return;
00031     adc_current_channel = channel;
00032     
00033     //Input Channel selection; obersten beiden bits wählen externe Referenz, wenn 0
00034     //=> ADMUX entspricht Channel
00035     ADMUX = (channel-1) + (1<<ADLAR) + (1<<REFS0);
00036     //ADMUX |= (1<<ADLAR);  //left-align bits to allow to read the 8bit-value from ADCH
00037 }

void adc_set_nr_channels int8_t  nr  ) 
 

sets the nr of active channels

00053                                    {
00054     adc_channels = nr;
00055 }

void adc_set_presc int8_t  presc  ) 
 

sets the prescaler

00066                                 {
00067     if(presc <= 7 && presc >= 1) adc_prescaler = presc;
00068 }

void adc_single_channel void   ) 
 

00072                              {
00073     //fast measurement
00074     loop_until_bit_is_set(ADCSRA,ADIF);
00075     ADCSRA |= (1<<ADIF);
00076     adc[0] = ADCH;
00077     for(uint16_t x=0; x < ADC_BUF_SIZE; x++){
00078         loop_until_bit_is_set(ADCSRA,ADIF);
00079         adc[x] = ADCH;
00080 //      adc_next_channel();
00081         ADCSRA |= (1<<ADIF);
00082     }
00083 }

void adc_stop void   ) 
 

stops the ADC

switches off the free running mode.

Todo:
still needed?
00127                      {
00128     ADCSRA &= ~(1<<ADSC) & ~(1<<ADFR) & ~(1<<ADIE);
00129     adc_stop_flag = 1;
00130 
00131     adc_duration = us_time_get_difference_d(adc_starttime) / ((double)ADC_BUF_SIZE / (double)adc_channels);
00132 }

int8_t adc_stopped void   ) 
 

checks, whether the ADC has stopped

Todo:
still needed?
00135                         {
00136     return adc_stop_flag;
00137 }


Variable Documentation

volatile uint8_t adc_channels = 1
 

nr of active channels

volatile uint16_t adc_count = 0
 

volatile uint8_t adc_current_channel = 0
 

volatile double adc_duration = 0
 

volatile uint8_t adc_prescaler = 1
 

the current prescaler

volatile uint32_t adc_starttime = 0
 

volatile int8_t adc_stop_flag = 1
 

still needed ???


Generated on Fri Aug 11 21:47:31 2006 for OpenOsci by  doxygen 1.4.6