Embedded-Software-Projects

Log | Files | Refs | README | LICENSE

commit 063a2d796f339baab608227d908d0d9f842d3ee9
parent 4870817c7d8aa12848f7f6c82813666780e8a620
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Wed,  9 Oct 2024 12:56:39 +0100

Changed to static local variables for cylon state.

Diffstat:
MProject1/Project1/main.c | 35+++++++++++++----------------------
1 file changed, 13 insertions(+), 22 deletions(-)

diff --git a/Project1/Project1/main.c b/Project1/Project1/main.c @@ -10,6 +10,7 @@ #include <avr/io.h> #include <avr/interrupt.h> #include <avr/cpufunc.h> +#include <stdbool.h> /* Use a struct to make the association between PORTs and bits connected to the LED array more explicit */ struct LED_BITS @@ -18,19 +19,10 @@ struct LED_BITS uint8_t bit_mapping; }; -enum DIRECTION -{ - LEFT, - RIGHT -}; - struct LED_BITS LED_Array[10] = { {&PORTC, PIN5_bm}, {&PORTC, PIN4_bm}, {&PORTA, PIN0_bm}, {&PORTF, PIN5_bm}, {&PORTC, PIN6_bm}, {&PORTB, PIN2_bm}, {&PORTF, PIN4_bm}, {&PORTA, PIN1_bm}, {&PORTA, PIN2_bm}, {&PORTA, PIN3_bm} }; -uint8_t CURRENT_CYLON_LED = 0; // Index of the current LED -enum DIRECTION CURRENT_CYLON_DIRECTION = LEFT; - void CLOCK_init (void); void InitialiseLED_PORT_bits(void); void Set_Clear_Ports(uint8_t set); @@ -125,27 +117,26 @@ void TCA0_init_bits(void) } -void Toggle_Cylon_Direction() { - CURRENT_CYLON_DIRECTION = (CURRENT_CYLON_DIRECTION == LEFT) ? RIGHT : LEFT; -} - ISR(TCA0_OVF_vect) { - if (CURRENT_CYLON_DIRECTION == LEFT) + static uint8_t i = 0; // Index of the current LED + static bool direction = 1; // direction of the cylon. 0 = left, 1 = right + + if (direction) { - LED_Array[CURRENT_CYLON_LED].LED_PORT->OUTCLR = LED_Array[CURRENT_CYLON_LED].bit_mapping; - LED_Array[CURRENT_CYLON_LED+1].LED_PORT->OUTSET = LED_Array[CURRENT_CYLON_LED+1].bit_mapping; - CURRENT_CYLON_LED += 1; + LED_Array[i].LED_PORT->OUTCLR = LED_Array[i].bit_mapping; + LED_Array[i+1].LED_PORT->OUTSET = LED_Array[i+1].bit_mapping; + i += 1; } else { - LED_Array[CURRENT_CYLON_LED].LED_PORT->OUTCLR = LED_Array[CURRENT_CYLON_LED].bit_mapping; - LED_Array[CURRENT_CYLON_LED-1].LED_PORT->OUTSET = LED_Array[CURRENT_CYLON_LED-1].bit_mapping; - CURRENT_CYLON_LED -= 1; + LED_Array[i].LED_PORT->OUTCLR = LED_Array[i].bit_mapping; + LED_Array[i-1].LED_PORT->OUTSET = LED_Array[i-1].bit_mapping; + i -= 1; } - if (CURRENT_CYLON_LED >= 9 || CURRENT_CYLON_LED <= 0) { - Toggle_Cylon_Direction(); + if (i >= 9 || i <= 0) { + direction = !direction; } //Toggle_Ports();