Fix port addressing for joystick analog read

This commit is contained in:
a-chol 2019-12-29 18:04:50 +01:00
parent b030c45705
commit d88bdc6a1b
3 changed files with 12 additions and 31 deletions

View file

@ -46,7 +46,6 @@ int16_t analogRead(uint8_t pin) {
#endif
}
<<<<<<< HEAD
int16_t analogReadPin(pin_t pin) { return adc_read(pinToMux(pin)); }
uint8_t pinToMux(pin_t pin) {
@ -95,36 +94,13 @@ uint8_t pinToMux(pin_t pin) {
case C5: return _BV(MUX2) | _BV(MUX0); // ADC5
// ADC7:6 not present in DIP package and not shared by GPIO pins
default: return _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0); // 0V
=======
// Mux input
int16_t adc_read(uint8_t mux)
{
#if defined(__AVR_AT90USB162__)
return 0;
#else
uint16_t res = 0;
ADCSRA = (1<<ADEN) | ADC_PRESCALER; // enable ADC
#ifndef __AVR_ATmega32A__
ADCSRB = (1<<ADHSM) | (mux & 0x20); // high speed mode
#endif
ADMUX = aref | (mux & 0x1F); // configure mux input
ADCSRA |= (1<<ADSC); // start the conversion
while (ADCSRA & (1<<ADSC)) ; // wait for result
res = ADCL; // must read LSB first
res |= (ADCH << 8) | res;
ADCSRA &= ~(1<<ADEN);//turn off the ADC
return res;
>>>>>>> 5939a4430... Add save and restore of each pin used in reading joystick (AVR).
#endif
// clang-format on
}
}
int16_t adc_read(uint8_t mux) {
uint8_t low;
uint16_t low;
// Enable ADC and configure prescaler
ADCSRA = _BV(ADEN) | ADC_PRESCALER;
@ -152,5 +128,10 @@ int16_t adc_read(uint8_t mux) {
// Must read LSB first
low = ADCL;
// Must read MSB only once!
return (ADCH << 8) | low;
low |= (ADCH << 8);
//turn off the ADC
ADCSRA &= ~(1<<ADEN);
return low;
}