2017-03-29 04:20:36 +06:00
|
|
|
/* Copyright 2015 Jack Humbert
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-08-17 03:52:03 +06:00
|
|
|
// Simple analog to digitial conversion
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "analog.h"
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
static uint8_t aref = (1 << REFS0); // default to AREF = Vcc
|
2015-08-17 03:52:03 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
void analogReference(uint8_t mode) { aref = mode & 0xC0; }
|
2015-08-17 03:52:03 +06:00
|
|
|
|
|
|
|
// Arduino compatible pin input
|
2019-08-31 00:19:03 +06:00
|
|
|
int16_t analogRead(uint8_t pin) {
|
2015-08-17 03:52:03 +06:00
|
|
|
#if defined(__AVR_ATmega32U4__)
|
2019-08-31 00:19:03 +06:00
|
|
|
static const uint8_t PROGMEM pin_to_mux[] = {0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20};
|
|
|
|
if (pin >= 12) return 0;
|
|
|
|
return adc_read(pgm_read_byte(pin_to_mux + pin));
|
2015-08-17 03:52:03 +06:00
|
|
|
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
|
2019-08-31 00:19:03 +06:00
|
|
|
if (pin >= 8) return 0;
|
|
|
|
return adc_read(pin);
|
2015-08-17 03:52:03 +06:00
|
|
|
#else
|
2019-08-31 00:19:03 +06:00
|
|
|
return 0;
|
2015-08-17 03:52:03 +06:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mux input
|
2019-08-31 00:19:03 +06:00
|
|
|
int16_t adc_read(uint8_t mux) {
|
2015-08-17 03:52:03 +06:00
|
|
|
#if defined(__AVR_AT90USB162__)
|
2019-08-31 00:19:03 +06:00
|
|
|
return 0;
|
2015-08-17 03:52:03 +06:00
|
|
|
#else
|
2019-08-31 00:19:03 +06:00
|
|
|
uint8_t low;
|
|
|
|
|
|
|
|
ADCSRA = (1 << ADEN) | ADC_PRESCALER; // enable ADC
|
|
|
|
ADCSRB = (1 << ADHSM) | (mux & 0x20); // high speed mode
|
|
|
|
ADMUX = aref | (mux & 0x1F); // configure mux input
|
|
|
|
ADCSRA = (1 << ADEN) | ADC_PRESCALER | (1 << ADSC); // start the conversion
|
|
|
|
while (ADCSRA & (1 << ADSC))
|
|
|
|
; // wait for result
|
|
|
|
low = ADCL; // must read LSB first
|
|
|
|
return (ADCH << 8) | low; // must read MSB only once!
|
2015-08-17 03:52:03 +06:00
|
|
|
#endif
|
|
|
|
}
|