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 dla echo, PB7 wbudowany led na arduino
- uint16_t Measurement;
- uint16_t Distance;
- uint16_t Measure(void);
- uint16_t Calculation(uint16_t);
- int main () //Temporary main for testing. In official version this file will be included in main.c
- {
- DDRB |= (1 << TRIGGER_PIN); //Make trigger pin as output
- TIMSK4 = (1 << TOIE4); //Enable Timer1 overflow interrupts
- TCCR4B |= (1 << CS40); //Prescaler = 1
- sei(); //Enable global interrupt
- while(1) //Temporary while(1) in main for testing. In official version this file will be included in main.c
- {
- Measure(); //Calling a Measure function
- }
- }
- uint16_t Measure(void)
- {
- //Giving 10us trigger pulse on trig pin to HC-SR04, then disabling it without making an interrupt
- /*
- * TCNT4 - stores ticks that timer gets by clock ticking (divided by prescaler)
- * PINx, x - A, B, C... - read a state of output (high/low)
- *
- * How we calculate a timer:
- * - prescaler divided by clock (e.g. 1/16000000)
- * - A) our sensor needs a 10us high output on trig pin,
- * - B) then it's making x8 40kHz pulse and we're waiting for echo back - its from 150 us to 25 ms, 38ms if no obstacle
- *
- * A) To make 10us high output, we aren't using prescaler here, so 1/16000000 = 62.5 ns per tick -> 10us/62.5ns = 0.00001s/0.0000000625s = 160 ticks
- * We need to count 160 ticks to get 10us. To avoid using an interruptions, we're not making a overflow (its 16bit timer - we can store 65536 ticks) nor using a CTC mode in timer.
- * We're checking it by two if statements. Second if is inverse of first if.
- * Below we're doing a part A).
- */
- if ((TCNT4 == 160) && (~(PINB & (1 << PB7))))
- {
- PORTB ^= (1 << TRIGGER_PIN); //1 on trig pin of HC-SR04 - TRIGGER ENABLED
- TCNT4 = 0; //Reseting Timer4 to 0
- }
- //Timer is counting from 0 do 160 again
- if ((TCNT4 == 160) && (PINB & (1 << PB7)))
- {
- PORTB &= (~(1 << TRIGGER_PIN)); //0 on trig pin of HC-SR04 - TRIGGER DISABLED
- TCNT4 = 0; //Reseting Timer4 to 0
- }
- /*
- * part B) - we're using here a CTC mode. We will use a prescaler 64.
- * 64/16000000 = 0.000004 s = 4*10^-6 = 4 us
- * We need to count minimum 150 us, so:
- * 150us/0.000004s = 0.00015/0.000004 = 37.5
- * We have a problem here, because its not an integer. If we use 38 ticks as 150us, in fact we will have 152 us (38*0.000004=152). We have a rounding error.
- * To fix this, we must subtract a ticks from final result.
- * We can calculate, how much we need to subtract:
- * (38*0.000004) - (37.5*0.000004) = (152-150) = 2
- *
- * What if we have more than 150 us? Take 10ms = 0.01s as another example.
- *
- * HERE JAK TO PRZELICZYC DALEJ I NAPISA OGOLNY WZOR
- *
- *
- * Final formula is:
- *
- *
- */
- return Measurement;
- }
- uint16_t Calculation(uint16_t Measurement)
- {
- Distance = Measurement/932.8;
- return Distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement