#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>

volatile unsigned int adc_result=0;

SIGNAL(SIG_ADC)
{
 adc_result = ADC;
}

void enable_adc_interrupt(unsigned char channel)
{
 ADMUX = ( ADMUX & 0xE0) | (channel & 0x1F);// External Vref, Channel setting 
 ADCSRA = (1<<ADEN) | (1<<ADIE) | 7;  // AD enable, Interrupt enable, 1/128
 ADCSRA |= 1<<ADFR | 1<<ADSC;    // Free run mode, ADC start
}

void usart1_setup_ver1(void)
{ // TX enable | RX enable
 UCSR1B = (1<<TXEN1) | (1<<RXEN1);
 // parity none, 1 stop bit, 8 data
 UBRR1H = 0;  // 38400 bps at 16MHz
 UBRR1L = 25;
}

int usart1_getc_ver1(FILE *stream)
{
  while( (UCSR1A & (1<<RXC1))==0 );
 return UDR1;
}

int usart1_putc_ver1(char data, FILE *stream)
{
 while( (UCSR1A & (1<<UDRE1))==0 );
 UDR1 = data;
 return data;
}

//////////////////////////////////////////////////////////////
int main(void)
{ FILE *fp;
 
 enable_adc_interrupt(0);
 usart1_setup_ver1();
 
 fp = fdevopen(usart1_putc_ver1, usart1_getc_ver1);
 
 sei();

 while(1){
  printf("adc_result = %d\r\n", adc_result);
 }
}

