Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Doubt in multiple ADC conversion

Status
Not open for further replies.

sribalaji

Junior Member level 2
Joined
Jul 12, 2010
Messages
23
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
India
Activity points
1,441
Doubt regarding multiple ADC conversion

My objective is to convert voltage and current simultaneously in an atmega controller and to find the power by multiplying it.
I've written the following code for ADC conversion:

Code:
interrupt [ADC_INT] void adc_isr(void)
{
data= ADCW;
 if(adc_channel==0)
 {
   voltage_data_available=TRUE;
   adc_channel++;
 } 
 else if(adc_channel==1)
 {
   line_current_data_available=TRUE;
   adc_channel++;
 }
 else
 {
   neutral_current_data_available=TRUE;
   adc_channel=0;
 } 
 }


   
  
void main(void)
{
ADMUX=adc_channel;
ADCSRA=0b11101101;
#asm("sei")

while (1)
      {
        if(voltage_data_available==TRUE)
          {
            voltage=data;
            voltage_data_available=FALSE;
            }
         
        else if(line_current_data_available==TRUE)
          {
            line_current=data;
            line_current_data_available=FALSE;
          }
          
        else if(neutral_current_data_available==TRUE)
         {
           neutral_current=data;
           neutral_current_data_available=FALSE;
         } 
         
         power=voltage*line_current;       
            
      };
}

Initially adc_channel was set to 0. Is this the correct way to write the code in case of multiple ADC conversion? I'm new to atmel programming and I've used codevision avr compiler.
 

This code does not change the ADC channel. You must change the ADMUX register to change the channel. I think this is what you mean isn't it?
Moreover, although the flags work properly for this small project, later on it might get more confusing. So you could edit the interrupt code as follows:

Code:
interrupt [ADC_INT] void adc_isr(void)
{
  data= ADCW;
  if(adc_channel==0)
  {
    voltage_data_available=TRUE;
    line_current_data_available=FALSE;
    neutral_current_data_available=FALSE;
    adc_channel++;
  } 
  else if(adc_channel==1)
  {
    line_current_data_available=TRUE;
    voltage_data_available=FALSE;
    neutral_current_data_available=FALSE;
    adc_channel++;
  }
  else
  {
    neutral_current_data_available=TRUE;
    voltage_data_available=FALSE;
    line_current_data_available=FALSE;
    adc_channel=0;
  } 
}

In this way you are 100% sure that flags always have the correct value.

In addition, if the project gets bigger and the main loop runs slower with software delays added etc, then there is a possibility that an interrupt will come before you process the acquired analog value. Thus it is maybe better to save ADCW in three different variables, one for each case. An array of values would be even better.

Hope this helps.
 
can i do computation in the adc interrupt subroutine? Is this efficient?
 

can i do computation in the adc interrupt subroutine? Is this efficient?
It depends on the sample rate. Since you are using interrupt, for high sample rates you better keep it short. I think you can go up to 1MHz for 8-bit resolution. Also it depends on the project. Are there interrupts more important than this? And in what rate? And how much time consuming is this computation? If it is not critical for this algorithm to take place inside the interrupt routine, maybe it is better to do it from outside.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top