Hi, I read [TUT] [C] Newbie's Guide to the AVR ADC..
Very good toturial, Now I can use ADC to read value but just in loop..
I use ATMega 8
Question 1.
I want to do it better and use Interrupt.
I have written a ADC driver, and Main code..
Very easy but this code doesn't do anything, and nobody of the LEDs turning on..
I think my program never run ISR function..
ADC Driver:
void ADC_init(uint8_t _ADPS2, uint8_t _ADPS1, uint8_t _ADPS0){ if(_ADPS2 == 1){ setHigh(&ADCSRA, ADPS2); }else{ setLow(&ADCSRA, ADPS2); } if(_ADPS1 == 1){ setHigh(&ADCSRA, ADPS1); }else{ setLow(&ADCSRA, ADPS1); } if(_ADPS0 == 1){ setHigh(&ADCSRA, ADPS0); }else{ setLow(&ADCSRA, ADPS0); } } //Starts conversation void ADC_start(){ setHigh(&ADCSRA, ADSC); } void ADC_refs(uint8_t refs1, uint8_t refs0){ if(refs1 == 1){ setHigh(&ADMUX, REFS1); }else{ setLow(&ADMUX, REFS1); } if(refs0 == 1){ setHigh(&ADMUX, REFS0); }else{ setLow(&ADMUX, REFS0); } } //Enable ADC void ADC_enable(void){ setHigh(&ADCSRA, ADEN); } //Enbale ADC Interrupt void ADC_enableInt(void){ setHigh(&ADCSRA, ADIE); }
And the main code:
int main (void) { //Setup I/O setOutput(&DDRB, 0); setOutput(&DDRB, 1); setLow(&PORTB, 0); setLow(&PORTB, 1); //Choose Prescaler ADC_init(1, 1, 1); //Choose REFS ADC_refs(0, 1); //Set ADLAR high for Left Adjust Result for easy to read setHigh(&ADMUX, ADLAR); //Set on Free-Running modus setHigh(&ADCSRA, ADFR); //Enable ADC ADC_enable(); //Enable ADC Interrupt ADC_enableInt(); //Enable global Inerrupt sei(); //Start conversation ADC_start(); while(1){ } } ISR(ADC_vect){ if(ADCH >= 127){ setHigh(&PORTB, 0); setLow(&PORTB, 1); }else{ setHigh(&PORTB, 1); setLow(&PORTB, 0); } }
setOutput, setHigh and setLow is very simple function to choose output,
set value high or low and they works fine..
Question 2.
In ISR function, how is possible to check wich of MUX is running..
If I get value from ADC, how can I know this value is from PortC.x / MUX number x?