Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
void adcSingleConversion(AdcChannel channel){ // Set ADC channel. ADMUX |= channel; // Set ADC to single conversion mode. ADCSRA |= ADC_SINGLE_CONVERSION_MODE; // Start the conversion. ADCSRA |= ADC_START_CONVERSION; // Enable ADC interrupts. ADCSRA |= ADC_INTERRUPT_ENABLE; // Enable global interrupts. SREG |= GLOBAL_INT_MASK;}ISR(ADC_vect){ // Convert ADC result to centimeters, anything over 150 cm is infinite range. adcResult = IR_CONVERSION_CONSTANT/(ADCL|ADCH<<8); // Store the scan into the array. scan[scanIndex++] = adcResult; servoAngle += servoDirection;}
I've tried both modes, what you probably want to do is single conversion mode. When you want to get a reading from your sensor start a conversion, then in your ADC interrupt subroutine store that conversion so you can use it. The continuous mode was far to rapid and took up too much the the AVR's resources for me. Maybe this will help, this is for the ATmega 128. The key is just setting the flags correctly. Here is one function to start a conversion, and the interrupt routine that will be called when the conversion is done. When you want to get another reading, just start another conversion. I would make sure your waiting a little bit of time (very little) to make sure the conversion is complete before you call another one.Code: [Select]void adcSingleConversion(AdcChannel channel){ // Set ADC channel. ADMUX |= channel; // Set ADC to single conversion mode. ADCSRA |= ADC_SINGLE_CONVERSION_MODE; // Start the conversion. ADCSRA |= ADC_START_CONVERSION; // Enable ADC interrupts. ADCSRA |= ADC_INTERRUPT_ENABLE; // Enable global interrupts. SREG |= GLOBAL_INT_MASK;}ISR(ADC_vect){ // Convert ADC result to centimeters, anything over 150 cm is infinite range. adcResult = IR_CONVERSION_CONSTANT/(ADCL|ADCH<<8); // Store the scan into the array. scan[scanIndex++] = adcResult; servoAngle += servoDirection;}