Commit 8da267d4cb07581c46392cecfbf1733dcfce5723
1 parent
6637616f
code distance effectif
Showing
1 changed file
with
82 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,82 @@ |
1 | +#include <avr/io.h> | |
2 | +#include <avr/interrupt.h> | |
3 | +#include <util/delay.h> | |
4 | +#include <string.h> | |
5 | +#include <stdlib.h> | |
6 | +#include <stdio.h> | |
7 | + | |
8 | +//#ifndef RETOUR_H_ | |
9 | +//#define RETOUR_H_ | |
10 | + | |
11 | +#include "libretour.h" | |
12 | + | |
13 | +//#endif | |
14 | +#define PRESCALER 1024 | |
15 | +#define TIME_SLOT 20 | |
16 | +#define BAUDRATE 103 | |
17 | + | |
18 | +static volatile int pulse = 0; | |
19 | +static volatile int i = 0; | |
20 | + | |
21 | + | |
22 | +////////////////////////////////////////////// | |
23 | + | |
24 | +ISR(INT0_vect){ | |
25 | + //i=1 indique l'écho HIGH | |
26 | + if(i == 1) | |
27 | + { | |
28 | + //On arrête le timer | |
29 | + TCCR1B = 0; | |
30 | + //On stocke la valeur précédente du timer (l'aller-retour) | |
31 | + pulse = TCNT1; | |
32 | + //On reset la valeur de TCNT1 et de i à Zero | |
33 | + TCNT1 = 0; | |
34 | + i = 0; | |
35 | + } | |
36 | + | |
37 | + //i=0 indique l'écho à LOW | |
38 | + if(i==0) | |
39 | + { | |
40 | + //On set le bit CS10 à HIGH, debut du timer qui compte | |
41 | + TCCR1B |= (1<<CS10); | |
42 | + i = 1; | |
43 | + } | |
44 | +} | |
45 | + | |
46 | + | |
47 | +int main(void){ | |
48 | + init_serial(); | |
49 | + int16_t count_a = 0; | |
50 | + char show_a[16]; | |
51 | + //Trigger = PIND0 et ECHO = PIND2 | |
52 | + DDRD = 0b11111011; | |
53 | + _delay_ms(50); | |
54 | + //Selection INT0 | |
55 | + EIMSK |= (1<<INT0); | |
56 | + //Interruption sur changement logique | |
57 | + EICRA = 0x01; | |
58 | + sei(); | |
59 | + | |
60 | + while(1) | |
61 | + { | |
62 | + //TRIGGER HIGH | |
63 | + PORTD |= 1<<PIND0; | |
64 | + //Durée de l'impulsion TRIGGER | |
65 | + _delay_us(15); | |
66 | + //TRIGGER LOW | |
67 | + PORTD &= ~(1<<PIND0); | |
68 | + //On calcule la distance en cm : pulse/(58/2/10) (en cm) | |
69 | + count_a = pulse/1160.0; | |
70 | + | |
71 | + itoa(count_a,show_a,10); | |
72 | + //dtostrf(count_a, 5,2, show_a); | |
73 | + sprintf(show_a, "%d", count_a); | |
74 | + //send_serial('D'); | |
75 | + //send_serial('='); | |
76 | + //RETOUR | |
77 | + retour(show_a); | |
78 | + _delay_ms(1000); | |
79 | + | |
80 | + } | |
81 | + return 0; | |
82 | +} | ... | ... |