Created
June 17, 2019 04:14
-
-
Save CreateRemoteThread/57722d83c8c051ccf16a0de527b349c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define F_CPU 16000000UL | |
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <util/delay.h> | |
#define BAUD 9600 // define baud | |
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // set baud rate value for UBRR | |
// function to initialize UART | |
void uart_init (void) | |
{ | |
UBRR0H = (BAUDRATE>>8); // shift the register right by 8 bits | |
UBRR0L = BAUDRATE; // set baud rate | |
UCSR0B|= (1<<TXEN0)|(1<<RXEN0); // enable receiver and transmitter | |
UCSR0C = (3<<UCSZ00); | |
} | |
int uart_transmit(char data ) | |
{ | |
while (!( UCSR0A & (1<<UDRE0))); // wait while register is free | |
UDR0 = data; // load data in the register | |
return 0; | |
} | |
unsigned char uart_receive( void ) | |
{ | |
while ( !(UCSR0A & (1<<RXC0)) ); | |
return UDR0; | |
} | |
int main(void) | |
{ | |
uart_init(); | |
FILE mystdio = FDEV_SETUP_STREAM(uart_transmit, uart_receive, _FDEV_SETUP_RW); | |
stdout = &mystdio; | |
stdin = &mystdio; | |
char lol[5]; | |
while (1) | |
{ | |
printf("Hello World\n\r"); | |
fgets(lol,5,stdin); | |
_delay_ms(1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment