summaryrefslogtreecommitdiff
path: root/src/app/adctest/main.cc
blob: 7a87eded9f82b0cdc656562903f6b619997255e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright 2021 Daniel Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#include "arch.h"
#include "driver/adc.h"
#include "driver/gpio.h"
#include "driver/stdout.h"

void loop(void)
{
	uint16_t vcc = adc.getVCC_mV();
	uint16_t temp = adc.getTemp_mdegC();

	kout << "Voltage     " << vcc << " mv" << endl;
	kout << "Temperature " << temp << " m°C" <<  endl;

	for (uint8_t admux_sel = 0; admux_sel < 8; admux_sel++) {
		// measure with avcc reference
		ADMUX = _BV(REFS0) | admux_sel;

		// Enable ADC with /64 prescaler
		ADCSRA = _BV(ADEN) | _BV(ADPS2);

		// Start conversion
		ADCSRA |= _BV(ADSC);

		// wait until conversion is complete
		while (ADCSRA & _BV(ADSC)) ;

		uint8_t adcr_l = ADCL;
		uint8_t adcr_h = ADCH;
		uint16_t adcr = adcr_l + (adcr_h << 8);
		uint16_t vadc = (uint32_t)vcc * adcr / 1023L;

		kout << "ADC" << admux_sel << "        " << vadc << " mV vs AVCC" << endl;
	}

	// enable bandgap; wait for it to stabilise
	ADMUX = _BV(REFS1) |  _BV(REFS0);
	ADCSRA = _BV(ADEN) | _BV(ADPS2);
	arch.delay_ms(1);

	for (uint8_t admux_sel = 0; admux_sel < 8; admux_sel++) {
		// measure with bandgap reference
		ADMUX = _BV(REFS1) | _BV(REFS0) | admux_sel;

		// Enable ADC with /64 prescaler
		ADCSRA = _BV(ADEN) | _BV(ADPS2);

		// Start conversion
		ADCSRA |= _BV(ADSC);

		// wait until conversion is complete
		while (ADCSRA & _BV(ADSC)) ;

		uint8_t adcr_l = ADCL;
		uint8_t adcr_h = ADCH;
		uint16_t adcr = adcr_l + (adcr_h << 8);
		uint16_t vadc = 1100L * adcr / 1023L;

		kout << "ADC" << admux_sel << "        " << vadc << " mV vs 1.1V bandgap" << endl;
	}

	// disable ADC
	ADCSRA &= ~_BV(ADEN);
}

int main(void)
{
	arch.setup();
	gpio.setup();
	kout.setup();

	arch.idle_loop();

	return 0;
}