Fixed wrong register in Taster.c and cleand up main.c
This commit is contained in:
parent
48ea8062c0
commit
8cd2a63ce5
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
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
|
||||||
|
|
||||||
PIND &= ~(0b111<<5); // Initialize bit 5-7 of PORTD as low
|
PORTD |= (0b111<<5); // Initialize bit 5-7 of PORTD as for pull up resistor
|
||||||
PINC &= ~(1<<2); // Initialize bit 2 of PORTC as low
|
PORTC |= (1<<2); // Initialize bit 2 of PORTC as for pull up resistor
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Taster1_get(void){
|
uint8_t Taster1_get(void){
|
||||||
|
@ -12,47 +12,60 @@
|
|||||||
#define F_CPU 1000000
|
#define F_CPU 1000000
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int main(void)
|
/* function declarations */
|
||||||
|
void task1 ();
|
||||||
|
|
||||||
|
/* global variables */
|
||||||
|
int g_counter = 0;
|
||||||
|
|
||||||
|
/* functions */
|
||||||
|
int
|
||||||
|
main (void)
|
||||||
{
|
{
|
||||||
// Initialize the LEDs and Buttons
|
// Initialize the LEDs and Buttons
|
||||||
Led_init();
|
Led_init();
|
||||||
Taster_init();
|
Taster_init();
|
||||||
|
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
_delay_ms(50);
|
// Run Task 1
|
||||||
|
task1();
|
||||||
/* 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()) counter++;
|
|
||||||
|
|
||||||
// Check if counter needs to be decremented
|
|
||||||
if (Taster2_get()) counter--;
|
|
||||||
|
|
||||||
// Keep counter within boundaries (0-8)
|
|
||||||
counter = (counter + 9) % 9;
|
|
||||||
|
|
||||||
// Set the LEDs according to the counter
|
|
||||||
counter >= 1 ? Led1_On() : Led1_Off();
|
|
||||||
counter >= 2 ? Led2_On() : Led2_Off();
|
|
||||||
counter >= 3 ? Led3_On() : Led3_Off();
|
|
||||||
counter >= 4 ? Led4_On() : Led4_Off();
|
|
||||||
counter >= 5 ? Led5_On() : Led5_Off();
|
|
||||||
counter >= 6 ? Led6_On() : Led6_Off();
|
|
||||||
counter >= 7 ? Led7_On() : Led7_Off();
|
|
||||||
counter >= 8 ? Led8_On() : Led8_Off();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Tast 1 (2021-10-21) */
|
||||||
|
void
|
||||||
|
task1 () {
|
||||||
|
_delay_ms(50);
|
||||||
|
|
||||||
|
/* 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();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user