Skip to content

Instantly share code, notes, and snippets.

@bigjosh
Created August 22, 2021 07:22
Show Gist options
  • Save bigjosh/e0a386d10d8dff3a584e9489ac97ddb3 to your computer and use it in GitHub Desktop.
Save bigjosh/e0a386d10d8dff3a584e9489ac97ddb3 to your computer and use it in GitHub Desktop.
// put your main code here, to run repeatedly:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
void setup() {
// put your setup code here, to run once:
main();
}
void loop() {
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) )
;
/* Get and return received data from buffer */
return UDR0;
}
void USART_Init( unsigned int ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (0<<USBS0)|(1<<UCSZ00)|(1<<UCSZ01);
/*enable RX interrupt */
UCSR0B |= (1 << RXCIE0);
sei();
}
int main(void)
{
USART_Init(103);
while (1)
{
//a = USART_Receive();
//USART_Transmit(a);
_delay_ms(100);
}
}
ISR(USART_RX_vect, ISR_BLOCK){
char ReceivedByte ;
ReceivedByte = UDR0 ; // Fetch the received byte value into the variable " ByteReceived "
UDR0 = ReceivedByte ; // Echo back the received byte back to the computer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment