#define F_CPU 16000000L
#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>


void uart_putchar(uint8_t u8Data, FILE *stream )
{
	while(!(UCSR1A&(1<<UDRE1))){};
	UDR1 = u8Data;
}
FILE uart_output = FDEV_SETUP_STREAM((void *)uart_putchar, NULL, _FDEV_SETUP_WRITE);

ISR(TIMER1_OVF_vect){
	wdt_reset();
	TCNT1 = 50000;
}

void Timer1_Init(){
	TCCR1B |= ((1<<CS12)|(1<<CS10)); /* Prescaler 1024 */
	TIMSK |= (1<<TOIE1); /* Enable Timer1 overflow interrupt */
}

int main(void)
{
	TCNT1 = 50000; /* 1 sec */
	wdt_enable(WDTO_2S); /* Watchdog timer expires in about 2 sec */
	
	/* USART1 initialization */
	UCSR1A = 0x00;
	UCSR1B = 0x98;
	UCSR1C = 0x06;
	UBRR1H = 0x00; /* baud rate 115200 UBRR1=8*/
	UBRR1L = 0x08;
	stdout = &uart_output;
	
	Timer1_Init();
	sei(); /* Enable global interrupt */
	
	printf("*** Initialization...\r\n");
	
	uint16_t count = 0;
	while (1)
	{
		count++;
		printf("count : %d\r\n", count);
		_delay_ms(1000);
	}
	return 0;
}

