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.

problem in analog to digital convertion in atmega 8

Status
Not open for further replies.

selva murugesan

Advanced Member level 4
Joined
Mar 30, 2012
Messages
116
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,298
Activity points
2,007
what is wrong with this following code? i am using 1Mhz clk




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void adc_init (void);
int adc_read (unsigned char ch_select);
 
void adc_init (void)
{
    ADCSRA |= 0x83;
}
 
int adc_read (unsigned char ch_select)
{
    int tenbit_value,adc_low;
    ADMUX |= ch_select;
 
    ADCSRA = ADCSRA | (1<<ADSC);    // Start ADC conversion
    while(!(ADCSRA & (1<<ADIF)))    // Wait for AD interrupt flag 
    adc_low = ADCL;
 
    tenbit_value = (ADCH << 2 | adc_low>>6); 
    ADCSRA.ADSC |= 0;
 
    return tenbit_value;
}


there is show no error.but i cant get output.
 
Last edited by a moderator:

this is how i do it !
Code:
void InitADC()
{
ADMUX=(1<<REFS0);                  // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}

uint16_t ReadADC(char ch_select)
{
   ADMUX |= ch_select;  
   ADCSRA|=(1<<ADSC);
   while(!(ADCSRA & (1<<ADIF)));
   ADCSRA|=(1<<ADIF);
   return(ADC);
}

hope this will help you
 

what is wrong with this following code? i am using 1Mhz clk




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void adc_init (void);
int adc_read (unsigned char ch_select);
 
void adc_init (void)
{
    ADCSRA |= 0x83;
}
 
int adc_read (unsigned char ch_select)
{
    int tenbit_value,adc_low;
    ADMUX |= ch_select;
 
    ADCSRA = ADCSRA | (1<<ADSC);    // Start ADC conversion
    while(!(ADCSRA & (1<<ADIF)))    // Wait for AD interrupt flag 
    adc_low = ADCL;
 
    tenbit_value = (ADCH << 2 | adc_low>>6); 
    ADCSRA.ADSC |= 0;
 
    return tenbit_value;
}


there is show no error.but i cant get output.

Since your ATmega8 is running at 1MHz there are 2 valid ADC clock frequencies you can select:
125KHz and 62.5kHz

If you want to run the ADC at 125kHz you can initialize it this way:
ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0);
This expression is similar to
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0);
and
ADCSRA = 0x83;

Publish the whole code with main and circuit if you want detailed help

---------- Post added at 14:56 ---------- Previous post was at 14:50 ----------

https://extremeelectronics.co.in/avr-tutorials/using-the-analog-to-digital-converter/

---------- Post added at 14:56 ---------- Previous post was at 14:56 ----------

https://extremeelectronics.co.in/avr-tutorials/using-the-analog-to-digital-converter/
 

here i have phased main() program. my task is get 2 analog value from out side,change into corresponding decimal value and finally ,show the values in 6 (7 segments display) as (0-999) format.here, i used max 7219 interface for LED driver.the header files of the SPI and max 7219 are download from net


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <mega8.h>
#include <delay.h>
#include <macros.h>
#include <ADC_EDT_10bit.H>
#include <spi_header.h>
#include <max7219.h>
 
#define mul 199.8
//display test normal mode
 
 
 
// Declare your global variables here
unsigned char w,x,y,z;
unsigned char flag;
int adc_result1,adc_result2,i;
int adc_display1,adc_display2;
 
 
void adc_read_in(void);
void hex_dec(unsigned int value);
void main(void)
{                              
     
    //PORTC = 0x00;
    //DDRC  = 0x00;
                 
    DDRB|=0x20; 
    
    PORTD = 0x01;
    DDRD  = 0x01; 
    
    adc_init(); 
    spi_init();
    max_init(); 
 
    TCCR0 = 0x00;
    TCNT0 = 0x00;
    TIMSK  = 0x02;
 
    #asm("sei")
 
while (1)
      { 
        PORTD.0 = 1;                           //for LED
        adc_read_in();
        hex_dec(adc_display1);       
      }
}
void adc_read_in(void)
{
    adc_result1  = adc_read(0x61);                                  //adc connected to 1 so 61(0110 0001) see the adc header
    flag = 0;
    adc_display1 =(int) (adc_result1 * mul);                       //mul for bring (0-999) format
    hex_dec (adc_display1);
    
    delay_ms(100);
    
    adc_result2  = adc_read(0x62);
    flag = 1;
    adc_display2 = (int)(adc_result2 * mul);
    hex_dec (adc_display2);
  
    delay_ms(100);
}
void hex_dec(unsigned int value)
{ 
  if (value>100)
  {
    for (i=900;i>=100;i-=100)
    {
        if (value>=i)
        {
            value = (value - i);
            i = i/100;
            x=i;
            w = value/10;
            z = value%10;
            y = w%10;
            break;
        }
    }
  }
  else 
  {
        w = value/10;
        x = w/10;
        z = value%10;
        y = w%10;
        w = x/10;
        x = x%10;
  }
    
  if (flag == 0)
  {         
        max_data(6,(z + 0x30));
        delay_ms(10);
        max_data(5,(y+0x30));
        delay_ms(10);
        max_data(4,(x+0x30));
        delay_ms(10);
           
  }
  else if (flag == 1)
  {
        max_data(3,(z+0x30));
        delay_ms(10);
        max_data(2,(y+0x30));
        delay_ms(10);
        max_data(1,(x+0x30));
        delay_ms(10);
  }  
}

 
Last edited by a moderator:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void InitADC()
{
ADMUX=(1<<REFS0);                         // For Aref=AVcc;
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); //Rrescalar div factor =28 , 125KHz
//same as ADCSRA=0x83;
}
 
unsigned int ReadADC(unsigned char ch)
{
   
   ch=ch&0b00000111;
   ADMUX|=ch;
   ADCSRA|=(1<<ADSC);
   while(!(ADCSRA & (1<<ADIF)));
// mind the ; in the previous line
    ADCSRA|=(1<<ADIF);
 
   return(ADC);
}
 
void main()
{
float mul;
 unsigned int  adc_result;
mul=199.8;
float temp;
--------------------
---------------------- 
     InitADC();
-----------------------
  
   while(1)
   {
        adc_result=ReadADC(0); // Read Analog value from channel-0
    temp=adc_result*mul;
    adc_result=temp;
    //Display routine here
    Wait(); //enough delay
    adc_result=ReadADC(1); // Read Analog value from channel-1
    temp=adc_result*mul;
    adc_result=temp;
    //display routine here
    Wait();//enough delay
 
 
   }
}

 
Last edited:
i check my whole code . i got problem in ADC ,this code is not run in proteus software,properly .my point is ,ADC conversion is not finished.it means the code struggle at this line - while(!(ADCSRA & (1<<ADIF))); // Wait for AD interrupt flag

ADC program is
Code:
void adc_init (void);                            //adc initialisation
unsigned int adc_read (unsigned char ch_select); //channel selection

void adc_init (void)
{
     ADCSRA  = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); //125Khz
     ADMUX   = (1<<REFS0) | (1<<ADLAR);             //For Aref = AVcc 
}

unsigned int adc_read (unsigned char ch_select)
{
    unsigned int tenbit_value;
    unsigned char adc_low;
    
    ADCSRA  = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); //125Khz    
    ADMUX |= ch_select; 
    ADCSRA = ADCSRA | (1<<ADSC);	            // Start ADC conversion
    while(!(ADCSRA & (1<<ADIF)));               // Wait for AD interrupt flag 
    ADCSRA |= (1<<ADIF);                        // Stop convertion 
    delay_ms(100);
    ADCSRA &= 0X00;    
    adc_low = ADCL;
    tenbit_value = (ADCH << 2 | adc_low>>6);     //make 10 bit value
    return tenbit_value;
}
 

i check my whole code . i got problem in ADC ,this code is not run in proteus software,properly .my point is ,ADC conversion is not finished.it means the code struggle at this line - while(!(ADCSRA & (1<<ADIF))); // Wait for AD interrupt flag

ADC program is
Code:
void adc_init (void);                            //adc initialisation
unsigned int adc_read (unsigned char ch_select); //channel selection

void adc_init (void)
{
     ADCSRA  = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); //125Khz
     ADMUX   = (1<<REFS0) | (1<<ADLAR);             //For Aref = AVcc 
}

unsigned int adc_read (unsigned char ch_select)
{
    unsigned int tenbit_value;
    unsigned char adc_low;
    
    ADCSRA  = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); //125Khz    
    ADMUX |= ch_select; 
    ADCSRA = ADCSRA | (1<<ADSC);	            // Start ADC conversion
    while(!(ADCSRA & (1<<ADIF)));               // Wait for AD interrupt flag 
    ADCSRA |= (1<<ADIF);                        // Stop convertion 
    delay_ms(100);
    ADCSRA &= 0X00;    
    adc_low = ADCL;
    tenbit_value = (ADCH << 2 | adc_low>>6);     //make 10 bit value
    return tenbit_value;
}
Dear Selva,
I just have had like this problem with atmega8 ADC within proteus software, the firmware codes works over the PCB board but it does not work in proteus simulator;
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top