Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. #define F_CPU 16000000
  2. #define USART_BAUDRATE 115200
  3. #define NUMBER_OF_DIGITS 16
  4.  
  5. #include <avr/io.h>
  6. #include <avr/interrupt.h>  
  7. #include <util/delay.h>    
  8. #include "avrduino.h"
  9.  
  10. //HC-SR04 sensor -> Arduino UNO
  11. #define TRIGGER_PIN     PIND3           // arduino PIN 3.  
  12. #define ECHO_PIN        PINB0           // arduino PIN 8. (ICP1)
  13. #define LED_13          PINB5           // arduino PIN 13 Led on board.
  14. #define echo_On         PINB & 0x01
  15.  
  16. volatile uint16_t TCNT1_copy;  
  17. volatile uint8_t echoOff;
  18.  
  19. uint8_t string [50];                        // usart buff
  20. uint16_t distance[10];
  21. uint8_t nDelay;                             //
  22. uint32_t average;
  23.  
  24. void uitoa(unsigned int value, char* string, int radix);
  25. void ping(void);
  26.  
  27. ISR(TIMER0_OVF_vect)
  28. {
  29.         // send request Echo to HCSR04
  30.         PORTD &= ~(1 << TRIGGER_PIN);       // Trigger Pin Low
  31.         _delay_us(1);
  32.         PORTD |= (1 << TRIGGER_PIN);        // Trigger Pin high
  33.         _delay_us(10);                      // min 10us ( datasheet)
  34.         PORTD &= ~(1 << TRIGGER_PIN);       // Trigger Pin low ( pulse) -> HCSR04 outputs(modulate) 8 cycles@40Khz ( 200us) (datasheet)
  35. }
  36. ISR(TIMER1_CAPT_vect)  
  37. {
  38.     if (echo_On)                            //Test echo input pin ( Arduino)
  39.       {
  40.         TIMSK0 &= ~(1 << TOIE0);
  41.         TCNT1 = 0;                          //clear counter
  42.         TCCR1B &= ~(1 << ICES1);            //invert edge select ( falling)
  43.       }
  44.     else
  45.       {
  46.         TCNT1_copy = ICR1;                  //save steps    
  47.         TCCR1B |=  (1 << ICES1);            //invert edge select ( rising)
  48.         TCNT0 = 0;
  49.         TIMSK0 |= (1 << TOIE0);             // Start tmr0 to make 64ms delay between echo (datasheet)
  50.       }
  51. }
  52. int main (void)
  53. {
  54.     DDRB = 0;                               // Clear
  55.     DDRD = 0;                               //
  56.     PORTB = 0;                              // 
  57.     TCCR1B = 0;                             //
  58.     TIMSK1 = 0;                             //
  59.     cli();                                  // Clear global interrupt
  60.  
  61.     DDRB |=  (1 << LED_13);                 // led_13 set as output.   
  62.     DDRB &= ~(1 << ECHO_PIN);               // ECHO_PIN = PINB0(ICP1) set as input.
  63.     DDRD |=  (1 << TRIGGER_PIN);            // Sonar trigger PIND3 set as output.
  64.  
  65.     TCCR0A = 0;                             // Normal mode (counter) port operation.
  66.     TCCR0B = 0;                             // Normal mode (counter) port operation.
  67.     TCCR0B |= (1 << CS01) | (1 << CS00);    // TMR0 prescaler 1/64 -> overflow 64ms@16Mhz
  68.    
  69.     TCCR1A = 0;                             // Normal mode (counter) port operation.
  70.     TCCR1B = 0;                             // Normal mode (counter) port operation.
  71.     TCCR1B |= (0 << ICNC1) | (1 << ICES1) \
  72.            | (1 << CS10) | (1 << CS11);     // Echo cancelling ( add 4cyc before to copy icr1 value to tcnt1), RISING EDGE,  Prescaler -> 16:64 tick = 4us
  73.     TCCR1C = 0;                             // FOC1A/FOC1B inactive
  74.     TIFR1 |= (1 << TOV1) | ( 1 << ICF1);    // clear any flag occurred
  75.    
  76.     USART_puts("Hello !");
  77.     USART_puts("\n");
  78.     sei();
  79.     TIMSK0 &= ~(1 << TOIE0);                // overflow interrupt tmr0 OFF 
  80.     TIMSK1 |= (1 << ICIE1) | (1 << TOV1);   // Input capture ON
  81.     TCNT1 = 0;                              // clear counter tmr1.
  82.    
  83.     ping();                                 // First ping
  84.  
  85.     for(;;)
  86.      {
  87.                 do  //  8 sample
  88.                 {
  89.                     average+= distance[nDelay = (nDelay + 1 == 9 ? 0: nDelay + 1)] = (TCNT1_copy << 2) /58;
  90.                 } while ( !(nDelay & (1 << 3)));
  91.                
  92.                 average >>=3;               // sample/8
  93.                 uitoa(average,string,10);
  94.                 USART_puts(string);         // print average value
  95.                 USART_puts("\n");
  96.     }
  97.     return 0;
  98. }
  99. void ping (void)
  100. {
  101.     // send request Echo to HCSR04
  102.     PORTD &= ~(1 << TRIGGER_PIN);           // Trigger Pin Low
  103.     _delay_us(1);
  104.     PORTD |= (1 << TRIGGER_PIN);            // Trigger Pin high
  105.     _delay_us(10);                          // min 10us
  106.     PORTD &= ~(1 << TRIGGER_PIN);           // Trigger Pin low ( pulse) -> HCSR04 outputs(modulate) 8 cycles@40Khz ( 200us)
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement