Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
59 pfowler 1
#include <avr/io.h>
2
 
3
#include "util.h"
4
#include <util/delay.h>
5
 
6
#include <stdlib.h>
7
 
8
inline uint8_t map_8(uint8_t x, uint8_t in_min, uint8_t in_max, uint8_t out_min, uint8_t out_max) {
9
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
10
}
11
 
12
uint8_t analogRead(uint8_t channel) {
13
        ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (channel & 0x0f);
14
        ADCSRA |= (1<<ADSC);            // Start converting
15
 
16
        while (((ADCSRA >> ADSC) & 1)) {}       //Wait until conversion finished
17
        uint8_t result = ADCH;
18
        //ADCSRA |= (0<<ADSC);          // Stop converting
19
 
20
        return result;
21
}
22
 
23
void analogInit() {
24
	ACSR |= (1<<ACD); // Disable analog comparator
25
 
26
	/*
27
        Setup ADC
28
        ADMUX: 8 bit mode, Avcc ref
29
        ADCSRA: Enable, 128 prescale
30
	*/
31
	ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
32
	ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
33
}