Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <string.h>
- #include <stdlib.h>
- #include <avr/sfr_defs.h>
- #define TRIGGER_PIN PB7 //PA0
- uint16_t Measurement;
- uint16_t Distance;
- uint16_t Measure(void);
- uint16_t Calculation(uint16_t);
- int main ()
- {
- DDRB |= (1 << TRIGGER_PIN); //Make trigger pin as output
- TIMSK4 = (1 << TOIE4); //Enable Timer1 overflow interrupts
- TCCR4B |= (1 << CS42); //prescaler = 256
- TCCR4B &= ~(1 << CS41);
- TCCR4B &= ~(1 << CS40);
- sei(); /* Enable global interrupt */
- while(1)
- {
- Measure();
- }
- }
- uint16_t Measure(void)
- {
- //Giving 10us trigger pulse on trig pin to HC-SR04, then disabling it without making an interrupt
- if ((TCNT4 == 31250) && (~(PINB & (1 << PB7))))
- {
- PORTB |= (1 << TRIGGER_PIN); //1 on trig pin of HC-SR04
- TCNT4 = 0; //Reseting Timer4 to 0, going back to start
- }
- // _delay_ms(1000);
- if ((TCNT4 == 31250) && (PINB & (1 << PB7)))
- {
- PORTB &= (~(1 << TRIGGER_PIN)); //0 on trig pin of HC-SR04
- TCNT4 = 0; //Reseting Timer4 to 0, going back to start
- }
- // _delay_ms(1000);
- return Measurement;
- }
- uint16_t Calculation(uint16_t Measurement)
- {
- Distance = Measurement/932.8;
- return Distance;
- }
- /*We use Timer 4 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement