forked from mpgiii/MSP432-Weather-Station
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
127 lines (105 loc) · 3.31 KB
/
main.c
File metadata and controls
127 lines (105 loc) · 3.31 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "msp.h"
#include "adc.h"
#include "dht.h"
#include "lcd.h"
#include "delay.h"
#include "raindrops.h"
#include "keypad.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#define DEGREE_CHARACTER 223
#define LOOP_DELAY 1000
/**
* main.c
*/
void main(void) {
char buffer[100];
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
/* Variables to hold wind speed, and keypad characters */
char key;
float wind_mph;
char current = 0xFF;
int32_t raw_windspd = -1;
set_DCO(FREQ_12_MHZ);
/* Initialize all sensors/modules used */
ADC_init();
DHT_init();
LCD_init();
RD_init();
keypad_init();
LCD_turn_off_cursor();
/* loop delays 1 ms each loops */
while(1) {
key = keypad_get_key();
/* if we have pressed a new key, clear the display. */
if (current != key && key != 0xFF) {
current = key;
LCD_clear();
}
/* when 1 or 2 is pressed, switch to DHT data on screen*/
if (current == '1' || current == '2') {
DHT_read_data();
if (DHT_check_checksum()) {
LCD_go_to_line(1);
/* if "1" read, display Humidity data */
if (current == '1') {
sprintf(buffer, "Humidity = %2d%%", dht_data[0]);
}
/* if "2" read, display Temperatue data */
else if (current == '2') {
sprintf(buffer, "Temp = %3d %cC", dht_data[2],
DEGREE_CHARACTER);
}
LCD_write_string(buffer);
}
}
/* when 3 pressed, display rain drop (RD) sensor data */
else if (current == '3') {
LCD_go_to_line(1);
LCD_write_string("Rain detected?");
LCD_go_to_line(2);
/* Read RD digital value to detect presence of moisture */
if(RD_read_digital()) {
LCD_write_string("Yes! :o");
}
else {
LCD_write_string("Nope :)");
}
}
/* when 4 pressed, display wind sensor data converted by the ADC */
else if (current == '4'){
LCD_go_to_line(1);
LCD_write_string("Wind Speed ");
LCD_go_to_line(2);
if (raw_windspd == -1){
sprintf(buffer, "Data not ready..");
} else {
sprintf(buffer, "%4.3f MPH ", wind_mph);
}
LCD_write_string(buffer);
if(ADC_get_flag()){
/* reset the flag for next read */
ADC_set_flag(0);
/* get the valid 10-bit raw value from the ADC */
raw_windspd = ADC_get_digital() & 0x3FF;
/* wind speed conversion to MPH */
wind_mph = powf(((raw_windspd - TEMP_COMP) / WIND_DIVISOR),
WIND_POWER);
/* Scale wind speed */
wind_mph *= SCALE_FACTOR;
/* Start ADC conversion again */
ADC14 -> CTL0 |= ADC14_CTL0_SC;
}
}
/* when any other characters are input, wait for next key pressed */
else {
LCD_go_to_line(1);
LCD_write_string("Waiting for");
LCD_go_to_line(2);
LCD_write_string("input...");
}
/* Delay on each loop to slow down measurement times */
delay_us(LOOP_DELAY);
}
}