I have been tinkering with an ATMega328P trying to figure out how to use the different functionalities because i need to use it for a school project. i'm currently trying to figure out the usart part. this is my setup on a breadboard.
and I am trying to transmit data using this CH340 USB-ttl module.
I'm quite new to programming bare metal microcontrollers but i followed the datasheet and several youtube videos and came up with this code.
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
void USARTTransmit(char my_data) {//send data via uart0
while (!(UCSR0A & (1 << UDRE0)));
UDR0 = my_data;
}
void USARTInit(unsigned int ubrr) {//initialise uart0
PRR &= ~(1 << PRUSART0); //disable power reduction mode
UBRR0H = (unsigned char)(ubrr >> 8);
UBRR0L = (unsigned char)ubrr;
//set frame format: 8 data bits, 1 stop bit, no parity bit
UCSR0C |= (3 << UCSZ00);
UCSR0B |= (1 << TXEN0); //enable transmitter
}
void sendNumber(uint16_t number) {
char buffer[6];
itoa(number, buffer, 10); //convert the number to a string base 10
for (char *ptr = buffer; *ptr != '\0'; ptr++) {
USARTTransmit(*ptr);
}
USARTTransmit('\n');
//blink led twice to show data is sent
for (int i =0; i<2; i++) {
PORTB |= (1 << PB1);
_delay_ms(500);
PORTB &= ~(1 << PB1);
_delay_ms(500);
}
}
int main(void) {
DDRB |= (1 << PB1); //set pb1 as output
USARTInit(MYUBRR);
uint16_t my_number = 120;//sample number
_delay_ms(5000);
while (1){
sendNumber(my_number);
_delay_ms(1000);//send every second
}
return 0;
}
When i try to view the data using SSCOM, I am getting some but it's all garbled up.
I am using the internal 8MHz clock and the lfuse is set to 0xE2. the hfuse and efuse are both left at the default values. I'm not quite sure where the issue is. it would be great if anyone could help me out



