Subversion Repositories zxusbnet

Rev

Blame | Last modification | View Log | Download | RSS feed | ?url?

  1. #include <avr/io.h>
  2.  
  3. #include <stdio.h>
  4. #include "uart.h"
  5.  
  6. static int USART0_Transmit( char data )
  7. {
  8.         /* Wait for empty transmit buffer */
  9. //      while ( !(UCSR0A & (1<<UDRE0)) );
  10.         loop_until_bit_is_set(UCSR0A, UDRE0);
  11.  
  12.         /* Start transmittion */
  13.         UDR0 = data;
  14.  
  15.         return 0;
  16. }
  17.  
  18. static int USART0_Receive( void )
  19. {
  20.         char c;
  21.         /* Wait for incomming data */
  22. //      while ( !(UCSR0A & (1<<RXC0)) );
  23.         loop_until_bit_is_set(UCSR0A, RXC0);
  24.  
  25.         c = UDR0;
  26.         /* Echo */
  27.         loop_until_bit_is_set(UCSR0A, UDRE0);
  28.         UDR0 = c;
  29.         if(c == '\r') {
  30.         loop_until_bit_is_set(UCSR0A, UDRE0);
  31.         UDR0 = '\n';
  32.         }
  33.         /* Return the data */
  34.         return c;
  35. }
  36.  
  37. void initUART(void){
  38.         /* set baud rate */
  39.         UBRR0H = (unsigned char) (UART_BAUD_SELECT>>8);
  40.         UBRR0L = (unsigned char) UART_BAUD_SELECT;
  41.  
  42.         /* Enable UART receiver and transmitter */
  43.         UCSR0A = ( 1 << U2X0 );
  44.         UCSR0B = ( ( 1 << RXEN0 ) | ( 1 << TXEN0 ) );
  45.  
  46.         fdevopen((void*)USART0_Transmit, (void*)USART0_Receive);
  47. }
  48.