/* 接收中斷 */ void USART_RX_interrupt( void ) { unsigned char data; unsigned char tmphead; data = UDR0; /* read the received data */ tmphead = ( USART_RxHead + 1 ) & USART_RX_BUFFER_MASK;/* calculate buffer index */ USART_RxHead = tmphead; /* store new index */ if ( tmphead == USART_RxTail ) { return;/* ERROR! Receive buffer overflow, you can use flux cortorl */ } USART_RxBuf[tmphead] = data; /* store received data in buffer */ }
while ( tmphead == USART_TxTail )/* wait for free space in buffer */ ; USART_TxBuf[tmphead] = data; /* store data in buffer */ USART_TxHead = tmphead; /* store new index */ UCSR0B |= (1<<UDRIE0); /* enable UDRE interrupt */ }
/* 查詢是否有數據 */ unsigned char DataInReceiveBuffer( void ) { return ( USART_RxHead != USART_RxTail ); /* return 0 (FALSE) if the receive buffer is empty */ }