All pastes #259955 Raw Edit

Stuff

public text v1 · immutable
#259955 ·published 2006-11-27 22:02 UTC
rendered paste body
void usart_init(unsigned char baud_divider) {

  // Baud rate selection
  UBRRH = 0x00;       
  UBRRL = baud_divider;

  // USART setup
  UCSRA = 0x80;        // 1000 0000
                       // U2X disabled
  UCSRC = 0x06;        // 1000 0110
                       // Access UCSRC, Asyncronous 8N1
  UCSRB = 0x98;        // 1001 1000
                       // Receiver enabled, Transmitter enabled
                       // RX Complete interrupt enabled
  sei();               // Enable interrupts globally
}

void usart_putc(char data) {
    while (!(UCSRA & 0x20)); // Wait untill USART data register is empty
    // Transmit data
    UDR = data;
}

int main(void)
{

	DDRD |= _BV(PD6);
	PORTD |= _BV(PD6);



	usart_init(USART_BAUDRATE(9600, F_CPU));
	usart_puts("Atmel AVR USART test: ");
	while(1) {
		if (usart_unread_data()) 
			usart_putc(usart_getc());
	}