summaryrefslogtreecommitdiff
path: root/md/writeup/avr_echo.md
blob: 0dac9fb2c0f2c73e9fa13c6ae76117e61b005587 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
title:AVR echo
keywords:avr,echo,c,atmega


# AVR echo
This code tested on ATmega16

```c
#include <avr/io.h>
 
#define FOSC 16000000UL
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
 
void USART_Init( unsigned int ubrr)
{
    UBRRH = (unsigned char)(ubrr>>8);
    UBRRL = (unsigned char)ubrr;
    UCSRB = (1<<RXEN)|(1<<TXEN);
    UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
 
int main()
{
    char c;
    USART_Init( MYUBRR );
    while(1)
    {
        while ( !(UCSRA & (1<<RXC))){};
        c = UDR;
        while (!(UCSRA & (1<<UDRE))){};
        UDR = c;
    }
    return 0;
}
```