Skip to content
This repository
Browse code

Simple ADC code from http://pjrc.com/teensy/adc.html

  • Loading branch information...
commit 49cbd6b6e2d2a49cba9a6471000a26cfcba1131e 1 parent 5448d73
authored June 10, 2010

Showing 2 changed files with 89 additions and 0 deletions. Show diff stats Hide diff stats

  1. 53  analog.c
  2. 36  analog.h
53  analog.c
... ...
@@ -0,0 +1,53 @@
  1
+// Simple analog to digitial conversion
  2
+
  3
+#include <avr/io.h>
  4
+#include <avr/pgmspace.h>
  5
+#include <stdint.h>
  6
+#include "analog.h"
  7
+
  8
+
  9
+static uint8_t aref = (1<<REFS0); // default to AREF = Vcc
  10
+
  11
+
  12
+void analogReference(uint8_t mode)
  13
+{
  14
+	aref = mode & 0xC0;
  15
+}
  16
+
  17
+
  18
+// Arduino compatible pin input
  19
+int16_t analogRead(uint8_t pin)
  20
+{
  21
+#if defined(__AVR_ATmega32U4__)
  22
+	static const uint8_t PROGMEM pin_to_mux[] = {
  23
+		0x00, 0x01, 0x04, 0x05, 0x06, 0x07,
  24
+		0x25, 0x24, 0x23, 0x22, 0x21, 0x20};
  25
+	if (pin >= 12) return 0;
  26
+	return adc_read(pgm_read_byte(pin_to_mux + pin));
  27
+#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  28
+	if (pin >= 8) return 0;
  29
+	return adc_read(pin);
  30
+#else
  31
+	return 0;
  32
+#endif
  33
+}
  34
+
  35
+// Mux input
  36
+int16_t adc_read(uint8_t mux)
  37
+{
  38
+#if defined(__AVR_AT90USB162__)
  39
+	return 0;
  40
+#else
  41
+	uint8_t low;
  42
+
  43
+	ADCSRA = (1<<ADEN) | ADC_PRESCALER;		// enable ADC
  44
+	ADCSRB = (1<<ADHSM) | (mux & 0x20);		// high speed mode
  45
+	ADMUX = aref | (mux & 0x1F);			// configure mux input
  46
+	ADCSRA = (1<<ADEN) | ADC_PRESCALER | (1<<ADSC);	// start the conversion
  47
+	while (ADCSRA & (1<<ADSC)) ;			// wait for result
  48
+	low = ADCL;					// must read LSB first
  49
+	return (ADCH << 8) | low;			// must read MSB only once!
  50
+#endif
  51
+}
  52
+
  53
+
36  analog.h
... ...
@@ -0,0 +1,36 @@
  1
+#ifndef _analog_h_included__
  2
+#define _analog_h_included__
  3
+
  4
+#include <stdint.h>
  5
+
  6
+void analogReference(uint8_t mode);
  7
+int16_t analogRead(uint8_t pin);
  8
+int16_t adc_read(uint8_t mux);
  9
+
  10
+#define ADC_REF_POWER     (1<<REFS0)
  11
+#define ADC_REF_INTERNAL  ((1<<REFS1) | (1<<REFS0))
  12
+#define ADC_REF_EXTERNAL  (0)
  13
+
  14
+// These prescaler values are for high speed mode, ADHSM = 1
  15
+#if F_CPU == 16000000L
  16
+#define ADC_PRESCALER ((1<<ADPS2) | (1<<ADPS1))
  17
+#elif F_CPU == 8000000L
  18
+#define ADC_PRESCALER ((1<<ADPS2) | (1<<ADPS0))
  19
+#elif F_CPU == 4000000L
  20
+#define ADC_PRESCALER ((1<<ADPS2))
  21
+#elif F_CPU == 2000000L
  22
+#define ADC_PRESCALER ((1<<ADPS1) | (1<<ADPS0))
  23
+#elif F_CPU == 1000000L
  24
+#define ADC_PRESCALER ((1<<ADPS1))
  25
+#else
  26
+#define ADC_PRESCALER ((1<<ADPS0))
  27
+#endif
  28
+
  29
+// some avr-libc versions do not properly define ADHSM
  30
+#if defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  31
+#if !defined(ADHSM)
  32
+#define ADHSM (7)
  33
+#endif
  34
+#endif
  35
+
  36
+#endif

0 notes on commit 49cbd6b

Please sign in to comment.
Something went wrong with that request. Please try again.