avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Troubles with ADC on ATMega16


From: Olav Torheim
Subject: [avr-gcc-list] Troubles with ADC on ATMega16
Date: Thu, 8 May 2003 19:50:18 +0200 (CEST)

Hi!
 
I have a little problem with creating a real time system on my AVR ATMega 16. The response time for my system is 60 us and I need to alway have digitized data ready from four analogue channels.
 
What I am trying to do is to create a program that is running through a loop, sampling four channels, one after another. My program is running fine when only sampling one channel. But when I try to sequencially sample through four channels, I am not able to get a stable result from any conversion. It seems like the program is not able to handle the switching between the channels. Does anybody understand why?
 
Olav Torheim
 
--
 
Cut and paste from my main program:
 
startADC(channelcounter+4); // Starting first AD conversion
while(1)
{
                                if (gAdcConversionCompleted == 1)
                                {
                                        mL[channelcounter] = returnAILow();
                                        mH[channelcounter] = returnAIHigh();
 
// If I comment away this if-statement, the program is running fine while
//only following one channel! :
 
                                        if (channelcounter == 3)
                                        {
                                                channelcounter = 0;
                                        }
                                        else
                                        {
                                                channelcounter++;
                                        }
                                        startADC(channelcounter+4); // Starting next conversion

                                }
}
 
Source code for ADC_AVR.C:
 
#include <io.h>
#include <interrupt.h>
#include <sig-avr.h>
#ifndef ADC_AVR
#define ADC_AVR
volatile int gAdcConversionCompleted;
char mAiLow, mAiHigh;
SIGNAL(SIG_ADC)
{
        gAdcConversionCompleted = 1;
}
void initAI(void)
{
  outp(0x0F, DDRA);
        sbi(ADCSR, ADEN); // Slær på AD-umsetjaren
        sbi(ADCSR, ADIE); // Slær på Interrupt-Enable (IE)
}
void convertADC(char channel)
{
        outp(channel, ADMUX); // Vel kanal
  
        sbi(ADCSR, ADSC); // Startar AD-umsetjing
        while(gAdcConversionCompleted!=1);
        gAdcConversionCompleted =0;
        mAiLow = inp(ADCL);
        mAiHigh = inp(ADCH);
}
void startADC(char channel)
{
  
        outp(channel, ADMUX); // Vel kanal
  gAdcConversionCompleted =0;
  outp(0x00, ADCL);
  outp(0x00,ADCH); 
        sbi(ADCSR, ADSC); // Startar AD-umsetjing
       
}
char returnAILow(void)
{
 return inp(ADCL);
}
char returnAIHigh(void)
{
 return inp(ADCH);
}
char readAIHigh(void)
{
        return mAiHigh;
}
char readAILow(void)
{
        return mAiLow;
}
#endif

Ny versjon av Yahoo! Messenger
Nye ikoner og bakgrunner, webkamera med superkvalitet og dobbelt så morsom


reply via email to

[Prev in Thread] Current Thread [Next in Thread]