Created
May 6, 2015 13:33
-
-
Save Bouni/b96ce85f16d9863392ab to your computer and use it in GitHub Desktop.
Arduino uart AVR-GCC
This file contains hidden or 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
#ifndef F_CPU | |
#define F_CPU 16000000UL | |
#endif | |
#include <inttypes.h> | |
#include <avr/io.h> | |
#include <util/delay.h> | |
int main(void) { | |
uint16_t baudrate = 9600; | |
/* calculate and set the baudrate */ | |
uint16_t baud = (F_CPU / 8 / baudrate - 1) / 2; | |
UBRR0H = (uint8_t) (baud >> 8); | |
UBRR0L = (uint8_t) (baud & 0x0ff); | |
/* activate transmitter, receiver and receiver interrupt */ | |
UCSR0B |= (1 << 3); /* TXEN */ | |
UCSR0B |= (1 << 4); /* RXEN */ | |
UCSR0B |= (1 << 7); /* RXCIE */ | |
/* set 8 data bits */ | |
UCSR0C |= (1 << 1); /* UCSZ0 = 1 */ | |
UCSR0C |= (1 << 2); /* UCSZ1 = 1 */ | |
UCSR0B &= ~(1 << 2); /* UCSZ2 = 0 */ | |
/* set parity NONE */ | |
UCSR0C &= ~(1 << 4); /* UPM0 = 0 */ | |
UCSR0C &= ~(1 << 5); /* UPM1 = 0 */ | |
/* set 1 stopbit */ | |
UCSR0C |= (1 << 3); /* USBS = 1 */ | |
while(1) { | |
UDR0 = 0x56; /* sende ein grosses V */ | |
_delay_ms(100); | |
} | |
return 0; | |
} | |
/* UART Empfangs Interrupt Routine */ | |
ISR(USART0_RX_vect){ | |
uint8_t data = UDR0; | |
/* Hier kannst du dann selbst überlegen was du mit dem empfangenen Byte machst ;-) */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first thing I found with the correct registers of atmega328p.