Skip to content

Instantly share code, notes, and snippets.

@Bouni
Created May 6, 2015 13:33
Show Gist options
  • Save Bouni/b96ce85f16d9863392ab to your computer and use it in GitHub Desktop.
Save Bouni/b96ce85f16d9863392ab to your computer and use it in GitHub Desktop.
Arduino uart AVR-GCC
#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 ;-) */
}
@YokiToki
Copy link

YokiToki commented Aug 4, 2017

The first thing I found with the correct registers of atmega328p.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment