示例#1
0
void init(void){
	_usart_init();
	_timer_init();

	DDRB |= (1<<PB0)|(1<<PB1)|(1<<PB2)|(1<<PB3)|(1<<PB4)|(1<<PB5)|(1<<PB6)|(1<<PB7); // Ausgänge für die Relais

	sei();
}
struct usart_dev* usart_init(uint8_t usart,
			     uint32_t baudrate, 
			     uint8_t bits, 
			     uint8_t sbits, 
			     uint8_t parity, 
			     uint8_t flags)
{
	uint8_t ucsra = 0x20;
	uint8_t ucsrb = 0x00;
	uint8_t ucsrc = 0x06;
	uint16_t ubrr = 0x0000;

	struct usart_dev * dev;

	if (usart == USART_0) {
		dev = &usart_0;
		_usart_init(dev, &UCSR0A, &UCSR0B, &UCSR0C, &UBRR0L, &UBRR0H, &UDR0, flags);
	} else {
		dev = &usart_1;
		_usart_init(dev, &UCSR1A, &UCSR1B, &UCSR1C, &UBRR1L, &UBRR1H, &UDR1, flags);
	}

	/*Set baud rate */
	//ubrr = USART_UBRR_VAL(baudrate) - 3;
	ubrr = USART_UBRR_VAL(baudrate);

	/*Enable receiver and transmitter */
	if (!(flags & USART_NO_RX))
		ucsrb |= (1 << RXEN);
	if (!(flags & USART_NO_TX))
		ucsrb |= (1 << TXEN);
	
	/* character bits */
	if (bits == USART_BITS_9)
		ucsrb |= (1 << UCSZ2);
	ucsrc &= (bits << 1) | 0xF9;
	
	/* transmission mode */
	if (flags & USART_SYNC)
		ucsrc |= (1 << UMSEL);

	/* parity */
	ucsrc |= (parity << UPM0);

	/* stop bits */
	ucsrc |= (sbits << USBS);

#if defined(USART_USE_CTS)
	/* cts, rts */
	if (flags & USART_CTS) {
		if (usart == USART_0) {
			DDRE &= ~(1 << DDE3); /* input */
			PORTE &= ~(1 << PE3); /* no int pull-up */
			dev->cts = &PINE;
			dev->cts_pin = PINE3;
		} else {
			DDRD &= ~(1 << DDD4); /* input */
			PORTD &= ~(1 << PD4); /* no int pull-up */
			usart_1.cts = &PORTD;
			usart_1.cts_pin = PIND4;
		}
	}
#endif
#if defined(USART_USE_RTS)
	if (flags & USART_RTS) {
		if (usart == USART_0) {
			DDRE |= (1 << DDE2); /* output */
			PORTE &= ~(1 << PE2); /* low */
			dev->rts = &PORTE;
			dev->rts_pin = PORTE2;
		} else {
			/** ?? **/
		}
	}
		
	/* interrupts */
#endif
#if defined(USART_USE_ISR)
	ucsrb |= (1 << RXCIE0);
#endif

	cli();
	*(dev->ucsrc) = ucsrc;
	*(dev->ucsrb) = ucsrb;
	*(dev->ucsra) = ucsra;
	*(dev->ubrrh) = (uint8_t)(ubrr >> 8);
	sei();
	*(dev->ubrrl) = (uint8_t)ubrr;

	return dev;
}