//-------------------------------------------------------------------------------------------------------
// Title          : Diplomarbeit
//-------------------------------------------------------------------------------------------------------
// Funktion       : LED Beleuchtungskzept
// Schaltung      : Controllerboard
// Datei          : Main.c
//-------------------------------------------------------------------------------------------------------
// Prozessor      : ATmega8
// Takt           : 1 MHz
// Sprache        : C
// letzte Änderung: 02.09.2014
// Autor          : Kay Ulbricht
//-------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <stdint.h>
#include <util/delay.h>								// AVR Timemanager
#include <stdbool.h>								// Variablentyp Bool
#include <avr/interrupt.h>							// AVR Routinen for Interrupts
#include <avr/io.h>									// AVR IO Header

//#include "General.h"
#include "RS232.h"
#include "TWI_Master.h"
#include "PCA9635.h"
#include "Taster.h"

//**************************
//** Ports initialisieren **
//**************************
void port_init(void)
{
	// Pin 0 und 1 auf Eingang und andere im ursprünglichen Zustand belassen:
	DDRC = 0b00000000; //&= ~((1 << DDC0) | (1 << DDC1));
	// Pin 5 auf Ausgang und andere im ursprünglichen Zustand belassen:
	DDRD |= (1 << DDD5);
	DDRC |= (1 << DDC2);
	
	// Pull Up von Pin 0 und Pin 1 setzen:
	PORTC = ( 1 << PC0 ) | ( 1 << PC1 );
}
/** Main **/

int main (void)
{
	
/** Initzialisiere Ports **/
	
	port_init();
		
/** Configure debouncing routines **/

	KEY_DDR &= ~ALL_KEYS;										// configure key port for input
	KEY_PORT |= ALL_KEYS;										// and turn on pull up resistors
		
	TCCR0 = (1<<CS02)|(1<<CS00);								// divide by 1024
	TCNT0 = (uint8_t)(int16_t)-(F_CPU / 1024 * 10e-3 + 0.5);	// preload for 10ms
	TIMSK |= 1<<TOIE0;											// enable timer interrupt
		
	sei();
		
/** Clear any interrupt **/

	cli ();

/** Wait 1 second for POR **/

	_delay_ms(1000);

/** Initzialisiere RS232 **/
	
	uart_init();

/** Initzialisiere TWI Master Interface mit bitrate von 1000 Hz **/

	if (!TWIM_Init (1000))
		{
		printf ("Error in initiating TWI interface\n");
		while (1);
		}
		
/** Endlosschleife **/

	while(1)
		{
			Variablen_Init();
			PCA9635();
					
/** Write byte(s) to the slave **/

		if( get_key_short( 1<<KEY0 ))
		{
			Full_Output();
			PCA9635();
			PORTD = PORTD | ( 1<<PD5 );
			_delay_ms(1000);
			LED0 = 0x20;
			LED1 = 0x33;
			LED2 = 0x20;
			LED11 = 0x42;
			PCA9635();
		}
		if ( get_key_long( 1<<KEY0 ))
		{
			Variablen_Init();
			PCA9635();
			PORTD &= ~( 1<<PD5 );
		}
								
		}
return 0;
}

