// =================================== CPU초기화및 인터럽트 ===================
// 참    고 : CPU를 설정 하고 인터럽트 서비스 루틴 관리함.
// ----------------------------------------------------------------------------
// 통신 관련 선언
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// 함수 선언
int putchar_0(char data);					// COM1 송신
int getchar_0(void);						// COM1 수신

// 변수 선언
char com1_rx_data,com1_rx_flg = 0;				// COM1 통신 수신 인터럽트 관련 데이터/플레그

int timer0_cnt = 0;						// 타이머0 인터럽트 관련 카운터
int timer0_touch_cnt = 0;					// timer0 터치 관리용 10ms 카운터


static int USART0_putchar(char c, FILE *stream);
static FILE device1 = FDEV_SETUP_STREAM(USART0_putchar, NULL, _FDEV_SETUP_WRITE);

static int USART0_putchar(char c, FILE *stream)	/* print a character to USART0 */
{
  if(c == '\n')                                 // process CR(carriage return)
    { loop_until_bit_is_set(UCSR0A,UDRE0);
      UDR0 = 0x0D;
      loop_until_bit_is_set(UCSR0A,UDRE0);
      UDR0 = 0x0A;
    }
  else
    { loop_until_bit_is_set(UCSR0A,UDRE0);	// Tx ready ?
      UDR0 = c;					// if yes, print
    }
  return 0;
}


/*
unsigned char cursor;

ISR(USART0_RX_vect)				// UART0 interrupt function 
{
 
    }
}
*/


void init()
{
unsigned char RXD;  
	DDRA=0xff;
	DDRB=0xff;
	DDRC=0xff;
	DDRD=0xfB;
	DDRE=0x7f;
	DDRF=0xff;

	PORTD= 0xff;

// Timer/Counter0 설정(Overflow)
	ASSR=0x00;
	TCCR0 = 0x07;						// system clock,16,000,000 / 1024 = 15.625Khz
	TCNT0 = 99;						// 255-156 = 99	
	OCR0=0x00;
	TIMSK |= 0x01;						// TIMER0 인터럽트 허가
// Baud9600   103  //=>11.0592M (71), 16M => 103
// Baud384000   25  //=>11.0592M(17) , 25
  UBRR0H = 0;                                   //16M 25=> 38400 bps
  UBRR0L = 25;   //8M=>12
  UCSR0A = 0x00;                                // asynchronous normal mode
  UCSR0B = 0x98;                                // interrupt enable, 8 data  18 , 98
  UCSR0C = 0x06;                                // no parity, 1 stop, 8 data
 RXD = UDR0;
fdevopen(putchar_0,getchar_0,0);

   sei();

}


/*


unsigned char RX0_char_scan(void)		// receive a character by USART0 
{
  if((UCSR0A & 0x80) == 0x00)   		// if data not received,
    return 0x00;                                //    return with 0x00
  else                                          // if data received,
    return UDR0;                                //    return with data
}


unsigned char RX0_char(void)		    // receive a character by USART0 
{
  while((UCSR0A & 0x80) == 0x00);		// data received ?
  return UDR0;
}

void TX0_char(unsigned char data)		// transmit a character by USART0 
{
  while((UCSR0A & 0x20) == 0x00);		// data register empty ?
  UDR0 = data;
}

void TX0_string(unsigned char *string)  // transmit a string by USART0 
{
  while(*string != '\0')
    { TX0_char(*string);
       string++;
    }
}


*/




// ========================== TIMER0 overflow 인터럽트 ========================
// [인수] void
// [참고] 16,000,000 / 1024 / 156 =  1 / 100.1602564 = 9.9984ms
// ----------------------------------------------------------------------------
//SIGNAL(SIG_OVERFLOW0)
ISR(TIMER0_OVF_vect)
{
	TCNT0 = 99;						// 255-156 = 100

timer0_cnt++;						// 타이머0 카운터
	timer0_touch_cnt++;					// 타이머0 터치 관리 카운터++
}


// =============================== COM1수신 인터럽트 ===========================
// [인수] void
// ----------------------------------------------------------------------------
//SIGNAL(SIG_UART0_RECV)

ISR(USART0_RX_vect)
{
char status,data;

	status=UCSR0A;
	data=UDR0;
	if((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0){
		com1_rx_data = data;
		com1_rx_flg = 1;
	}

}


// ============================== COM1 1바이트 송신 =========================
// [인수] char data
// item1: 전송할 데이터
// --------------------------------------------------------------------------
int putchar_0(char data)
{
	if(data == '\n'){
		while((UCSR0A & DATA_REGISTER_EMPTY) == 0);
		UDR0 = '\r';
	}
	while((UCSR0A & DATA_REGISTER_EMPTY) == 0);
	UDR0 = data;
	return(0);
}

// ============================== COM1 1바이트 수신 =========================
// [인수] void
// [리턴] 수신 데이터
// --------------------------------------------------------------------------
int getchar_0(void)
{
	while((UCSR0A & RX_COMPLETE) == 0);
	return(UDR0);
}










