-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
146 lines (118 loc) · 2.03 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "adc.h"
#include "lcd.h"
void delay_10us(void);
float pulse_width_read(void);
void mode_change(void);
char a = 'c';
char b = 'm';
float dist;
float adjust = 1.0;
void main(void)
{
DDRC |= (1<<0);
DDRC &= ~(1<<1);
DDRD |= (1<<7);
DDRD &= ~(1<<5);
DDRD &= ~(1<<3);
DDRB |= (1<<6);
PORTD |= (1<<7);
PORTB |= (1<<6);
while(1)
{
int state = 0;
while(state != 1)
{
if(bit_is_clear(PIND, 5))
{
while(bit_is_clear(PIND, 5))
{
}
state = 1;
}
}
ADC_init();
float temp = ADC_read()*(500.0/1023);
float speed = 331 + (temp*0.6);
LCDInit();
char line[16];
sprintf(line, "%0.1fC - %0.1fm/s", temp, speed);
LCDGotoXY(1, 2);
LCDString(line);
mode_change();
PORTC |= (1<<0);
delay_10us();
PORTC &= ~(1<<0);
float time_us = pulse_width_read();
dist = time_us*(speed/20000);
char line2[16];
sprintf(line2, "Length: %0.2f%c%c", dist*adjust, a, b);
LCDGotoXY(1, 1);
LCDString(line2);
}
}
void delay_10us(void)
{
TCCR0 |= (1<<CS00);
TCCR0 &= ~(1<<CS01);
TCCR0 &= ~(1<<CS02);
TCNT0 = 0;
while(TCNT0 < 80)
{
}
}
float pulse_width_read(void)
{
while(bit_is_clear(PINC, 1))
{
}
TCCR0 |= (1<<CS00);
TCCR0 &= ~(1<<CS01);
TCCR0 &= ~(1<<CS02);
TCNT0 = 0;
int count = 0;
while(bit_is_set(PINC, 1))
{
if(TCNT0 == 255)
{
while(TCNT0 == 255)
{
}
count++;
}
}
unsigned int t1 = TCNT0 + (count*256);
float t2 = t1/8.0;
return t2;
}
void mode_change(void)
{
sei();
MCUCR &= ~(1<<ISC10);
MCUCR &= ~(1<<ISC11);
GICR |= (1<<INT1);
}
ISR(INT1_vect)
{
while(bit_is_clear(PIND, 3))
{
}
if(adjust == 1.0)
{
adjust = 0.394;
a = 'i';
b = 'n';
}
else
{
adjust = 1.0;
a = 'c';
b = 'm';
}
char line3[16];
sprintf(line3, "Length: %0.2f%c%c", dist*adjust, a, b);
LCDGotoXY(1, 1);
LCDString(line3);
}