SHARE
TWEET

Untitled

a guest Jul 17th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define F_CPU 16000000UL
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5. #include <util/delay.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <avr/sfr_defs.h>
  9.  
  10. #define TRIGGER_PIN PB7 //PA0
  11. uint16_t Measurement;
  12. uint16_t Distance;
  13.  
  14. uint16_t Measure(void);
  15. uint16_t Calculation(uint16_t);
  16.  
  17. int main ()
  18. {
  19.     DDRB |= (1 << TRIGGER_PIN); //Make trigger pin as output
  20.  
  21.     TIMSK4 = (1 << TOIE4);  //Enable Timer1 overflow interrupts
  22.     TCCR4B |= (1 << CS42); //prescaler = 256
  23.     TCCR4B &= ~(1 << CS41);
  24.     TCCR4B &= ~(1 << CS40);
  25.  
  26.     sei();          /* Enable global interrupt */
  27.  
  28.     while(1)
  29.     {
  30.         Measure();
  31.     }
  32. }
  33.  
  34. uint16_t Measure(void)
  35. {
  36.     //Giving 10us trigger pulse on trig pin to HC-SR04, then disabling it without making an interrupt
  37.  
  38.     if ((TCNT4 == 31250) && (~(PINB & (1 << PB7))))
  39.     {
  40.         PORTB |= (1 << TRIGGER_PIN); //1 on trig pin of HC-SR04
  41.         TCNT4 = 0; //Reseting Timer4 to 0, going back to start
  42.     }
  43.  
  44.     //  _delay_ms(1000);
  45.  
  46.     if ((TCNT4 == 31250) && (PINB & (1 << PB7)))
  47.     {
  48.         PORTB &= (~(1 << TRIGGER_PIN)); //0 on trig pin of HC-SR04
  49.         TCNT4 = 0; //Reseting Timer4 to 0, going back to start
  50.     }
  51.  
  52.     //  _delay_ms(1000);
  53.  
  54.     return Measurement;
  55. }
  56.  
  57. uint16_t Calculation(uint16_t Measurement)
  58. {
  59.     Distance = Measurement/932.8;
  60.     return Distance;
  61. }
  62.  
  63. /*We use Timer 4 */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top