/
main.c
120 lines (103 loc) · 2.64 KB
/
main.c
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
/* Copyright (C) 2013, 2016 Enrico Rossi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "usart.h"
#include "rtc.h"
#include "sonar.h"
void sonar_print(void)
{
uint8_t i;
for (i=0; i<10; i++) {
usart->tx0_buffer = utoa(sonar[i], usart->tx0_buffer, 10);
usart_printstr(0, NULL);
usart_printstr(0, " ");
}
usart_printstr(0, "\n");
}
int main(void)
{
uint16_t counter;
char c;
uint8_t stop = TRUE;
usart_init();
sonar_init();
rtc_setup();
counter = 0;
usart_resume(0);
strcpy_P(usart->tx0_buffer, PSTR("\nTsunami Simulator "));
strcat_P(usart->tx0_buffer, PSTR(GITREL));
strcat_P(usart->tx0_buffer, PSTR("\n\nConnected!\n"));
usart_printstr(0, NULL);
rtc_start();
while (1) {
/* Restart the counter */
rtc_clear();
c = usart_getchar(0, FALSE);
switch (c) {
case '0':
stop = TRUE;
break;
case '1':
stop = FALSE;
break;
default:
break;
}
if (stop) {
/* stop the code */
while(usart_getchar(0, FALSE) != '1');
stop = FALSE;
rtc_clear();
}
/* send the trigger */
sonar_trigger();
/* clear all the data */
sonar_clear();
/*
* Wait 40mS maximum and collect all the
* data during the period.
*/
while (rtc_us < 4000)
sonar_set();
/*
* speed = ((i * SCALEuS)/1000000) * 340 / 2
* where:
* i * SCALEuS is the duration in uS of the signal.
* (i * SCALEuS)/1000000 is the same in seconds.
* 340 is the speed of the sound and
* /2 we need only half of the way.
* The simplyfied formula in cm.
* 340 mm/msec = 34cm/msec = 0.029 msec/cm = 29 uS/cm
* dist (cm) = T (uS) / 29 /2.
*/
usart->tx0_buffer = utoa(counter, usart->tx0_buffer, 10);
usart_printstr(0, NULL);
usart_printstr(0, " ");
counter++;
sonar_print();
/* if the counter has already reach 50mS,
* then this cycle takes too long.
*/
if (rtc_us > 5000)
usart_printstr(0, "Warning! Time overrun.\n");
/* Wait up to 50mS before restart */
while (rtc_us < 5000);
}
return(0);
}