Added Timer.h and Timer.c. Completed Task to use timers to blink LED1 with a 1Hz frequency
This commit is contained in:
parent
578a2fd693
commit
2bd2215ee9
@ -150,6 +150,12 @@
|
||||
<Compile Include="Taster.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Timer.c">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Timer.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
|
||||
</Project>
|
@ -1,62 +1,80 @@
|
||||
#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
|
||||
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);
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
#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
|
||||
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<<PIND7)) == 0);
|
||||
}
|
||||
uint8_t Taster2_get(void){
|
||||
uint8_t
|
||||
Taster2_get (void){
|
||||
return ((PIND & (1<<PIND6)) == 0);
|
||||
}
|
||||
uint8_t Taster3_get(void){
|
||||
uint8_t
|
||||
Taster3_get (void){
|
||||
return ((PIND & (1<<PIND5)) == 0);
|
||||
}
|
||||
uint8_t Taster4_get(void){
|
||||
uint8_t
|
||||
Taster4_get (void){
|
||||
return ((PINC & (1<<PINC2)) == 0);
|
||||
}
|
40
EmbeddedSystemsTHM/Timer.c
Normal file
40
EmbeddedSystemsTHM/Timer.c
Normal 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++;
|
||||
}
|
||||
//*/
|
16
EmbeddedSystemsTHM/Timer.h
Normal file
16
EmbeddedSystemsTHM/Timer.h
Normal 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_ */
|
@ -7,34 +7,61 @@
|
||||
|
||||
#include "Led.h"
|
||||
#include "Taster.h"
|
||||
#include "Timer.h"
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h>
|
||||
#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();
|
||||
|
Loading…
Reference in New Issue
Block a user