Advertisement
Guest User

Sonar

a guest
Feb 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. uint16_t sonar1Read(void)
  2. {
  3.     sonar1_ack = 0;
  4.     trig1_PORT &= ~(1 << trig1_PIN);
  5.     trig1_PORT |= (1 << trig1_PIN);
  6.     _delay_us(10);
  7.     trig1_PORT &= ~(1 << trig1_PIN);
  8.    
  9.     while (sonar1_ack == 0)
  10.     {
  11.         TCCR1B |= (1 << CS11);
  12.         if ((PIND & (1 << PIND3)))
  13.         {
  14.             TCNT1 = 0;
  15.             TCCR1B |= (1 << CS11);
  16.             while ((PIND & (1 << PIND3)))
  17.             {
  18.                 if (TCNT1 > 5000)
  19.                 {
  20.                     TCCR1B = 0;
  21.                     TCNT1 = 0;
  22.                     return 111;
  23.                 }
  24.             }
  25.             TCCR1B = 0;
  26.             pulse1 = TCNT1 / 2;
  27.             TCNT1 = 0;
  28.             sonar1_ack = 1;
  29. //          Serial_sendInt(pulse1, DEC);
  30. //          Serial_sendString("\t");
  31.         }
  32.         else if (TCNT1 > 10000)
  33.         {
  34.             TCCR1B = 0;
  35.             TCNT1 = 0;
  36.             return 112;
  37.         }
  38.     }
  39.     return (.035 * pulse1 / 2);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement