summaryrefslogtreecommitdiff
path: root/src/app/wetterstation/main.cc
blob: fb62ae92148cdf9fd229018fc798c7f828fc5466 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright 2020 Daniel Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */
#include "arch.h"
#include "driver/adc.h"
#include "driver/gpio.h"
#include "driver/stdout.h"

#include <avr/interrupt.h>

volatile uint16_t anemometer_count = 0;

void loop(void)
{
	static uint8_t wind_count = 0;
	static uint8_t dew_count = 0;
	static uint16_t anemometer_copy;

	wind_count++;
	dew_count++;

	if (wind_count == 10) {

		cli();
		anemometer_copy = anemometer_count;
		anemometer_count = 0;
		sei();

		kout << "Anemometer Count = " << anemometer_copy << endl;
		wind_count = 0;
	}

	if (dew_count == 59) {
		gpio.output(GPIO::pc5, 0);
		gpio.output(GPIO::pc4, 1);
	}
	else if (dew_count == 60) {
		// Measure ADC6 with AVCC reference
		ADMUX = _BV(REFS0) | 6;

		// 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 dew_raw = adcr_l + (adcr_h << 8);
		dew_raw = 1024 - dew_raw;

		// Disable ADC
		ADCSRA &= ~_BV(ADEN);

		kout << "Dew Raw = " << dew_raw << endl;

		dew_count = 0;
		gpio.input(GPIO::pc5, 0);
		gpio.input(GPIO::pc4, 0);

		kout << "Voltage = " << adc.getVCC_mV() << endl;
		kout << "Temperature = " << adc.getTemp_mdegC() << endl;
	}
}

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

	gpio.output(GPIO::pd2, 0); // Anemometer Reed Contact
	gpio.input(GPIO::pd3, 1); // Anemometer Reed Contact

	gpio.input(GPIO::pc5, 0); // Dew Sensor GND
	gpio.input(GPIO::pc4, 0); // Dew Sensor VCC

	EICRA = _BV(ISC11);
	EIMSK = _BV(INT1);

	arch.idle_loop();

	return 0;
}

ISR(INT1_vect) {
	anemometer_count++;
}