/*
 * 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 PB1			// PWM-Out OCR0B

int main(void)
{
	// PWM = 157 / 100 x %, gerundet
	unsigned char PWM_10 = 15;			// 8-bit PWM value 10%
	unsigned char PWM_20 = 31;			// 8-bit PWM value 20%
	unsigned char PWM_30 = 47;			// 8-bit PWM value 30%	
	unsigned char PWM_50 = 78;			// 8-bit PWM value 50%
	unsigned char PWM_100 = 157;		// 8-bit PWM value 100%

	
	// 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 Mode7 Timer0
	TCCR0A = (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
	TCCR0B = (1 << WGM02) | (1 << CS01) | (1 << CS00);	// clock source = CLK/64, start PWM
	OCR0A = 157;			// 1MHz/64/157 = 99,52 Hz
	OCR0B = PWM_10;								// initial PWM pulse width
	
 unsigned char x=0;

	 while(1)
	 {
		 x = PINB;								//SW einlesen
		 switch (x & 0b00011100) {				// x-maskieren
			 case 24: OCR0B = PWM_100; break;
			 case 16: OCR0B = PWM_50; break;
			 case 8: OCR0B = PWM_30; break;
			 case 0: OCR0B = PWM_20; break;
			 default: OCR0B = PWM_10;
		 }
	 }
}