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.

Error in using multiple channel ADC in AVR ATMEGA8

Status
Not open for further replies.

habibparacha

Junior Member level 2
Joined
Oct 25, 2010
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,482
I am trying to use multiple channels of ADC using interrupts but whenever the I run the simulation in proteus it gives the error.

Only the first conversion is written to the ADCL register and the next conversion is not written to it. It gives the error .

Below is my code and the picture of error I get
Kindly Help,


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
#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/sleep.h>
 
#define NUMCHANNELS 2
 
volatile uint8_t CurrentCh;
volatile uint8_t Channel;
 
ISR(ADC_vect)
{   
    PORTD= ADCL;
    
    Channel++;
    CurrentCh = Channel & 0b00000111;
    
     if(Channel<NUMCHANNELS)
     { 
        ADMUX |= CurrentCh;
        PORTB= 0xff;
     }       
     else
     { 
        Channel=0;
        ADMUX |= CurrentCh;
        PORTB= 0x00;     
     }       
    
    ADCSRA|= (1<<ADSC);
    
     //PORTD= ADCL;
     //PORTB= 0xff;
    
}   
 
int main(void)
{
    DDRC=0x00; //initilize as input
    DDRB=0xff;
    DDRD=0xff;
    PORTB=0X00;
    PORTD=0X00;
    Channel = 0;
    CurrentCh = Channel & 0b00000111;
    
    //** Setting Sleep mode idle **//
    //MCUCR |= (1<<SE);
    //MCUCR &= !((1 << SM0) | (1 << SM1) | (1 << SM2));
    
    ADMUX|=(1<<REFS0);
    //ADMUX |= (1 << MUX0) | (1<<MUX1);//| (1 << MUX2) | (1 << MUX3) );
    ADMUX |= CurrentCh;
    
    //Prescaler = 64, free running mode = off, interrupts off.
    ADCSRA |= (1 << ADPS2) | (1 << ADPS1);
    //ADCSRA |= (1 << ADIF);    //Reset any pending ADC interrupts
    
    ADCSRA |= (1 << ADEN) | (1 << ADIE);  //Enable the ADC
    sei();
    ADCSRA|= (1<<ADSC);
    
    //ADCSRA|=(1<<ADIF);    
    
    //sei();
    while(1)
    {SLEEP_MODE_IDLE;
    }
}



Error.png
 
Last edited by a moderator:

The problem is PORTD= ADCL;

If you check the datasheet it says
When ADCL is read, the ADC Data Register is not updated until ADCH is read. Consequently, if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read ADCH.
Otherwise, ADCL must be read first, then ADCH.

So you must either read ADCH too or replace the above line with PORTD= ((0xff) & ADCW) or left adjust the result and read ADCH
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top