diff options
| author | skullY <skullydazed@gmail.com> | 2019-08-30 11:19:03 -0700 |
|---|---|---|
| committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-08-30 15:01:52 -0700 |
| commit | b624f32f944acdc59dcb130674c09090c5c404cb (patch) | |
| tree | bc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /drivers/avr/analog.c | |
| parent | 61af76a10d00aba185b8338604171de490a13e3b (diff) | |
| download | qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip | |
clang-format changes
Diffstat (limited to 'drivers/avr/analog.c')
| -rw-r--r-- | drivers/avr/analog.c | 53 |
1 files changed, 21 insertions, 32 deletions
diff --git a/drivers/avr/analog.c b/drivers/avr/analog.c index 1ec38df75..1a8da4261 100644 --- a/drivers/avr/analog.c +++ b/drivers/avr/analog.c | |||
| @@ -21,49 +21,38 @@ | |||
| 21 | #include <stdint.h> | 21 | #include <stdint.h> |
| 22 | #include "analog.h" | 22 | #include "analog.h" |
| 23 | 23 | ||
| 24 | static uint8_t aref = (1 << REFS0); // default to AREF = Vcc | ||
| 24 | 25 | ||
| 25 | static uint8_t aref = (1<<REFS0); // default to AREF = Vcc | 26 | void analogReference(uint8_t mode) { aref = mode & 0xC0; } |
| 26 | |||
| 27 | |||
| 28 | void analogReference(uint8_t mode) | ||
| 29 | { | ||
| 30 | aref = mode & 0xC0; | ||
| 31 | } | ||
| 32 | |||
| 33 | 27 | ||
| 34 | // Arduino compatible pin input | 28 | // Arduino compatible pin input |
| 35 | int16_t analogRead(uint8_t pin) | 29 | int16_t analogRead(uint8_t pin) { |
| 36 | { | ||
| 37 | #if defined(__AVR_ATmega32U4__) | 30 | #if defined(__AVR_ATmega32U4__) |
| 38 | static const uint8_t PROGMEM pin_to_mux[] = { | 31 | static const uint8_t PROGMEM pin_to_mux[] = {0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20}; |
| 39 | 0x00, 0x01, 0x04, 0x05, 0x06, 0x07, | 32 | if (pin >= 12) return 0; |
| 40 | 0x25, 0x24, 0x23, 0x22, 0x21, 0x20}; | 33 | return adc_read(pgm_read_byte(pin_to_mux + pin)); |
| 41 | if (pin >= 12) return 0; | ||
| 42 | return adc_read(pgm_read_byte(pin_to_mux + pin)); | ||
| 43 | #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) | 34 | #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) |
| 44 | if (pin >= 8) return 0; | 35 | if (pin >= 8) return 0; |
| 45 | return adc_read(pin); | 36 | return adc_read(pin); |
| 46 | #else | 37 | #else |
| 47 | return 0; | 38 | return 0; |
| 48 | #endif | 39 | #endif |
| 49 | } | 40 | } |
| 50 | 41 | ||
| 51 | // Mux input | 42 | // Mux input |
| 52 | int16_t adc_read(uint8_t mux) | 43 | int16_t adc_read(uint8_t mux) { |
| 53 | { | ||
| 54 | #if defined(__AVR_AT90USB162__) | 44 | #if defined(__AVR_AT90USB162__) |
| 55 | return 0; | 45 | return 0; |
| 56 | #else | 46 | #else |
| 57 | uint8_t low; | 47 | uint8_t low; |
| 58 | 48 | ||
| 59 | ADCSRA = (1<<ADEN) | ADC_PRESCALER; // enable ADC | 49 | ADCSRA = (1 << ADEN) | ADC_PRESCALER; // enable ADC |
| 60 | ADCSRB = (1<<ADHSM) | (mux & 0x20); // high speed mode | 50 | ADCSRB = (1 << ADHSM) | (mux & 0x20); // high speed mode |
| 61 | ADMUX = aref | (mux & 0x1F); // configure mux input | 51 | ADMUX = aref | (mux & 0x1F); // configure mux input |
| 62 | ADCSRA = (1<<ADEN) | ADC_PRESCALER | (1<<ADSC); // start the conversion | 52 | ADCSRA = (1 << ADEN) | ADC_PRESCALER | (1 << ADSC); // start the conversion |
| 63 | while (ADCSRA & (1<<ADSC)) ; // wait for result | 53 | while (ADCSRA & (1 << ADSC)) |
| 64 | low = ADCL; // must read LSB first | 54 | ; // wait for result |
| 65 | return (ADCH << 8) | low; // must read MSB only once! | 55 | low = ADCL; // must read LSB first |
| 56 | return (ADCH << 8) | low; // must read MSB only once! | ||
| 66 | #endif | 57 | #endif |
| 67 | } | 58 | } |
| 68 | |||
| 69 | |||
