From 2bd2215ee99ac6fb7d2e9a170bd26359ae020fa3 Mon Sep 17 00:00:00 2001 From: _N0x Date: Thu, 28 Oct 2021 19:24:52 +0200 Subject: [PATCH] Added Timer.h and Timer.c. Completed Task to use timers to blink LED1 with a 1Hz frequency --- EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj | 24 ++++++---- EmbeddedSystemsTHM/Led.c | 52 ++++++++++++++------- EmbeddedSystemsTHM/Led.h | 34 +++++++------- EmbeddedSystemsTHM/Taster.c | 15 ++++-- EmbeddedSystemsTHM/Taster.h | 10 ++-- EmbeddedSystemsTHM/Timer.c | 40 ++++++++++++++++ EmbeddedSystemsTHM/Timer.h | 16 +++++++ EmbeddedSystemsTHM/main.c | 47 +++++++++++++++---- 8 files changed, 175 insertions(+), 63 deletions(-) create mode 100644 EmbeddedSystemsTHM/Timer.c create mode 100644 EmbeddedSystemsTHM/Timer.h diff --git a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj index bccc70a..4555e20 100644 --- a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj +++ b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj @@ -43,15 +43,15 @@ - - - - - - - - - + + + + + + + + + @@ -150,6 +150,12 @@ compile + + compile + + + compile + \ No newline at end of file diff --git a/EmbeddedSystemsTHM/Led.c b/EmbeddedSystemsTHM/Led.c index 30004b9..2dd0f82 100644 --- a/EmbeddedSystemsTHM/Led.c +++ b/EmbeddedSystemsTHM/Led.c @@ -1,62 +1,80 @@ #include -void Led_init(void){ +void +Led_init (void){ DDRB = 0xFF; // Set all bits in the B Data-Direction Register to output PORTB = 0x00; // Set all bits in the PORT B Register to low to turn all LEDs off } -void Led1_On(void){ +void +Led1_On (void){ PORTB |= (1<<7); // Set the bit for LED_1 to hight to turn it on } -void Led1_Off(void){ +void + Led1_Off (void){ PORTB &= ~(1<<7); // Set the bit for LED_1 to low to turn it off } -void Led2_On(void){ +void +Led2_On (void){ PORTB |= (1<<6); } -void Led2_Off(void){ +void +Led2_Off (void){ PORTB &= ~(1<<6); } -void Led3_On(void){ +void +Led3_On (void){ PORTB |= (1<<5); } -void Led3_Off(void){ +void +Led3_Off (void){ PORTB &= ~(1<<5); } -void Led4_On(void){ +void +Led4_On (void){ PORTB |= (1<<4); } -void Led4_Off(void){ +void + + Led4_Off (void){ PORTB &= ~(1<<4); } -void Led5_On(void){ +void +Led5_On (void){ PORTB |= (1<<3); } -void Led5_Off(void){ +void +Led5_Off (void){ PORTB &= ~(1<<3); } -void Led6_On(void){ +void +Led6_On (void){ PORTB |= (1<<2); } -void Led6_Off(void){ +void +Led6_Off (void){ PORTB &= ~(1<<2); } -void Led7_On(void){ +void +Led7_On (void){ PORTB |= (1<<1); } -void Led7_Off(void){ +void +Led7_Off (void){ PORTB &= ~(1<<1); } -void Led8_On(void){ +void +Led8_On (void){ PORTB |= (1<<0); } -void Led8_Off(void){ +void +Led8_Off (void){ PORTB &= ~(1<<0); } \ No newline at end of file diff --git a/EmbeddedSystemsTHM/Led.h b/EmbeddedSystemsTHM/Led.h index 072924e..126bdcb 100644 --- a/EmbeddedSystemsTHM/Led.h +++ b/EmbeddedSystemsTHM/Led.h @@ -5,33 +5,33 @@ /* Initialisiert die Hardware für die Led */ -void Led_init(void); +void Led_init (void); /* Funktionen schalten entsprechende LEDs ein oder aus. */ -void Led1_On(void); -void Led1_Off(void); +void Led1_On (void); +void Led1_Off (void); -void Led2_On(void); -void Led2_Off(void); +void Led2_On (void); +void Led2_Off (void); -void Led3_On(void); -void Led3_Off(void); +void Led3_On (void); +void Led3_Off (void); -void Led4_On(void); -void Led4_Off(void); +void Led4_On (void); +void Led4_Off (void); -void Led5_On(void); -void Led5_Off(void); +void Led5_On (void); +void Led5_Off (void); -void Led6_On(void); -void Led6_Off(void); +void Led6_On (void); +void Led6_Off (void); -void Led7_On(void); -void Led7_Off(void); +void Led7_On (void); +void Led7_Off (void); -void Led8_On(void); -void Led8_Off(void); +void Led8_On (void); +void Led8_Off (void); #endif /* LED_H_ */ \ No newline at end of file diff --git a/EmbeddedSystemsTHM/Taster.c b/EmbeddedSystemsTHM/Taster.c index 19278fb..772c30b 100644 --- a/EmbeddedSystemsTHM/Taster.c +++ b/EmbeddedSystemsTHM/Taster.c @@ -1,6 +1,7 @@ #include -void Taster_init(void){ +void +Taster_init (void){ DDRD &= ~(0b111<<5); // Set bit 5-7 of Data Direction Register D as input DDRC &= ~(1<<2); // Set bit 2 of Data Direction Register C as input @@ -8,15 +9,19 @@ void Taster_init(void){ PORTC |= (1<<2); // Initialize bit 2 of PORTC as for pull up resistor } -uint8_t Taster1_get(void){ +uint8_t +Taster1_get (void){ return ((PIND & (1< +#include + +volatile uint16_t cntr = 0; + +void +Timer_init (void){ + 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 */ + 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) */ +} + + +uint16_t +Timer_getTick(void){ + return cntr; +} + + +ISR(TIMER0_COMPA_vect){ + cntr++; +} + +/* +ISR(TIMER0_OVF_vect){ + cntr++; +} +//*/ \ No newline at end of file diff --git a/EmbeddedSystemsTHM/Timer.h b/EmbeddedSystemsTHM/Timer.h new file mode 100644 index 0000000..a70a8bf --- /dev/null +++ b/EmbeddedSystemsTHM/Timer.h @@ -0,0 +1,16 @@ +/* + * Timer.h + * + * Created: 28/10/2021 17:49:26 + * Author: n0x + */ + + +#ifndef TIMER_H_ +#define TIMER_H_ +#include + +void Timer_init(void); +uint16_t Timer_getTick(void); + +#endif /* TIMER_H_ */ \ No newline at end of file diff --git a/EmbeddedSystemsTHM/main.c b/EmbeddedSystemsTHM/main.c index 4870e37..3141fa8 100644 --- a/EmbeddedSystemsTHM/main.c +++ b/EmbeddedSystemsTHM/main.c @@ -7,34 +7,61 @@ #include "Led.h" #include "Taster.h" +#include "Timer.h" #include +#include #ifdef F_CPU #define F_CPU 1000000 #endif /* function declarations */ -void task1 (); +void task1 (void); +void tast2 (void); /* global variables */ -int g_counter = 0; +volatile int g_counter = 0; +int ledStatus = 0; /* functions */ int main (void) { - // Initialize the LEDs and Buttons + sei(); + + /* Initialize the LEDs and Buttons */ Led_init(); Taster_init(); + Timer_init(); + while (1) { - // Run Task 1 - task1(); + /* Run Task 1 */ + //task1(); + + /* Run Task 2 */ + task2(); } } -/* Tast 1 (2021-10-21) */ +/* Task 2 (2021-10-28) */ +void +task2 () { + + if(Timer_getTick() % 1000 == 0){ + ledStatus++; + } + + if(ledStatus % 2 == 0){ + Led1_On(); + } else { + Led1_Off(); + } +} + + +/* Task 1 (2021-10-21) */ void task1 () { _delay_ms(50); @@ -50,16 +77,16 @@ task1 () { * 8 = alle LEDs an. */ - // Check if counter needs to be incremented + /* Check if counter needs to be incremented */ if (Taster1_get()) g_counter++; - // Check if counter needs to be decremented + /* Check if counter needs to be decremented */ if (Taster2_get()) g_counter--; - // Keep counter within boundaries (0-8) + /* Keep counter within boundaries (0-8) */ g_counter = (g_counter + 9) % 9; - // Set the LEDs according to the counter + /* 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();