#include <avr/io.h> // Library for input/output control
#define F_CPU 16000000UL // Define CPU clock speed
// Function prototype for delay in milliseconds
void delay_ms(uint16_t milliseconds);
int main() {
DDRD = 0xFF; // Set PD0-7 as Output for LEDs
int i = 3; // Variable for LED index
int j = 4; // Variable for LED index
int n = 0; // Variable for LED index
int l = 7; // Variable for LED index
while (1) { // Infinite loop for LED animation
// Active running LED from center to edge
for (int k = 0; k <= 3; ++k) {
PORTD |= (1 << i - k); // Turn on LED from center to edge
PORTD |= (1 << j + k); // Turn on mirrored LED from edge to center
delay_ms(750); // Delay of 750ms
PORTD &= ~(1 << i - k); // Turn off LED
PORTD &= ~(1 << j + k); // Turn off mirrored LED
}
// Active running LED from edge to center
for (int k = 0; k <= 3; ++k) {
PORTD |= (1 << l - k); // Turn on LED from edge to center
PORTD |= (1 << n + k); // Turn on mirrored LED from center to edge
delay_ms(750); // Delay of 750ms
PORTD &= ~(1 << l - k); // Turn off LED
PORTD &= ~(1 << n + k); // Turn off mirrored LED
}
}
}
void delay_ms(uint16_t milliseconds) {
while (milliseconds) {
// Setting prescaler 64 for timer1 (16-bit timer) for 1ms period
TCCR1B |= (1 << CS11) | (1 << CS10); // Prescaler 64
TCNT1 = 0; // Reset counter value
while (TCNT1 < 125); // Timer counts 125 cycles for 1ms at prescaler 64
milliseconds--;
}
}