#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define LED_LSB_PORT       PORTA
#define LED_LSB_DDR        DDRA
#define LED_MSB_PORT       PORTB
#define LED_MSB_DDR        DDRB

#define LED_POWER_LED_PORT PORTC
#define LED_POWER_LED_DDR  DDRC
#define LED_POWER_LED_MASK (1 << 6)

#define SWITCHES_PORT      PORTC
#define SWITCHES_DDR       DDRC
#define SWITCHES_PIN       PINC

#define FORWARDS           1
#define BACKWARDS          0

int main(void)
{
	LED_LSB_DDR |= 0b11111111; // All outputs
	LED_MSB_DDR |= 0b00111111; // Lower 6 bits as outputs

	LED_POWER_LED_DDR  |= LED_POWER_LED_MASK; // Power LED as output
	LED_POWER_LED_PORT |= LED_POWER_LED_MASK; // Turn on Power LED

	SWITCHES_DDR |= 0b0000000; // Lower 3 bits as inputs

	uint16_t Mask      = 0x01;
	uint8_t  Direction = FORWARDS;

	while (1)
	{
		LED_LSB_PORT = (Mask & 0x00FF); // Show lower bits
		LED_MSB_PORT = (Mask >> 8);     // Show upper bits
	
		_delay_ms(50);

		if (Direction == FORWARDS)
		{
			Mask <<= 1;

			if (Mask == (1 << 13))
			  Direction == BACKWARDS;
		}
		else
		{
			Mask >>= 1;

			if (Mask == (1 << 0))
			  Direction == FORWARDS;
		}
	}
}
