//ICC-AVR application builder : 2011-01-21 오전 11:40:54
// Target : M128
// Crystal: 16.000Mhz

#include <iom128v.h>
#include <macros.h>
#include <stdio.h>

#define OK	  	   1
#define NO 		   0 

#define X		   0
#define Y 		   1
#define Z 		   2
#define P		   3

volatile unsigned int value, adcstatus=NO , XYZstate = X, xval, yval, zval;

// printf 함수 사용시 추가할 것.
int putchar(char c)
{
    while (((UCSR0A>>UDRE0)&0x01) == 0) ;  // UDRE, data register empty
        
    UDR0 = c;
    return c;
}

// scanf 함수 사용시 추가할 것.
int getchar(void)
{
    while ((UCSR0A & 0x80) == 0);
    return UDR0;
}

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 PORTE = 0x00;
 DDRE  = 0x00;
 PORTF = 0x00;
 DDRF  = 0x00;
 PORTG = 0x00;
 DDRG  = 0x00;
}

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UBRR0L = 0x67; //set baud rate lo
 UBRR0H = 0x00; //set baud rate hi
 UCSR0B = 0x18;
}

//ADC initialize
// Conversion time: 104uS
void adc_init(void)
{
 ADCSRA = 0x00; //disable adc
 ADMUX = 0x00; //select adc input 0
 ACSR  = 0x80;
 ADCSRA = 0x0F;
 ADCSRA |=0xC0;
}

#pragma interrupt_handler adc_isr:iv_ADC
void adc_isr(void)
{
 //conversion complete, read value (int) using...
  value=ADCL;            //Read 8 low bits first (important)
  value|=(int)ADCH << 8; //read 2 high bits and shift into top byte
  adcstatus = OK ;
  switch(XYZstate){
  		case X : xval  = value; break;
		case Y : yval  = value; break;
		case Z : zval  = value; break;
		default: break; 
		}
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 port_init();
 uart0_init();
 adc_init();

 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

void delay(int delaytime){
	 	int i,j; 
		for(i=0; i<1000;i++){
	 		  for(j=0; j<delaytime;j++){
			  }
		}
}

//
void main(void)
{
 init_devices();
 //insert your functional code here...
 ADCSRA = 0x7f;
 ADMUX = 0x00; 
 ADCSRA |= 0xc0; 
 while(1){
	if(adcstatus == OK){
			switch(XYZstate){
				case X : ADMUX = 0x00;
					   	 ADCSRA |=0x40;
						 adcstatus = NO;
						 XYZstate = Y ;
						 delay(10); 
						 break;
				
				case Y : ADMUX = 0x02;
					   	 ADCSRA |=0x40;
						 adcstatus = NO;
						 XYZstate = Z ;
						 delay(10); 
						 break;
				
				case Z : ADMUX = 0x04;
					   	 ADCSRA |=0x40;
						 adcstatus = NO;
						 XYZstate = P ;
						 delay(10); 
						 break;
						 
				default : printf("\r\n value : %d %d %d", xval, yval, zval);
						  delay(10);  
						  XYZstate = X; 
						  break;
						}
 		}
 }
}
