Society of Robots - Robot Forum

Software => Software => Topic started by: hendrachubby on May 26, 2009, 11:08:05 AM

Title: Newbie ask : Read ADC value simultaneously
Post by: hendrachubby on May 26, 2009, 11:08:05 AM
Hello all,

I'm trying to program my ATmega16 to collect data from a 5 analog sensor and display it on the LCD, But i got a problem because i can't read the value simultaneously, any one can help me ?

here is my code

Code: [Select]
// Header Files
#include <mega16.h>

// Alphanumeric LCD Module Functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

#include <delay.h>
#include <stdlib.h>

#define ADC_VREF_TYPE 0x40

// Read The AD Conversion Result
unsigned int read_adc(unsigned char adc_input)
{
    ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
    // Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
    // Start the AD conversion
    ADCSRA|=0x40;
    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    return ADCW;
}

// Global Variable
char hasil_1[], hasil_2[], hasil_3[], hasil_4[], hasil_5[];
long voltage_1, voltage_2, voltage_3, voltage_4, voltage_5;

// Fungsi Ambil Data
void ambil_Data()
{
   
    lcd_gotoxy(0,0);
    lcd_putsf("1:");
    read_adc(1);
    voltage_1 = (((long)5*(long)ADCW)/((long)10.23))-(long)16;
    ltoa(voltage_1,hasil_1);
    lcd_gotoxy(2,0);
    //delay_ms(15);
    lcd_puts(hasil_1);
   
    lcd_gotoxy(6,0);
    lcd_putsf("2:");
    read_adc(2);
    voltage_2 = (((long)5*(long)ADCW)/((long)10.23))-(long)16;       
    ltoa(voltage_2,hasil_2);
    lcd_gotoxy(8,0);
    //delay_ms(15);
    lcd_puts(hasil_2);
   
    lcd_gotoxy(11,0);
    lcd_putsf("3:");
    read_adc(3);
    voltage_3 = (((long)5*(long)ADCW)/((long)10.23))-(long)16;
    ltoa(voltage_3,hasil_3);
    lcd_gotoxy(13,0);
    //delay_ms(15);
    lcd_puts(hasil_3);
   
    lcd_gotoxy(0,1);
    lcd_putsf("4:");
    read_adc(4);
    voltage_4 = (((long)5*(long)ADCW)/((long)10.23))-(long)16;
    ltoa(voltage_4,hasil_4);
    lcd_gotoxy(2,1);
    //delay_ms(15);
    lcd_puts(hasil_4);

    lcd_gotoxy(6,1);
    lcd_putsf("5:");
    read_adc(5);
    voltage_5 = (((long)5*(long)ADCW)/((long)10.23))-(long)16;
    ltoa(voltage_5,hasil_5);
    lcd_gotoxy(8,1);
    //delay_ms(15);
    lcd_puts(hasil_5);
   
}

void chip_Initialization()
{
    PORTA=0x00;
    DDRA=0x00;

    // ADC initialization
    // ADC Clock frequency: 1000.000 kHz
    // ADC Voltage Reference: AVCC pin
    // ADC Auto Trigger Source: Free Running
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0xA4;
    SFIOR&=0x1F;

    // LCD module initialization
    lcd_init(16);
}
   
void main(void)
{
    // Local Variable

    chip_Initialization();
    while(1)
    {
        ambil_Data();   
    }

}



   
   
Title: Re: Newbie ask : Read ADC value simultaneously
Post by: Razor Concepts on May 26, 2009, 03:36:53 PM
I'm fairly certain atmegas cannot read ADC values simultaneously, there is only one set of registers that do the ADC conversions.
Title: Re: Newbie ask : Read ADC value simultaneously
Post by: galannthegreat on May 26, 2009, 03:55:36 PM
Well how long do you think it takes to do all of them? With that you may be able to use a delay, then print to the LCD everything at once, then it appears to be simultaneous.
Title: Re: Newbie ask : Read ADC value simultaneously
Post by: Admin on May 31, 2009, 01:29:39 PM
Only one value can be read at a time. The time it takes between each is defined by your pre-scaler, and can be found in the datasheet. Its like one reading per 100us or something like that.
Title: Re: Newbie ask : Read ADC value simultaneously
Post by: paulstreats on May 31, 2009, 02:26:02 PM
The computer in front of you appears to run tasks simultaneously but in reality it doesnt. The key word is "appears", definately read the sensors 1 at a time, keep the readings as variables and then print them all out at once
Title: Re: Newbie ask : Read ADC value simultaneously
Post by: hendrachubby on June 01, 2009, 05:33:51 AM
Okay...thx all...