/*
* LED_fading_02.c
* with 10bit valuetable and phasecorrect pwm
* Controller: ATmega88 @ 8MHz
*/

#include <avr/io.h>
#include <util/delay.h>

const unsigned int pwm_table_10[64] =
{
	0, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 9, 10,
	11, 12, 13, 15, 17, 19, 21, 23, 26, 29, 32, 36, 40, 44, 49, 55,
	61, 68, 76, 85, 94, 105, 117, 131, 146, 162, 181, 202, 225, 250,
	279, 311, 346, 386, 430, 479, 534, 595, 663, 739, 824, 918, 1023
};

int main(void)
{
	DDRB |= (1<<PB1);	// OC1A = output
	ICR1 = 1023;		// ICR1 = Top Value
	
	// Mode 10: PWM, Phase Correct, Prescaler = 1, Clear OC1A on compare match
	TCCR1A = (1 << COM1A1) + (1 << WGM11);
	TCCR1B = (1 << WGM13) + (1 << CS10);
	
	while(1)
	{
		for(int i=0; i<=63; i++)		// PWM inkrementieren
		{
			OCR1A = pwm_table_10[i];	// Tabellenwert i dem OCR1A zuweisen
			_delay_ms(50);
		}
		
		for(int i=63; i>= 1; i--)		// PWM dekrementieren
		{
			OCR1A = pwm_table_10[i];	// Tabellenwert i dem OCR1A zuweisen
			_delay_ms(50);
		}
	}
}