From 956e25bc6ac1394fe6319a491c699fbd0db8daf6 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 31 Mar 2021 09:47:58 +0200 Subject: [PATCH] [ADD] files to include --- Microcontrollers/Eindopdracht/lcd_control.c | 132 ++++++++++++++++++ Microcontrollers/Eindopdracht/lcd_control.h | 30 ++++ .../Eindopdracht/ultrasonic_sensor.c | 91 ++++++++++++ .../Eindopdracht/ultrasonic_sensor.h | 12 ++ 4 files changed, 265 insertions(+) create mode 100644 Microcontrollers/Eindopdracht/lcd_control.c create mode 100644 Microcontrollers/Eindopdracht/lcd_control.h create mode 100644 Microcontrollers/Eindopdracht/ultrasonic_sensor.c create mode 100644 Microcontrollers/Eindopdracht/ultrasonic_sensor.h diff --git a/Microcontrollers/Eindopdracht/lcd_control.c b/Microcontrollers/Eindopdracht/lcd_control.c new file mode 100644 index 0000000..64bacb3 --- /dev/null +++ b/Microcontrollers/Eindopdracht/lcd_control.c @@ -0,0 +1,132 @@ +/* + * lcd_controlc.c + * + * Created: 24-2-2021 11:55:12 + * Author: Sem + */ +#define F_CPU 10e6 +#include +#include +#include +#include +#include +#include "lcd_control.h" + +void _delay_ms(double __ms); + +void lcd_clear() { + lcd_write_command (0x01); //Leeg display + _delay_ms(2); + lcd_write_command (0x80); //Cursor terug naar start +} + +void lcd_strobe_lcd_e(void) { + + sbi_porta(LCD_E); // E high + _delay_ms(1); + cbi_porta(LCD_E); // E low + _delay_ms(1); + +} + +void sbi_portc(int index){ + PORTC |= (1< +#include +#include + +#include "lcd_control.h" + +static uint16_t timer_dist = 0; + +enum interrupt_status {INTERRUPT_FALLING, INTERRUPT_RISING}; + +static enum interrupt_status int_stat = INTERRUPT_RISING; + +void wait_us(unsigned int us) +{ + for(int i = 0; i < us; i++) + { + _delay_us(1); + } +} + +void wait_ms(unsigned int ms) +{ + + for(int i = 0; i < ms; i++) + { + _delay_ms(1); + } +} +void ultrasonic_init() +{ + DDRG = 0xFF; // port g all output. pin 0 is trig, the rest is for debug + DDRD = 0x00; // port D pin 0 on input. 0 is echo and also interrupt + + EICRA = 0x03; // interrupt PORTD on pin 0, rising edge + + EIMSK |= 0x01; // enable interrupt on pin 0 (INT0) + + TCCR1A = 0b00000000; // initialize timer1, prescaler=256 + TCCR1B = 0b00001100; // CTC compare A, RUN + + sei(); // turn on interrupt system +} + +void ultrasonic_send_pulse() +{ + PORTG = 0x00; // 10 us low pulse + wait_us(10); + PORTG = 0x01; +} + +void ultrasonic_handle_interrupt() +{ + // if the interrupt was generated on a rising edge (start sending echo) + if (int_stat == INTERRUPT_RISING) + { + // set interrupt pin 0 on PORTD to falling edge + EICRA = 0x02; + + // reset the time in timer1 + TCNT1 = 0x00; + + // set interrupt status + int_stat = INTERRUPT_FALLING; + } else + // else if it was generated on a falling edge (end sending echo) + { + // set interrupt pin 0 on PORTD to rising edge + EICRA = 0x03; + + // read timer1 into time_dist + timer_dist = TCNT1; + + // set interrupt status + int_stat = INTERRUPT_RISING; + } +} + +uint16_t ultrasonic_get_timer_dist() +{ + return timer_dist; +} + diff --git a/Microcontrollers/Eindopdracht/ultrasonic_sensor.h b/Microcontrollers/Eindopdracht/ultrasonic_sensor.h new file mode 100644 index 0000000..0981c46 --- /dev/null +++ b/Microcontrollers/Eindopdracht/ultrasonic_sensor.h @@ -0,0 +1,12 @@ +/* + * ultrasonic_sensor.h + * + * Created: 24-3-2021 13:14:50 + * Author: Sem + */ + + +void ultrasonic_init(); +void ultrasonic_send_pulse(); +uint16_t ultrasonic_get_timer_dist(); +void ultrasonic_handle_interrupt();