Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 16000000UL
- #define BAUD 9600
- #include <stdio.h>
- #include <stdint.h>
- #include <avr/io.h>
- #include <util/delay.h>
- #include <util/setbaud.h>
- void uart_init(void) {
- UBRR0H = UBRRH_VALUE;
- UBRR0L = UBRRL_VALUE;
- #if USE_2X
- UCSR0A |= _BV(U2X0);
- #else
- UCSR0A &= ~(_BV(U2X0));
- #endif
- UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
- UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
- }
- void uart_putchar(char c) {
- //loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
- while(!(UCSR0A & (1<<UDRE0)));
- UDR0 = c;
- }
- char uart_getchar(void) {
- //loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
- while(!(UCSR0A & (1<<RXC0)));
- return UDR0;
- }
- void uart_putstring(char* string)
- {
- char *p = string;
- while(*(p++))
- {
- uart_putchar(*p);
- }
- }
- char String[] = "Hello world!!\n";
- static FILE uart_output = FDEV_SETUP_STREAM((int (*)(char, struct __file *)) uart_putchar, NULL, _FDEV_SETUP_WRITE);
- static FILE uart_input = FDEV_SETUP_STREAM(NULL, (int (*)(struct __file *)) uart_getchar, _FDEV_SETUP_READ);
- int main(int main, char *argv[])
- {
- uart_init();
- stdout = &uart_output;
- stdin = &uart_input;
- char input;
- while (1)
- {
- puts("Hello world!");
- input = getchar();
- printf("You wrote %c\n", input);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement