Hello,
I am trying to read multiple ADC values. Based on the ADC5 value I want to turn ON or OFF an LED Connected to PA1 and based on the ADC7 value I want to turn ON or OFF a led Connected to PA0. I have read multiple posts in the forum and tried to use the solution mentioned there as well. But did not have any luck. It looks like when I run only one ADC everything works just fine but after adding the code for the second ADC, both the LEDs turn ON or OFF based on either of the ADC value change. Or change their brightness. Any suggestion on what I am doing wrong?
Thank you.
Here is my code:
- #include<avr/io.h>
- #define F_CPU 8000000UL
- void adc_setup_R()
- {
- ADCSRA|=(1<<ADEN); //Enable ADC
- ADMUX=0b00000101; // Select ADC5
- ADCSRB=0x00; //free running mode
- ADCSRA|=(1<<ADSC)|(1<<ADATE); //Start ADC conversion and enable Auto trigger
- //while(ADCSRA & (1<<ADSC)) {} // wait for the conversion to be complete
- }
- void adc_setup_L()
- {
- ADCSRA|=(1<<ADEN); //Enable ADC
- ADMUX=0b00000111; // Select ADC7
- ADCSRB=0x00; //free running mode
- ADCSRA|=(1<<ADSC)|(1<<ADATE); //Start ADC conversion and enable Auto trigger
- //while(ADCSRA & (1<<ADSC)) {} // wait for the conversion to be complete
- }
- int main()
- {
- DDRA =0b00000011; //PA0 & PA1 as output
- while(1)
- {
- adc_setup_R();
- int adc_val_1=(ADCH<<8)|ADCL; //ADC Read
- ADCSRA|=(1<<ADIF); //clearing the ADIF flag before starting the next conversion
- adc_setup_L();
- int adc_val_2=(ADCH<<8)|ADCL; //ADC Read
- ADCSRA|=(1<<ADIF); //clearing the ADIF flag before starting the next conversion
- if (adc_val_1>=250)
- {
- PORTA=0b00000010;
- }
- else
- {
- PORTA =0b00000000;
- }
- if (adc_val_2>=250)
- {
- PORTA=0b00000001;
- }
- else
- {
- PORTA =0b00000000;
- }
- }
- }














