[ADD] interrupt isr
This commit is contained in:
@@ -4,16 +4,16 @@
|
|||||||
* Created: 17-3-2021 09:47:12
|
* Created: 17-3-2021 09:47:12
|
||||||
* Author : Sem
|
* Author : Sem
|
||||||
|
|
||||||
ultrasoon op timer 1 want 16 bits
|
|
||||||
timer aanzetten met tccrn1
|
|
||||||
interrupt op falling edge van echo
|
|
||||||
in interrupt timer op 0 zetten en tccrn1 op 0
|
|
||||||
interrupt op rising edge in echo,
|
interrupt op rising edge in echo,
|
||||||
dan timer aanzetten
|
dan timer1 aanzetten -> timer1 want 16 bits en willen nauwkeurig afstand kunnen meten, en afstand kan van 2 cm tot 4 m, dus willen zeker zijn dat het past
|
||||||
en interrupt zetten op falling edge in echo
|
en interrupt zetten op falling edge in echo
|
||||||
als falling edge interrupt geeft ->
|
als falling edge interrupt geeft ->
|
||||||
waarde uit timer uitlezen
|
waarde uit timer1 uitlezen
|
||||||
en formule gebruiken high level time * velocity (340M/S) / 2
|
en formule gebruiken high level time * velocity (340M/S) / 2
|
||||||
|
timer1 uitzetten
|
||||||
|
interrupt weer op rising edge van echo zetten
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
#define F_CPU 20e6
|
#define F_CPU 20e6
|
||||||
@@ -23,6 +23,10 @@
|
|||||||
|
|
||||||
#include "lcd_control.h"
|
#include "lcd_control.h"
|
||||||
|
|
||||||
|
enum interrupt_status {INTERRUPT_FALLING, INTERRUPT_RISING};
|
||||||
|
|
||||||
|
static enum interrupt_status int_stat = INTERRUPT_RISING;
|
||||||
|
|
||||||
void wait_us(unsigned int us)
|
void wait_us(unsigned int us)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < us; i++)
|
for(int i = 0; i < us; i++)
|
||||||
@@ -40,18 +44,42 @@ void wait_ms(unsigned int ms)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ultrasonic_send_pulse()
|
||||||
|
{
|
||||||
|
PORTG = 0x00; // 10 us low pulse
|
||||||
|
wait_us(10);
|
||||||
|
PORTG = 0x01;
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(INT0_vect)
|
||||||
|
{
|
||||||
|
// set interrupt pin 0 on PORTD to falling edge
|
||||||
|
if (int_stat == INTERRUPT_RISING)
|
||||||
|
{
|
||||||
|
|
||||||
|
int_stat = INTERRUPT_FALLING;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
int_stat = INTERRUPT_RISING;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
DDRG = 0x01; // port g pin 0 on output, 1 on input. 0 is trig, 1 is echo
|
DDRG = 0x01; // port g pin 0 on output, 1 on input. 0 is trig, 1 is echo
|
||||||
|
|
||||||
|
EICRA |= 0x03; // interrupt PORTD on pin 0, rising edge
|
||||||
|
|
||||||
|
EIMSK |= 0x01; // enable interrupt on pin 0 (INT0)
|
||||||
|
|
||||||
|
sei(); // turn on interrupt system
|
||||||
|
|
||||||
/* Replace with your application code */
|
/* Replace with your application code */
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
PORTG = 0x00; // 10 us low pulse
|
ultrasonic_send_pulse();
|
||||||
wait_us(10);
|
|
||||||
PORTG = 0x01;
|
|
||||||
|
|
||||||
wait_ms(100);
|
wait_ms(100);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user