/*
 * Programming_Assignment_1.c
 *
 * Created: 1/20/2015 7:38:30 PM
 *  Author: GTRUDEL
 *  Purpose: Make the microcontroller count to 7 in binary, with a 2 second delay b/w each number
 *  Equipment: Atmega 168
 *  Setup: LED 1 connected to pin 23 (PC0), LED 2 pin 24 (PC1), LED 3 pin 25 (PC2)
 */ 


#include <avr/io.h>

//Define functions
//Forward Declarations
//=================================================
void ioinit(void);
void delay_ms(uint16_t x);
//=================================================


int main(void)
{
	ioinit(	); //setup IO pins and defaults
	int x = 0; //variable used to count and set the LED pins on or off
    while(1)
    {
		if ( x > 7 ) x = 0; // once we hit 8, reset back to 0
        PORTC = x;          // set the portc to the value of x (which will be between 0 and 7)
        x++;                // increment x for the next round
        delay_ms(20000);    // set a delay of 2 seconds between iterations
    }
	
	return 0;
}

void ioinit(void)
{
	
	//1= output, 0 = input
	DDRB = 0b11111111;  // All pins to output
	DDRC = 0b11111111; //  All pins to output
	DDRD = 0b11111110; //  All pins except the RX on PD0 are set to high
	
}

void delay_ms(uint16_t x)
{
	uint8_t y, z;

	for (; x > 0; x--)
	{
		for(y = 0;y < 90; y++)
		{
			for(z = 0; z < 6; z++)
			{
				asm volatile ("nop");
			}
		}
		
	}
}
