#include /* uC: ATMEGA88 */ #include #include #include #include #define FOSC 8000000 // Clock Speed #define BAUD 9600 #define MYUBRR FOSC/16/BAUD-1 #define sizeOfReceiveBuffer 50 unsigned char receivebuffer[sizeOfReceiveBuffer]; uint8_t returnFlagOfReceiveBuffer; bool dataAreRead = false; /* void UART_Flush( void ) { unsigned char dummy; while ( UCSR0A & (1<>8); UBRR0L = ubrr; /* 7 6 5 4 3 2 1 0 */ /* RXCn TXCn UDREn FEn DORn UPEn U2Xn MPCMn = UCSRnA */ /* R | R/W | R | R | R | R | R/W | R/W */ /* 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 */ /* • Bit 7 – RXCn: USART Receive Complete This flag bit is set when there are unread data in the receive buffer and cleared when the receive buffer is empty (i.e., does not contain any unread data). If the Receiver is disabled, the receive buffer will be flushed and consequently the RXCn bit will become zero. The RXCn Flag can be used to generate a Receive Complete interrupt (see description of the RXCIEn bit). • Bit 6 – TXCn: USART Transmit Complete This flag bit is set when the entire frame in the Transmit Shift Register has been shifted out and there are no new data currently present in the transmit buffer (UDRn). The TXCn Flag bit is automatically cleared when a transmit complete interrupt is executed, or it can be cleared by writing a one to its bit location. The TXCn Flag can generate a Transmit Complete interrupt (see description of the TXCIEn bit). • Bit 5 – UDREn: USART Data Register Empty The UDREn Flag indicates if the transmit buffer (UDRn) is ready to receive new data. If UDREn is one, the buffer is empty, and therefore ready to be written. The UDREn Flag can generate a Data Register Empty interrupt (see description of the UDRIEn bit). UDREn is set after a reset to indicate that the Transmitter is ready. • Bit 4 – FEn: Frame Error This bit is set if the next character in the receive buffer had a Frame Error when received. I.e., when the first stop bit of the next character in the receive buffer is zero. This bit is valid until the receive buffer (UDRn) is read. The FEn bit is zero when the stop bit of received data is one. Always set this bit to zero when writing to UCSRnA. • Bit 3 – DORn: Data OverRun This bit is set if a Data OverRun condition is detected. A Data OverRun occurs when the receive buffer is full (two characters), it is a new character waiting in the Receive Shift Register, and a new start bit is detected. This bit is valid until the receive buffer (UDRn) is read. Always set this bit to zero when writing to UCSRnA. • Bit 2 – UPEn: USART Parity Error This bit is set if the next character in the receive buffer had a Parity Error when received and the Parity Checking was enabled at that point (UPMn1 = 1). This bit is valid until the receive buffer (UDRn) is read. Always set this bit to zero when writing to UCSRnA. • Bit 1 – U2Xn: Double the USART Transmission Speed This bit only has effect for the asynchronous operation. Write this bit to zero when using synchronous operation. Writing this bit to one will reduce the divisor of the baud rate divider from 16 to 8 effectively doubling the transfer rate for asynchronous communication. • Bit 0 – MPCMn: Multi-processor Communication Mode This bit enables the Multi-processor Communication mode. When the MPCMn bit is written to one, all the incoming frames received by the USART Receiver that do not contain address information will be ignored. The Transmitter is unaffected by the MPCMn setting. For more detailed information see */ /*Enable receiver and transmitter no interrupt enabled */ UCSR0B |= (1< asynchron */ /* UPMn1 UPMn0 Parity Mode 0 0 Disabled 0 1 Reserved 1 0 Enabled, Even Parity 1 1 Enabled, Odd Parity */ /* UPMn1 and UPMn0 = 0 (default) -> Paritiy bit disabled */ /* USBSn Stop Bit(s) 0 1-bit 1 2-bit */ /* USBSn = 0 (default) -> 1 Stop bit */ /* UCSZn2 UCSZn1 UCSZn0 Character Size 0 0 0 5-bit 0 0 1 6-bit 0 1 0 7-bit 0 1 1 8-bit 1 0 0 Reserved 1 0 1 Reserved 1 1 0 Reserved 1 1 1 9-bit */ /* UCSZn1 and UCSZn0 = 1 (default) -> 8 bit */ /* UCPOLn Transmitted Data Changed Received Data Sampled (Output of TxDn Pin) (Input on RxDn Pin) 0 Rising XCKn Edge Falling XCKn Edge 1 Falling XCKn Edge Rising XCKn Edge */ } unsigned char UART_Receive(void) { while (!(UCSR0A & (1<