From 488e42605b785397d07f210ff0a4aad671acf3d9 Mon Sep 17 00:00:00 2001 From: Michael Moon Date: Mon, 16 Aug 2010 14:06:37 +1000 Subject: [PATCH] skeleton of analog read subsystem --- analog.c | 15 +++++++++++++++ analog.h | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 analog.c create mode 100644 analog.h diff --git a/analog.c b/analog.c new file mode 100644 index 0000000..e618e19 --- /dev/null +++ b/analog.c @@ -0,0 +1,15 @@ +#include "analog.h" + +void analog_init() { + PRR &= ~MASK(PRADC); + ADMUX = REFERENCE; + ADCSRA = MASK(ADEN) | MASK(ADPS2) | MASK(ADPS1) | MASK(ADPS0); +} + +uint16_t analog_read(uint8_t channel) { + ADMUX = (ADMUX & 0xF0) | channel; + ADCSRA |= MASK(ADSC); + // waits. I hate waiting + for (;ADCSRA | MASK(ADSC);); + return ADC; +} diff --git a/analog.h b/analog.h new file mode 100644 index 0000000..49bc7b5 --- /dev/null +++ b/analog.h @@ -0,0 +1,18 @@ +#ifndef _ANALOG_H +#define _ANALOG_H + +#define REFERENCE_AREF 0 +#define REFERENCE_AVCC 64 +#define REFERENCE_1V1 192 + +#ifndef REFERENCE +#warning define REFERENCE as one of +#warning REFERENCE_AREF, REFERENCE_AVCC or REFERENCE_1V1 +#warning in your machine.h +#error REFERENCE undefined +#endif + +void analog_init(); +uint16_t analog_read(uint8_t channel); + +#endif /* _ANALOG_H */