Skip to content

Instantly share code, notes, and snippets.

@moomdate
Last active May 28, 2019 06:20
Show Gist options
  • Select an option

  • Save moomdate/24eb6bb24b3ae4e476336b5d405b3e75 to your computer and use it in GitHub Desktop.

Select an option

Save moomdate/24eb6bb24b3ae4e476336b5d405b3e75 to your computer and use it in GitHub Desktop.
#define BAUD 9600
#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. */
UDR0 = c;
}
int main(void)
{
DDRB = 0x80; //Nakes PORTC as Output
uart_init();
while(1) //infinite loop
{
PORTB = 0x80; //Turns ON All LEDs
_delay_ms(300); //1 second delay
PORTB= 0x00; //Turns OFF All LEDs
_delay_ms(100); //1 second delay
uart_putchar('A');
}
}
//https://appelsiini.net/2011/simple-usart-with-avr-libc/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment