/*
 * Tiny25 fastPWM.c
 *
 * Created: 29.11.2015 17:32:47
 *  Author: Joerg
 */ 

#define  F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>

#define INPUT( Port, Bit )  ( !((Port) & ( 1 << (Bit) )) )

#define IN_1  PB2			// Input 1
#define IN_2  PB3			// Input 2
#define IN_3  PB4			// Input 3
#define PWM_OUT PB0			// PWM-Out 

int main(void)
{
	// PWM = 255 / 100 x %, gerundet
	unsigned char PWM_10 = 25;			// 8-bit PWM value 10%
	unsigned char PWM_30 = 80;			// 8-bit PWM value 30%
	unsigned char PWM_100 = 255;		// 8-bit PWM value 100%
	unsigned char PWM_50 = 127;			// 8-bit PWM value 50%
	
	// output Pin
	DDRB |= (1 << PWM_OUT);			// PWM_out an PB0 - OC0A
	// input Pin
	DDRB &= ~(1 << IN_1)|(1 << IN_2)|(1 << IN_3);	// Input an PB2-PB4
	PORTB |= (1 << IN_1)|(1 << IN_2)|(1 << IN_3);	// Pull up PD0-PD2 on
	
	// fast PWM mode Timer0
	TCCR0A = (1 << COM0A1) | (1 << WGM01) | (1 << WGM00);
	TCCR0B = (1 << CS01);				// clock source = CLK/8, start PWM
	OCR0A = PWM_30;						// initial PWM pulse width
	
	while(1)
	{
		if  (INPUT( PINB, IN_1 )) {				// Sw1 an?
			OCR0A = PWM_100 ;					// PWM = 100%
			
			if (INPUT( PINB, IN_2)	&&			// SW2 an?
				!(INPUT( PINB, IN_3))) {		// SW3 aus?
				OCR0A = PWM_50;					// PWM = 50%				
				}
			if (INPUT( PINB, IN_2)	&&			// SW2 und
				(INPUT( PINB, IN_3))) {			// SW3 an?
				OCR0A = PWM_30;					// PWM = 30%
				}
			}
			
		else {
			OCR0A = PWM_10;			// PWM = 10%
		}
	}
}