Skip to content

Instantly share code, notes, and snippets.

@goncalor
Created July 4, 2018 23:44
Show Gist options
  • Save goncalor/759fed544836044c92e65ba1a116bd14 to your computer and use it in GitHub Desktop.
Save goncalor/759fed544836044c92e65ba1a116bd14 to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <util/delay.h>
#define BAUD 9600
#include <util/setbaud.h>
#define BLINK_DELAY_MS 1000
void initUSART(void) { /* requires BAUD */
UBRR0H = UBRRH_VALUE; /* defined in setbaud.h */
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
/* Enable USART transmitter/receiver */
UCSR0B = (1 << TXEN0) | (1 << RXEN0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); /* 8 data bits, 1 stop bit */
}
void transmitByte(uint8_t data) {
/* Wait for empty transmit buffer */
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = data; /* send data */
}
uint8_t receiveByte(void) {
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait for incoming data */
return UDR0; /* return register value */
}
/* Here are a bunch of useful printing commands */
void printString(const char myString[]) {
uint8_t i = 0;
while (myString[i]) {
transmitByte(myString[i]);
i++;
}
}
int main (void)
{
/* set pin 5 of PORTB for output*/
DDRB |= _BV(DDB5);
initUSART();
while (1) {
/* set pin 5 high to turn led on */
PORTB |= _BV(PORTB5);
_delay_ms(BLINK_DELAY_MS);
/* set pin 5 low to turn led off */
PORTB &= ~_BV(PORTB5);
_delay_ms(BLINK_DELAY_MS);
//transmitByte(0x41);
printString("Hi\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment