From 0eace5b6fef4f69aa53db4b7aa62a8ec28253f7a Mon Sep 17 00:00:00 2001 From: _N0x Date: Fri, 5 Nov 2021 09:00:41 +0100 Subject: [PATCH] Fixed issues with timer --- EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj | 26 +++++++++++++--- EmbeddedSystemsTHM/Timer.c | 18 +++++------ EmbeddedSystemsTHM/main.c | 34 ++++++++++++--------- 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj index 4555e20..dd4e4ae 100644 --- a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj +++ b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj @@ -28,18 +28,20 @@ 0 0 - com.atmel.avrdbg.tool.simulator + com.atmel.avrdbg.tool.stk500 0x1E9705 - + + + - com.atmel.avrdbg.tool.simulator - + com.atmel.avrdbg.tool.simulator + - Simulator + Simulator @@ -53,6 +55,20 @@ + ISP + + + + 115200 + + ISP + + com.atmel.avrdbg.tool.stk500 + + + STK500 + + 115200 diff --git a/EmbeddedSystemsTHM/Timer.c b/EmbeddedSystemsTHM/Timer.c index 969177b..ef1a655 100644 --- a/EmbeddedSystemsTHM/Timer.c +++ b/EmbeddedSystemsTHM/Timer.c @@ -12,29 +12,27 @@ 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 */ 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; + uint16_t tmp; + cli(); + tmp = cntr; + sei(); + return tmp; } ISR(TIMER0_COMPA_vect){ cntr++; -} - -/* -ISR(TIMER0_OVF_vect){ - cntr++; -} -//*/ \ No newline at end of file +} \ No newline at end of file diff --git a/EmbeddedSystemsTHM/main.c b/EmbeddedSystemsTHM/main.c index 3141fa8..4619c29 100644 --- a/EmbeddedSystemsTHM/main.c +++ b/EmbeddedSystemsTHM/main.c @@ -21,6 +21,7 @@ void tast2 (void); /* global variables */ volatile int g_counter = 0; int ledStatus = 0; +uint16_t startMS; /* functions */ int @@ -34,30 +35,35 @@ main (void) Timer_init(); + /* Run Task 1 while (1) { - /* Run Task 1 */ - //task1(); - - /* Run Task 2 */ - task2(); - } + task1(); + }//*/ + + + /* Run Task 2 */ + startMS=Timer_getTick(); + while (1) + { + task2(); + }//*/ } /* Task 2 (2021-10-28) */ void task2 () { - - if(Timer_getTick() % 1000 == 0){ + if(Timer_getTick()-startMS >= 1000) { /* Wait 1000ms before switching LED1 */ + startMS=Timer_getTick(); + + if(ledStatus % 2 == 0){ + Led1_On(); + } else { + Led1_Off(); + } ledStatus++; } - - if(ledStatus % 2 == 0){ - Led1_On(); - } else { - Led1_Off(); - } }