diff --git a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj index a3c4b3c..71c367d 100644 --- a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj +++ b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj @@ -166,6 +166,12 @@ compile + + compile + + + compile + compile @@ -178,6 +184,12 @@ compile + + compile + + + compile + \ No newline at end of file diff --git a/EmbeddedSystemsTHM/StopLight.c b/EmbeddedSystemsTHM/StopLight.c index 2f80645..5f8b79a 100644 --- a/EmbeddedSystemsTHM/StopLight.c +++ b/EmbeddedSystemsTHM/StopLight.c @@ -8,7 +8,7 @@ #include "Led.h"; #include "Taster.h"; #include "Timer.h" -#include ; +#include "StopLight.h" /* enums */ enum{ @@ -102,7 +102,8 @@ runStopLightTask1() break; case S1_RED_YELLOW: - setS2ToRedYellow(); + setS1ToRedYellow(); + if(Timer_getTick() - g_startMS >= delayShort) { g_startMS=Timer_getTick(); StoplightState = S1_GREEN; @@ -111,6 +112,7 @@ runStopLightTask1() case S1_GREEN: setS1ToGreen(); + if(Timer_getTick() - g_startMS >= delayLong) { g_startMS=Timer_getTick(); StoplightState = S1_YELLOW; @@ -182,8 +184,6 @@ runStopLightTask2(void) case S1_GREEN: setS1ToGreen(); - - // Wait 30 seconds before allowing the button to be pressed again if(Timer_getTick() - g_startMS >= delayLong) { g_startMS=Timer_getTick(); StoplightState = WAIT; diff --git a/EmbeddedSystemsTHM/StopLight.h b/EmbeddedSystemsTHM/StopLight.h index dbc4b6a..f0b0d22 100644 --- a/EmbeddedSystemsTHM/StopLight.h +++ b/EmbeddedSystemsTHM/StopLight.h @@ -5,6 +5,7 @@ * Author: n0x */ + #ifndef STOPLIGHT_H_ #define STOPLIGHT_H_ diff --git a/EmbeddedSystemsTHM/Tasks.c b/EmbeddedSystemsTHM/Tasks.c new file mode 100644 index 0000000..d917864 --- /dev/null +++ b/EmbeddedSystemsTHM/Tasks.c @@ -0,0 +1,73 @@ +/* + * Tasks.c + * + * Created: 17/11/2021 16:58:20 + * Author: n0x + */ + +#include "Led.h" +#include "Timer.h" +#include "Taster.h" +#include + +int g_counter = 0; +uint8_t g_ledStatus = 0; +uint16_t g_startMS; + +void +initTasks (void){ + g_startMS=Timer_getTick(); +} + +/* Task 1 (2021-10-21) */ +void +runningLight (void) +{ + _delay_ms(100); + + /* Programmieren Sie ein Lauflicht. Nutzen Sie dazu die in „Led.h“ deklarierten + * Funktionen. + * 2.Schreiben Sie ein Programm mit folgenden Funktionen: + * Wenn Taste 1 gedrückt wird, wird die Variable „Counter“ inkrementiert. + * Wird Taste 2 gedrückt wird, wird die Variable „Counter“ dekrementiert. + * Variable „Counter“ soll sich dabei zwischen 0 und 8 bewegen. + * Der Inhalt des Wertes soll mit Hilfe der LEDs angezeigt werden. + * 0 = keine LED an, 1 = LED1 an, 2 = LED1+LED2 an, usw. + * 8 = alle LEDs an. + */ + + /* Check if counter needs to be incremented */ + if (Taster1_get()) g_counter++; + + /* Check if counter needs to be decremented */ + if (Taster2_get()) g_counter--; + + /* Keep counter within boundaries (0-8) */ + g_counter = (g_counter + 9) % 9; + + /* Set the LEDs according to the counter */ + g_counter >= 1 ? Led1_On() : Led1_Off(); + g_counter >= 2 ? Led2_On() : Led2_Off(); + g_counter >= 3 ? Led3_On() : Led3_Off(); + g_counter >= 4 ? Led4_On() : Led4_Off(); + g_counter >= 5 ? Led5_On() : Led5_Off(); + g_counter >= 6 ? Led6_On() : Led6_Off(); + g_counter >= 7 ? Led7_On() : Led7_Off(); + g_counter >= 8 ? Led8_On() : Led8_Off(); +} + +/* Task 2 (2021-10-28) */ +void +blinkLedWithTimer (void) +{ + if(Timer_getTick() - g_startMS >= 1000) { /* Wait 1000ms before switching LED1 */ + g_startMS=Timer_getTick(); + + if(g_ledStatus % 2 == 0){ + Led1_On(); + } else { + Led1_Off(); + } + g_ledStatus++; + } +} \ No newline at end of file diff --git a/EmbeddedSystemsTHM/Tasks.h b/EmbeddedSystemsTHM/Tasks.h new file mode 100644 index 0000000..d147414 --- /dev/null +++ b/EmbeddedSystemsTHM/Tasks.h @@ -0,0 +1,16 @@ +/* + * Tasks.h + * + * Created: 17/11/2021 16:58:04 + * Author: n0x + */ + + +#ifndef TASKS_H_ +#define TASKS_H_ + +void initTasks(void); +void blinkLedWithTimer(void); +void runningLight(void); + +#endif /* TASKS_H_ */ \ No newline at end of file diff --git a/EmbeddedSystemsTHM/Timer.c b/EmbeddedSystemsTHM/Timer.c index ef1a655..42689e6 100644 --- a/EmbeddedSystemsTHM/Timer.c +++ b/EmbeddedSystemsTHM/Timer.c @@ -12,14 +12,14 @@ volatile uint16_t cntr = 0; void Timer_init (void){ - TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */ - TCCR0B |= (1<<1); /* Set the clock select bit to pre-scaler 2 - 1MHz / 8 (pre-scaler 2) ==> 125KHz */ + TCCR0A |= (1<<1); /* Set WGM01 to high to enable CTC mode */ + TCCR0B |= (11<<0); /* Set the clock select bit to pre-scaler 3 + 16MHz / 64 (pre-scaler 3) ==> 250KHz */ - TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */ + TIMSK0 |= (1<<1); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */ - OCR0A = 125; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms - 1MHz / 8 (pre-scaler 2) / 125 ==> 1KHz (1ms) */ + OCR0A = 250 ; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms + 16MHz / 64 (pre-scaler 3) / 250 ==> 1KHz (1ms) */ } diff --git a/EmbeddedSystemsTHM/UART.c b/EmbeddedSystemsTHM/UART.c new file mode 100644 index 0000000..b897129 --- /dev/null +++ b/EmbeddedSystemsTHM/UART.c @@ -0,0 +1,106 @@ +/* + * UART.c + * + * Created: 18/11/2021 17:13:25 + * Author: n0x + */ + +#include +#include +#include +#include "UART.h" + +volatile int enabled = 1; + +/* Schreiben Sie eine Funktion, welche die UART Schnittstelle für das Senden von + Daten mit + 9600Baud, + 8 Bit, + keine Parität und + 1 Stopbit + initialisiert. */ +void +uart_init(void) +{ + /* set BAUD rate to 9600 */ + UBRR0 = 103; + + /* Async UART */ + UCSR0C |= (0< #define F_CPU 1000000 #include /* function declarations */ void buttonTest (void); -void task1 (void); -void task2 (void); - - -/* global variables */ -volatile int g_counter = 0; -uint8_t g_ledStatus = 0; -uint16_t g_startMS; /* functions */ @@ -31,20 +25,28 @@ main (void) { sei(); - /* Initialize the LEDs and Buttons */ - Led_init(); - Taster_init(); - Timer_init(); + /* Initialize the LEDs, Buttons, etc. */ + //Led_init(); + //Taster_init(); + //Timer_init(); + //initTasks(); + + //uart_init(); + uart_init_isr(); + - g_startMS=Timer_getTick(); while (1) { //buttonTest(); - //task1(); - //task2(); + //runningLight(); + //blinkLedWithTimer(); //runStopLightTask1(); - runStopLightTask2(); + //runStopLightTask2(); + + //uart_send ("Hello World\n"); + uart_send_isr ("Hello World\n"); + } } @@ -57,56 +59,5 @@ buttonTest (void) Taster4_get() ? Led4_On() : Led4_Off(); } -/* Task 1 (2021-10-21) */ -void -task1 (void) -{ - _delay_ms(100); - - /* Programmieren Sie ein Lauflicht. Nutzen Sie dazu die in „Led.h“ deklarierten - * Funktionen. - * 2.Schreiben Sie ein Programm mit folgenden Funktionen: - * Wenn Taste 1 gedrückt wird, wird die Variable „Counter“ inkrementiert. - * Wird Taste 2 gedrückt wird, wird die Variable „Counter“ dekrementiert. - * Variable „Counter“ soll sich dabei zwischen 0 und 8 bewegen. - * Der Inhalt des Wertes soll mit Hilfe der LEDs angezeigt werden. - * 0 = keine LED an, 1 = LED1 an, 2 = LED1+LED2 an, usw. - * 8 = alle LEDs an. - */ - - /* Check if counter needs to be incremented */ - if (Taster1_get()) g_counter++; - /* Check if counter needs to be decremented */ - if (Taster2_get()) g_counter--; - - /* Keep counter within boundaries (0-8) */ - g_counter = (g_counter + 9) % 9; - - /* Set the LEDs according to the counter */ - g_counter >= 1 ? Led1_On() : Led1_Off(); - g_counter >= 2 ? Led2_On() : Led2_Off(); - g_counter >= 3 ? Led3_On() : Led3_Off(); - g_counter >= 4 ? Led4_On() : Led4_Off(); - g_counter >= 5 ? Led5_On() : Led5_Off(); - g_counter >= 6 ? Led6_On() : Led6_Off(); - g_counter >= 7 ? Led7_On() : Led7_Off(); - g_counter >= 8 ? Led8_On() : Led8_Off(); -} - -/* Task 2 (2021-10-28) */ -void -task2 (void) -{ - if(Timer_getTick() - g_startMS >= 1000) { /* Wait 1000ms before switching LED1 */ - g_startMS=Timer_getTick(); - - if(g_ledStatus % 2 == 0){ - Led1_On(); - } else { - Led1_Off(); - } - g_ledStatus++; - } -}