Hello everyone.
I'm having some problems trying to read alternately the different ADC channels of an attiny85. In essence, the problem I have is when trying to manipulate which channel I am reading at a given moment and the transition to the next channel I want to read.
I have searched numerous threads and tried different proposed solutions but none has been successful, until now. Therefore, I think it's time to see if someone can guide me a little. I will try to be as clear and simple as possible.
What do I have to do: given an ISR function (ADC_vect), depending on the ADC channel I am reading, I must call another function "x_channel". After calling that function "x_channel" I must change the channel that I am reading by another, in order to alternate between the different channels so that I can read at some point all that are useful. In other words, the algorithm should read ADC0 in one call, ADC2 in the next, ADC3 in the later and then reread ADC0 to repeat the loop...
Channels that I should read:
- ADC0 = PB5 => (MUX = 00)
- ADC2 = PB4 => (MUX = 10)
- ADC3 = PB3 => (MUX = 11)
Note: ADC1 I don't need to read it.
Pseudocode:
ISR (ADC_vec) { if (ADC_source is PB5) { call ADC0 channel function; ADC_source = PB4; } else { if (ADC_source is PB4) { call function channel ADC2; ADC_source = PB3; } else { if (ADC_source is PB3) { call function ADC3 channel; ADC_source = PB5; } } } }
where ADC_source can be implemented through ADMUX, as I understand it.
My intent:
ADMUX is initialized as:
ADMUX = (1 << ADLAR) | (1 << MUX1) | (1 << MUX0);
Code:
ISR(ADC_vect) { uint8_t sample_value; sample_value = ADCH; if (ADMUX & 0x00) // sample from PB5 { TurnOnLED_ADC0(); // call function for ADC0 ADMUX = (1 << MUX1) | (0 << MUX0); // Swap ADMUX to 10 } else { if (ADMUX & 0x02) { TurnOnLED_ADC2(); // call function for ADC2 ADMUX = (1 << MUX1) | (1 << MUX0); // Swap ADMUX to 11 } else { if (ADMUX & 0x03) { TurnOnLED_ADC3(); // call function for ADC3 ADMUX = (0 << MUX1) | (0 << MUX0); // Swap ADMUX to 00 } } } }
Results: until now the algorithm does not respond as expected. The biggest difficulty is that when debugging it I can not distinguish if the error is in the way of comparing ADMUX to know if it is reading a given channel, either the modification of ADMUX is being done correctly, or even if both proposals are wrong.
Any opinion about it? Thank you very much in advance.