NXPCup_Cachan / Mbed 2 deprecated Ultrason_SRF05

Dependencies:   mbed

Committer:
theoduval
Date:
Wed Mar 04 14:50:47 2020 +0000
Revision:
0:b3c728b7daba
Programme pour ultrason SRF05

Who changed what in which revision?

UserRevisionLine numberNew contents of line
theoduval 0:b3c728b7daba 1 #include "mbed.h"
theoduval 0:b3c728b7daba 2 DigitalOut UltrasonTriger(p18);
theoduval 0:b3c728b7daba 3 DigitalIn UltrasonEcho(p17);
theoduval 0:b3c728b7daba 4 DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
theoduval 0:b3c728b7daba 5 Serial pc(USBTX, USBRX,460800);
theoduval 0:b3c728b7daba 6 Serial blueth(p9, p10,115200); // tx,rx,baudrate
theoduval 0:b3c728b7daba 7
theoduval 0:b3c728b7daba 8 void Imp_Ultra(void);
theoduval 0:b3c728b7daba 9 int Rec_Ultra();
theoduval 0:b3c728b7daba 10
theoduval 0:b3c728b7daba 11 int main() {
theoduval 0:b3c728b7daba 12
theoduval 0:b3c728b7daba 13
theoduval 0:b3c728b7daba 14 while(1)
theoduval 0:b3c728b7daba 15 {
theoduval 0:b3c728b7daba 16 Imp_Ultra();
theoduval 0:b3c728b7daba 17 }
theoduval 0:b3c728b7daba 18 }
theoduval 0:b3c728b7daba 19
theoduval 0:b3c728b7daba 20 void Imp_Ultra(void)
theoduval 0:b3c728b7daba 21 {
theoduval 0:b3c728b7daba 22 static Timer timer;
theoduval 0:b3c728b7daba 23 float ultrason_distance;
theoduval 0:b3c728b7daba 24 //blueth.printf("fonct\n");
theoduval 0:b3c728b7daba 25
theoduval 0:b3c728b7daba 26
theoduval 0:b3c728b7daba 27 static short etat = 0;
theoduval 0:b3c728b7daba 28
theoduval 0:b3c728b7daba 29 switch(etat)
theoduval 0:b3c728b7daba 30 {
theoduval 0:b3c728b7daba 31 case 0:
theoduval 0:b3c728b7daba 32 led1 = 1;
theoduval 0:b3c728b7daba 33 UltrasonTriger = 1; // envoie une impulsion de 10us
theoduval 0:b3c728b7daba 34 timer.start();
theoduval 0:b3c728b7daba 35 etat = 1;
theoduval 0:b3c728b7daba 36 break;
theoduval 0:b3c728b7daba 37 case 1:
theoduval 0:b3c728b7daba 38
theoduval 0:b3c728b7daba 39 if(timer.read_us() > 10) // fin de l'impulsion
theoduval 0:b3c728b7daba 40 {
theoduval 0:b3c728b7daba 41 led1 = 0;
theoduval 0:b3c728b7daba 42 UltrasonTriger = 0;
theoduval 0:b3c728b7daba 43 timer.reset();
theoduval 0:b3c728b7daba 44 etat = 2;
theoduval 0:b3c728b7daba 45 }
theoduval 0:b3c728b7daba 46 break;
theoduval 0:b3c728b7daba 47 case 2:
theoduval 0:b3c728b7daba 48
theoduval 0:b3c728b7daba 49 if(UltrasonEcho.read()==1) // Si on a une reception
theoduval 0:b3c728b7daba 50 {
theoduval 0:b3c728b7daba 51 timer.start(); // lance timer pour mersurer l'impulsion
theoduval 0:b3c728b7daba 52 etat = 3;
theoduval 0:b3c728b7daba 53 }
theoduval 0:b3c728b7daba 54 if(timer.read_ms()>30) etat = 4; // Si au bout de 30ms pas de reception alors aucune mesure
theoduval 0:b3c728b7daba 55 break;
theoduval 0:b3c728b7daba 56 case 3:
theoduval 0:b3c728b7daba 57
theoduval 0:b3c728b7daba 58 if(UltrasonEcho.read()==0) // Mesurer le temps d'impulsion de la reception
theoduval 0:b3c728b7daba 59 {
theoduval 0:b3c728b7daba 60 ultrason_distance=timer.read_us()/58-8; // Calcul de la distance
theoduval 0:b3c728b7daba 61 blueth.printf("La dustance est de %3.2f cm\n",ultrason_distance);
theoduval 0:b3c728b7daba 62 timer.stop();
theoduval 0:b3c728b7daba 63 timer.reset();
theoduval 0:b3c728b7daba 64 etat = 5;
theoduval 0:b3c728b7daba 65 }
theoduval 0:b3c728b7daba 66 if(timer.read_ms()>30) etat = 4;
theoduval 0:b3c728b7daba 67 break;
theoduval 0:b3c728b7daba 68 case 4:
theoduval 0:b3c728b7daba 69 blueth.printf("erreur"); // Si pas de recption
theoduval 0:b3c728b7daba 70 timer.stop();
theoduval 0:b3c728b7daba 71 timer.reset();
theoduval 0:b3c728b7daba 72 etat = 5;
theoduval 0:b3c728b7daba 73 break;
theoduval 0:b3c728b7daba 74 case 5:
theoduval 0:b3c728b7daba 75 break;
theoduval 0:b3c728b7daba 76
theoduval 0:b3c728b7daba 77
theoduval 0:b3c728b7daba 78 }
theoduval 0:b3c728b7daba 79 }