Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************
- Utilisation d'un télémètre HC-SR04 avec une carte STM32 Nucleo
- ********************************************************************/
- #include "mbed.h"
- Serial pc(USBTX,USBRX); // pour envoi de données par communciation série 9600 bauds
- DigitalOut trig(D8); // pin trigger du HC-SR04 branchée à D8
- DigitalIn echo(D9); // pin echo du HC-SR04 branché à D9
- Timer t; //chronomètre
- #define vitesse_son 343.595*100; // vitesse du son en cm/s
- int main() {
- double time, distance;
- while(1) {
- t.reset(); //chronomètre à zéro
- trig = 1; // émission de l'ultrasons
- time = 0;
- wait_us(10);
- trig = 0;
- if (echo == 1) { //réception d'un écho
- t.start(); // démarrage du chronomètre
- while(1){
- if(echo == 0) {
- t.stop(); // arrêt du chronomètre
- time = t.read(); // lecture du chronomètre
- distance = time / 2.0 * vitesse_son; // conversion en cm
- pc.printf("%1.1f cm\n\r",distance); // envoie sur résultat par communication série
- t.reset(); // chronomètre à zéro
- wait(1); // une mesure par seconde
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement