Thank you @maxgerhardt for clearing up my mistakes. As far as the topic goes the problem was I didn’t know how to include libraries and did not check if the fuses of an ATmega8 and ATmega328P are the same (they are not).
Here is the code alternated from the site that you provided to turn a LED ON and OFF using UART on ATmega8.
// This code waits for a character and transmits the character back (with interrupts)
#include <avr/io.h>
#include <stdint.h> // needed for uint8_t
#include <avr/interrupt.h>
#define FOSC 8000000 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD -1
volatile char ReceivedChar;
volatile char rx_byte;
int main( void )
{
/*Set baud rate */
UBRRH = (MYUBRR >> 8);
UBRRL = MYUBRR;
UCSRB |= (1 << RXEN) | (1 << TXEN); // Enable receiver and transmitter
UCSRB |= (1 << RXCIE); // Enable the receiver interrupt
UCSRC |= (1 << URSEL) |(1 << UCSZ1) | (1 << UCSZ0); // Set frame: 8data, 1 stp
DDRB = 0xff;
sei();
while(1)
{
if (!(UCSRA & (1 << RXC))) { // data received?
rx_byte = UDR; // get the byte
switch (rx_byte) {
case 'a':
PORTB = 0xff; // switch LED on
break;
case 'b':
PORTB = 0x00; // switch LED off
break;
}
}
}
}
ISR (USART_RXC_vect)
{
ReceivedChar = UDR; // Read data from the RX buffer
UDR = ReceivedChar; // Write the data to the TX buffer
}
And pltaformio.ini file for usbtiny:
[env:ATmega8]
platform = atmelavr
board = ATmega8
board_build.f_cpu = 8000000L
upload_protocol = custom
upload_speed = 9600
lib_extra_dirs =
/home/pi/Documents/PlatformIO/Projects/UART_ATmega8/lib
upload_flags =
-C
; use "tool-avrdude-megaavr" for the atmelmegaavr platform
$PROJECT_PACKAGES_DIR/tool-avrdude/avrdude.conf
-p
$BOARD_MCU
-c
usbtiny
-Uhfuse:w:0xd9:m ;set fuses manually
-Uefuse:w:0xff:m
-Ulfuse:w:0xe4:m
upload_command = avrdude $UPLOAD_FLAGS -e -u -U flash:w:$SOURCE:i