200 lines
4.0 KiB
C
200 lines
4.0 KiB
C
/*
|
|
* EmbeddedSystemProject.c
|
|
*
|
|
* Created: 21/10/2021 16:55:21
|
|
* Author : n0x
|
|
*/
|
|
|
|
#include "Led.h"
|
|
#include "Taster.h"
|
|
#include "Timer.h"
|
|
#include <avr/interrupt.h>
|
|
#define F_CPU 1000000
|
|
#include <util/delay.h>
|
|
|
|
/* function declarations */
|
|
void buttonTest (void);
|
|
void task1 (void);
|
|
void task2 (void);
|
|
void task3 (void);
|
|
|
|
/* enums */
|
|
enum{
|
|
S1_GREEN, S1_YELLOW, S1_RED, S1_RED_YELLOW,
|
|
S2_GREEN, S2_YELLOW, S2_RED, S2_RED_YELLOW,
|
|
INIT,
|
|
} StoplightState = INIT; /* Emus for the stoplight task (task 3) */
|
|
|
|
/* global variables */
|
|
volatile int g_counter = 0;
|
|
int g_ledStatus = 0;
|
|
uint16_t g_startMS;
|
|
|
|
/* functions */
|
|
int
|
|
main (void)
|
|
{
|
|
sei();
|
|
|
|
/* Initialize the LEDs and Buttons */
|
|
Led_init();
|
|
Taster_init();
|
|
Timer_init();
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
while (1)
|
|
{
|
|
//buttonTest();
|
|
//task1();
|
|
//task2();
|
|
task3();
|
|
}
|
|
}
|
|
|
|
void
|
|
buttonTest (void)
|
|
{
|
|
Taster1_get() ? Led1_On() : Led1_Off();
|
|
Taster2_get() ? Led2_On() : Led2_Off();
|
|
Taster3_get() ? Led3_On() : Led3_Off();
|
|
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++;
|
|
}
|
|
}
|
|
|
|
|
|
/* Task 3 */
|
|
void
|
|
task3 (void)
|
|
{
|
|
/* Programmieren eine Ampelsteuerung auf einer Straßenkreuzung */
|
|
|
|
/*
|
|
int delayShort = 5000;
|
|
int delayLong = 30000;
|
|
//*/
|
|
int delayShort = 2500;
|
|
int delayLong = 7500;
|
|
|
|
/* Stoplight and the corresponding Leds
|
|
| GREEN | YELLOW | RED
|
|
----------------------------------
|
|
SL 1 | Led1 | Led4 | Led6
|
|
SL 2 | Led2 | Led5 | Led7
|
|
*/
|
|
switch(StoplightState){
|
|
case INIT:
|
|
Led1_On(); // Set stoplight 1 to green
|
|
Led2_Off();
|
|
Led3_Off();
|
|
Led4_Off();
|
|
Led5_Off();
|
|
Led6_Off();
|
|
Led7_On(); // Set stoplight 2 to red
|
|
Led8_Off();
|
|
StoplightState = S1_YELLOW;
|
|
|
|
case S1_YELLOW: // Set stoplight 2 to yellow
|
|
_delay_ms(delayLong);
|
|
Led1_Off();
|
|
Led4_On();
|
|
StoplightState = S1_RED;
|
|
|
|
case S1_RED: // Set stoplight 2 to red
|
|
_delay_ms(delayShort);
|
|
Led4_Off();
|
|
Led6_On();
|
|
StoplightState = S2_RED_YELLOW;
|
|
|
|
case S2_RED_YELLOW:
|
|
_delay_ms(delayShort);
|
|
Led5_On();
|
|
Led7_On();
|
|
StoplightState = S2_GREEN;
|
|
|
|
case S2_GREEN:
|
|
_delay_ms(delayShort);
|
|
Led5_Off();
|
|
Led7_Off();
|
|
Led2_On();
|
|
StoplightState = S2_YELLOW;
|
|
|
|
case S2_YELLOW:
|
|
_delay_ms(delayLong);
|
|
Led2_Off();
|
|
Led5_On();
|
|
StoplightState = S2_RED;
|
|
|
|
case S2_RED:
|
|
_delay_ms(delayShort);
|
|
Led5_Off();
|
|
Led7_On();
|
|
StoplightState = S1_RED_YELLOW;
|
|
|
|
case S1_RED_YELLOW:
|
|
_delay_ms(delayShort);
|
|
Led4_On();
|
|
Led6_On();
|
|
StoplightState = S1_GREEN;
|
|
|
|
case S1_GREEN:
|
|
_delay_ms(delayShort);
|
|
Led4_Off();
|
|
Led6_Off();
|
|
Led1_On();
|
|
StoplightState = S1_YELLOW;
|
|
|
|
}
|
|
} |