Added Timer.h and Timer.c. Completed Task to use timers to blink LED1 with a 1Hz frequency

This commit is contained in:
_N0x 2021-10-28 19:24:52 +02:00
parent 578a2fd693
commit 2bd2215ee9
8 changed files with 175 additions and 63 deletions

View File

@ -51,7 +51,7 @@
<dependencies> <dependencies>
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.49.1" /> <content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.49.1" />
</dependencies> </dependencies>
</framework-data> </framework-data>
</AsfFrameworkConfig> </AsfFrameworkConfig>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
@ -150,6 +150,12 @@
<Compile Include="Taster.h"> <Compile Include="Taster.h">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="Timer.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="Timer.h">
<SubType>compile</SubType>
</Compile>
</ItemGroup> </ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" /> <Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project> </Project>

View File

@ -1,62 +1,80 @@
#include <avr/io.h> #include <avr/io.h>
void Led_init(void){ void
Led_init (void){
DDRB = 0xFF; // Set all bits in the B Data-Direction Register to output 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 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 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 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); PORTB |= (1<<6);
} }
void Led2_Off(void){ void
Led2_Off (void){
PORTB &= ~(1<<6); PORTB &= ~(1<<6);
} }
void Led3_On(void){ void
Led3_On (void){
PORTB |= (1<<5); PORTB |= (1<<5);
} }
void Led3_Off(void){ void
Led3_Off (void){
PORTB &= ~(1<<5); PORTB &= ~(1<<5);
} }
void Led4_On(void){ void
Led4_On (void){
PORTB |= (1<<4); PORTB |= (1<<4);
} }
void Led4_Off(void){ void
Led4_Off (void){
PORTB &= ~(1<<4); PORTB &= ~(1<<4);
} }
void Led5_On(void){ void
Led5_On (void){
PORTB |= (1<<3); PORTB |= (1<<3);
} }
void Led5_Off(void){ void
Led5_Off (void){
PORTB &= ~(1<<3); PORTB &= ~(1<<3);
} }
void Led6_On(void){ void
Led6_On (void){
PORTB |= (1<<2); PORTB |= (1<<2);
} }
void Led6_Off(void){ void
Led6_Off (void){
PORTB &= ~(1<<2); PORTB &= ~(1<<2);
} }
void Led7_On(void){ void
Led7_On (void){
PORTB |= (1<<1); PORTB |= (1<<1);
} }
void Led7_Off(void){ void
Led7_Off (void){
PORTB &= ~(1<<1); PORTB &= ~(1<<1);
} }
void Led8_On(void){ void
Led8_On (void){
PORTB |= (1<<0); PORTB |= (1<<0);
} }
void Led8_Off(void){ void
Led8_Off (void){
PORTB &= ~(1<<0); PORTB &= ~(1<<0);
} }

View File

@ -5,33 +5,33 @@
/* /*
Initialisiert die Hardware für die Led Initialisiert die Hardware für die Led
*/ */
void Led_init(void); void Led_init (void);
/* /*
Funktionen schalten entsprechende LEDs ein oder aus. Funktionen schalten entsprechende LEDs ein oder aus.
*/ */
void Led1_On(void); void Led1_On (void);
void Led1_Off(void); void Led1_Off (void);
void Led2_On(void); void Led2_On (void);
void Led2_Off(void); void Led2_Off (void);
void Led3_On(void); void Led3_On (void);
void Led3_Off(void); void Led3_Off (void);
void Led4_On(void); void Led4_On (void);
void Led4_Off(void); void Led4_Off (void);
void Led5_On(void); void Led5_On (void);
void Led5_Off(void); void Led5_Off (void);
void Led6_On(void); void Led6_On (void);
void Led6_Off(void); void Led6_Off (void);
void Led7_On(void); void Led7_On (void);
void Led7_Off(void); void Led7_Off (void);
void Led8_On(void); void Led8_On (void);
void Led8_Off(void); void Led8_Off (void);
#endif /* LED_H_ */ #endif /* LED_H_ */

View File

@ -1,6 +1,7 @@
#include <avr/io.h> #include <avr/io.h>
void Taster_init(void){ void
Taster_init (void){
DDRD &= ~(0b111<<5); // Set bit 5-7 of Data Direction Register D as input 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 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 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<<PIND7)) == 0); return ((PIND & (1<<PIND7)) == 0);
} }
uint8_t Taster2_get(void){ uint8_t
Taster2_get (void){
return ((PIND & (1<<PIND6)) == 0); return ((PIND & (1<<PIND6)) == 0);
} }
uint8_t Taster3_get(void){ uint8_t
Taster3_get (void){
return ((PIND & (1<<PIND5)) == 0); return ((PIND & (1<<PIND5)) == 0);
} }
uint8_t Taster4_get(void){ uint8_t
Taster4_get (void){
return ((PINC & (1<<PINC2)) == 0); return ((PINC & (1<<PINC2)) == 0);
} }

View File

@ -6,16 +6,16 @@
/* /*
Initialisiert die Hardware für die Taster Initialisiert die Hardware für die Taster
*/ */
void Taster_init(void); void Taster_init (void);
/* /*
Gibt den aktuellen Status der Tasten aus. Gibt den aktuellen Status der Tasten aus.
Rückgabewert 0 = Taster nicht gedr<EFBFBD>ckt, 1 = Taster gedr<EFBFBD>ckt. Rückgabewert 0 = Taster nicht gedr<EFBFBD>ckt, 1 = Taster gedr<EFBFBD>ckt.
*/ */
uint8_t Taster1_get(void); uint8_t Taster1_get (void);
uint8_t Taster2_get(void); uint8_t Taster2_get (void);
uint8_t Taster3_get(void); uint8_t Taster3_get (void);
uint8_t Taster4_get(void); uint8_t Taster4_get (void);
#endif /* TASTER_H_ */ #endif /* TASTER_H_ */

View File

@ -0,0 +1,40 @@
/*
* Timer.c
*
* Created: 28/10/2021 17:51:29
* Author: n0x
*/
#include <avr/io.h>
#include <avr/interrupt.h>
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++;
}
//*/

View File

@ -0,0 +1,16 @@
/*
* Timer.h
*
* Created: 28/10/2021 17:49:26
* Author: n0x
*/
#ifndef TIMER_H_
#define TIMER_H_
#include <avr/io.h>
void Timer_init(void);
uint16_t Timer_getTick(void);
#endif /* TIMER_H_ */

View File

@ -7,34 +7,61 @@
#include "Led.h" #include "Led.h"
#include "Taster.h" #include "Taster.h"
#include "Timer.h"
#include <util/delay.h> #include <util/delay.h>
#include <avr/interrupt.h>
#ifdef F_CPU #ifdef F_CPU
#define F_CPU 1000000 #define F_CPU 1000000
#endif #endif
/* function declarations */ /* function declarations */
void task1 (); void task1 (void);
void tast2 (void);
/* global variables */ /* global variables */
int g_counter = 0; volatile int g_counter = 0;
int ledStatus = 0;
/* functions */ /* functions */
int int
main (void) main (void)
{ {
// Initialize the LEDs and Buttons sei();
/* Initialize the LEDs and Buttons */
Led_init(); Led_init();
Taster_init(); Taster_init();
Timer_init();
while (1) while (1)
{ {
// Run Task 1 /* Run Task 1 */
task1(); //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 void
task1 () { task1 () {
_delay_ms(50); _delay_ms(50);
@ -50,16 +77,16 @@ task1 () {
* 8 = alle LEDs an. * 8 = alle LEDs an.
*/ */
// Check if counter needs to be incremented /* Check if counter needs to be incremented */
if (Taster1_get()) g_counter++; if (Taster1_get()) g_counter++;
// Check if counter needs to be decremented /* Check if counter needs to be decremented */
if (Taster2_get()) g_counter--; if (Taster2_get()) g_counter--;
// Keep counter within boundaries (0-8) /* Keep counter within boundaries (0-8) */
g_counter = (g_counter + 9) % 9; 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 >= 1 ? Led1_On() : Led1_Off();
g_counter >= 2 ? Led2_On() : Led2_Off(); g_counter >= 2 ? Led2_On() : Led2_Off();
g_counter >= 3 ? Led3_On() : Led3_Off(); g_counter >= 3 ? Led3_On() : Led3_Off();