void uart_init() { PORTD |= 0x01; //Pullup an RXD an UCSRB |= (1<<TXEN); //UART TX einschalten #ifdef ATMEGA644 UCSRA = 0; UCSRC = (3<<UCSZ0); //Asynchron 8N1 #else UCSRA = 0; UCSRC |= (1<<URSEL)|(3<<UCSZ0); //Asynchron 8N1 #endif UCSRB |= ( 1 << RXEN ); //Uart RX einschalten UBRRH=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8); UBRRL=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)); #ifdef UART_INTERRUPT // init buffers rxhead = rxtail = rxbuf; txhead = txtail = txbuf; // activate rx IRQ UCSRB |= _BV(RXCIE) | _BV(UDRIE); sei(); // #ifdef ATMEGA644 // UCSRB |= _BV(UDRIE); // #endif #endif // UART_INTERRUPT }
BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) { UCHAR ucUCSRC = 0; /* prevent compiler warning. */ (void)ucPORT; UBRR = UART_BAUD_CALC( ulBaudRate, F_CPU ); switch ( eParity ) { case MB_PAR_EVEN: ucUCSRC |= _BV( UPM1 ); break; case MB_PAR_ODD: ucUCSRC |= _BV( UPM1 ) | _BV( UPM0 ); break; case MB_PAR_NONE: break; } switch ( ucDataBits ) { case 8: ucUCSRC |= _BV( UCSZ0 ) | _BV( UCSZ1 ); break; case 7: ucUCSRC |= _BV( UCSZ1 ); break; } #if defined (__AVR_ATmega168__) UCSRC |= ucUCSRC; #elif defined (__AVR_ATmega169__) UCSRC |= ucUCSRC; #elif defined (__AVR_ATmega8__) UCSRC = _BV( URSEL ) | ucUCSRC; #elif defined (__AVR_ATmega16__) UCSRC = _BV( URSEL ) | ucUCSRC; #elif defined (__AVR_ATmega32__) UCSRC = _BV( URSEL ) | ucUCSRC; #elif defined (__AVR_ATmega128__) UCSRC |= ucUCSRC; #endif vMBPortSerialEnable( FALSE, FALSE ); #ifdef RTS_ENABLE RTS_INIT; #endif return TRUE; }
void UART_Init(void) { UCSRB |= (1<<TXEN)|(1<<RXEN)|(1<<RXCIE); UCSRC |= (1<<URSEL)|(3<<UCSZ0); UBRRH=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8); UBRRL=(uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_CPU); UART_Rx_Cmd = 0; UART_Rx_Index = 0; UART_Tx_Ring_txptr = 0; UART_Tx_Ring_rxptr = 0; UART_Tx_Ring_Size = 0; #ifdef printfoverrs232 stdout = &mystdout; #endif }
void uart_init(void) { #ifdef UART_LEDS DDRC |= 0x03; // Port C LED outputs #endif PORTD |= 0x01; //Pullup an RXD an UCSRA = 0; UCSRB = (1<<TXEN) | ( 1 << RXEN); //UART RX und TX einschalten UCSRC = (3<<UCSZ0); //Asynchron 8N1 UBRRH=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)>>8); UBRRL=(uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_CPU)); #ifdef UART_INTERRUPT // init buffers rxhead = rxtail = rxbuf; txhead = txtail = txbuf; // activate rx IRQ UCSRB |= (1 << RXCIE); #endif // UART_INTERRUPT }
uint8_t uart_len, uart_done, sem_uart; unsigned char uartbuf[32]; /* Initialize UART */ void uart_init( void) { UBRRH = (uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_OSC)>>8); UBRRL = (uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_OSC); UCSRB = ( (1<<RXEN) | (1<<TXEN) ); /* Enable UART receiver and transmitter */ UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); uart_len=0; uart_done=0; unlock(&sem_uart); UCSRB |= (1<<RXCIE); /* interrupts toestaan*/ sei(); }
char sMBPortSerialInit(unsigned long ulBaudRate, unsigned char ucDataBits, sMBParityMode eParity) { switch ( eParity ) { case MB_PAR_EVEN: if(ucDataBits == 8) { *SCICCR = 0x67; // 01100111 } else if(ucDataBits == 7) { *SCICCR = 0x66; // 01100110 } else { return 0; } break; case MB_PAR_ODD: if(ucDataBits == 8) { *SCICCR = 0x27; // 00100111 } else if(ucDataBits == 7) { *SCICCR = 0x26; // 00100110 } else { return 0; } break; case MB_PAR_NONE: if(ucDataBits == 8) { // No Parity is implemented, an additional stop bit is required. *SCICCR = 0x87; // 10000111 } else if(ucDataBits == 7) { // No Parity is implemented, an additional stop bit is required. *SCICCR = 0x86; // 10000110 } else { return 0; } break; default: break; } *SCIHBAUD = (UART_BAUD_CALC(ulBaudRate, F_CPU) & 0xFF00) >> 8; *SCILBAUD = UART_BAUD_CALC(ulBaudRate, F_CPU) & 0x00FF; *SCIPRI = 0x60; *MCRA = (*MCRA) | 0x0003; *IMR = (*IMR) | 0x0010; sMBPortSerialEnable( 0, 0 ); return 1; }