Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes

USART (ATmega168)

Last-modified: 2012-08-20 (Mon) 00:29:30 (2279d)
Top / USART (ATmega168)

USART0Test

result.jpg
//-----------------------------------------------------------------------------
//  ATmega168
//  Internal 8 MH RC Osc.
//  (1/8M = 125 ns)
//-----------------------------------------------------------------------------

#include <avr/io.h>

#define FOSC    8000000
#define BAUD    9600
#define UBRR    FOSC/16/BAUD-1

//-----------------------------------------------------------------------------
//  Initialize USART0
//-----------------------------------------------------------------------------
void init_USART0(unsigned int baud)
{
    UBRR0 = baud;                       // Set Baudrate
    UCSR0C = (3<<UCSZ00);               // Character Size 8 bit
    UCSR0B |= _BV(RXEN0) | _BV(TXEN0);  // Receiver and Transmitter Enable
}

//-----------------------------------------------------------------------------
//  Set Receive Interrupt Enable
//-----------------------------------------------------------------------------
void setRXCIE_USART0()
{
    UCSR0B |= _BV(RXCIE0);
}

//-----------------------------------------------------------------------------
//  Receive 1 byte Data
//-----------------------------------------------------------------------------
unsigned char receive_1byte_USART0(void)
{
    loop_until_bit_is_set(UCSR0A, RXC0);
    return UDR0;
}

//-----------------------------------------------------------------------------
//  Transmit 1 byte Data
//-----------------------------------------------------------------------------
void transmit_1byte_USART0(unsigned char data)
{
    loop_until_bit_is_set(UCSR0A, UDRE0);
    UDR0 = data;
}

//-----------------------------------------------------------------------------
//  Transmit String Data
//-----------------------------------------------------------------------------
void transmit_str_USART0(char *str)
{
    while (*str != 0) {
        transmit_1byte_USART0(*str);
        *str++;
    }
}

//-----------------------------------------------------------------------------
//  Transmit Four-Digit Integer
//-----------------------------------------------------------------------------
void transmit_4digit_USART0(int num)
{
    unsigned char temp;
    int digit = 1000;

    while (digit != 0) {
        temp = num / digit;
        transmit_1byte_USART0('0'+temp);
        num -= (digit*temp);
        digit /= 10;
    }
}

//-----------------------------------------------------------------------------
//  Main
//-----------------------------------------------------------------------------
int main()
{
    init_USART0(UBRR);  // initialize USART0

    while (1) {
        if ('a' == receive_1byte_USART0()) {
            transmit_str_USART0("Hello World!\r\n");
        }
    }

    return 0;
}

Attach file: fileUSART0Test.c 1361 download [Information] fileresult.jpg 1081 download [Information]